-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(PR-40): auto-page list requests (#100)
When calling `workspaces.list`, `forms.list`, `themes.list` or `responses.list` you can supply `page: "auto"` and the library will fetch all items across all pages for you automatically. It will fetch with maximum available `pageSize` to minimize number of requests.
- Loading branch information
Showing
14 changed files
with
316 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { rateLimit } from './utils' | ||
|
||
// request with maximum available page size to minimize number of requests | ||
const MAX_PAGE_SIZE = 200 | ||
|
||
type RequestItemsFn<Item> = ( | ||
page: number, | ||
pageSize: number | ||
) => Promise<{ | ||
total_items: number | ||
page_count: number | ||
items: Item[] | ||
}> | ||
|
||
const requestPageItems = async <Item>( | ||
requestFn: RequestItemsFn<Item>, | ||
page = 1 | ||
): Promise<Item[]> => { | ||
await rateLimit() | ||
const { items = [] } = (await requestFn(page, MAX_PAGE_SIZE)) || {} | ||
const moreItems = | ||
items.length === MAX_PAGE_SIZE | ||
? await requestPageItems(requestFn, page + 1) | ||
: [] | ||
return [...items, ...moreItems] | ||
} | ||
|
||
export const autoPageItems = async <Item>( | ||
requestFn: RequestItemsFn<Item> | ||
): Promise<{ | ||
total_items: number | ||
page_count: 1 | ||
items: Item[] | ||
}> => { | ||
const { total_items = 0, items = [] } = | ||
(await requestFn(1, MAX_PAGE_SIZE)) || {} | ||
return { | ||
total_items, | ||
page_count: 1, | ||
items: [ | ||
...items, | ||
...(total_items > items.length | ||
? await requestPageItems(requestFn, 2) | ||
: []), | ||
], | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.