Skip to content

Pagination

List methods return a PagedList. await it for the first page (the { object: "list", data, has_more, next_cursor } envelope), or iterate it with for await to read every item, page by page, as you go. .toArray({ limit }) collects items into an array. Nothing is requested until you use the list.

Methods that return one: mv.assistants.list(), mv.calls.list(), mv.calls.listCopilotEvents(), mv.campaigns.contacts.list(), mv.campaigns.contacts.listAttempts(), mv.campaigns.list(), mv.connections.list(), mv.consents.list(), mv.dnc.list(), mv.events.list(), mv.inboundRoutes.list(), mv.phoneNumbers.list(), mv.voices.list(), mv.webhookDeliveries.list(), mv.webhookEndpoints.list().

import { createClient, createResources } from "@morevoice/sdk";
const mv = createResources({
client: createClient({ baseUrl: "https://api.morevoice.ai/v1", auth: process.env.MOREVOICE_API_KEY }),
});
for await (const assistant of mv.assistants.list()) {
console.log(assistant);
}

Exported from @morevoice/sdk (source: packages/sdk-ts/src/lib/pagination.ts).

interface

One page of a /v1 list.

interface ListPage<T> {
object: "list";
data: T[];
has_more: boolean;
/** Pass back as `starting_after` (or `ending_before` when paging backwards) for the next page. */
next_cursor: string | null;
url?: string;
}
interface

The cursor parameters every /v1 list accepts (plus its own filters).

interface CursorParams {
/** Page size, 1–100 (server default 20). */
limit?: number;
starting_after?: string;
ending_before?: string;
}
type
type ListFn<T, P> = (params: P) => Promise<ListPage<T>>;
class

A lazily paged list: await it for the first page, for await it for every item, .pages() for every page, .toArray({ limit }) to collect. Nothing is requested until one of these is used; the first page is fetched once and shared between them.

class PagedList<T, P extends CursorParams = CursorParams> implements PromiseLike<ListPage<T>>, AsyncIterable<T> {
constructor(listFn: ListFn<T, P>, params: P);
/** The first page. */
firstPage(): Promise<ListPage<T>>;
then<R1 = ListPage<T>, R2 = never>(onfulfilled?: ((page: ListPage<T>) => R1 | PromiseLike<R1>) | null, onrejected?: ((reason: unknown) => R2 | PromiseLike<R2>) | null): Promise<R1 | R2>;
/** Every page, in order, fetching the next only when the previous one has been consumed. */
async *pages(): AsyncGenerator<ListPage<T>, void, undefined>;
async *[Symbol.asyncIterator](): AsyncGenerator<T, void, undefined>;
/** Collect up to `limit` items (required, so an unbounded list never fills memory by accident). */
async toArray(options: { limit: number }): Promise<T[]>;
}
function

Wrap a single-page list call (the generated one, unwrapped) into an auto-paginating list.

function autoPaginate<T, P extends CursorParams = CursorParams>(listFn: ListFn<T, P>, params: P = {} as P): PagedList<T, P>;