Skip to content

Commit

Permalink
feat(pagination): avoid fetching when has_more: false (#1305)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-app[bot] authored and stainless-bot committed Feb 6, 2025
1 parent da94424 commit b6944c6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
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

0 comments on commit b6944c6

Please sign in to comment.