diff --git a/src/common/utils/prefetch-all-42-events-until/prefetch-all-42-events-until.ts b/src/common/utils/prefetch-all-42-events-until/prefetch-all-42-events-until.ts index ffb523b..6baa216 100644 --- a/src/common/utils/prefetch-all-42-events-until/prefetch-all-42-events-until.ts +++ b/src/common/utils/prefetch-all-42-events-until/prefetch-all-42-events-until.ts @@ -3,6 +3,11 @@ import { FetchUntilFunctionItems } from '../../../ft-api/ft-api.service.js'; import { lastItemIsAlreadyCached } from './last-item-is-already-cached.js'; export function prefetchAll42EventsUntil(latestEvent: FindEventsResponseDto[number]) { - return ({ lastItem }: FetchUntilFunctionItems) => - lastItemIsAlreadyCached(latestEvent, lastItem); + return ({ result }: FetchUntilFunctionItems) => { + if (result.length === 0) { + return true; + } + const lastItem = result[result.length - 1]; + return lastItemIsAlreadyCached(latestEvent, lastItem); + }; } diff --git a/src/ft-api/ft-api.service.ts b/src/ft-api/ft-api.service.ts index e31119c..4087532 100644 --- a/src/ft-api/ft-api.service.ts +++ b/src/ft-api/ft-api.service.ts @@ -24,7 +24,7 @@ import { getDataFromResponseOrThrow } from '../common/utils/get-data-from-respon export type RequestWithSchemas = RequestInit & { schema: FetchSchemas> }; export type FetchUntilFunctionItems = { iteration: number; - lastItem?: T; + result: Array; }; export type FetchUntilFunction = (items: FetchUntilFunctionItems) => boolean; @@ -121,7 +121,7 @@ export class FtApiService { } while ( route !== null && iteration < this.apiPaginationMaxDepth && - (untilFn === undefined || !untilFn({ iteration, lastItem: result[result.length - 1] })) + (untilFn === undefined || !untilFn({ iteration, result })) ); return result; }