From 143b5a1d042304117a0e2ec08265b0c4cd1927ea Mon Sep 17 00:00:00 2001 From: Anurag Dhungana <36888347+Aarekaz@users.noreply.github.com> Date: Fri, 21 Aug 2026 13:01:19 -0400 Subject: [PATCH] fix: accept exhausted WHOOP collection pages --- src/__tests__/whoop/client.test.ts | 13 +++++++++++++ src/schemas/whoop.ts | 2 +- src/services/whoop/client.ts | 2 +- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/__tests__/whoop/client.test.ts b/src/__tests__/whoop/client.test.ts index 3557135..7a22490 100644 --- a/src/__tests__/whoop/client.test.ts +++ b/src/__tests__/whoop/client.test.ts @@ -232,6 +232,19 @@ describe("WHOOP v2 client", () => { ); }); + it("treats a live null next token as collection exhaustion", async () => { + vi.spyOn(globalThis, "fetch").mockResolvedValue(jsonResponse({ + records: [SLEEP], + next_token: null, + })); + const client = new WhoopClient(ENV, "access"); + + await expect(client.getCollection("sleep")).resolves.toMatchObject({ + nextToken: undefined, + records: [{ id: SLEEP.id }], + }); + }); + it("rejects provider collection limits outside the whole-number range 1 through 25 before fetching", async () => { const fetchMock = vi.spyOn(globalThis, "fetch"); const client = new WhoopClient(ENV, "access"); diff --git a/src/schemas/whoop.ts b/src/schemas/whoop.ts index 916f1e0..bd1adb2 100644 --- a/src/schemas/whoop.ts +++ b/src/schemas/whoop.ts @@ -105,7 +105,7 @@ export const whoopWorkoutSchema = z.object({ export const whoopCollectionResponseSchema = (recordSchema: T) => z.object({ records: z.array(recordSchema), - next_token: z.string().optional(), + next_token: z.string().nullish(), }).passthrough(); export type WhoopWebhook = z.infer; diff --git a/src/services/whoop/client.ts b/src/services/whoop/client.ts index 00f35e6..b3fd685 100644 --- a/src/services/whoop/client.ts +++ b/src/services/whoop/client.ts @@ -234,7 +234,7 @@ export class WhoopClient { record, rawRecords[index], )) as Array>, - nextToken: parsed.data.next_token, + nextToken: parsed.data.next_token ?? undefined, }; }