Release candidate — 1.0.0-rc.6
StitchAPI
GuidesData shaping

Pagination

Auto-loop pages and aggregate items, with auth, retry, and throttle applied to every page.

Add paginate when one logical call spans many pages and you want the stitch to follow them for you — it loops until you say stop, aggregates every page's items into one array, and applies auth, retry, and throttle to each page along the way.

Example

import {  } from 'stitchapi';

const  = ({
    : 'https://api.example.com',
    : '/users',
    : {
        : () => {
            const  = ( as { ?: string }).;
            return  ? { : {  } } : ;
        },
        : () => ( as { : unknown[] }).,
        : 20,
    },
});

Each page reads nextCursor off the raw body and asks for the next page; when the body has no cursor, next returns undefined and the loop stops. prevBody and value arrive as unknown — narrow them before you reach in.

Options

next(prevBody, pagesFetched) receives the previous page's raw body plus the page count and returns the StitchInput for the next page, merged over the original call — set only what changes, like a cursor or page number. Return undefined to stop.

items(value) selects the array to aggregate from each page's picked value; it defaults to the value itself when that value is already an array. The pick runs first, so pair items with pick when the array sits under an envelope, and use items to dig into a nested shape.

pages caps the page count as a safety net (default 50) so a misbehaving next can't loop forever.

Auth, retry, and throttle apply per page, not once for the whole run: a throttle of "2/s" paces the page loop, and a retry recovers each page on its own.

Anti-pattern: don't read the next-page cursor in next from the picked array, expecting it where items looks — next receives the raw body and items receives the value after pick runs, so read the cursor in next from where it lives in the raw response instead. See pick.

See also

On this page