Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pagination): avoid fetching when has_more: false #1305

Merged
merged 1 commit into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .stats.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
configured_endpoints: 69
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/openai-7c699d4503077d06a4a44f52c0c1f902d19a87c766b8be75b97c8dfd484ad4aa.yml
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/openai-dfb00c627f58e5180af7a9b29ed2f2aa0764a3b9daa6a32a1cc45bc8e48dfe15.yml
13 changes: 13 additions & 0 deletions src/pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export class Page<Item> extends AbstractPage<Item> implements PageResponse<Item>

export interface CursorPageResponse<Item> {
data: Array<Item>;

has_more: boolean;
}

export interface CursorPageParams {
Expand All @@ -57,6 +59,8 @@ export class CursorPage<Item extends { id: string }>
{
data: Array<Item>;

has_more: boolean;

constructor(
client: APIClient,
response: Response,
Expand All @@ -66,12 +70,21 @@ export class CursorPage<Item extends { id: string }>
super(client, response, body, options);

this.data = body.data || [];
this.has_more = body.has_more || false;
}

getPaginatedItems(): Item[] {
return this.data ?? [];
}

override hasNextPage() {
if (this.has_more === false) {
return false;
}

return super.hasNextPage();
}

// @deprecated Please use `nextPageInfo()` instead
nextPageParams(): Partial<CursorPageParams> | null {
const info = this.nextPageInfo();
Expand Down