UNPKG

@replit/connectors

Version:

The official TypeScript library for the Replit API

177 lines (146 loc) 4.47 kB
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. import { ReplitError } from './error'; import { FinalRequestOptions } from '../internal/request-options'; import { defaultParseResponse } from '../internal/parse'; import { type Replit } from '../client'; import { APIPromise } from './api-promise'; import { type APIResponseProps } from '../internal/parse'; import { maybeObj } from '../internal/utils/values'; export type PageRequestOptions = Pick<FinalRequestOptions, 'query' | 'headers' | 'body' | 'path' | 'method'>; export abstract class AbstractPage<Item> implements AsyncIterable<Item> { #client: Replit; protected options: FinalRequestOptions; protected response: Response; protected body: unknown; constructor(client: Replit, 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<this> { const nextOptions = this.nextPageRequestOptions(); if (!nextOptions) { throw new ReplitError( 'No next page expected; please check `.hasNextPage()` before calling `.getNextPage()`.', ); } return await this.#client.requestAPIList(this.constructor as any, nextOptions); } async *iterPages(): AsyncGenerator<this> { let page: this = this; yield page; while (page.hasNextPage()) { page = await page.getNextPage(); yield page; } } async *[Symbol.asyncIterator](): AsyncGenerator<Item> { 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>, Item = ReturnType<PageClass['getPaginatedItems']>[number], > extends APIPromise<PageClass> implements AsyncIterable<Item> { constructor( client: Replit, request: Promise<APIResponseProps>, Page: new (...args: ConstructorParameters<typeof AbstractPage>) => 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<Item> { const page = await this; for await (const item of page) { yield item; } } } export interface OffsetPaginationResponse<Item> { items: Array<Item>; total: number; offset: number; limit: number; } export interface OffsetPaginationParams { /** * The number of elements to skip. */ offset?: number; } export class OffsetPagination<Item> extends AbstractPage<Item> implements OffsetPaginationResponse<Item> { items: Array<Item>; total: number; offset: number; limit: number; constructor( client: Replit, response: Response, body: OffsetPaginationResponse<Item>, options: FinalRequestOptions, ) { super(client, response, body, options); this.items = body.items || []; this.total = body.total || 0; this.offset = body.offset || 0; this.limit = body.limit || 0; } getPaginatedItems(): Item[] { return this.items ?? []; } nextPageRequestOptions(): PageRequestOptions | null { const offset = this.offset ?? 0; const length = this.getPaginatedItems().length; const currentCount = offset + length; const totalCount = this.total; if (!totalCount) { return null; } if (currentCount < totalCount) { return { ...this.options, query: { ...maybeObj(this.options.query), offset: currentCount, }, }; } return null; } }