Stainless provides helpers for easily implementing pagination that follows the pristine convention.
pageAfter(optional string) - if given, this should be theendCursorof the previous page. The API will fetch items immediately after this cursor.pageBefore(optional string) - if given, this should be thestartCursorof the next page. The API will fetch items immediately before this cursor.pageSize(optional number) - the number of items to include in the resultsortBy(optional string) - an enum constant indicating the field(s) to sort bysortDirection(optional,"asc"or"desc") - the sort direction
z.PaginationParams provides a Zod schema for these parameters.
items(array) - up topageSizeitemsstartCursor(string or null) - the cursor of the first item initems, ornullifitemsis emptyendCursor(string or null) - the cursor of the last item initems, ornullifitemsis emptyhasNextPage(optional boolean) - whether there are more items after this page. May be omitted when paginating backward withpageBefore.hasPreviousPage(optional boolean) - whether there are more items before this page. May be omitted when paginating backward withpageBefore.
z.pageResponse provides a Zod schema for these response fields.
Paginating backwards does not change the sort order; in other words,
if we have page A, and we get page B = pageAfter: A.endCursor,
then get page C = pageBefore: B.startCursor, C should be deep
equal to A as long as no underlying data has changed.
stainless exports the following schemas and functions from
import { z } from 'stainless':
A Zod schema for the base request query parameters. You may call z.PaginationParams.extend({...}) to override defaults or add parameters to it.
Creates a Zod schema for a page response with the given item type
schema.
The output type of z.pageResponse(item: I).
Extracts the item type from the page data type D.
A Zod schema for a page response whose items are any type.
A Zod schema for sortDirection ("asc" | "desc").
The @stl-api/prisma plugin makes it easy to implement paginated
list endpoints from Prisma schemas.