Packbase SDK

Pagination

Load long lists in pages with cursors, offsets, and page numbers.

An API usually does not return thousands of items in one response. It returns a smaller set of items called a page. The response also contains a method to request the next page. This process is pagination.

Packbase uses different pagination methods for different resources. The inbox uses a cursor. Search uses an offset. Some endpoints return the full list.

Cursor-based pagination

The inbox uses cursor-based pagination. A cursor is a bookmark that the server returns. Do not read or change the cursor. Save the value and send it unchanged in the next request.

The first request does not require a cursor. Its response contains a cursor for the next request. The has_more Boolean shows whether another page is available.

This method lets new notifications arrive while you request older notifications.

Paginated<T> shape

import type { , Notification } from "@packbase/sdk-ts";

// Paginated<T> wraps any list response
type  = <Notification>;
  • data: the items in this page.
  • has_more: true if another page is available.
  • cursor: the bookmark to pass with the next request.
  • total_count: the total number of matching items, when the endpoint provides it.

Example: fetch all inbox notifications

async function () {
  const  = [];
  let : string | undefined;

  let  = true;
  do {
    const page = await ..({ : 50,  });
const page: Paginated<Notification>
.(....); = .; = .; } while (); return ; }

The loop always makes one request. After each response, it saves the cursor and checks has_more. A large inbox can cause this loop to make many requests. For a large inbox, use a "Load more" button.

Pack members use cursors

Pack member pages default to 50 and are capped at 100. Request subsequent pages on demand; keep the membership IDs and cursors as strings:

async function (: string, ?: string) {
  return .().({ : 50,  });
}

"Load more" UI pattern

For a button interface, keep the loaded items, latest cursor, and hasMore value in the application state. This example omits framework-specific state code:

async function () {
  const page = await ..({ : 20,  });
const page: Paginated<Notification>
= [..., ....]; = .; = .; }

Call loadMore() once for the first page and again whenever the user asks for more. Disable or hide the button when hasMore becomes false.

Offset pagination with the search builder

Search uses offset-based pagination. An offset is the number of matching items to omit. .take(25).skip(0) requests the first 25 items. .take(25).skip(25) requests the next 25 items.

Calculate these numbers with .skip() and .take(). Alternatively, use .page():

// Page 1 (items 0-24)
const  = ("packs").("created_at").(25).(0).();

// Page 2 (items 25-49)
const  = ("packs").("created_at").(25).(25).();

// Or use the helper (1-indexed)
const  = ("packs").("created_at").(3, 25).();
// Equivalent: take(25).skip(50)

You can request multiple pages at the same time with Promise.all(). Most interfaces request a page only when it is necessary:

const [{  }, {  }] = await .([
  .({ : ("packs").(1, 20).() }),
  .({ : ("packs").(2, 20).() }),
]);

Custom feed pages

A runFeed() result pages by number: pass page (from 1, at most 1000) and continue while has_more is true. Page 1 returns the feed's presentation and every declared query; later pages return only the results listed in paged. The feed's owner also chooses how readers page, and every result reports it as pagination:

const { feed } = await pb.search({ feed: runFeed(feedId, { page }) })
if (!isErrorEntry(feed)) {
  if (feed.pagination === 'paged') {
    // Show this page alone, with Previous (page > 1) and Next (feed.has_more).
  } else {
    // 'infinite': append this page and request page + 1 when the reader scrolls near the end.
  }
}

Keep page 1's presentation for the templates of later pages. See feed.json pagination.

Select the pagination method

The endpoint specifies the pagination method. Examine the return value and use the applicable continuation value:

ResourceStrategyKey
InboxCursorcursor + has_more
Pack membersCursorcursor + has_more; decimal strings
Access codesCursornext_cursor; stop when absent or null
Pack reports, unanswered posts, tag and username historyCursorcursor + has_more
FeedsPage numberpage, starting at 1, and has_more
Custom feed runsPage numberpage + has_more; pagination mode
SearchOffset.skip() / .page()

On this page