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

fix(pagination): correct type annotation object field #590

Merged
merged 1 commit into from
Dec 20, 2023
Merged
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
30 changes: 14 additions & 16 deletions src/pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { AbstractPage, Response, APIClient, FinalRequestOptions, PageInfo } from
export interface PageResponse<Item> {
data: Array<Item>;

object: 'list';
object: string;
}

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

object: 'list';
object: string;

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

this.data = body.data;
this.data = body.data || [];
this.object = body.object;
}

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

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

export interface CursorPageParams {
/**
* Identifier for the last job from the previous pagination request.
*/
after?: string;

/**
* Number of fine-tuning jobs to retrieve.
*/
limit?: number;
}

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

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

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

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

nextPageInfo(): PageInfo | null {
if (!this.data?.length) {
const data = this.getPaginatedItems();
if (!data.length) {
return null;
}

const id = data[data.length - 1]?.id;
if (!id) {
return null;
}

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