Skip to content

Commit 49c574e

Browse files
feat(api): pagination properties added to response (has_more, next_offset)
1 parent d1169c0 commit 49c574e

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

.stats.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
configured_endpoints: 46
22
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/kernel%2Fkernel-cb38560915edce03abce2ae3ef5bc745489dbe9b6f80c2b4ff42edf8c2ff276d.yml
33
openapi_spec_hash: a869194d6c864ba28d79ec0105439c3e
4-
config_hash: 1f28d5c3c063f418ebd2799df1e4e781
4+
config_hash: ed56f95781ec9b2e73c97e1a66606071

src/core/pagination.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { defaultParseResponse } from '../internal/parse';
66
import { type Kernel } from '../client';
77
import { APIPromise } from './api-promise';
88
import { type APIResponseProps } from '../internal/parse';
9-
import { maybeObj } from '../internal/utils/values';
9+
import { maybeCoerceBoolean, maybeCoerceInteger, maybeObj } from '../internal/utils/values';
1010

1111
export type PageRequestOptions = Pick<FinalRequestOptions, 'query' | 'headers' | 'body' | 'path' | 'method'>;
1212

@@ -118,6 +118,10 @@ export interface OffsetPaginationParams {
118118
export class OffsetPagination<Item> extends AbstractPage<Item> {
119119
items: Array<Item>;
120120

121+
has_more: boolean | null;
122+
123+
next_offset: number | null;
124+
121125
constructor(
122126
client: Kernel,
123127
response: Response,
@@ -127,14 +131,24 @@ export class OffsetPagination<Item> extends AbstractPage<Item> {
127131
super(client, response, body, options);
128132

129133
this.items = body || [];
134+
this.has_more = maybeCoerceBoolean(this.response.headers.get('x-has-more')) ?? null;
135+
this.next_offset = maybeCoerceInteger(this.response.headers.get('x-next-offset')) ?? null;
130136
}
131137

132138
getPaginatedItems(): Item[] {
133139
return this.items ?? [];
134140
}
135141

142+
override hasNextPage(): boolean {
143+
if (this.has_more === false) {
144+
return false;
145+
}
146+
147+
return super.hasNextPage();
148+
}
149+
136150
nextPageRequestOptions(): PageRequestOptions | null {
137-
const offset = (this.options.query as OffsetPaginationParams).offset ?? 0;
151+
const offset = this.next_offset;
138152
if (!offset) {
139153
return null;
140154
}

0 commit comments

Comments
 (0)