From e0384c5f6768fad53e6676e10950df5c2c55f80d Mon Sep 17 00:00:00 2001 From: Santese Smith <43831817+santese@users.noreply.github.com> Date: Tue, 30 Aug 2022 09:59:30 -0700 Subject: [PATCH] feat: add pagination to events (#102) --- src/lib/events/events.ts | 18 +++++++++++++----- src/lib/http/paginated-request.ts | 28 ++++++++++++++++++++++++++++ src/lib/reports/reports.ts | 7 ++++++- 3 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 src/lib/http/paginated-request.ts diff --git a/src/lib/events/events.ts b/src/lib/events/events.ts index 431f3fc..f8bc86d 100644 --- a/src/lib/events/events.ts +++ b/src/lib/events/events.ts @@ -1,4 +1,5 @@ import { AxiosInstance } from 'axios' +import { paginatedRequest } from '../http/paginated-request' import { DateString, TimelyAppConfig, @@ -18,7 +19,7 @@ export class Events { params.since = startDate params.upto = endDate } - const { data } = await this.http.get(`/${this.config.accountId}/events`, { params }) + const data = await paginatedRequest(this.http, `/${this.config.accountId}/events`, params) return data } @@ -29,10 +30,17 @@ export class Events { ): Promise { // Ensure given date range conforms to ISO string format const [startDate, endDate] = [start, end].map((s) => this.ensureISOFormat(s)) - const { data } = await this.http.get( - `/${this.config.accountId}/projects/${projectId}/events${ - start ? `?since=${startDate}` : '' - }${end ? `&upto=${endDate}` : ''}`, + const params = {} as { since?: string; upto?: string } + if (startDate) { + params.since = startDate + } + if (endDate) { + params.upto = endDate + } + const data = await paginatedRequest( + this.http, + `/${this.config.accountId}/projects/${projectId}/events`, + params, ) return data } diff --git a/src/lib/http/paginated-request.ts b/src/lib/http/paginated-request.ts new file mode 100644 index 0000000..8ebae07 --- /dev/null +++ b/src/lib/http/paginated-request.ts @@ -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 => { + 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 +} diff --git a/src/lib/reports/reports.ts b/src/lib/reports/reports.ts index 317c4dc..669a53d 100644 --- a/src/lib/reports/reports.ts +++ b/src/lib/reports/reports.ts @@ -1,4 +1,5 @@ import { AxiosInstance } from 'axios' +import { paginatedRequest } from '../http/paginated-request' import { TimelyAppConfig } from '../types' export class Reports { @@ -13,7 +14,11 @@ export class Reports { until?: string }, ) { - const { data } = await this.http.post(`/${this.config.accountId}/reports/filter`, filter) + const data = await paginatedRequest( + this.http, + `/${this.config.accountId}/reports/filter`, + filter, + ) return data } }