-
Notifications
You must be signed in to change notification settings - Fork 2
/
pagination.ts
388 lines (318 loc) · 12.1 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import { OAuthRestClientInterface, PaginationArgs } from '@/common'
import { AxiosRequestConfig, AxiosStatic } from 'axios'
import { logResponse } from './common'
import { Paginated } from '../../common/graphql'
export type GetPageResultCursor<T> = { data: T[], hasNext: boolean, nextCursor?: string, total?: number }
type GetPageResult<T> = { data: T[], total: number }
type PropMapper = <T>(data: any) => T
type StringPropMapper = string | PropMapper
/**
* Get an URL with added params from the given object.
* @param url The original URL
* @param params The params to append to the URL
* @returns The URL with the added params
*/
function applyParams(url: string, params: any): string {
const isRelative = url.startsWith('/')
if (isRelative) {
// TODO: better solution?
url = 'http://a'+ url
}
const urlObj = new URL(url)
for (const key of Object.keys(params)) {
urlObj.searchParams.append(key, params[key])
}
url = urlObj.toString()
if (isRelative) {
url = url.substring(8)
}
return url
}
/**
* Return a method that maps from an object to a property within it defined by the given string, if a custom one is not provided.
* @param mapper Mapping string or method
* @returns Mapping method
*/
function getPropMapper(mapper: StringPropMapper): PropMapper {
if (typeof mapper === 'string') {
return <T>(data: any) => data[mapper] as T
}
return mapper
}
/**
* Return a generator for a function that gets a page from an oauth endpoint with query parameters.
* @param offsetQuery Query key to use for item offset
* @param countQuery Query key to use for page size
* @param totalProp Property to extract total assets from
* @param resultProp Property to extract result items from
* @returns A generator that takes a client, url and base params and generates a function that gets a page.
*/
export function getPageByQuery(offsetQuery: string, countQuery: string, totalProp: StringPropMapper, resultProp: StringPropMapper) {
const totalPropMap = getPropMapper(totalProp)
const resultPropMap = getPropMapper(resultProp)
return <T>(client: OAuthRestClientInterface, url: string, params: any = {}) =>
async (page: number, pageSize: number): Promise<GetPageResult<T>> => {
const allParams = {
...params,
[offsetQuery]: page * pageSize,
[countQuery]: pageSize
}
const newUrl = applyParams(url, allParams)
const response = await client.get({url: newUrl})
logResponse('get', newUrl, response)
if (response == null) {
return {
data: [],
total: 0
}
}
return {
data: resultPropMap(response) ?? [],
total: totalPropMap(response) ?? 0
}
}
}
/**
* Return a generator for a function that gets a page using an axios client with query parameters.
* @param offsetQuery Query key to use for item offset
* @param countQuery Query key to use for page size
* @param totalProp Property to extract total assets from
* @param resultProp Property to extract result items from
* @param offsetFunc Function for getting the offset from the page number and size
* @returns A generator that takes a client, url and base params and generates a function that gets a page.
*/
export function getPageByQueryAxios(offsetQuery: string, countQuery: string, totalProp: StringPropMapper, resultProp: StringPropMapper, offsetFunc?: (page: number, pageSize: number) => number) {
const totalPropMap = getPropMapper(totalProp)
const resultPropMap = getPropMapper(resultProp)
return <T>(axios: AxiosStatic, url: string, config: AxiosRequestConfig<any>, params: any = {}) =>
async (page: number, pageSize: number): Promise<GetPageResult<T>> => {
const allParams = {
...params,
[offsetQuery]: offsetFunc ? offsetFunc(page, pageSize) : page * pageSize,
[countQuery]: pageSize
}
const newUrl = applyParams(url, allParams)
const response = await axios.get(newUrl, config)
logResponse('get', newUrl, response.data)
return {
data: resultPropMap(response.data) ?? [],
total: totalPropMap(response.data) ?? 0
}
}
}
/**
* Iterate through fetching pages and build an array out of the results.
* @param requestPage Method to use to request pages. Takes page number and size. Must return at least one page-size worth of items if the total allows it.
* @param args Pagination arguments, new cursor and offset is written back into the object.
* @param defaultPageSize Default page size if not provided by the arguments (default: 20)
* @returns List of items fetched from the paginated endpoint
*/
export async function paginateArgs<T>(
requestPage: (page: number, pageSize: number) => Promise<GetPageResult<T>>,
args: PaginationArgs,
defaultPageSize = 20
): Promise<T[]> {
const pageSize = Number(args.pageSize ?? defaultPageSize)
const pageNum = Number(args.pageNum ?? 0)
const pageCount = args.pageCount == null ? args.pageCount : Number(args.pageCount)
const {result, total} = await paginate(
requestPage,
pageSize,
pageNum,
pageCount
)
args.pageSize = pageSize
args.pageNum = pageNum
args.pageCount = pageCount
args.total = total
return result
}
/**
* Iterate through fetching pages and build an array out of the results.
* @param requestPage Method to use to request pages. Takes page number and size. Must return at least one page-size worth of items if the total allows it.
* @param pageSize Page size (default: 20)
* @param pageNum Page number to start at (default: 0)
* @param pageCount Number of pages to fetch (default: all)
* @returns List of items fetched from the paginated endpoint
*/
export const paginate = async <T>(
requestPage: (page: number, pageSize: number) => Promise<GetPageResult<T>>,
pageSize = 20,
pageNum = 0,
pageCount?: number
): Promise<{result: T[], total: number}> => {
const result: T[] = []
if (pageCount === undefined) {
pageCount = Infinity
}
const startOffset = pageNum * pageSize
const targetCount = pageCount * pageSize
let finalTotal = 0
for (let i = 0; i < pageCount; i++) {
const {data, total} = await requestPage(pageNum + i, pageSize)
finalTotal = total
// There's a possibility that the implementation has returned more than one page.
// Allow multiple pages to be completed at a time.
const pagesReturned = Math.floor(data.length / pageSize)
let dataCount = data.length
if (pagesReturned > 0) {
dataCount = pagesReturned * pageSize
i += pagesReturned - 1
}
const targetMin = Math.min(total - startOffset, targetCount)
const end = targetMin - result.length
const toAdd = Math.min(dataCount, end)
result.push(...(data.slice(0, toAdd)))
if (result.length === targetMin) {
break
}
}
return { result, total: finalTotal }
}
/**
* Iterate through fetching pages and build an array out of the results.
* @param requestPage Method to use to request pages. Takes cursor and page size.
* @param args Pagination arguments, new cursor and offset is written back into the object.
* @param defaultPageSize Default page size if not provided by the arguments (default: 20)
* @returns List of items fetched from the paginated endpoint
*/
export async function paginateCursorArgs<T>(
requestPage: (cursor: string, pageSize: number) => Promise<GetPageResultCursor<T>>,
args: PaginationArgs,
defaultPageSize = 20
): Promise<GetPageResultCursor<T>> {
const pageSize = Number(args.pageSize ?? defaultPageSize)
const pageNum = Number(args.pageNum ?? 0)
const argsPageCount = args.pageCount == null ? args.pageCount : Number(args.pageCount)
// Find the quickest way to get to the desired offset.
if (args.cursor === undefined || args.cursorPage === undefined || Number(args.cursorPage) > pageNum) {
// If the cursor is from a previous page, we have to start from the beginning.
args.cursor = undefined
args.cursorPage = undefined
}
const cursor = args.cursor
const cursorPage = Number(args.cursorPage ?? 0)
// We might need to get additional pages to catch up to the requested page.
const fetchedExtra = cursorPage < pageNum
const pageCount = argsPageCount == null ? null : (fetchedExtra ? (pageNum - cursorPage) + argsPageCount : argsPageCount)
const resultCursor = await paginateCursor(requestPage, pageSize, cursor, pageCount)
if (fetchedExtra) {
resultCursor.data = resultCursor.data.slice((pageNum - cursorPage) * pageSize)
}
args.total = resultCursor.total ?? (resultCursor.hasNext ? undefined : (pageNum * pageSize + resultCursor.data.length))
args.pageSize = pageSize
args.cursor = resultCursor.nextCursor
args.cursorPage = (argsPageCount != null) ? pageNum + argsPageCount : undefined
return resultCursor
}
/**
* Iterate through fetching pages and build an array out of the results.
* @param requestPage Method to use to request pages. Takes cursor and page size.
* @param pageSize Page size (default: 20)
* @param cursor Start cursor (default: null)
* @param pageCount Number of pages to fetch (default: all)
* @returns List of items fetched from the paginated endpoint
*/
export const paginateCursor = async <T>(
requestPage: (cursor: string, pageSize: number) => Promise<GetPageResultCursor<T>>,
pageSize = 20,
cursor?: string,
pageCount?: number
): Promise<GetPageResultCursor<T>> => {
const result: T[] = []
if (pageCount == null) {
pageCount = Infinity
}
const targetCount = pageCount * pageSize
let finalTotal: number | undefined = undefined
for (let i = 0; i < pageCount; i++) {
const {data, hasNext, nextCursor, total} = await requestPage(cursor, pageSize)
finalTotal = total
const dataCount = data.length
const end = targetCount - result.length
const toAdd = Math.min(dataCount, end)
result.push(...(data.slice(0, toAdd)))
cursor = nextCursor
if (!hasNext) {
return {
data: result,
hasNext: false,
nextCursor: cursor,
total
}
}
else if (result.length === targetCount)
{
break
}
}
return {
data: result,
hasNext: true,
nextCursor: cursor,
total: finalTotal
}
}
/**
* Generate a method to paginate through a list of resources with the given arguments.
* @param items List of items to paginate
* @returns A method to paginate through a list of resources
*/
export function getListPage<T>(list: T[]) {
return async (pageNum: number, pageSize: number): Promise<GetPageResult<T>> => {
if (pageNum < 0 || pageNum * pageSize > list.length) {
return {
data: [],
total: list.length
}
}
return {
data: list.slice(pageNum * pageSize, (pageNum + 1) * pageSize),
total: list.length
}
}
}
/**
* Handle pagination as if the result list were empty.
* @param args Pagination arguments, new cursor and offset is written back into the object.
* @returns Empty list
*/
export function paginateBlankArgs<T>(args: PaginationArgs): T[] {
args.pageSize = Number(args.pageSize ?? 0)
args.pageNum = Number(args.pageNum ?? 0)
args.pageCount = args.pageCount == null ? args.pageCount : Number(args.pageCount)
args.total = 0
delete args.cursor
delete args.cursorPage
return []
}
type GqlRequestMethod = <T>(query: string, variables: any, isAdmin?: boolean) => Promise<T>;
/**
* Generate a function that gets a page from a GraphQL API.
* @param query The GraphQL query string
* @param variables Variables to use with the GraphQL query
* @param getPaginated Function that gets the Paginated<T2> type from the request type T
* @param isAdmin Whether the admin credentials must be used or not
* @returns A function that gets a page from a cursor and pageSize.
*/
export function getPageGql<T, T2>(gqlRequest: GqlRequestMethod, query: string, variables: any, getPaginated: (response: T) => Paginated<T2>, isAdmin = false) {
return async (cursor: string | undefined, pageSize: number): Promise<GetPageResultCursor<T2>> => {
const result = await gqlRequest<T>(query, {...variables, pageSize, after: cursor}, isAdmin)
const paginated = getPaginated(result)
if (paginated.edges.length > pageSize) {
paginated.edges = paginated.edges.slice(0, pageSize)
return {
data: paginated.edges.map(edge => edge.node),
nextCursor: paginated.edges[paginated.edges.length - 1].cursor,
total: paginated.collectionInfo?.totalItems,
hasNext: true
}
}
return {
data: paginated.edges.map(edge => edge.node),
nextCursor: paginated.pageInfo.endCursor,
hasNext: paginated.pageInfo.hasNextPage,
total: paginated.collectionInfo?.totalItems
}
}
}