If the api in a ladda-wrapped call returns an array, only the initial call (that does the actual request) returns the array as expected. Any subsequent call (that's fetching the entity from the cache) will return the array parsed an object instead.
It's surprising that the type changes "on the fly" and ladda should probably return the entities more consistently (ideally the way they came from the backend).
A workaround is to wrap the ladda call in another function that extracts the values from the response if it's an object:
export const getEntitiesAsArray = async (id: string) => {
const entities = await api.myEntities.getAllForId(id);
// Because ladda parses arrays as objects when returning from cache, we need to retransform to an array here.
return typeof entities === 'object' ? Object.values(entities) : entities;
};
If the api in a ladda-wrapped call returns an array, only the initial call (that does the actual request) returns the array as expected. Any subsequent call (that's fetching the entity from the cache) will return the array parsed an object instead.
It's surprising that the type changes "on the fly" and ladda should probably return the entities more consistently (ideally the way they came from the backend).
Example
Initial ladda call returns:
Second and subsequent ladda calls return:
Current workaround
A workaround is to wrap the ladda call in another function that extracts the values from the response if it's an object: