cloudflare/cloudflare-typescript

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
fc0e3df7a7617630a04034afaebe2b244b14213c

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/pagination.ts

195lines · modecode

1// File generated from our OpenAPI spec by Stainless.
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 limit?: 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}