Skip to Content

Pagination

Page through a list with Fetch.paginate, with the cursor in the body or in a header.

Lists are often split into pages: you ask for the first, and the response says how to get the next. Writing that loop by hand for every list means tracking the state, stopping at the end, and not fetching pages nobody reads. Fetch.paginate follows the pages for you, as a Stream of pages. This page shows how to page through a list, with the next page's state in the body or in a header.

Page through a list

Fetch.paginate calls a bound endpoint for the first page, then for the next one, until next says there's none:

import * as  from "effect/Effect";
import * as  from "effect/Option";
import * as  from "effect/Schema";
import * as  from "effect/Stream";

import {  } from "@monarcode/effetch";

const  = .({ : ., : . });

const  = .("GET /todos", {
  : .({ : . }),
  : .(),
});

class  extends .<>()("Api", {
  : "https://api.example.com",
}) {}

class  extends .<>()("TodoApi", , {
  : ,
}) {}

export const  = .(function* () {
  const  = yield* ;
  return yield* .(., {
    : 1,
    : () => ({ : {  } }),
    : (, ) =>
      . === 0 ? .() : .( + 1),
  }).(., .);
});

Fetch.paginate takes three options:

  • start is the state of the first page, such as a page number or a cursor.
  • request(state) gives the options for that page's call: params, query, body and headers, typed by the endpoint. Plugin overrides aren't among them, so give those when you bind the endpoint, and every page's call uses them. The endpoint's responseMode always decides what each page is.
  • next(page, state, response) gives the next page's state, or Option.none() after the last page. response is the page's HttpClientResponse when the endpoint's responseMode is "decoded-and-response", and undefined otherwise.

Each page is one value of the stream, so a page that's a list stays a list. Stream.flattenIterable gives its items one by one.

How pages are fetched

The stream is lazy: it sends a page's call only when the page is needed, so Stream.take(1) sends one. A call that fails fails the stream with the call's error.

Fetch.paginate takes a bound endpoint, from Fetch.Endpoints, api.bind, api.bindAll or api.endpoint. It throws when you pass it a definition made by Fetch.endpoint, which needs binding first, or a stream endpoint, which can't be paged.

Read the cursor from a header

Some APIs put the next page's cursor in a header, such as a Link header, rather than in the body. For those, define the endpoint with responseMode: "decoded-and-response", and next gets each page's response:

import * as  from "effect/Option";
import * as  from "effect/Schema";

import {  } from "@monarcode/effetch";

declare const : .;

const  = .({ : . });

const  = .("GET /items", {
  : .(),
  : "decoded-and-response",
});

// The cursor is percent-encoded in the URL, and `query` encodes it again.
const  = (: string | undefined) =>
  .(
    /[?&]cursor=([^&>]+)[^>]*>;\s*rel="next"/.( ?? "")?.[1],
  ).(.(.()));

export const  = .(.(), {
  :  as string | undefined,
  : () => ( ===  ? {} : { : {  } }),
  : (, , ) => (..),
});

Each page is still the decoded body, here the array of items. Only next gets the response.

What's next

On this page