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/add page tokens #29

Merged
merged 2 commits into from
Sep 6, 2024
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
25 changes: 18 additions & 7 deletions src/core/pinataSDK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import type {
OptimizeImageOptions,
GroupListResponse,
SignedUrlOptions,
FileListResponse,
} from "./types";
import { testAuthentication } from "./authentication/testAuthentication";
import { uploadFile } from "./uploads/file";
Expand Down Expand Up @@ -284,17 +285,22 @@ class FilterFiles {
return this;
}

then(onfulfilled?: ((value: FileListItem[]) => any) | null): Promise<any> {
pageToken(pageToken: string): FilterFiles {
this.query.pageToken = pageToken;
return this;
}

then(onfulfilled?: ((value: FileListResponse) => any) | null): Promise<any> {
return this.fetchPage().then(onfulfilled);
}

private async fetchPage(): Promise<FileListItem[]> {
private async fetchPage(): Promise<FileListResponse> {
if (this.currentPageToken) {
this.query.pageToken = this.currentPageToken;
}
const response = await listFiles(this.config, this.query);
this.currentPageToken = response.next_page_token;
return response.files;
return response;
}

// // rate limit, hopefully temporary?
Expand All @@ -315,7 +321,7 @@ class FilterFiles {
async *[Symbol.asyncIterator](): AsyncGenerator<FileListItem, void, unknown> {
while (true) {
const items = await this.fetchPage();
for (const item of items) {
for (const item of items.files) {
yield item;
}
if (!this.currentPageToken) {
Expand Down Expand Up @@ -635,13 +641,18 @@ class FilterGroups {
return this;
}

pageToken(pageToken: string): FilterGroups {
this.query.pageToken = pageToken;
return this;
}

then(
onfulfilled?: ((value: GroupResponseItem[]) => any) | null,
): Promise<GroupResponseItem[]> {
onfulfilled?: ((value: GroupListResponse) => any) | null,
): Promise<GroupListResponse> {
return this.fetchPage()
.then((response) => {
this.nextPageToken = response.next_page_token;
return response.groups;
return response;
})
.then(onfulfilled);
}
Expand Down