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 3 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
}
2 changes: 1 addition & 1 deletion 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 Down
16 changes: 10 additions & 6 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 @@ -209,7 +212,8 @@ export default (easypostClient) =>
throw new EndOfPaginationError();
}

let params = {
const params = {
...(collection._params ?? collectionArray[0]._params ?? {}),
page_size: pageSize,
before_id: collectionArray[collectionArray.length - 1].id,
...optionalParams,
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/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
2 changes: 1 addition & 1 deletion 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
2 changes: 1 addition & 1 deletion src/services/report_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._get(url, params);
const responseObject = this._convertToEasyPostObject(response.body);
const responseObject = this._convertToEasyPostObject(response.body, params);
responseObject.type = type;

return responseObject;
Expand Down
12 changes: 6 additions & 6 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 @@ -272,7 +272,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
4 changes: 2 additions & 2 deletions src/services/user_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,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 Expand Up @@ -111,7 +111,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/webhook_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default (easypostClient) =>
try {
const response = await easypostClient._patch(url, params);

return this._convertToEasyPostObject(response.body);
return this._convertToEasyPostObject(response.body, params);
} catch (e) {
return Promise.reject(e);
}
Expand Down
35 changes: 35 additions & 0 deletions test/helpers/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Utils to remove the _params key and all params keys from nested objects from
* an EasyPostObject for sake of comparison in tests
* @param {*} obj an EasyPostObject to remove the params from
* @returns {*} obj without the _params key
*/
export const withoutParams = (obj) =>
Object.fromEntries(
Object.entries(obj)
.map(([key, value]) => {
if (key === '_params') {
return null;
}

if (!value) {
return [key, value];
}

if (Array.isArray(value)) {
return [
key,
value.map((arrValue) =>
typeof arrValue === 'object' ? withoutParams(arrValue) : arrValue,
),
];
}

if (typeof value === 'object') {
return [key, withoutParams(value)];
}

return [key, value];
})
.filter(Boolean),
);
3 changes: 2 additions & 1 deletion test/services/address.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import EndOfPaginationError from '../../src/errors/general/end_of_pagination_err
import Address from '../../src/models/address';
import Fixture from '../helpers/fixture';
import * as setupPolly from '../helpers/setup_polly';
import { withoutParams } from '../helpers/utils';

/* eslint-disable func-names */
describe('Address Service', function () {
Expand Down Expand Up @@ -66,7 +67,7 @@ describe('Address Service', function () {
const retrievedAddress = await this.client.Address.retrieve(address.id);

expect(retrievedAddress).to.be.an.instanceOf(Address);
expect(retrievedAddress).to.deep.include(address);
expect(withoutParams(retrievedAddress)).to.deep.include(withoutParams(address));
});

it('retrieves all addresses', async function () {
Expand Down
3 changes: 2 additions & 1 deletion test/services/carrier_account.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import EasyPostClient from '../../src/easypost';
import CarrierAccount from '../../src/models/carrier_account';
import Fixture from '../helpers/fixture';
import * as setupPolly from '../helpers/setup_polly';
import { withoutParams } from '../helpers/utils';

/* eslint-disable func-names */
describe('CarrierAccount Service', function () {
Expand Down Expand Up @@ -60,7 +61,7 @@ describe('CarrierAccount Service', function () {
const retrievedCarrierAccount = await this.client.CarrierAccount.retrieve(carrierAccount.id);

expect(retrievedCarrierAccount).to.be.an.instanceOf(CarrierAccount);
expect(retrievedCarrierAccount).to.deep.include(carrierAccount);
expect(withoutParams(retrievedCarrierAccount)).to.deep.include(withoutParams(carrierAccount));

// Remove the carrier account once we have tested it so we don't pollute the account with test accounts
await this.client.CarrierAccount.delete(carrierAccount.id);
Expand Down
3 changes: 2 additions & 1 deletion test/services/customs_info.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import EasyPostClient from '../../src/easypost';
import CustomsInfo from '../../src/models/customs_info';
import Fixture from '../helpers/fixture';
import * as setupPolly from '../helpers/setup_polly';
import { withoutParams } from '../helpers/utils';

describe('CustomsInfo Service', function () {
setupPolly.startPolly();
Expand All @@ -31,6 +32,6 @@ describe('CustomsInfo Service', function () {
const retrievedCustomsInfo = await this.client.CustomsInfo.retrieve(customsInfo.id);

expect(customsInfo).to.be.an.instanceOf(CustomsInfo);
expect(retrievedCustomsInfo).to.deep.include(customsInfo);
expect(withoutParams(retrievedCustomsInfo)).to.deep.include(withoutParams(customsInfo));
});
});
3 changes: 2 additions & 1 deletion test/services/customs_item.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import EasyPost from '../../src/easypost';
import CustomsItem from '../../src/models/customs_item';
import Fixture from '../helpers/fixture';
import * as setupPolly from '../helpers/setup_polly';
import { withoutParams } from '../helpers/utils';

describe('CustomsItem Service', function () {
setupPolly.startPolly();
Expand All @@ -31,6 +32,6 @@ describe('CustomsItem Service', function () {
const retrievedCustomsInfo = await this.client.CustomsItem.retrieve(customsItem.id);

expect(customsItem).to.be.an.instanceOf(CustomsItem);
expect(retrievedCustomsInfo).to.deep.include(customsItem);
expect(withoutParams(retrievedCustomsInfo)).to.deep.include(withoutParams(customsItem));
});
});
3 changes: 2 additions & 1 deletion test/services/parcel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import EasyPostClient from '../../src/easypost';
import Parcel from '../../src/models/parcel';
import Fixture from '../helpers/fixture';
import * as setupPolly from '../helpers/setup_polly';
import { withoutParams } from '../helpers/utils';

describe('Parcel Service', function () {
setupPolly.startPolly();
Expand All @@ -31,6 +32,6 @@ describe('Parcel Service', function () {
const retrievedParcel = await this.client.Parcel.retrieve(parcel.id);

expect(parcel).to.be.an.instanceOf(Parcel);
expect(retrievedParcel).to.deep.include(parcel);
expect(withoutParams(retrievedParcel)).to.deep.include(withoutParams(parcel));
});
});
Loading