cloudflare/cloudflare-typescript

Public

mirrored fromhttps://github.com/cloudflare/cloudflare-typescriptAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
generated-49ad31a149

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/pagination.ts

299lines · modecode

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