Skip to content

Commit 4066eda

Browse files
fix(pagination): correct type annotation object field (#590)
1 parent eb6ceeb commit 4066eda

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

src/pagination.ts

+14-16
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { AbstractPage, Response, APIClient, FinalRequestOptions, PageInfo } from
55
export interface PageResponse<Item> {
66
data: Array<Item>;
77

8-
object: 'list';
8+
object: string;
99
}
1010

1111
/**
@@ -14,17 +14,17 @@ export interface PageResponse<Item> {
1414
export class Page<Item> extends AbstractPage<Item> implements PageResponse<Item> {
1515
data: Array<Item>;
1616

17-
object: 'list';
17+
object: string;
1818

1919
constructor(client: APIClient, response: Response, body: PageResponse<Item>, options: FinalRequestOptions) {
2020
super(client, response, body, options);
2121

22-
this.data = body.data;
22+
this.data = body.data || [];
2323
this.object = body.object;
2424
}
2525

2626
getPaginatedItems(): Item[] {
27-
return this.data;
27+
return this.data ?? [];
2828
}
2929

3030
// @deprecated Please use `nextPageInfo()` instead
@@ -46,14 +46,8 @@ export interface CursorPageResponse<Item> {
4646
}
4747

4848
export interface CursorPageParams {
49-
/**
50-
* Identifier for the last job from the previous pagination request.
51-
*/
5249
after?: string;
5350

54-
/**
55-
* Number of fine-tuning jobs to retrieve.
56-
*/
5751
limit?: number;
5852
}
5953

@@ -71,11 +65,11 @@ export class CursorPage<Item extends { id: string }>
7165
) {
7266
super(client, response, body, options);
7367

74-
this.data = body.data;
68+
this.data = body.data || [];
7569
}
7670

7771
getPaginatedItems(): Item[] {
78-
return this.data;
72+
return this.data ?? [];
7973
}
8074

8175
// @deprecated Please use `nextPageInfo()` instead
@@ -89,12 +83,16 @@ export class CursorPage<Item extends { id: string }>
8983
}
9084

9185
nextPageInfo(): PageInfo | null {
92-
if (!this.data?.length) {
86+
const data = this.getPaginatedItems();
87+
if (!data.length) {
88+
return null;
89+
}
90+
91+
const id = data[data.length - 1]?.id;
92+
if (!id) {
9393
return null;
9494
}
9595

96-
const next = this.data[this.data.length - 1]?.id;
97-
if (!next) return null;
98-
return { params: { after: next } };
96+
return { params: { after: id } };
9997
}
10098
}

0 commit comments

Comments
 (0)