Skip to content

Commit

Permalink
Merge pull request #81 from superglue-ai/bugfix/cursor-based-pagination
Browse files Browse the repository at this point in the history
add the cursor to the response
  • Loading branch information
stefanfaistenauer authored Mar 6, 2025
2 parents 6466e36 + 037a3c8 commit 526fe17
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/core/utils/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ describe('API Utilities', () => {

const result = await callEndpoint(config, {}, {}, {});

expect(result.data).toHaveLength(3);
expect(result.data.results).toHaveLength(3);
expect(result.data.next_cursor).toBeNull();
});

it('should stop pagination when receiving duplicate data', async () => {
Expand Down
11 changes: 10 additions & 1 deletion packages/core/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export async function callEndpoint(endpoint: ApiConfig, payload: Record<string,
const cursorParts = (endpoint.pagination?.cursorPath || 'next_cursor').split('.');
let nextCursor = response.data;
for (const part of cursorParts) {
nextCursor = nextCursor?.[part] ?? nextCursor;
nextCursor = nextCursor?.[part];
}
cursor = nextCursor;
if(!cursor) {
Expand All @@ -223,6 +223,15 @@ export async function callEndpoint(endpoint: ApiConfig, payload: Record<string,
loopCounter++;
}

if(endpoint.pagination?.type === PaginationType.CURSOR_BASED) {
return {
data: {
results: allResults,
next_cursor: cursor
}
};
}

return {
data: allResults?.length == 1 ? allResults[0] : allResults
};
Expand Down

0 comments on commit 526fe17

Please sign in to comment.