-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add pagination to events (#102)
- Loading branch information
Showing
3 changed files
with
47 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import type { AxiosInstance } from 'axios' | ||
|
||
export const paginatedRequest = async ( | ||
httpAgent: AxiosInstance, | ||
route: string, | ||
filter?: { [index: string]: any }, | ||
per_page = 1000, | ||
): Promise<any[]> => { | ||
const params = { | ||
per_page, | ||
page: 1, | ||
} | ||
|
||
if (filter) Object.assign(params, { ...filter }) | ||
|
||
const finalData = [] | ||
let res: any[] = [] | ||
|
||
do { | ||
const { data } = await httpAgent.get(route, { params }) | ||
finalData.push(...data) | ||
res = data | ||
params.page++ | ||
/// If the results are less than per_page, we can assume there are no more results | ||
} while (res.length === per_page) | ||
|
||
return finalData | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters