Skip to content

Commit 3be1f24

Browse files
Copilotvobu
andcommitted
fix: convert BigInt totalItems to number in fetchAllPages to prevent TypeError
Co-authored-by: vobu <6573426+vobu@users.noreply.github.com>
1 parent 19ff3a7 commit 3be1f24

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

src/client.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export const DEFAULT_MAX_ITEMS = 1_000_000;
6262
type PagedResponse<T> = {
6363
items?: T[];
6464
page?: {
65-
totalItems?: number;
65+
totalItems?: bigint | number;
6666
endCursor?: string;
6767
startCursor?: string;
6868
hasMoreTotalItems?: boolean;
@@ -111,8 +111,9 @@ export async function fetchAllPages<T>(
111111
break;
112112
}
113113

114-
const endCursor = result.page?.endCursor ?? (result as any).page?.endCursor as string | undefined;
115-
const totalItems = result.page?.totalItems ?? (result as any).page?.totalItems as number | undefined;
114+
const endCursor = result.page?.endCursor;
115+
// totalItems is BigInt from the SDK's Zod schema (z.coerce.bigint()); convert to number
116+
const totalItems = result.page?.totalItems !== undefined ? Number(result.page.totalItems) : undefined;
116117

117118
if (!endCursor || seenCursors.has(endCursor)) break;
118119
if (totalItems !== undefined && allItems.length >= totalItems) break;

tests/unit/fetch-all-pages.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,26 @@ describe('fetchAllPages', () => {
218218
test('DEFAULT_MAX_ITEMS is 1000000', () => {
219219
assert.strictEqual(DEFAULT_MAX_ITEMS, 1_000_000);
220220
});
221+
222+
test('handles BigInt totalItems from SDK Zod schema without throwing', async () => {
223+
// The Camunda 8 SDK uses z.coerce.bigint() for totalItems, so it returns BigInt at runtime.
224+
// fetchAllPages must convert it to number before comparing with allItems.length (a number).
225+
let callCount = 0;
226+
const bigintTotalItemsSearch = async (_filter: any, _opts?: any) => {
227+
callCount++;
228+
return {
229+
items: [{ id: 1 }, { id: 2 }],
230+
page: {
231+
totalItems: 2n, // BigInt, as returned by the SDK
232+
endCursor: undefined,
233+
},
234+
};
235+
};
236+
237+
// Should NOT throw TypeError: Cannot mix BigInt and other types
238+
const items = await fetchAllPages(bigintTotalItemsSearch, {});
239+
240+
assert.strictEqual(callCount, 1, 'should only make one API call');
241+
assert.strictEqual(items.length, 2);
242+
});
221243
});

0 commit comments

Comments
 (0)