From b7e19c2501bf4ff105579076a964a3da3eb18149 Mon Sep 17 00:00:00 2001 From: Justintime50 <39606064+Justintime50@users.noreply.github.com> Date: Fri, 7 Aug 2026 14:44:34 -0600 Subject: [PATCH] TSM-08: convert Group C services to TypeScript --- ...{billing_service.js => billing_service.ts} | 35 +++++++++++++------ ..._service.js => carrier_account_service.ts} | 19 ++++++---- ...service.js => carrier_metadata_service.ts} | 5 ++- ...ype_service.js => carrier_type_service.ts} | 2 +- 4 files changed, 42 insertions(+), 19 deletions(-) rename src/services/{billing_service.js => billing_service.ts} (79%) rename src/services/{carrier_account_service.js => carrier_account_service.ts} (89%) rename src/services/{carrier_metadata_service.js => carrier_metadata_service.ts} (87%) rename src/services/{carrier_type_service.js => carrier_type_service.ts} (93%) diff --git a/src/services/billing_service.js b/src/services/billing_service.ts similarity index 79% rename from src/services/billing_service.js rename to src/services/billing_service.ts index a4732fefe..0b480ddb0 100644 --- a/src/services/billing_service.js +++ b/src/services/billing_service.ts @@ -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 & { + 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. @@ -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 { const paymentInfo = await this._getPaymentInfo(priority.toLowerCase()); const endpoint = paymentInfo[0]; const paymentMethodID = paymentInfo[1]; @@ -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 { const paymentInfo = await this._getPaymentInfo(priority.toLowerCase()); const endpoint = paymentInfo[0]; const paymentMethodID = paymentInfo[1]; @@ -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 { const url = 'payment_methods'; const res = await easypostClient._get(url); @@ -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') { diff --git a/src/services/carrier_account_service.js b/src/services/carrier_account_service.ts similarity index 89% rename from src/services/carrier_account_service.js rename to src/services/carrier_account_service.ts index c1ae93bd7..a83c6ae11 100644 --- a/src/services/carrier_account_service.js +++ b/src/services/carrier_account_service.ts @@ -4,6 +4,8 @@ import Constants from '../constants'; import InvalidParameterError from '../errors/general/invalid_parameter_error'; import baseService from './base_service'; +type CarrierAccountParams = Record & { type?: string }; + export default (easypostClient) => /** * The CarrierAccountService class provides methods for interacting with EasyPost @{link CarrierAccount} objects. @@ -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 { const carrierAccountType = params.type; if (!carrierAccountType) { @@ -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): Promise { const wrappedParams = { carrier_account: params }; try { @@ -56,7 +58,7 @@ export default (easypostClient) => * @param {string} id - The id of the carrier account to be deleted. * @returns {Promise|Promise} - A promise that resolves when the carrier account has been deleted. */ - static async delete(id) { + static async delete(id: string): Promise { const url = `carrier_accounts/${id}`; try { @@ -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)) { @@ -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, + ): Record { if (Constants.CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_OAUTH.includes(carrierAccountType)) { return { carrier_account_oauth_registrations: params }; } @@ -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 = {}): Promise { const url = 'carrier_accounts'; return this._all(url, params); @@ -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 { const url = `carrier_accounts/${id}`; return this._retrieve(url); diff --git a/src/services/carrier_metadata_service.js b/src/services/carrier_metadata_service.ts similarity index 87% rename from src/services/carrier_metadata_service.js rename to src/services/carrier_metadata_service.ts index 13a4ba81e..11460f55b 100644 --- a/src/services/carrier_metadata_service.js +++ b/src/services/carrier_metadata_service.ts @@ -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 { const url = 'metadata/carriers'; const params = { ...(carriers && carriers.length > 0 && { carriers: carriers.join(',') }), diff --git a/src/services/carrier_type_service.js b/src/services/carrier_type_service.ts similarity index 93% rename from src/services/carrier_type_service.js rename to src/services/carrier_type_service.ts index bb4b9ded9..669ada7bd 100644 --- a/src/services/carrier_type_service.js +++ b/src/services/carrier_type_service.ts @@ -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 = {}): Promise { const url = 'carrier_types'; try {