Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[MS-316] Refactor get all recursively to use requestParams instead of limit #45

Merged
merged 1 commit into from
Sep 20, 2021
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
13 changes: 9 additions & 4 deletions src/util/requestHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,20 @@ import { defaultParams } from '../index';
*
* @param {T} api The INSTANCE of an api (ex. ordersApi, inventoryApi, etc)
* @param {string} methodName The name of the instanced method to use for request (ex. ordersApi.getAllOrders.name)
* @param {string} limit The limit param sent to all endpoints that require pagination
* @param {object} requestParams The request parameters for the type of API.
* @param {string} nextCursor Data returned from previous response to handle requesting next pagination records - Optional and should be null for first call.
*
* Note: Please use with caution. It is possible for this to be long running and quickly consume your request
* rate limits!
*
* @returns {Promise<T[]>} Array of data based on the endpoint requested
*/
const getAllRecursively = async <T>(api: T, methodName: string, limit = '100', nextCursor?: string): Promise<T[]> => {
const getAllRecursively = async <T>(
api: T,
methodName: string,
requestParams = { limit: '100' },
nextCursor?: string
): Promise<T[]> => {
// parse out cursor params
const nextCursorUrlParams = new URLSearchParams(nextCursor);

Expand All @@ -34,7 +39,7 @@ const getAllRecursively = async <T>(api: T, methodName: string, limit = '100', n
const ordersResponse = await api[methodName](
{
...defaultParams,
limit,
...requestParams,
},
options
);
Expand All @@ -43,7 +48,7 @@ const getAllRecursively = async <T>(api: T, methodName: string, limit = '100', n
const { meta, elements } = ordersResponse.data.list;
let data = [...elements.order];
if (meta.nextCursor) {
const recursiveData = await getAllRecursively(api, methodName, limit, meta.nextCursor);
const recursiveData = await getAllRecursively(api, methodName, requestParams, meta.nextCursor);
data = [...data, ...recursiveData];
}

Expand Down