// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. import { ScenarioError } from './error'; import { FinalRequestOptions } from '../internal/request-options'; import { defaultParseResponse } from '../internal/parse'; import { type Scenario } from '../client'; import { APIPromise } from './api-promise'; import { type APIResponseProps } from '../internal/parse'; import { maybeObj } from '../internal/utils/values'; export type PageRequestOptions = Pick; export abstract class AbstractPage implements AsyncIterable { #client: Scenario; protected options: FinalRequestOptions; protected response: Response; protected body: unknown; constructor(client: Scenario, 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 ScenarioError( 'No next page expected; please check `.hasNextPage()` before calling `.getNextPage()`.', ); } return await this.#client.requestAPIList(this.constructor as any, nextOptions); } async *iterPages(): AsyncGenerator { 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: Scenario, request: Promise, Page: new (...args: ConstructorParameters) => PageClass, ) { super( client, request, async (client, props) => new Page(client, props.response, await defaultParseResponse(client, 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](): AsyncGenerator { const page = await this; for await (const item of page) { yield item; } } } export interface AssetsCursorResponse { assets: Array; nextPaginationToken: string; } export interface AssetsCursorParams { paginationToken?: string; } export class AssetsCursor extends AbstractPage implements AssetsCursorResponse { assets: Array; nextPaginationToken: string; constructor( client: Scenario, response: Response, body: AssetsCursorResponse, options: FinalRequestOptions, ) { super(client, response, body, options); this.assets = body.assets || []; this.nextPaginationToken = body.nextPaginationToken || ''; } getPaginatedItems(): Item[] { return this.assets ?? []; } nextPageRequestOptions(): PageRequestOptions | null { const cursor = this.nextPaginationToken; if (!cursor) { return null; } return { ...this.options, query: { ...maybeObj(this.options.query), paginationToken: cursor, }, }; } } export interface SnapshotsCursorResponse { nextPaginationToken: string; snapshots: Array; } export interface SnapshotsCursorParams { paginationToken?: string; } export class SnapshotsCursor extends AbstractPage implements SnapshotsCursorResponse { nextPaginationToken: string; snapshots: Array; constructor( client: Scenario, response: Response, body: SnapshotsCursorResponse, options: FinalRequestOptions, ) { super(client, response, body, options); this.nextPaginationToken = body.nextPaginationToken || ''; this.snapshots = body.snapshots || []; } getPaginatedItems(): Item[] { return this.snapshots ?? []; } nextPageRequestOptions(): PageRequestOptions | null { const cursor = this.nextPaginationToken; if (!cursor) { return null; } return { ...this.options, query: { ...maybeObj(this.options.query), paginationToken: cursor, }, }; } } export interface CollectionsCursorResponse { collections: Array; nextPaginationToken: string; } export interface CollectionsCursorParams { paginationToken?: string; } export class CollectionsCursor extends AbstractPage implements CollectionsCursorResponse { collections: Array; nextPaginationToken: string; constructor( client: Scenario, response: Response, body: CollectionsCursorResponse, options: FinalRequestOptions, ) { super(client, response, body, options); this.collections = body.collections || []; this.nextPaginationToken = body.nextPaginationToken || ''; } getPaginatedItems(): Item[] { return this.collections ?? []; } nextPageRequestOptions(): PageRequestOptions | null { const cursor = this.nextPaginationToken; if (!cursor) { return null; } return { ...this.options, query: { ...maybeObj(this.options.query), paginationToken: cursor, }, }; } } export interface JobsCursorResponse { jobs: Array; nextPaginationToken: string; } export interface JobsCursorParams { paginationToken?: string; } export class JobsCursor extends AbstractPage implements JobsCursorResponse { jobs: Array; nextPaginationToken: string; constructor( client: Scenario, response: Response, body: JobsCursorResponse, options: FinalRequestOptions, ) { super(client, response, body, options); this.jobs = body.jobs || []; this.nextPaginationToken = body.nextPaginationToken || ''; } getPaginatedItems(): Item[] { return this.jobs ?? []; } nextPageRequestOptions(): PageRequestOptions | null { const cursor = this.nextPaginationToken; if (!cursor) { return null; } return { ...this.options, query: { ...maybeObj(this.options.query), paginationToken: cursor, }, }; } } export interface ModelsCursorResponse { models: Array; nextPaginationToken: string; } export interface ModelsCursorParams { paginationToken?: string; } export class ModelsCursor extends AbstractPage implements ModelsCursorResponse { models: Array; nextPaginationToken: string; constructor( client: Scenario, response: Response, body: ModelsCursorResponse, options: FinalRequestOptions, ) { super(client, response, body, options); this.models = body.models || []; this.nextPaginationToken = body.nextPaginationToken || ''; } getPaginatedItems(): Item[] { return this.models ?? []; } nextPageRequestOptions(): PageRequestOptions | null { const cursor = this.nextPaginationToken; if (!cursor) { return null; } return { ...this.options, query: { ...maybeObj(this.options.query), paginationToken: cursor, }, }; } } export interface TagsCursorResponse { nextPaginationToken: string; tags: Array; } export interface TagsCursorParams { paginationToken?: string; } export class TagsCursor extends AbstractPage implements TagsCursorResponse { nextPaginationToken: string; tags: Array; constructor( client: Scenario, response: Response, body: TagsCursorResponse, options: FinalRequestOptions, ) { super(client, response, body, options); this.nextPaginationToken = body.nextPaginationToken || ''; this.tags = body.tags || []; } getPaginatedItems(): Item[] { return this.tags ?? []; } nextPageRequestOptions(): PageRequestOptions | null { const cursor = this.nextPaginationToken; if (!cursor) { return null; } return { ...this.options, query: { ...maybeObj(this.options.query), paginationToken: cursor, }, }; } } export interface WorkflowsCursorResponse { nextPaginationToken: string; workflows: Array; } export interface WorkflowsCursorParams { paginationToken?: string; } export class WorkflowsCursor extends AbstractPage implements WorkflowsCursorResponse { nextPaginationToken: string; workflows: Array; constructor( client: Scenario, response: Response, body: WorkflowsCursorResponse, options: FinalRequestOptions, ) { super(client, response, body, options); this.nextPaginationToken = body.nextPaginationToken || ''; this.workflows = body.workflows || []; } getPaginatedItems(): Item[] { return this.workflows ?? []; } nextPageRequestOptions(): PageRequestOptions | null { const cursor = this.nextPaginationToken; if (!cursor) { return null; } return { ...this.options, query: { ...maybeObj(this.options.query), paginationToken: cursor, }, }; } } export interface SearchHitsOffsetResponse { estimatedTotalHits: number; hits: Array; offset: number; } export interface SearchHitsOffsetParams { offset?: number; } export class SearchHitsOffset extends AbstractPage implements SearchHitsOffsetResponse { estimatedTotalHits: number; hits: Array; offset: number; constructor( client: Scenario, response: Response, body: SearchHitsOffsetResponse, options: FinalRequestOptions, ) { super(client, response, body, options); this.estimatedTotalHits = body.estimatedTotalHits || 0; this.hits = body.hits || []; this.offset = body.offset || 0; } getPaginatedItems(): Item[] { return this.hits ?? []; } nextPageRequestOptions(): PageRequestOptions | null { const offset = this.offset ?? 0; const length = this.getPaginatedItems().length; const currentCount = offset + length; const totalCount = this.estimatedTotalHits; if (!totalCount) { return null; } if (currentCount < totalCount) { return { ...this.options, body: { ...maybeObj(this.options.body), offset: currentCount, }, }; } return null; } }