cloudflare
Version:
The official TypeScript library for the Cloudflare API
256 lines • 8.15 kB
JavaScript
var _AbstractPage_client;
import { __classPrivateFieldGet, __classPrivateFieldSet } from "../internal/tslib.mjs";
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
import { CloudflareError } from "./error.mjs";
import { defaultParseResponse } from "../internal/parse.mjs";
import { APIPromise } from "./api-promise.mjs";
import { maybeObj } from "../internal/utils/values.mjs";
export var AbstractPage = /* @__PURE__ */ (() => {
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 CloudflareError('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;
}
}
}
}
return AbstractPage;
})();
/**
* 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 var PagePromise = /* @__PURE__ */ (() => {
/**
* 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)
* }
*/
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;
}
}
}
return PagePromise;
})();
export class V4PagePagination extends AbstractPage {
constructor(client, response, body, options) {
super(client, response, body, options);
this.result = body.result || {};
this.result_info = body.result_info || {};
}
getPaginatedItems() {
return this.result?.items ?? [];
}
nextPageRequestOptions() {
const query = this.options.query;
const currentPage = query?.page ?? 1;
return {
...this.options,
query: {
...maybeObj(this.options.query),
page: currentPage + 1,
},
};
}
}
export class V4PagePaginationArray extends AbstractPage {
constructor(client, response, body, options) {
super(client, response, body, options);
this.result = body.result || [];
this.result_info = body.result_info || {};
}
getPaginatedItems() {
return this.result ?? [];
}
nextPageRequestOptions() {
const query = this.options.query;
const currentPage = query?.page ?? 1;
return {
...this.options,
query: {
...maybeObj(this.options.query),
page: currentPage + 1,
},
};
}
}
export class CursorPagination extends AbstractPage {
constructor(client, response, body, options) {
super(client, response, body, options);
this.result = body.result || [];
this.result_info = body.result_info || {};
}
getPaginatedItems() {
return this.result ?? [];
}
nextPageRequestOptions() {
const cursor = this.result_info?.cursor;
if (!cursor) {
return null;
}
return {
...this.options,
query: {
...maybeObj(this.options.query),
cursor,
},
};
}
}
export class CursorPaginationAfter extends AbstractPage {
constructor(client, response, body, options) {
super(client, response, body, options);
this.result = body.result || [];
this.result_info = body.result_info || {};
}
getPaginatedItems() {
return this.result ?? [];
}
nextPageRequestOptions() {
const cursor = this.result_info?.cursors?.after;
if (!cursor) {
return null;
}
return {
...this.options,
query: {
...maybeObj(this.options.query),
cursor,
},
};
}
}
export class CursorLimitPagination extends AbstractPage {
constructor(client, response, body, options) {
super(client, response, body, options);
this.result = body.result || [];
this.result_info = body.result_info || {};
}
getPaginatedItems() {
return this.result ?? [];
}
nextPageRequestOptions() {
const cursor = this.result_info?.cursor;
if (!cursor) {
return null;
}
return {
...this.options,
query: {
...maybeObj(this.options.query),
cursor,
},
};
}
}
export class ContainersInstancesV1Pagination extends AbstractPage {
constructor(client, response, body, options) {
super(client, response, body, options);
this.result = body.result || {};
this.result_info = body.result_info || {};
}
getPaginatedItems() {
return this.result?.instances ?? [];
}
nextPageRequestOptions() {
const cursor = this.result_info?.next_page_token;
if (!cursor) {
return null;
}
return {
...this.options,
query: {
...maybeObj(this.options.query),
page_token: cursor,
},
};
}
}
export class PageTokenPagination extends AbstractPage {
constructor(client, response, body, options) {
super(client, response, body, options);
this.result = body.result || [];
this.result_info = body.result_info || {};
}
getPaginatedItems() {
return this.result ?? [];
}
nextPageRequestOptions() {
const cursor = this.result_info?.next_page_token;
if (!cursor) {
return null;
}
return {
...this.options,
query: {
...maybeObj(this.options.query),
page_token: cursor,
},
};
}
}
export class SinglePage extends AbstractPage {
constructor(client, response, body, options) {
super(client, response, body, options);
this.result = body.result || [];
}
getPaginatedItems() {
return this.result ?? [];
}
nextPageRequestOptions() {
return null;
}
}
//# sourceMappingURL=pagination.mjs.map