Skip to content

Commit

Permalink
create helper function to fetch data with concurrent requests
Browse files Browse the repository at this point in the history
  • Loading branch information
mkholjuraev committed Jan 8, 2024
1 parent 47cd91c commit 326cb11
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 3 deletions.
44 changes: 43 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
"@patternfly/react-core": "^5.0.0",
"@patternfly/react-table": "^5.0.0",
"@redhat-cloud-services/rbac-client": "^1.0.100",
"cypress": ">=12.0.0 < 14.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-redux": ">=7.0.0",
"react-router-dom": "^5.0.0 || ^6.0.0",
"cypress": ">=12.0.0 < 14.0.0"
"react-router-dom": "^5.0.0 || ^6.0.0"
},
"dependencies": {
"@redhat-cloud-services/types": "^0.0.24",
Expand All @@ -43,6 +43,7 @@
"axios": "^0.27.2",
"commander": "^2.20.3",
"mkdirp": "^1.0.4",
"p-all": "^5.0.0",
"react-content-loader": "^6.2.0"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ export { useInventory } from './useInventory';
export * from './CypressUtils';
export * from './useInsightsNavigate';
export * from './useExportPDF';
export * from './paginatedRequest';
1 change: 1 addition & 0 deletions packages/utils/src/paginatedRequest/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './paginatedRequest';
40 changes: 40 additions & 0 deletions packages/utils/src/paginatedRequest/paginatedRequest.js
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;

0 comments on commit 326cb11

Please sign in to comment.