Skip to content
Open
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
18 changes: 15 additions & 3 deletions src/errors/api/api_error.js → src/errors/api/api_error.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import EasyPostError from '../easypost_error';

type ApiErrorParams = {
message?: unknown;
code?: string;
statusCode?: number;
errors?: unknown[];
};

/**
* The ApiError class is used to represent errors that occurred while communicating with the EasyPost API.
* This class should not be instantiated directly.
Expand All @@ -12,11 +19,16 @@ import EasyPostError from '../easypost_error';
* @property {EasyPostError[]} [errors] - An array of sub-errors returned by the EasyPost API.
*/
export default class ApiError extends EasyPostError {
constructor({ message, code, statusCode, errors } = {}) {
super({ message });
code?: string;
errors?: unknown[];
statusCode?: number;

constructor({ message, code, statusCode, errors }: ApiErrorParams = {}) {
const normalizedMessage = typeof message === 'string' ? message : String(message ?? '');
super({ message: normalizedMessage });
this.code = code;
this.errors = errors;
this.message = message;
this.message = normalizedMessage;
this.statusCode = statusCode;
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @param {string} [message] - The message to be displayed when the error is logged.
*/
export default class EasyPostError extends Error {
constructor({ message } = {}) {
constructor({ message }: { message?: string } = {}) {
super(message);
}
}
32 changes: 24 additions & 8 deletions src/errors/error_handler.js → src/errors/error_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ import UnauthorizedError from './api/unauthorized_error';
import UnknownApiError from './api/unknown_api_error';
import EasyPostError from './easypost_error';

type ApiErrorBody = {
error?: {
code?: string;
message?: unknown;
errors?: unknown[];
};
};

type ApiErrorResponse = {
status?: number;
statusCode?: number;
headers?: Record<string, string>;
body?: ApiErrorBody;
[key: string]: unknown;
};

export default class ErrorHandler {
/**
* Recursively traverses a JSON object or array and extracts error messages
Expand All @@ -23,7 +39,7 @@ export default class ErrorHandler {
* @param {object|array|string} errorMessage - The JSON object or array to traverse.
* @param {array} messagesList - The array to which extracted error messages will be added.
*/
static traverseJsonElement(errorMessage, messagesList) {
static traverseJsonElement(errorMessage: unknown, messagesList: string[]): void {
if (errorMessage instanceof Object) {
for (const value of Object.values(errorMessage)) {
this.traverseJsonElement(value, messagesList);
Expand All @@ -33,17 +49,17 @@ export default class ErrorHandler {
this.traverseJsonElement(value, messagesList);
}
} else {
messagesList.push(errorMessage.toString());
messagesList.push(String(errorMessage));
}
}
/**
* Calculate and generate the appropriate {@link ApiError} based on a received HTTP response error.
* @param {*} error - The errored HTTP response.
* @returns {ApiError} The `ApiError`-based error corresponding to the HTTP status code.
*/
static handleApiError(error) {
const { statusCode } = error;
const { code, message, errors } = error.body.error;
static handleApiError(error: ApiErrorResponse): Error {
const statusCode = error.statusCode ?? error.status;
const { code, message, errors } = error.body?.error ?? {};
const errorParams = {
message,
code,
Expand All @@ -52,18 +68,18 @@ export default class ErrorHandler {
};

try {
const messages = [];
const messages: string[] = [];
this.traverseJsonElement(errorParams.message, messages);
errorParams.message = messages.join(', ');
} catch (e) {
} catch (_error) {
const errorParams = {
message: Constants.ERROR_DESERIALIZATION,
code: 'ERROR_DESERIALIZATION_ERROR',
};
return new EasyPostError(errorParams);
}

if (statusCode >= 300 && statusCode < 400) {
if (typeof statusCode === 'number' && statusCode >= 300 && statusCode < 400) {
return new RedirectError(errorParams);
}

Expand Down
6 changes: 3 additions & 3 deletions src/services/address_service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import baseService from './base_service';
import Address from '../models/address';
import type EasyPostClient from '../easypost';
import Address from '../models/address';
import baseService from './base_service';

type AddressCreateParameters = Record<string, unknown> & {
export type AddressCreateParameters = Record<string, unknown> & {
name?: string | null;
company?: string | null;
street1?: string | null;
Expand Down
6 changes: 3 additions & 3 deletions src/services/batch_service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { IShipmentCreateParameters } from '../../types/Shipment';
import type { DeepPartial } from '../../types/utils';
import type EasyPostClient from '../easypost';
import Batch from '../models/batch';
import baseService from './base_service';

export const DEFAULT_LABEL_FORMAT = 'pdf';

type ShipmentReferenceInput = { id?: string | null } & DeepPartial<IShipmentCreateParameters>;
type ShipmentReferenceInput = Record<string, unknown> & {
id?: string | null;
};

type BatchCreateParameters = Record<string, unknown> & {
shipments?: Array<string | ShipmentReferenceInput> | null;
Expand Down
3 changes: 2 additions & 1 deletion src/services/beta_referral_customer_service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type EasyPostClient from '../easypost';
import baseService from './base_service';
import type { PaymentMethodObject } from './billing_service';

type BetaPaymentMethodResponse = Record<string, unknown>;
type BetaPaymentMethodResponse = PaymentMethodObject;
type BetaRefundResponse = Record<string, unknown>;
type BetaClientSecretResponse = Record<string, unknown>;

Expand Down
4 changes: 2 additions & 2 deletions src/services/billing_service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Constants from '../constants';
import type EasyPostClient from '../easypost';
import InvalidObjectError from '../errors/general/invalid_object_error';
import baseService from './base_service';
import type EasyPostClient from '../easypost';

type PaymentMethodObject = {
export type PaymentMethodObject = Record<string, unknown> & {
id: string;
object: string;
};
Expand Down
8 changes: 4 additions & 4 deletions src/services/customs_info_service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import baseService from './base_service';
import CustomsInfo from '../models/customs_info';
import type EasyPostClient from '../easypost';
import CustomsInfo from '../models/customs_info';
import baseService from './base_service';

type CustomsItemInput = Record<string, unknown>;
export type CustomsItemInput = Record<string, unknown>;

type CustomsInfoCreateParameters = Record<string, unknown> & {
export type CustomsInfoCreateParameters = Record<string, unknown> & {
eel_pfc?: string | null;
contents_type?: string | null;
contents_explanation?: string | null;
Expand Down
6 changes: 3 additions & 3 deletions src/services/parcel_service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import baseService from './base_service';
import Parcel from '../models/parcel';
import type EasyPostClient from '../easypost';
import Parcel from '../models/parcel';
import baseService from './base_service';

type ParcelCreateParameters = Record<string, unknown> & {
export type ParcelCreateParameters = Record<string, unknown> & {
length?: number | null;
width?: number | null;
height?: number | null;
Expand Down
10 changes: 5 additions & 5 deletions src/services/referral_customer_service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import util from 'util';

import type { IPaymentMethod } from '../../types/PaymentMethod/PaymentMethod';
import Constants from '../constants';
import EasyPostClient from '../easypost';
import ExternalApiError from '../errors/api/external_api_error';
import User from '../models/user';
import baseService from './base_service';
import type { PaymentMethodObject } from './billing_service';

type ReferralCreateParameters = Record<string, unknown> & {
reference?: string | null;
Expand Down Expand Up @@ -118,7 +118,7 @@ async function _sendCardDetailsToEasyPost(
referralApiKey: string,
stripeCreditCardToken: string,
priority: string,
): Promise<IPaymentMethod> {
): Promise<PaymentMethodObject> {
const _client = _getReferralClient(client, referralApiKey);
const url = 'credit_cards';
const params = { credit_card: { stripe_object_id: stripeCreditCardToken, priority } };
Expand Down Expand Up @@ -184,7 +184,7 @@ export default (easypostClient: EasyPostClient) =>
expirationYear: string,
cvc: string,
priority: string = 'primary',
): Promise<IPaymentMethod> {
): Promise<PaymentMethodObject> {
const stripeKey = await _getEasyPostStripeKey(easypostClient); // will throw if there's an error

const stripeCreditCardId = await _sendCardDetailsToStripe(
Expand Down Expand Up @@ -214,7 +214,7 @@ export default (easypostClient: EasyPostClient) =>
referralApiKey: string,
paymentMethodId: string,
priority: string = 'primary',
): Promise<IPaymentMethod> {
): Promise<PaymentMethodObject> {
const _client = _getReferralClient(easypostClient, referralApiKey);
const params = {
credit_card: {
Expand All @@ -239,7 +239,7 @@ export default (easypostClient: EasyPostClient) =>
financialConnectionsId: string,
mandateData: MandateData,
priority: string = 'primary',
): Promise<IPaymentMethod> {
): Promise<PaymentMethodObject> {
const _client = _getReferralClient(easypostClient, referralApiKey);
const params = {
financial_connections_id: financialConnectionsId,
Expand Down
6 changes: 3 additions & 3 deletions src/services/scan_form_service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { IShipmentCreateParameters } from '../../types/Shipment';
import type { DeepPartial } from '../../types/utils';
import type EasyPostClient from '../easypost';
import ScanForm from '../models/scan_form';
import baseService from './base_service';

type ShipmentReferenceInput = { id?: string | null } & DeepPartial<IShipmentCreateParameters>;
type ShipmentReferenceInput = Record<string, unknown> & {
id?: string | null;
};

type ScanFormCreateParameters = {
shipments?: Array<string | ShipmentReferenceInput> | null;
Expand Down
33 changes: 11 additions & 22 deletions src/services/shipment_service.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
import type { ICustomsInfoCreateParameters } from '../../types/Customs/CustomsInfo/CustomsInfoCreateParameters';
import type { DeepPartial } from '../../types/utils';
import Constants from '../constants';
import type EasyPostClient from '../easypost';
import Address from '../models/address';
import CustomsInfo from '../models/customs_info';
import Parcel from '../models/parcel';
import Shipment from '../models/shipment';
import type { AddressCreateParameters } from './address_service';
import baseService from './base_service';

type AddressCreateInput = Record<string, unknown> & {
verify?: boolean | string | string[] | null;
verify_strict?: boolean | string | string[] | null;
verify_carrier?: string | null;
};

type ParcelCreateInput = Record<string, unknown> & {
length?: number | null;
width?: number | null;
height?: number | null;
weight?: number | null;
predefined_package?: string | null;
};
import type { CustomsInfoCreateParameters } from './customs_info_service';
import type { ParcelCreateParameters } from './parcel_service';

type ShipmentTaxIdentifier = Record<string, unknown> & {
entity?: string | null;
Expand All @@ -34,16 +21,18 @@ type ShipmentLineItem = Record<string, unknown> & {
item_description?: string | null;
};

type CustomsInfoReferenceInput = DeepPartial<ICustomsInfoCreateParameters>;

type ShipmentCreateParameters = Record<string, unknown> & {
reference?: string | null;
to_address?: AddressCreateInput | Address | string | null;
from_address?: AddressCreateInput | Address | string | null;
parcel?: ParcelCreateInput | Parcel | string | null;
to_address?: AddressCreateParameters | Address | string | null;
from_address?: AddressCreateParameters | Address | string | null;
parcel?: ParcelCreateParameters | Parcel | string | null;
carrier_accounts?: string[] | null;
customs_info?:
CustomsInfo | CustomsInfo[] | CustomsInfoReferenceInput | CustomsInfoReferenceInput[] | null;
| CustomsInfo
| CustomsInfo[]
| CustomsInfoCreateParameters
| CustomsInfoCreateParameters[]
| null;
tax_identifiers?: Array<ShipmentTaxIdentifier | null | undefined> | null;
options?: Record<string, unknown> | null;
line_items?: ShipmentLineItem[] | null;
Expand Down
2 changes: 1 addition & 1 deletion test/types/permissive_types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import EasyPostClient from '../..';
import EasyPostClient from '../../src/easypost';

const client = new EasyPostClient('api-key', {
requestMiddleware: (request: any) => request,
Expand Down
9 changes: 0 additions & 9 deletions types/.eslintrc

This file was deleted.

Loading