forked from openai/openai-deno-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagination.ts
108 lines (87 loc) · 2.2 KB
/
pagination.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// File generated from our OpenAPI spec by Stainless.
import {
AbstractPage,
APIClient,
FinalRequestOptions,
PageInfo,
Response,
} from "./core.ts";
export interface PageResponse<Item> {
data: Array<Item>;
object: string;
}
/**
* Note: no pagination actually occurs yet, this is for forwards-compatibility.
*/
export class Page<Item> extends AbstractPage<Item>
implements PageResponse<Item> {
data: Array<Item>;
object: string;
constructor(
client: APIClient,
response: Response,
body: PageResponse<Item>,
options: FinalRequestOptions,
) {
super(client, response, body, options);
this.data = body.data || [];
this.object = body.object;
}
getPaginatedItems(): Item[] {
return this.data ?? [];
}
// @deprecated Please use `nextPageInfo()` instead
/**
* This page represents a response that isn't actually paginated at the API level
* so there will never be any next page params.
*/
nextPageParams(): null {
return null;
}
nextPageInfo(): null {
return null;
}
}
export interface CursorPageResponse<Item> {
data: Array<Item>;
}
export interface CursorPageParams {
after?: string;
limit?: number;
}
export class CursorPage<Item extends { id: string }> extends AbstractPage<Item>
implements CursorPageResponse<Item> {
data: Array<Item>;
constructor(
client: APIClient,
response: Response,
body: CursorPageResponse<Item>,
options: FinalRequestOptions,
) {
super(client, response, body, options);
this.data = body.data || [];
}
getPaginatedItems(): Item[] {
return this.data ?? [];
}
// @deprecated Please use `nextPageInfo()` instead
nextPageParams(): Partial<CursorPageParams> | null {
const info = this.nextPageInfo();
if (!info) return null;
if ("params" in info) return info.params;
const params = Object.fromEntries(info.url.searchParams);
if (!Object.keys(params).length) return null;
return params;
}
nextPageInfo(): PageInfo | null {
const data = this.getPaginatedItems();
if (!data.length) {
return null;
}
const id = data[data.length - 1]?.id;
if (!id) {
return null;
}
return { params: { after: id } };
}
}