Aerostack
bar_chart

data-paginate Edge Function — Data

Data

Paginates an array of items with offset-based page/limit logic, returning metadata like totalPages, hasNext, and hasPrev.

navin @navin verified
Updated Mar 12, 2026
GitHub

Edge function data-paginate Paginates an array of items with offset-based page/limit logic, returning metadata like totalPages, hasNext, and hasPrev.. Deployed on Cloudflare Workers — zero cold starts, globally distributed. Mount it via your Aerostack workspace to call it from any AI agent.

terminal — aerostack cli
$ npx aerostack add navin/data-paginate
smart_toy

Use with AI Assistants

MCP

Connect Claude, Cursor, or any MCP-compatible client — then call this function by slug

① Add MCP Server

Add this once — access all Aerostack functions from your AI tool.

claude_desktop_config.json
{
  "mcpServers": {
    "aerostack": {
      "url": "https://mcp.aerostack.dev",
      "type": "http"
    }
  }
}

② Call this function

Ask your AI to use the call_function tool with this slug:

call_function
call_function({
  slug: "data-paginate",
  args: {
    "items": null,
    "page": 1,
    "limit": 20
  }
})

data-paginate — Offset-based array pagination

Paginates any array with page/limit logic, returning the sliced items plus full pagination metadata.


API

POST /api/data-paginate

Request body

Field Type Required Default Description
items unknown[] The full array to paginate
page number 1 Page number (1-based, min 1)
limit number 20 Items per page (min 1, max 100)

Success response (200)

{
  "success": true,
  "data": {
    "items": [21, 22, 23],
    "total": 50,
    "page": 3,
    "limit": 10,
    "totalPages": 5,
    "hasNext": true,
    "hasPrev": true
  }
}

Error responses

Code HTTP When
INVALID_INPUT 400 Missing or invalid field
INTERNAL_ERROR 500 Unexpected error

Usage

cURL
curl -X POST "$FUNCTION_URL" \
  -H "Content-Type: application/json" \
  -d '{"items": [1,2,3,4,5,6,7,8,9,10], "page": 2, "limit": 3}'
TypeScript / JavaScript (HTTP)
const response = await fetch(FUNCTION_URL, {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ items: myArray, page: 2, limit: 20 }),
});
const { data } = await response.json();
console.log(data.items, data.hasNext);
Direct import (Node / Bun / Deno)
import { paginate } from '@aerostack/functions/data-paginate';

const result = paginate({ items: myArray, page: 2, limit: 20 });
console.log(result.items, result.totalPages);

Use Cases

  • Paginating API list endpoints (users, orders, products) before sending to a frontend
  • Slicing search results for a UI that uses next/prev page controls
  • Chunking large arrays in a workflow before passing each page to a downstream processor

Notes

  • page is clamped to a minimum of 1
  • limit is clamped between 1 and 100
  • An empty items array returns totalPages: 0, hasNext: false, hasPrev: false
  • Items beyond the last page return an empty items array

Metadata

upgrade Version 1.0.0
gavel License MIT
language Language typescript
cloud Provider aerostack

Tags

data
deployed_code

Build and publish your own functions

Write a TypeScript function, deploy it to the edge, and share it with thousands of developers — in minutes.

More Data Functions

Browse Data Functions →

Frequently asked questions

What does the data-paginate function do? +

data-paginate is a serverless edge function for data automation written in aerostack. Deploy it to Cloudflare Workers via your Aerostack workspace.

How do I deploy the data-paginate function? +

Install the Aerostack CLI and run: ```bash aerostack deploy function @navin/data-paginate ``` It will be live on Cloudflare Workers in seconds.

What runtime does data-paginate use? +

data-paginate runs on aerostack on the Cloudflare Workers edge runtime — zero cold starts, globally distributed.

Can I customise the data-paginate function? +

Yes. Fork the function from your Aerostack dashboard, modify the source, and redeploy. All changes are version-controlled.