-
Notifications
You must be signed in to change notification settings - Fork 3
/
pagination.ts
205 lines (186 loc) · 4.97 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
export interface PageIteratorOptions {
/**
* The Spotify API does not allow you to use a negative offset, but you can do so with this property. This will be useful when, for example, you want to get the last 100 elements.
*
* Under the hood, it will first get the total number of items by fetching with an offset of `0` and then calculate the starting offset.
*
* @default 0
*/
initialOffset?: number;
}
/**
* A helper class which allows you to iterate over items in a paginated API response with javascript async iterators.
*
* @example
* ```ts
* const playlistIter = new PageIterator((offset) =>
* getPlaylistTracks(client, "SOME_PLAYLITS_ID", { offset, limit: 50 })
* );
*
* // Iterate over the playlist tracks
* for await (const track of playlistIter) {
* console.log(track);
* }
*
* // Collect all the tracks
* const tracks = await playlistIter.collect();
*
* // Collect the last 100 tracks in playlist
* const lastHundredTracks = new PageIterator(
* (offset) =>
* getPlaylistTracks(client, "SOME_PLAYLITS_ID", { offset, limit: 50 }),
* { initialOffset: -100 },
* ).collect();
* ```
*/
export class PageIterator<TItem> {
private options: Required<PageIteratorOptions>;
constructor(
private readonly fetcher: (offset: number) => Promise<{
limit: number;
next: string | null;
total: number;
items: TItem[];
}>,
options: PageIteratorOptions = {},
) {
this.options = { initialOffset: 0, ...options };
}
async *[Symbol.asyncIterator](
initialOffset?: number,
): AsyncGenerator<TItem, null, void> {
let offset = typeof initialOffset === "number"
? initialOffset
: this.options.initialOffset;
if (offset < 0) {
const page = await this.fetcher(0);
if (page.total === 0) {
return null;
}
offset = page.total + offset;
}
while (true) {
const page = await this.fetcher(offset);
for (let i = 0; i < page.items.length; i++) {
yield page.items[i]!;
}
if (!page.next) {
return null;
}
offset = offset + page.limit;
}
}
/**
* @param limit The maximum number of items to collect. By default it set to `Infinity`, which means it will collect all items.
*/
async collect(limit = Infinity): Promise<TItem[]> {
if (limit < 0) {
throw new RangeError(
`The limit must be a positive number, got ${limit}`,
);
}
const items: TItem[] = [];
for await (const item of this) {
items.push(item);
if (items.length >= limit) {
break;
}
}
return items;
}
}
type Direction = "backward" | "forward";
export type CursorPageIteratorOptions<TDirection extends Direction> =
& {
direction?: TDirection;
}
& (TDirection extends "forward" ? { initialAfter?: string }
: { initialBefore?: string });
/**
* A helper class which allows you to iterate over items in a cursor paginated API response with javascript async iterators.
*
* @example
* ```ts
* // get the first 100 followed artists
* const artists = await new CursorPageIterator(
* opts => getFollowedArtists(client, { limit: 50, after: opts.after })
* ).collect(100);
* ```
*/
export class CursorPageIterator<
TItem,
TDirection extends "backward" | "forward" = "forward",
> {
private options: CursorPageIteratorOptions<TDirection> & {
direction: TDirection;
};
constructor(
private readonly fetcher: (
options: TDirection extends "forward" ? { after?: string }
: { before?: string },
) => Promise<{
next: string | null;
cursors: {
after?: string;
before?: string;
} | null;
items: TItem[];
}>,
options: CursorPageIteratorOptions<TDirection> = {},
) {
this.options = { direction: "forward", ...options } as
& CursorPageIteratorOptions<TDirection>
& {
direction: TDirection;
};
}
async *[Symbol.asyncIterator](): AsyncGenerator<TItem, null, void> {
const direction = this.options.direction;
let cursor = direction === "forward"
? "initialAfter" in this.options ? this.options.initialAfter : undefined
: "initialBefore" in this.options
? this.options.initialBefore
: undefined;
while (true) {
const page = await this.fetcher(
(direction === "forward" ? { after: cursor } : { before: cursor }) as {
after?: string;
before?: string;
},
);
if (direction === "forward") {
for (let i = 0; i < page.items.length; i++) {
yield page.items[i]!;
}
} else {
for (let i = page.items.length - 1; i >= 0; i--) {
yield page.items[i]!;
}
}
if (!page.next) {
return null;
}
cursor = direction === "forward"
? page.cursors?.after
: page.cursors?.before;
}
}
/**
* @param limit The maximum number of items to collect. By default it set to `Infinity`, which means it will collect all items.
*/
async collect(limit = Infinity): Promise<TItem[]> {
if (limit < 0) {
throw new RangeError(
`The limit must be a positive number, got ${limit}`,
);
}
const items: TItem[] = [];
for await (const item of this) {
items.push(item);
if (items.length >= limit) {
break;
}
}
return items;
}
}