I am getting data from a third party API that requires me to manage the paging manually by keeping track of the number of records retrieved and the total number of records.
I've tried to use the expand,reduce operators in RxJs as I've done something similar with other apis that provide me with a 'nextPage' link, but I am running into a infinite loop in this particular case.
I know the problem is that the nextPosition variable is not being updated when the output of expand is run through the expand again, but I am not sure if it is possible to solve this problem using this method.
Is it possible to use expand and reduce with a recursive function, and if so, what do I need to do to fix the below?
private async retreivePagedRecords<T>(companyURL: string, query: string, startPosition: number, totalCount: number, transformFn: (qbData: any) => T[]): Promise<T[]> {
const headers = this.getHttpConfig();
let pageQuery = `${query} STARTPOSITION ${startPosition} MAXRESULTS ${this.pagingSize}`;
const nextPosition = startPosition + this.pagingSize;
const records = await lastValueFrom(this.http.get(`${companyURL}/query?query=${pageQuery}`, headers)
.pipe(
map(x => x.data as any),
map(x => {
//Trivial transformation to property names etc.
return transformFn(x);
}),
expand(x => nextPosition > totalCount ? [] : this.retreivePagedRecords<T>(companyURL, query, nextPosition, totalCount, transformFn)),
reduce((acc: T[], x: T[]) => acc.concat(x ?? []), []),
catchError(error => {
return of([]);
})
));
return records;
}