// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. import type { OpenAI } from './client'; import { OpenAIError } from './error'; import { FinalRequestOptions } from './internal/request-options'; import { defaultParseResponse, APIResponseProps } from './internal/parse'; import { APIPromise } from './api-promise'; import { maybeObj } from './internal/utils'; export type PageRequestOptions = Pick; export abstract class AbstractPage implements AsyncIterable { #client: OpenAI; protected options: FinalRequestOptions; protected response: Response; protected body: unknown; constructor(client: OpenAI, response: Response, body: unknown, options: FinalRequestOptions) { this.#client = client; this.options = options; this.response = response; this.body = body; } abstract nextPageRequestOptions(): PageRequestOptions | null; abstract getPaginatedItems(): Item[]; hasNextPage(): boolean { const items = this.getPaginatedItems(); if (!items.length) return false; return this.nextPageRequestOptions() != null; } async getNextPage(): Promise { const nextOptions = this.nextPageRequestOptions(); if (!nextOptions) { throw new OpenAIError( 'No next page expected; please check `.hasNextPage()` before calling `.getNextPage()`.', ); } return await this.#client.requestAPIList(this.constructor as any, nextOptions); } async *iterPages(): AsyncGenerator { // eslint-disable-next-line @typescript-eslint/no-this-alias let page: this = this; yield page; while (page.hasNextPage()) { page = await page.getNextPage(); yield page; } } async *[Symbol.asyncIterator](): AsyncGenerator { for await (const page of this.iterPages()) { for (const item of page.getPaginatedItems()) { yield item; } } } } /** * This subclass of Promise will resolve to an instantiated Page once the request completes. * * It also implements AsyncIterable to allow auto-paginating iteration on an unawaited list call, eg: * * for await (const item of client.items.list()) { * console.log(item) * } */ export class PagePromise< PageClass extends AbstractPage, Item = ReturnType[number], > extends APIPromise implements AsyncIterable { constructor( client: OpenAI, request: Promise, Page: new (...args: ConstructorParameters) => PageClass, ) { super( request, async (props) => new Page(client, props.response, await defaultParseResponse(props), props.options), ); } /** * Allow auto-paginating iteration on an unawaited list call, eg: * * for await (const item of client.items.list()) { * console.log(item) * } */ async *[Symbol.asyncIterator]() { const page = await this; for await (const item of page) { yield item; } } } export interface PageResponse { data: Array; object: string; } /** * Note: no pagination actually occurs yet, this is for forwards-compatibility. */ export class Page extends AbstractPage implements PageResponse { data: Array; object: string; constructor(client: OpenAI, response: Response, body: PageResponse, options: FinalRequestOptions) { super(client, response, body, options); this.data = body.data || []; this.object = body.object; } getPaginatedItems(): Item[] { return this.data ?? []; } nextPageRequestOptions(): PageRequestOptions | null { return null; } } export interface CursorPageResponse { data: Array; } export interface CursorPageParams { after?: string; limit?: number; } export class CursorPage extends AbstractPage implements CursorPageResponse { data: Array; constructor( client: OpenAI, response: Response, body: CursorPageResponse, options: FinalRequestOptions, ) { super(client, response, body, options); this.data = body.data || []; } getPaginatedItems(): Item[] { return this.data ?? []; } nextPageRequestOptions(): PageRequestOptions | null { const data = this.getPaginatedItems(); const id = data[data.length - 1]?.id; if (!id) { return null; } return { ...this.options, query: { ...maybeObj(this.options.query), after: id, }, }; } }