Here's what I trying to do:
Given a paginated API, get all the resources using parallel requests.
The API returns a limited number of resources per call. So, you need to use an offset parameter to get to the next set of data until all data is extracted.
Here's my idea (but getting some warning because I'm using flat on the response), so maybe there's a better way to do this.
- Get the total count of items.
- Given the count and the limit, calculate how many requests are needed to get all data.
- Trigger all requests in parallel and combine all data into a flattened array.
Here's an example:
https://stackblitz.com/edit/paginated-api?embed=1&file=index.ts&hideExplorer=1&devtoolsheight=100
getCount().pipe(
mergeMap(count => range(0, Math.ceil(count / limit))),
map(offset => getDevices(offset, limit)),
combineAll(),
).subscribe(res => {
const a = res.flat(); // <--- warning: Property 'flat' does not exist on type '{ name: string; }[][]'.
console.log(JSON.stringify(a));
});
I feel this solution is a little hacky. It's flattening the response in the subscription. I'd like to know if there's an RXJS operator that I can use on the pipe to flatten the response, so I don't have to in the subscription?