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 unwrapped value;
it defaults to the value itself when that value is already an array. The unwrap
runs first, so pair items with unwrap when the
array sits under an envelope, and use items to dig into a nested shape.
max 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.
next reads the raw body; items reads the value after unwrap. Reach for
the cursor in next from where it actually lives in the response, not from
the unwrapped array.