First time I've run into this issue so apologies if the title doesn't make sense, couldn't figure out a good way to word it.
I'm working with an API that returns a response like this:
{
PaginatedResults: {
TotalResults: 100,
Offset: 0,
Limit: 0,
PageSize: 100,
},
Error: {
Message: '',
Code: '',
},
Customers: [{...}]
}
Where Customers can be replaced with any type the API may return. So it could also look something like this:
{
PaginatedResults: {
TotalResults: 100,
Offset: 0,
Limit: 0,
PageSize: 100,
},
Error: {
Message: '',
Code: '',
},
Sales: [{...}]
}
I currently express the type like this:
type PaginatedResponse<TResponse extends object> =
TResponse &
Readonly<{
PaginatedResponse: {
TotalResults: number;
Offset: number;
Limit: number;
PageSize: number;
};
Error?: {
Message: string;
Code: string;
};
}>;
This has been working fine but I recently ran into a issue when I was trying to create a generic helper function to auto paginate an endpoint.
To keep track of all the results I need to do something like this:
const allResults = [...initialResults];
const nextResp = await apiCall();
allResults.push(...nextResp['Customers'])
But the problem is the key 'Customers' can be any of the 50+ types the API may return. So I need a generic way to index into the response. I've tried a bunch of different things with no success so I'm looking to get some guidance on the best way to approach something like this.