cloudflare/cloudflare-typescript
Publicmirrored fromhttps://github.com/cloudflare/cloudflare-typescriptAvailable
src/pagination.ts
301lines · modecode
| 1 | // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
| 2 | |
| 3 | import { AbstractPage, Response, APIClient, FinalRequestOptions, PageInfo } from './core'; |
| 4 | |
| 5 | export interface V4PagePaginationResponse<Item> { |
| 6 | result: V4PagePaginationResponse.Result<Item>; |
| 7 | |
| 8 | result_info: V4PagePaginationResponse.ResultInfo; |
| 9 | } |
| 10 | |
| 11 | export 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 | |
| 23 | export interface V4PagePaginationParams { |
| 24 | page?: number; |
| 25 | |
| 26 | per_page?: number; |
| 27 | } |
| 28 | |
| 29 | export 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 | |
| 68 | export interface V4PagePaginationArrayResponse<Item> { |
| 69 | result: Array<Item>; |
| 70 | |
| 71 | result_info: V4PagePaginationArrayResponse.ResultInfo; |
| 72 | } |
| 73 | |
| 74 | export namespace V4PagePaginationArrayResponse { |
| 75 | export interface ResultInfo { |
| 76 | page?: number; |
| 77 | |
| 78 | per_page?: number; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | export interface V4PagePaginationArrayParams { |
| 83 | page?: number; |
| 84 | |
| 85 | per_page?: number; |
| 86 | } |
| 87 | |
| 88 | export 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 | |
| 130 | export interface CursorPaginationResponse<Item> { |
| 131 | result: Array<Item>; |
| 132 | |
| 133 | result_info: CursorPaginationResponse.ResultInfo; |
| 134 | } |
| 135 | |
| 136 | export namespace CursorPaginationResponse { |
| 137 | export interface ResultInfo { |
| 138 | count?: number; |
| 139 | |
| 140 | cursor?: string; |
| 141 | |
| 142 | per_page?: number; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | export interface CursorPaginationParams { |
| 147 | per_page?: number; |
| 148 | |
| 149 | cursor?: string; |
| 150 | } |
| 151 | |
| 152 | export 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 | |
| 197 | export interface CursorLimitPaginationResponse<Item> { |
| 198 | result: Array<Item>; |
| 199 | |
| 200 | result_info: CursorLimitPaginationResponse.ResultInfo; |
| 201 | } |
| 202 | |
| 203 | export namespace CursorLimitPaginationResponse { |
| 204 | export interface ResultInfo { |
| 205 | count?: number; |
| 206 | |
| 207 | cursor?: string; |
| 208 | |
| 209 | per_page?: number; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | export interface CursorLimitPaginationParams { |
| 214 | limit?: number; |
| 215 | |
| 216 | cursor?: string; |
| 217 | } |
| 218 | |
| 219 | export 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 | |
| 267 | export interface SinglePageResponse<Item> { |
| 268 | result: Array<Item>; |
| 269 | } |
| 270 | |
| 271 | export class SinglePage<Item> extends AbstractPage<Item> implements SinglePageResponse<Item> { |
| 272 | result: Array<Item>; |
| 273 | |
| 274 | constructor( |
| 275 | client: APIClient, |
| 276 | response: Response, |
| 277 | body: SinglePageResponse<Item>, |
| 278 | options: FinalRequestOptions, |
| 279 | ) { |
| 280 | super(client, response, body, options); |
| 281 | |
| 282 | this.result = body.result || []; |
| 283 | } |
| 284 | |
| 285 | getPaginatedItems(): Item[] { |
| 286 | return this.result ?? []; |
| 287 | } |
| 288 | |
| 289 | // @deprecated Please use `nextPageInfo()` instead |
| 290 | /** |
| 291 | * This page represents a response that isn't actually paginated at the API level |
| 292 | * so there will never be any next page params. |
| 293 | */ |
| 294 | nextPageParams(): null { |
| 295 | return null; |
| 296 | } |
| 297 | |
| 298 | nextPageInfo(): null { |
| 299 | return null; |
| 300 | } |
| 301 | } |
| 302 | |