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 } }