cloudflare/cloudflare-typescript
Publicmirrored from https://github.com/cloudflare/cloudflare-typescriptAvailable
src/pagination.ts
128lines · modecode
| 1 | // File generated from our OpenAPI spec by Stainless. |
| 2 | |
| 3 | import { AbstractPage, Response, APIClient, FinalRequestOptions, PageInfo } from './core'; |
| 4 | |
| 5 | export interface V4PagePaginationResponse<Item> { |
| 6 | result: V4PagePaginationResponse.Result; |
| 7 | |
| 8 | result_info: V4PagePaginationResponse.ResultInfo; |
| 9 | } |
| 10 | |
| 11 | export namespace V4PagePaginationResponse { |
| 12 | export interface Result { |
| 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; |
| 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 | |