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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ import Constants from '../constants';
import InvalidObjectError from '../errors/general/invalid_object_error';
import baseService from './base_service';

type PaymentMethodObject = {
id: string;
object: string;
};

type PaymentMethodsResponse = Record<string, unknown> & {
id: string | null;
primary_payment_method?: PaymentMethodObject | null;
secondary_payment_method?: PaymentMethodObject | null;
};

export default (easypostClient) =>
/**
* The BillingService class provides methods for interacting with EasyPost's billing capabilities.
Expand All @@ -14,7 +25,7 @@ export default (easypostClient) =>
* @param {String} amount - The amount to charge to your payment method.
* @param {String} priority - The priority of the payment method to charge. Can be either 'primary' or 'secondary'.
*/
static async fundWallet(amount, priority = 'primary') {
static async fundWallet(amount: string, priority: string = 'primary'): Promise<void> {
const paymentInfo = await this._getPaymentInfo(priority.toLowerCase());
const endpoint = paymentInfo[0];
const paymentMethodID = paymentInfo[1];
Expand All @@ -30,7 +41,7 @@ export default (easypostClient) =>
* See {@link https://docs.easypost.com/docs/users/billing#delete-a-payment-method EasyPost API Documentation} for more information.
* @param {String} priority - The priority of the payment method to delete. Can be either 'primary' or 'secondary'.
*/
static async deletePaymentMethod(priority) {
static async deletePaymentMethod(priority: string): Promise<void> {
const paymentInfo = await this._getPaymentInfo(priority.toLowerCase());
const endpoint = paymentInfo[0];
const paymentMethodID = paymentInfo[1];
Expand All @@ -45,7 +56,7 @@ export default (easypostClient) =>
* See {@link https://docs.easypost.com/docs/users/billing#retrieve-payment-methods EasyPost API Documentation} for more information.
* @returns {Object} - An object containing the payment methods associated with the current authenticated user.
*/
static async retrievePaymentMethods() {
static async retrievePaymentMethods(): Promise<PaymentMethodsResponse> {
const url = 'payment_methods';

const res = await easypostClient._get(url);
Expand All @@ -64,22 +75,26 @@ export default (easypostClient) =>
* @param {String} priority - The priority of the payment method to retrieve. Can be either 'primary' or 'secondary'.
* @returns {string[]} - An array of two strings, the first being the endpoint of the payment method and the second being the ID of the payment method.
*/
static async _getPaymentInfo(priority) {
static async _getPaymentInfo(priority: string): Promise<[string, string]> {
const paymentMethods = await this.retrievePaymentMethods();
const paymentMethodMap = {
const paymentMethodMap: Record<
string,
'primary_payment_method' | 'secondary_payment_method'
> = {
primary: 'primary_payment_method',
secondary: 'secondary_payment_method',
};

const paymentMethodToUse = paymentMethodMap[priority];
let paymentMethodID;
let paymentMethodObjectType;
let endpoint;
let paymentMethodID: string;
let paymentMethodObjectType: string;
let endpoint: string;
const errorString = 'The chosen payment method is not valid. Please try again.';

if (paymentMethodToUse !== undefined && paymentMethods[paymentMethodToUse] !== null) {
paymentMethodID = paymentMethods[paymentMethodToUse].id;
paymentMethodObjectType = paymentMethods[paymentMethodToUse].object;
const paymentMethod = paymentMethods[paymentMethodToUse] as PaymentMethodObject;
paymentMethodID = paymentMethod.id;
paymentMethodObjectType = paymentMethod.object;
if (paymentMethodObjectType === 'CreditCard') {
endpoint = 'credit_cards';
} else if (paymentMethodObjectType === 'BankAccount') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import Constants from '../constants';
import InvalidParameterError from '../errors/general/invalid_parameter_error';
import baseService from './base_service';

type CarrierAccountParams = Record<string, unknown> & { type?: string };

export default (easypostClient) =>
/**
* The CarrierAccountService class provides methods for interacting with EasyPost @{link CarrierAccount} objects.
Expand All @@ -16,7 +18,7 @@ export default (easypostClient) =>
* @param {Object} params - Parameters for the carrier account to be created.
* @returns {CarrierAccount} - The created carrier account.
*/
static async create(params) {
static async create(params: CarrierAccountParams): Promise<unknown> {
const carrierAccountType = params.type;

if (!carrierAccountType) {
Expand All @@ -38,7 +40,7 @@ export default (easypostClient) =>
* @param {Object} params - Parameters for the carrier account to be updated.
* @returns {CarrierAccount} - The updated carrier account.
*/
static async update(id, params) {
static async update(id: string, params: Record<string, unknown>): Promise<unknown> {
const wrappedParams = { carrier_account: params };

try {
Expand All @@ -56,7 +58,7 @@ export default (easypostClient) =>
* @param {string} id - The id of the carrier account to be deleted.
* @returns {Promise|Promise<never>} - A promise that resolves when the carrier account has been deleted.
*/
static async delete(id) {
static async delete(id: string): Promise<void> {
const url = `carrier_accounts/${id}`;

try {
Expand All @@ -74,7 +76,7 @@ export default (easypostClient) =>
* @param {string} carrierAccountType - The type of carrier account to be created.
* @returns {string} - The endpoint to be used for the carrier account creation request.
*/
static _selectCarrierAccountCreationEndpoint(carrierAccountType) {
static _selectCarrierAccountCreationEndpoint(carrierAccountType: string): string {
if (Constants.CARRIER_ACCOUNTS_WITH_CUSTOM_CREATE_WORKFLOWS.includes(carrierAccountType)) {
return 'carrier_accounts/register';
} else if (Constants.CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_OAUTH.includes(carrierAccountType)) {
Expand All @@ -91,7 +93,10 @@ export default (easypostClient) =>
* @param {Object} params - The parameters for the carrier account to be created.
* @returns {Object} - The wrapped carrier account parameters.
*/
static _wrapCarrierAccountParams(carrierAccountType, params) {
static _wrapCarrierAccountParams(
carrierAccountType: string,
params: Record<string, unknown>,
): Record<string, unknown> {
if (Constants.CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_OAUTH.includes(carrierAccountType)) {
return { carrier_account_oauth_registrations: params };
}
Expand All @@ -105,7 +110,7 @@ export default (easypostClient) =>
* @param {Object} [params] - Parameters to filter the list of carrier accounts.
* @returns {Object} - An object containing a list of {@link CarrierAccount carrier accounts} and pagination information.
*/
static async all(params = {}) {
static async all(params: Record<string, unknown> = {}): Promise<unknown> {
const url = 'carrier_accounts';

return this._all(url, params);
Expand All @@ -117,7 +122,7 @@ export default (easypostClient) =>
* @param {string} id - The ID of the carrier account to retrieve.
* @returns {CarrierAccount} - The retrieved carrier account.
*/
static async retrieve(id) {
static async retrieve(id: string): Promise<unknown> {
const url = `carrier_accounts/${id}`;

return this._retrieve(url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ export default (easypostClient) =>
* @param {Array} type - List of types in string
* @returns {Object[]} - List of carrier metadata
*/
static async retrieve(carriers = null, types = null) {
static async retrieve(
carriers: string[] | null = null,
types: string[] | null = null,
): Promise<unknown> {
const url = 'metadata/carriers';
const params = {
...(carriers && carriers.length > 0 && { carriers: carriers.join(',') }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default (easypostClient) =>
* @param {Object} [params] - Parameters to filter the list of carrier types.
* @returns {CarrierType[]} - A list of {@link CarrierType carrier types}.
*/
static async all(params = {}) {
static async all(params: Record<string, unknown> = {}): Promise<unknown> {
const url = 'carrier_types';

try {
Expand Down