Skip to content

Commit

Permalink
Merge pull request #29 from PinataCloud/feat/add-page-tokens
Browse files Browse the repository at this point in the history
feat/add page tokens
  • Loading branch information
stevedylandev authored Sep 6, 2024
2 parents ee0063f + ed8300d commit 333189c
Showing 1 changed file with 18 additions and 7 deletions.
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

0 comments on commit 333189c

Please sign in to comment.