-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create helper function to fetch data with concurrent requests
- Loading branch information
1 parent
47cd91c
commit 326cb11
Showing
5 changed files
with
88 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 @@ | ||
export * from './paginatedRequest'; |
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,40 @@ | ||
import pAll from 'p-all'; | ||
|
||
/*** | ||
* Helper function to allow fetching huge number of data from APIs using paginated concurrent requests. | ||
* @param {function} [fetchFunction] function to query your endpoint | ||
* @param {object} [fetchParameters] contains the query parameters associated to your API request | ||
* @param {object} [fetchOptions] options that change the function behavior. | ||
* requestSize - how many data should be fetched per request | ||
* responseLength - how many data should be fetched in total using this function | ||
* responseTransformer - you can pass function to do some process after all items are fetched | ||
* concurrencyCount - how many concurrent api calls should be made | ||
* @returns {historyPusher} function to trigger the push | ||
*/ | ||
const paginatedRequest = async (fetchFunction, fetchParams, fetchOptions) => { | ||
const { requestSize = 100, responseLength, responseTransformer, concurrencyCount = 5 } = fetchOptions; | ||
|
||
const requests = []; | ||
let totalItems = responseLength, | ||
offset = 0; | ||
|
||
const requestQueueSize = totalItems / requestSize; | ||
|
||
while (requestQueueSize > offset) { | ||
requests.push( | ||
fetchFunction({ | ||
...fetchParams, | ||
limit: requestSize, | ||
offset: offset++, | ||
}) | ||
); | ||
} | ||
|
||
const results = await pAll(requests, { | ||
concurrency: concurrencyCount, | ||
}).then((response) => (responseTransformer ? responseTransformer(response) : response)); | ||
|
||
return results; | ||
}; | ||
|
||
export default paginatedRequest; |