Skip to content
Draft
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
1 change: 1 addition & 0 deletions audit-ci.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"critical": true,
// Can't update ESLint yet because we must support Node 16
"allowlist": [
"GHSA-2v37-7h3g-55p8",
"GHSA-3ppc-4f35-3m26",
"GHSA-23c5-xmqv-rm74",
"GHSA-7r86-cg39-jmmj",
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,19 @@
"scripts": {
"build": "vite build",
"clean": "rm -rf ./dist ./nyc_output ./node_modules/.cache ./coverage",
"coverage": "cross-env NODE_ENV=test vitest run --coverage",
"coverage": "npm run typescript:test && cross-env NODE_ENV=test vitest run --coverage",
"docs": "jsdoc src/models src/services src/errors src/utils -d docs",
"format": "prettier --write .",
"formatCheck": "prettier --check .",
"lint": "eslint --ext .js,.ts --ignore-pattern 'examples/**' .",
"lintFix": "eslint --ext .js,.ts --ignore-pattern 'examples/**' --fix .",
"prepublishOnly": "npm run clean && npm run build && npm run test && npm run lint && npm run formatCheck",
"scan": "npx audit-ci -m --config ./audit-ci.jsonc",
"test": "cross-env NODE_ENV=test vitest run",
"test": "npm run typescript:test && cross-env NODE_ENV=test vitest run",
"test:node-compatibility": "cross-env NODE_ENV=test node ./test/node_compatibility",
"typescript": "npm run typescript:declarations && npm run typescript:source && npm run typescript:compat",
"typescript:declarations": "npx tsc -p tsconfig.json",
"typescript:test": "npx tsc -p tsconfig.test-services.json",
"typescript:source": "npx tsc -p tsconfig.build.json",
"typescript:compat": "npx tsc -p tsconfig.type-tests.json",
"watch": "vite build --watch"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
import baseService from './base_service';

type AddressCreateParameters = Record<string, unknown> & {
name?: string | null;
company?: string | null;
street1?: string | null;
street2?: string | null;
city?: string | null;
state?: string | null;
zip?: string | null;
country?: string | null;
phone?: string | null;
email?: string | null;
residential?: boolean | null;
federal_tax_id?: string | null;
state_tax_id?: string | null;
verify?: boolean | string | Array<boolean | string> | null;
verify_strict?: boolean | string | Array<boolean | string> | null;
verify_carrier?: string | null;
};

type PaginationCollection = Record<string, unknown>;

export default (easypostClient) =>
/**
* The AddressService class provides methods for interacting with EasyPost {@link Address} objects.
Expand All @@ -12,10 +33,10 @@ export default (easypostClient) =>
* @param {Object} params - Parameters for the address to be created.
* @returns {Address} - The created address.
*/
static async create(params) {
static async create(params: AddressCreateParameters): Promise<unknown> {
const url = 'addresses';

const wrappedParams = {};
const wrappedParams: Record<string, unknown> = {};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:
Looks like this is a known type, and could probably strengthen it just a little bit.

type MetaAddressKeys = "verify" | "verify_strict" | "verify_carrier";
type AddressCreateInternalParameters = 
  { address?: Omit<AddressCreateParameters, MetaAddressKeys> } & 
  Pick<AddressCreateParameters, MetaAddressKeys>;


if (params.verify) {
wrappedParams.verify = params.verify;
Expand Down Expand Up @@ -43,10 +64,10 @@ export default (easypostClient) =>
* @param {Object} params - Parameters for the address to be created.
* @returns {Address} - The created and verified address.
*/
static async createAndVerify(params) {
static async createAndVerify(params: AddressCreateParameters): Promise<unknown> {
const url = `addresses/create_and_verify`;

const wrappedParams = {};
const wrappedParams: Record<string, unknown> = {};

if (params.verify_carrier) {
wrappedParams.verify_carrier = params.verify_carrier;
Expand All @@ -70,7 +91,7 @@ export default (easypostClient) =>
* @param {Object} [params] - Parameters to filter the list of addresses.
* @returns {Object} - An object containing a list of {@link Address addresses} and pagination information.
*/
static async all(params = {}) {
static async all(params: Record<string, unknown> = {}): Promise<unknown> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:
Do we really not know what these return? It feels like these should be known.

const url = 'addresses';

return this._all(url, params);
Expand All @@ -82,7 +103,7 @@ export default (easypostClient) =>
* @param {Number} pageSize The number of records to return on each page
* @returns {EasyPostObject|Promise<never>} The retrieved {@link EasyPostObject}-based class instance, or a `Promise` that rejects with an error.
*/
static async getNextPage(addresses, pageSize = null) {
static async getNextPage(addresses: PaginationCollection, pageSize?: number): Promise<unknown> {
const url = 'addresses';
return this._getNextPage(url, 'addresses', addresses, pageSize);
}
Expand All @@ -93,7 +114,7 @@ export default (easypostClient) =>
* @param {string} id - The ID of the address to retrieve.
* @returns {Address} - The retrieved address.
*/
static async retrieve(id) {
static async retrieve(id: string): Promise<unknown> {
const url = `addresses/${id}`;

return this._retrieve(url);
Expand All @@ -105,12 +126,12 @@ export default (easypostClient) =>
* @param {string} id - The ID of the address to verify.
* @returns {Address} - The verified address.
*/
static async verifyAddress(id) {
static async verifyAddress(id: string): Promise<unknown> {
try {
const url = `addresses/${id}/verify`;
const response = await easypostClient._get(url);

return this._convertToEasyPostObject(response.body.address);
return this._convertToEasyPostObject(response.body.address, {});
} catch (e) {
return Promise.reject(e);
}
Expand Down
10 changes: 8 additions & 2 deletions src/services/base_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export default (easypostClient) =>
* @param {*} params The parameters passed when fetching the response.
* @returns {*} A plain object or array suitable for JSON serialization.
*/
static _convertToEasyPostObject(response, params = {}) {
static _convertToEasyPostObject(response: any, params: any = {}): any {
const modelResponse = this._buildEasyPostObject(response, params);

return this._toPlainEasyPostObject(modelResponse);
Expand Down Expand Up @@ -274,7 +274,13 @@ 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, key, collection, pageSize = null, optionalParams = {}) {
static async _getNextPage(
url: string,
key: string,
collection: any,
pageSize: number | null = null,
optionalParams: any = {},
): Promise<any> {
const collectionArray = collection[key];
if (collectionArray == undefined || collectionArray.length == 0 || !collection.has_more) {
throw new EndOfPaginationError();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import baseService from './base_service';

type CustomsItemInput = Record<string, unknown>;

type CustomsInfoCreateParameters = Record<string, unknown> & {
eel_pfc?: string | null;
contents_type?: string | null;
contents_explanation?: string | null;
customs_certify?: boolean | null;
customs_signer?: string | null;
non_delivery_option?: 'abandon' | 'return' | null;
restriction_type?: 'none' | 'other' | 'quarantine' | 'sanitary_phytosanitary_inspection' | null;
restriction_comments?: string | null;
customs_items?: CustomsItemInput[] | null;
declaration?: string | null;
};

export default (easypostClient) =>
/**
* The CustomsInfoService class provides methods for interacting with EasyPost {@link CustomsInfo} objects.
Expand All @@ -12,7 +27,7 @@ export default (easypostClient) =>
* @param {Object} params - Parameters for the customs info to be created.
* @returns {CustomsInfo} - The created customs info.
*/
static async create(params) {
static async create(params: CustomsInfoCreateParameters): Promise<unknown> {
const url = 'customs_infos';

const wrappedParams = {
Expand All @@ -28,7 +43,7 @@ export default (easypostClient) =>
* @param {string} id - The ID of the customs info to retrieve.
* @returns {CustomsInfo} - The retrieved customs info.
*/
static async retrieve(id) {
static async retrieve(id: string): Promise<unknown> {
const url = `customs_infos/${id}`;

return this._retrieve(url);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import baseService from './base_service';

type CustomsItemCreateParameters = Record<string, unknown> & {
description?: string | null;
quantity?: number | null;
value?: number | null;
weight?: number | null;
hs_tariff_number?: string | null;
code?: string | null;
origin_country?: string | null;
currency?: string | null;
};

export default (easypostClient) =>
/**
* The CustomsItemService class provides methods for interacting with EasyPost {@link CustomsItem} objects.
Expand All @@ -12,7 +23,7 @@ export default (easypostClient) =>
* @param {Object} params - Parameters for the customs item to be created.
* @returns {CustomsItem} - The created customs item.
*/
static async create(params) {
static async create(params: CustomsItemCreateParameters): Promise<unknown> {
const url = 'customs_items';

const wrappedParams = {
Expand All @@ -28,7 +39,7 @@ export default (easypostClient) =>
* @param {string} id - The ID of the customs item to retrieve.
* @returns {CustomsItem} - The retrieved customs item.
*/
static async retrieve(id) {
static async retrieve(id: string): Promise<unknown> {
const url = `customs_items/${id}`;

return this._retrieve(url);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import baseService from './base_service';

type ParcelCreateParameters = Record<string, unknown> & {
length?: number | null;
width?: number | null;
height?: number | null;
weight?: number | null;
predefined_package?: string | null;
};

export default (easypostClient) =>
/**
* The ParcelService class provides methods for interacting with EasyPost {@link Parcel} objects.
Expand All @@ -12,7 +20,7 @@ export default (easypostClient) =>
* @param {Object} params - The parameters to create a parcel with.
* @returns {Parcel} - The created parcel.
*/
static async create(params) {
static async create(params: ParcelCreateParameters): Promise<unknown> {
const url = 'parcels';

const wrappedParams = {
Expand All @@ -28,7 +36,7 @@ export default (easypostClient) =>
* @param {string} id - The ID of the parcel to retrieve.
* @returns {Parcel} - The retrieved parcel.
*/
static async retrieve(id) {
static async retrieve(id: string): Promise<unknown> {
const url = `parcels/${id}`;

return this._retrieve(url);
Expand Down
Loading
Loading