forked from akure/dao-dao-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagination.ts
48 lines (42 loc) · 1.06 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
import {
PageRequest,
PageResponse,
} from './protobuf/codegen/cosmos/base/query/v1beta1/pagination'
export const getAllRpcResponse = async <
P extends { pagination?: PageRequest; [key: string]: any },
R extends { pagination?: PageResponse; [key: string]: any },
K extends keyof R
>(
queryFn: (params: P) => Promise<R>,
params: P,
key: K,
reverse = false
): Promise<R[K]> => {
let pagination: Partial<PageRequest> | undefined
const data = [] as any[]
do {
const response = await queryFn({
...params,
pagination: {
key: new Uint8Array(),
...pagination,
reverse,
// Get all.
offset: 0n,
limit: BigInt(Number.MAX_SAFE_INTEGER),
},
})
pagination = response.pagination?.nextKey?.length
? {
key: response.pagination.nextKey,
}
: undefined
const results = response[key] as any[]
// If no results retrieved, stop.
if (!results?.length) {
break
}
data.push(...results)
} while (pagination !== undefined)
return data as R[K]
}