cloudflare/cloudflare-typescript

Public

mirrored from https://github.com/cloudflare/cloudflare-typescriptAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
generated-ef0636dc0f

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/pagination.ts

299lines · modeblame

2ad8c2a2stainless-app[bot]2 years ago1// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
5c7031d8stainless-app[bot]2 years ago2
3import { AbstractPage, Response, APIClient, FinalRequestOptions, PageInfo } from './core';
4
5export interface V4PagePaginationResponse<Item> {
dcafa07bstainless-app[bot]2 years ago6result: V4PagePaginationResponse.Result<Item>;
5c7031d8stainless-app[bot]2 years ago7
8result_info: V4PagePaginationResponse.ResultInfo;
9}
10
11export namespace V4PagePaginationResponse {
dcafa07bstainless-app[bot]2 years ago12export interface Result<Item> {
5c7031d8stainless-app[bot]2 years ago13items?: Array<Item>;
14}
15
16export interface ResultInfo {
17page?: number;
18
19per_page?: number;
20}
21}
22
23export interface V4PagePaginationParams {
24page?: number;
25
26per_page?: number;
27}
28
29export class V4PagePagination<Item> extends AbstractPage<Item> implements V4PagePaginationResponse<Item> {
dcafa07bstainless-app[bot]2 years ago30result: V4PagePaginationResponse.Result<Item>;
5c7031d8stainless-app[bot]2 years ago31
32result_info: V4PagePaginationResponse.ResultInfo;
33
34constructor(
35client: APIClient,
36response: Response,
37body: V4PagePaginationResponse<Item>,
38options: FinalRequestOptions,
39) {
40super(client, response, body, options);
41
42this.result = body.result || {};
43this.result_info = body.result_info || {};
44}
45
46getPaginatedItems(): Item[] {
47return this.result?.items ?? [];
48}
49
50// @deprecated Please use `nextPageInfo()` instead
51nextPageParams(): Partial<V4PagePaginationParams> | null {
52const info = this.nextPageInfo();
53if (!info) return null;
54if ('params' in info) return info.params;
55const params = Object.fromEntries(info.url.searchParams);
56if (!Object.keys(params).length) return null;
57return params;
58}
59
60nextPageInfo(): PageInfo | null {
61const query = this.options.query as V4PagePaginationParams;
62const currentPage = query?.page ?? 1;
63
64return { params: { page: currentPage + 1 } };
65}
66}
67
68export interface V4PagePaginationArrayResponse<Item> {
69result: Array<Item>;
70
71result_info: V4PagePaginationArrayResponse.ResultInfo;
72}
73
74export namespace V4PagePaginationArrayResponse {
75export interface ResultInfo {
76page?: number;
77
78per_page?: number;
79}
80}
81
82export interface V4PagePaginationArrayParams {
83page?: number;
84
85per_page?: number;
86}
87
88export class V4PagePaginationArray<Item>
89extends AbstractPage<Item>
90implements V4PagePaginationArrayResponse<Item>
91{
92result: Array<Item>;
93
94result_info: V4PagePaginationArrayResponse.ResultInfo;
95
96constructor(
97client: APIClient,
98response: Response,
99body: V4PagePaginationArrayResponse<Item>,
100options: FinalRequestOptions,
101) {
102super(client, response, body, options);
103
104this.result = body.result || [];
105this.result_info = body.result_info || {};
106}
107
108getPaginatedItems(): Item[] {
109return this.result ?? [];
110}
111
112// @deprecated Please use `nextPageInfo()` instead
113nextPageParams(): Partial<V4PagePaginationArrayParams> | null {
114const info = this.nextPageInfo();
115if (!info) return null;
116if ('params' in info) return info.params;
117const params = Object.fromEntries(info.url.searchParams);
118if (!Object.keys(params).length) return null;
119return params;
120}
121
122nextPageInfo(): PageInfo | null {
123const query = this.options.query as V4PagePaginationArrayParams;
124const currentPage = query?.page ?? 1;
125
126return { params: { page: currentPage + 1 } };
127}
128}
fc0e3df7stainless-app[bot]2 years ago129
130export interface CursorPaginationResponse<Item> {
131result: Array<Item>;
132
133result_info: CursorPaginationResponse.ResultInfo;
134}
135
136export namespace CursorPaginationResponse {
137export interface ResultInfo {
138count?: number;
139
140cursor?: string;
141
142per_page?: number;
143}
144}
145
146export interface CursorPaginationParams {
32d23492stainless-app[bot]2 years ago147per_page?: number;
fc0e3df7stainless-app[bot]2 years ago148
149cursor?: string;
150}
151
152export class CursorPagination<Item> extends AbstractPage<Item> implements CursorPaginationResponse<Item> {
153result: Array<Item>;
154
155result_info: CursorPaginationResponse.ResultInfo;
156
157constructor(
158client: APIClient,
159response: Response,
160body: CursorPaginationResponse<Item>,
161options: FinalRequestOptions,
162) {
163super(client, response, body, options);
164
165this.result = body.result || [];
166this.result_info = body.result_info || {};
167}
168
169getPaginatedItems(): Item[] {
170return this.result ?? [];
171}
172
173// @deprecated Please use `nextPageInfo()` instead
174nextPageParams(): Partial<CursorPaginationParams> | null {
175const info = this.nextPageInfo();
176if (!info) return null;
177if ('params' in info) return info.params;
178const params = Object.fromEntries(info.url.searchParams);
179if (!Object.keys(params).length) return null;
180return params;
181}
182
183nextPageInfo(): PageInfo | null {
184const cursor = this.result_info?.cursor;
185if (!cursor) {
186return null;
187}
188
189return {
190params: {
191cursor: cursor,
192},
193};
194}
195}
32d23492stainless-app[bot]2 years ago196
197export interface CursorLimitPaginationResponse<Item> {
198result: Array<Item>;
199
200result_info: CursorLimitPaginationResponse.ResultInfo;
201}
202
203export namespace CursorLimitPaginationResponse {
204export interface ResultInfo {
205count?: number;
206
207cursor?: string;
208
209per_page?: number;
210}
211}
212
213export interface CursorLimitPaginationParams {
214limit?: number;
215
216cursor?: string;
217}
218
219export class CursorLimitPagination<Item>
220extends AbstractPage<Item>
221implements CursorLimitPaginationResponse<Item>
222{
223result: Array<Item>;
224
225result_info: CursorLimitPaginationResponse.ResultInfo;
226
227constructor(
228client: APIClient,
229response: Response,
230body: CursorLimitPaginationResponse<Item>,
231options: FinalRequestOptions,
232) {
233super(client, response, body, options);
234
235this.result = body.result || [];
236this.result_info = body.result_info || {};
237}
238
239getPaginatedItems(): Item[] {
240return this.result ?? [];
241}
242
243// @deprecated Please use `nextPageInfo()` instead
244nextPageParams(): Partial<CursorLimitPaginationParams> | null {
245const info = this.nextPageInfo();
246if (!info) return null;
247if ('params' in info) return info.params;
248const params = Object.fromEntries(info.url.searchParams);
249if (!Object.keys(params).length) return null;
250return params;
251}
252
253nextPageInfo(): PageInfo | null {
254const cursor = this.result_info?.cursor;
255if (!cursor) {
256return null;
257}
258
259return {
260params: {
261cursor: cursor,
262},
263};
264}
265}
91f8b664stainless-app[bot]2 years ago266
267export type SinglePageResponse<Item> = Item[];
268
269export class SinglePage<Item> extends AbstractPage<Item> {
270items: Array<Item>;
271
272constructor(
273client: APIClient,
274response: Response,
275body: SinglePageResponse<Item>,
276options: FinalRequestOptions,
277) {
278super(client, response, body, options);
279
280this.items = body || [];
281}
282
283getPaginatedItems(): Item[] {
284return this.items ?? [];
285}
286
287// @deprecated Please use `nextPageInfo()` instead
288/**
289* This page represents a response that isn't actually paginated at the API level
290* so there will never be any next page params.
291*/
292nextPageParams(): null {
293return null;
294}
295
296nextPageInfo(): null {
297return null;
298}
299}