@@ -186,20 +186,26 @@ export const clickupConnector: ConnectorConfig = {
186186 const data = ( await response . json ( ) ) as Record < string , unknown >
187187 const rawDocs = Array . isArray ( data . docs ) ? data . docs : [ ]
188188
189- const documents : ExternalDocument [ ] = [ ]
189+ const previouslyFetched = ( syncContext ?. totalDocsFetched as number ) ?? 0
190+ const remaining =
191+ maxDocs > 0 ? Math . max ( 0 , maxDocs - previouslyFetched ) : Number . POSITIVE_INFINITY
192+
193+ const pageDocuments : ExternalDocument [ ] = [ ]
190194 for ( const rawDoc of rawDocs ) {
191195 const doc = parseDoc ( rawDoc )
192196 if ( ! doc || doc . deleted || doc . archived ) continue
193- documents . push ( docToStub ( doc , workspaceId ) )
197+ pageDocuments . push ( docToStub ( doc , workspaceId ) )
194198 }
199+ const documents = pageDocuments . slice ( 0 , remaining )
200+ const trimmedByCap = documents . length < pageDocuments . length
195201
196- const totalFetched = ( ( syncContext ?. totalDocsFetched as number ) ?? 0 ) + documents . length
202+ const totalFetched = previouslyFetched + documents . length
197203 if ( syncContext ) syncContext . totalDocsFetched = totalFetched
198- const hitLimit = maxDocs > 0 && totalFetched >= maxDocs
199- if ( hitLimit && syncContext ) syncContext . listingCapped = true
200-
201204 const nextCursor =
202205 typeof data . next_cursor === 'string' && data . next_cursor ? data . next_cursor : undefined
206+ const hitLimit = maxDocs > 0 && totalFetched >= maxDocs
207+ const capTruncatedListing = hitLimit && ( trimmedByCap || Boolean ( nextCursor ) )
208+ if ( capTruncatedListing && syncContext ) syncContext . listingCapped = true
203209
204210 return {
205211 documents,
0 commit comments