| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
- var _AbstractPage_client;
- import { __classPrivateFieldGet, __classPrivateFieldSet } from "../internal/tslib.mjs";
- import { AnthropicError } from "./error.mjs";
- import { defaultParseResponse } from "../internal/parse.mjs";
- import { APIPromise } from "./api-promise.mjs";
- import { maybeObj } from "../internal/utils/values.mjs";
- export class AbstractPage {
- constructor(client, response, body, options) {
- _AbstractPage_client.set(this, void 0);
- __classPrivateFieldSet(this, _AbstractPage_client, client, "f");
- this.options = options;
- this.response = response;
- this.body = body;
- }
- hasNextPage() {
- const items = this.getPaginatedItems();
- if (!items.length)
- return false;
- return this.nextPageRequestOptions() != null;
- }
- async getNextPage() {
- const nextOptions = this.nextPageRequestOptions();
- if (!nextOptions) {
- throw new AnthropicError('No next page expected; please check `.hasNextPage()` before calling `.getNextPage()`.');
- }
- return await __classPrivateFieldGet(this, _AbstractPage_client, "f").requestAPIList(this.constructor, nextOptions);
- }
- async *iterPages() {
- let page = this;
- yield page;
- while (page.hasNextPage()) {
- page = await page.getNextPage();
- yield page;
- }
- }
- async *[(_AbstractPage_client = new WeakMap(), Symbol.asyncIterator)]() {
- 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 extends APIPromise {
- constructor(client, request, Page) {
- 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]() {
- const page = await this;
- for await (const item of page) {
- yield item;
- }
- }
- }
- export class Page extends AbstractPage {
- constructor(client, response, body, options) {
- super(client, response, body, options);
- this.data = body.data || [];
- this.has_more = body.has_more || false;
- this.first_id = body.first_id || null;
- this.last_id = body.last_id || null;
- }
- getPaginatedItems() {
- return this.data ?? [];
- }
- hasNextPage() {
- if (this.has_more === false) {
- return false;
- }
- return super.hasNextPage();
- }
- nextPageRequestOptions() {
- if (this.options.query?.['before_id']) {
- // in reverse
- const first_id = this.first_id;
- if (!first_id) {
- return null;
- }
- return {
- ...this.options,
- query: {
- ...maybeObj(this.options.query),
- before_id: first_id,
- },
- };
- }
- const cursor = this.last_id;
- if (!cursor) {
- return null;
- }
- return {
- ...this.options,
- query: {
- ...maybeObj(this.options.query),
- after_id: cursor,
- },
- };
- }
- }
- export class TokenPage extends AbstractPage {
- constructor(client, response, body, options) {
- super(client, response, body, options);
- this.data = body.data || [];
- this.has_more = body.has_more || false;
- this.next_page = body.next_page || null;
- }
- getPaginatedItems() {
- return this.data ?? [];
- }
- hasNextPage() {
- if (this.has_more === false) {
- return false;
- }
- return super.hasNextPage();
- }
- nextPageRequestOptions() {
- const cursor = this.next_page;
- if (!cursor) {
- return null;
- }
- return {
- ...this.options,
- query: {
- ...maybeObj(this.options.query),
- page_token: cursor,
- },
- };
- }
- }
- export class PageCursor extends AbstractPage {
- constructor(client, response, body, options) {
- super(client, response, body, options);
- this.data = body.data || [];
- this.has_more = body.has_more || false;
- this.next_page = body.next_page || null;
- }
- getPaginatedItems() {
- return this.data ?? [];
- }
- hasNextPage() {
- if (this.has_more === false) {
- return false;
- }
- return super.hasNextPage();
- }
- nextPageRequestOptions() {
- const cursor = this.next_page;
- if (!cursor) {
- return null;
- }
- return {
- ...this.options,
- query: {
- ...maybeObj(this.options.query),
- page: cursor,
- },
- };
- }
- }
- //# sourceMappingURL=pagination.mjs.map
|