Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Forward params on pagination #422

Merged
merged 4 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## NEXT

- Fixes a pagination issue by passing along additional params used when fetching first page

## v6.8.2 (2023-10-20)

- Bump all dependencies to address security vulnerabilities in Babel
Expand Down
1 change: 1 addition & 0 deletions src/models/easypost_object.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export default class EasyPostObject {
static mode;
static created_at;
static updated_at;
static _params;
nwithan8 marked this conversation as resolved.
Show resolved Hide resolved
}
4 changes: 2 additions & 2 deletions src/services/address_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default (easypostClient) =>
try {
const response = await easypostClient._post(url, wrappedParams);

return this._convertToEasyPostObject(response.body.address);
return this._convertToEasyPostObject(response.body.address, wrappedParams);
} catch (e) {
return Promise.reject(e);
}
Expand All @@ -73,7 +73,7 @@ export default (easypostClient) =>
*/
static async getNextPage(addresses, pageSize = null) {
const url = 'addresses';
return this._getNextPage(url, addresses, pageSize);
return this._getNextPage(url, 'addresses', addresses, pageSize);
}

/**
Expand Down
26 changes: 16 additions & 10 deletions src/services/base_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,14 @@ export default (easypostClient) =>
* Converts a JSON response and all its nested elements to associated {@link EasyPostObject}-based class instances.
* @internal
* @param {*} response The JSON response to convert (usually a `Map` or `Array`).
* @param {*} params The parameters passed when fetching the response
* @returns {*} An {@link EasyPostObject}-based class instance or an `Array` of {@link EasyPostObject}-based class instances.
*/
static _convertToEasyPostObject(response) {
static _convertToEasyPostObject(response, params) {
if (Array.isArray(response)) {
return response.map((value) => {
if (typeof value === 'object') {
return this._convertToEasyPostObject(value);
return this._convertToEasyPostObject(value, params);
}
return value;
});
Expand All @@ -134,9 +135,11 @@ export default (easypostClient) =>
}

Object.keys(response).forEach((key) => {
classObject[key] = this._convertToEasyPostObject(response[key]);
classObject[key] = this._convertToEasyPostObject(response[key], params);
});

classObject._params = params;

return classObject;
}
return response;
Expand All @@ -153,7 +156,7 @@ export default (easypostClient) =>
try {
const response = await easypostClient._post(url, params);

return this._convertToEasyPostObject(response.body);
return this._convertToEasyPostObject(response.body, params);
} catch (e) {
return Promise.reject(e);
}
Expand All @@ -171,7 +174,7 @@ export default (easypostClient) =>
// eslint-disable-next-line no-param-reassign
const response = await easypostClient._get(url, params);

return this._convertToEasyPostObject(response.body);
return this._convertToEasyPostObject(response.body, params);
} catch (e) {
return Promise.reject(e);
}
Expand Down Expand Up @@ -203,20 +206,23 @@ export default (easypostClient) =>
* @returns {EasyPostObject|Promise<never>} The retrieved {@link EasyPostObject}-based class instance, or a `Promise` that rejects with an error.
* TODO: Implement this function in EndShippers and Batches once the API supports them properly.
*/
static async _getNextPage(url, collection, pageSize = null, optionalParams = {}) {
const collectionArray = collection[url];
static async _getNextPage(url, key, collection, pageSize = null, optionalParams = {}) {
const collectionArray = collection[key];
if (collectionArray == undefined || collectionArray.length == 0 || !collection.has_more) {
throw new EndOfPaginationError();
}

let params = {
page_size: pageSize,
const defaultParams = collection._params ?? collectionArray[0]._params ?? {};

const params = {
...defaultParams,
page_size: defaultParams.page_size ?? pageSize,
before_id: collectionArray[collectionArray.length - 1].id,
...optionalParams,
};

const response = await this._all(url, params);
if (response == undefined || response[url].length == 0) {
if (response == undefined || response[key].length == 0) {
throw new EndOfPaginationError();
}

Expand Down
8 changes: 4 additions & 4 deletions src/services/batch_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default (easypostClient) =>
try {
const response = await easypostClient._post(url, wrappedParams);

return this._convertToEasyPostObject(response.body);
return this._convertToEasyPostObject(response.body, wrappedParams);
} catch (e) {
return Promise.reject(e);
}
Expand All @@ -61,7 +61,7 @@ export default (easypostClient) =>
try {
const response = await easypostClient._post(url, wrappedParams);

return this._convertToEasyPostObject(response.body);
return this._convertToEasyPostObject(response.body, wrappedParams);
} catch (e) {
return Promise.reject(e);
}
Expand All @@ -81,7 +81,7 @@ export default (easypostClient) =>
try {
const response = await easypostClient._post(url, wrappedParams);

return this._convertToEasyPostObject(response.body);
return this._convertToEasyPostObject(response.body, wrappedParams);
} catch (e) {
return Promise.reject(e);
}
Expand Down Expand Up @@ -118,7 +118,7 @@ export default (easypostClient) =>
try {
const response = await easypostClient._post(url, wrappedParams);

return this._convertToEasyPostObject(response.body);
return this._convertToEasyPostObject(response.body, wrappedParams);
} catch (e) {
return Promise.reject(e);
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/beta_carrier_metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default (easypostClient) =>

try {
const response = await easypostClient._get(url, params);
return this._convertToEasyPostObject(response.body.carriers || []);
return this._convertToEasyPostObject(response.body.carriers || [], params);
} catch (e) {
return Promise.reject(e);
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/beta_rate_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default (easypostClient) =>
try {
const response = await easypostClient._post(url, wrappedParams);

return this._convertToEasyPostObject(response.body.rates);
return this._convertToEasyPostObject(response.body.rates, wrappedParams);
} catch (e) {
return Promise.reject(e);
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/carrier_account_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default (easypostClient) =>
try {
const response = await easypostClient._patch(url, wrappedParams);

return this._convertToEasyPostObject(response.body);
return this._convertToEasyPostObject(response.body, wrappedParams);
} catch (e) {
return Promise.reject(e);
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/carrier_metadata_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default (easypostClient) =>

try {
const response = await easypostClient._get(url, params);
return this._convertToEasyPostObject(response.body.carriers || []);
return this._convertToEasyPostObject(response.body.carriers || [], params);
} catch (e) {
return Promise.reject(e);
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/carrier_type_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default (easypostClient) =>
try {
const response = await easypostClient._get(url, params);

return this._convertToEasyPostObject(response.body);
return this._convertToEasyPostObject(response.body, params);
} catch (e) {
return Promise.reject(e);
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/end_shipper_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default (easypostClient) =>
try {
const response = await easypostClient._put(url, wrappedParams);

return this._convertToEasyPostObject(response.body);
return this._convertToEasyPostObject(response.body, wrappedParams);
} catch (e) {
return Promise.reject(e);
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/event_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default (easypostClient) =>
*/
static async getNextPage(events, pageSize = null) {
const url = 'events';
return this._getNextPage(url, events, pageSize);
return this._getNextPage(url, 'events', events, pageSize);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/services/insurance_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default (easypostClient) =>
*/
static async getNextPage(insurances, pageSize = null) {
const url = 'insurances';
return this._getNextPage(url, insurances, pageSize);
return this._getNextPage(url, 'insurances', insurances, pageSize);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/services/order_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default (easypostClient) =>
try {
const response = await easypostClient._post(url, wrappedParams);

return this._convertToEasyPostObject(response.body);
return this._convertToEasyPostObject(response.body, wrappedParams);
} catch (e) {
return Promise.reject(e);
}
Expand Down
4 changes: 2 additions & 2 deletions src/services/pickup_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default (easypostClient) =>
try {
const response = await easypostClient._post(url, wrappedParams);

return this._convertToEasyPostObject(response.body);
return this._convertToEasyPostObject(response.body, wrappedParams);
} catch (e) {
return Promise.reject(e);
}
Expand Down Expand Up @@ -79,7 +79,7 @@ export default (easypostClient) =>
*/
static async getNextPage(pickups, pageSize = null) {
const url = 'pickups';
return this._getNextPage(url, pickups, pageSize);
return this._getNextPage(url, 'pickups', pickups, pageSize);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/services/referral_customer_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,6 @@ export default (easypostClient) =>
*/
static async getNextPage(referralCustomers, pageSize = null) {
const url = 'referral_customers';
return this._getNextPage(url, referralCustomers, pageSize);
return this._getNextPage(url, 'referral_customers', referralCustomers, pageSize);
}
};
2 changes: 1 addition & 1 deletion src/services/refund_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default (easypostClient) =>
*/
static async getNextPage(refunds, pageSize = null) {
const url = 'refunds';
return this._getNextPage(url, refunds, pageSize);
return this._getNextPage(url, 'refunds', refunds, pageSize);
}

/**
Expand Down
9 changes: 5 additions & 4 deletions src/services/report_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ export default (easypostClient) =>

try {
const response = await easypostClient._get(url, params);
const responseObject = this._convertToEasyPostObject(response.body);
responseObject.type = type;
// we want to add it back in here so that it can be used in the
// "getNextPage" call below
const responseObject = this._convertToEasyPostObject(response.body, { ...params, type });

return responseObject;
} catch (e) {
Expand All @@ -49,8 +50,8 @@ export default (easypostClient) =>
* @returns {EasyPostObject|Promise<never>} The retrieved {@link EasyPostObject}-based class instance, or a `Promise` that rejects with an error.
*/
static async getNextPage(reports, pageSize = null) {
const url = `reports/${reports.type}`;
return this._getNextPage(url, reports, pageSize);
const url = `reports/${reports.reports[0]._params.type}`;
return this._getNextPage(url, 'reports', reports, pageSize);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/services/scan_form_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export default (easypostClient) =>
*/
static async getNextPage(scanforms, pageSize = null) {
const url = 'scan_forms';
return this._getNextPage(url, scanforms, pageSize);
return this._getNextPage(url, 'scan_forms', scanforms, pageSize);
}

/**
Expand Down
24 changes: 8 additions & 16 deletions src/services/shipment_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default (easypostClient) =>
try {
const response = await easypostClient._post(url, wrappedParams);

return this._convertToEasyPostObject(response.body);
return this._convertToEasyPostObject(response.body, wrappedParams);
} catch (e) {
return Promise.reject(e);
}
Expand All @@ -88,7 +88,7 @@ export default (easypostClient) =>
try {
const response = await easypostClient._get(url, wrappedParams);

return this._convertToEasyPostObject(response.body);
return this._convertToEasyPostObject(response.body, wrappedParams);
} catch (e) {
return Promise.reject(e);
}
Expand All @@ -108,7 +108,7 @@ export default (easypostClient) =>
try {
const response = await easypostClient._post(url, wrappedParams);

return this._convertToEasyPostObject(response.body);
return this._convertToEasyPostObject(response.body, wrappedParams);
} catch (e) {
return Promise.reject(e);
}
Expand Down Expand Up @@ -146,7 +146,7 @@ export default (easypostClient) =>
try {
const response = await easypostClient._post(url, wrappedParams);

return this._convertToEasyPostObject(response.body);
return this._convertToEasyPostObject(response.body, wrappedParams);
} catch (e) {
return Promise.reject(e);
}
Expand All @@ -172,7 +172,7 @@ export default (easypostClient) =>
try {
const response = await easypostClient._post(url, wrappedParams);

return this._convertToEasyPostObject(response.body);
return this._convertToEasyPostObject(response.body, wrappedParams);
} catch (e) {
return Promise.reject(e);
}
Expand Down Expand Up @@ -221,11 +221,7 @@ export default (easypostClient) =>
static async all(params = {}) {
const url = 'shipments';

const response = await this._all(url, params);
response.purchased = params.purchased;
response.include_children = params.include_children;

return response;
return this._all(url, params);
}

/**
Expand All @@ -236,12 +232,8 @@ export default (easypostClient) =>
*/
static async getNextPage(shipments, pageSize = null) {
const url = 'shipments';
const params = {
purchased: shipments.purchased ?? true,
include_children: shipments.include_children ?? false,
};

return this._getNextPage(url, shipments, pageSize, params);
return this._getNextPage(url, 'shipments', shipments, pageSize);
}

/**
Expand Down Expand Up @@ -272,7 +264,7 @@ export default (easypostClient) =>
try {
const response = await easypostClient._get(url, params);

return this._convertToEasyPostObject(response.body.rates ?? []);
return this._convertToEasyPostObject(response.body.rates ?? [], params);
} catch (e) {
return Promise.reject(e);
}
Expand Down
Loading