File tree Expand file tree Collapse file tree 2 files changed +26
-3
lines changed
Expand file tree Collapse file tree 2 files changed +26
-3
lines changed Original file line number Diff line number Diff line change @@ -62,7 +62,7 @@ export const DEFAULT_MAX_ITEMS = 1_000_000;
6262type 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 ;
Original file line number Diff line number Diff 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} ) ;
You can’t perform that action at this time.
0 commit comments