deduplicateById: fix Entity types
This commit is contained in:
parent
569296af36
commit
f7bd5c5951
|
@ -8,10 +8,17 @@ export interface PaginatedResult<T> {
|
||||||
link?: string,
|
link?: string,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface Entity {
|
||||||
|
id: string,
|
||||||
|
}
|
||||||
|
|
||||||
|
const isEntity = <T = unknown>(object: T): object is T & Entity => {
|
||||||
|
return object && typeof object === 'object' && 'id' in object;
|
||||||
|
};
|
||||||
|
|
||||||
/** Deduplicate an array of entities by their ID. */
|
/** Deduplicate an array of entities by their ID. */
|
||||||
const deduplicate = <T>(entities: T[]): T[] => {
|
const deduplicateById = <T extends Entity>(entities: T[]): T[] => {
|
||||||
const map = entities.reduce<Map<string, T>>((result, entity) => {
|
const map = entities.reduce<Map<string, T>>((result, entity) => {
|
||||||
// @ts-expect-error Entity might not have an ID... but it probably does.
|
|
||||||
return result.set(entity.id, entity);
|
return result.set(entity.id, entity);
|
||||||
}, new Map());
|
}, new Map());
|
||||||
|
|
||||||
|
@ -26,8 +33,10 @@ const flattenPages = <T>(queryData: InfiniteData<PaginatedResult<T>> | undefined
|
||||||
[],
|
[],
|
||||||
);
|
);
|
||||||
|
|
||||||
if (data) {
|
if (data && data.every(isEntity)) {
|
||||||
return deduplicate<T>(data);
|
return deduplicateById(data);
|
||||||
|
} else if (data) {
|
||||||
|
return data;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue