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 @@ -4,6 +4,12 @@ import Constants from '../constants';
import FilteringError from '../errors/general/filtering_error';
import baseService from './base_service';

type ApiKeyUser = Record<string, unknown> & {
id?: string;
keys?: unknown[];
children?: Array<Record<string, unknown> & { id?: string; keys?: unknown[] }>;
};

export default (easypostClient) =>
/**
* The ApiKeyService class provides methods for interacting with EasyPost {@link ApiKey} objects.
Expand All @@ -17,20 +23,20 @@ export default (easypostClient) =>
* @returns {Array} - List of associated API Keys.
* @throws {FilteringError} If user or API Keys are not found.
*/
static async retrieveApiKeysForUser(id) {
static async retrieveApiKeysForUser(id: string): Promise<unknown[]> {
const url = `api_keys`;

try {
const response = await easypostClient._get(url);
const user = this._convertToEasyPostObject(response.body);
const user = this._convertToEasyPostObject(response.body) as ApiKeyUser;

if (user.id == id) {
return user.keys;
return user.keys ?? [];
}

user.children.forEach((child) => {
user.children?.forEach((child) => {
if (child.id == id) {
return child.keys;
return child.keys ?? [];
}
});
} catch (e) {
Expand All @@ -45,7 +51,7 @@ export default (easypostClient) =>
* See {@link https://docs.easypost.com/docs/api-keys#retrieve-an-api-key EasyPost API Documentation} for more information.
* @returns {Object} - An object containing the API keys associated with the current authenticated user and its child users.
*/
static async all(params = {}) {
static async all(params: Record<string, unknown> = {}): Promise<unknown> {
const url = 'api_keys';

return this._all(url, params);
Expand All @@ -57,7 +63,7 @@ export default (easypostClient) =>
* @param {string} mode - The mode for the API key (either "production" or "test").
* @returns {ApiKey} - The created API key.
*/
static async create(mode) {
static async create(mode: string): Promise<unknown> {
const url = 'api_keys';
const params = { mode };

Expand All @@ -70,7 +76,7 @@ export default (easypostClient) =>
* @param {string} id - The ID of the API key to delete.
* @returns {Promise|Promise<never>} - A promise that resolves if the API key was successfully deleted.
*/
static async delete(id) {
static async delete(id: string): Promise<void> {
const url = `api_keys/${id}`;

try {
Expand All @@ -88,7 +94,7 @@ export default (easypostClient) =>
* @param {string} id - The ID of the API key to enable.
* @returns {ApiKey} - The enabled API key.
*/
static async enable(id) {
static async enable(id: string): Promise<unknown> {
const url = `api_keys/${id}/enable`;

try {
Expand All @@ -106,7 +112,7 @@ export default (easypostClient) =>
* @param {string} id - The ID of the API key to disable.
* @returns {ApiKey} - The disabled API key.
*/
static async disable(id) {
static async disable(id: string): Promise<unknown> {
const url = `api_keys/${id}/disable`;

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default (easypostClient) =>
* @param {Object} [params] - The parameters to create a session from.
* @returns {Object} - An object containing the created session.
*/
static async createAccountLink(params = {}) {
static async createAccountLink(params: Record<string, unknown> = {}): Promise<unknown> {
const url = 'customer_portal/account_link';

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default (easypostClient) =>
* @param {Object} [params] - The parameters to create a session from.
* @returns {Object} - An object containing the created session.
*/
static async createSession(params = {}) {
static async createSession(params: Record<string, unknown> = {}): Promise<unknown> {
const url = 'embeddables/session';

try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import baseService from './base_service';

type EndShipperParams = Record<string, unknown>;

export default (easypostClient) =>
/**
* The EndShipperService class provides methods for interacting with EasyPost {@link EndShipper} objects.
Expand All @@ -12,7 +14,7 @@ export default (easypostClient) =>
* @param {Object} params - Parameters for the end shipper to be created.
* @returns {EndShipper} - The created end shipper.
*/
static async create(params) {
static async create(params: EndShipperParams): Promise<unknown> {
const url = 'end_shippers';
const wrappedParams = { address: params };

Expand All @@ -26,7 +28,7 @@ export default (easypostClient) =>
* @param {Object} params - Parameters for the end shipper to be updated.
* @returns {EndShipper} - The updated end shipper.
*/
static async update(id, params) {
static async update(id: string, params: EndShipperParams): Promise<unknown> {
const url = `end_shippers/${id}`;
const wrappedParams = { address: params };

Expand All @@ -45,7 +47,7 @@ export default (easypostClient) =>
* @param {string} id - The ID of the end shipper to retrieve.
* @returns {EndShipper} - The retrieved end shipper.
*/
static async retrieve(id) {
static async retrieve(id: string): Promise<unknown> {
const url = `end_shippers/${id}`;

return this._retrieve(url);
Expand All @@ -57,7 +59,7 @@ export default (easypostClient) =>
* @param {Object} [params] - Parameters to filter the list of end shippers.
* @returns {Object} - An object containing a list of {@link EndShipper end shippers} and pagination information.
*/
static async all(params = {}) {
static async all(params: Record<string, unknown> = {}): Promise<unknown> {
const url = 'end_shippers';

return this._all(url, params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import { v4 as uuid } from 'uuid';

import baseService from './base_service';

type FedExValidationMap = Record<string, unknown> & { name?: string | null };
type FedExParams = Record<string, unknown> & {
address_validation?: FedExValidationMap;
pin_validation?: FedExValidationMap;
invoice_validation?: FedExValidationMap;
easypost_details?: Record<string, unknown>;
};

export default (easypostClient) =>
/**
* The FedExRegistrationService class provides methods for registering FedEx carrier accounts with MFA.
Expand All @@ -14,7 +22,10 @@ export default (easypostClient) =>
* @param {Object} params - Map of parameters.
* @returns {Object}
*/
static async registerAddress(fedexAccountNumber, params) {
static async registerAddress(
fedexAccountNumber: string,
params: FedExParams,
): Promise<unknown> {
const wrappedParams = this._wrapAddressValidation(params);
const endpoint = `fedex_registrations/${fedexAccountNumber}/address`;

Expand All @@ -33,7 +44,11 @@ export default (easypostClient) =>
* @param {Object} params - Map of parameters.
* @returns {Object}
*/
static async requestPin(fedexAccountNumber, pinMethodOption, params) {
static async requestPin(
fedexAccountNumber: string,
pinMethodOption: string,
params: FedExParams,
): Promise<unknown> {
const wrappedParams = this._wrapPinValidation(params);
wrappedParams.pin_method = {
option: pinMethodOption,
Expand All @@ -54,7 +69,7 @@ export default (easypostClient) =>
* @param {Object} params - Map of parameters.
* @returns {Object}
*/
static async validatePin(fedexAccountNumber, params) {
static async validatePin(fedexAccountNumber: string, params: FedExParams): Promise<unknown> {
const wrappedParams = this._wrapPinValidation(params);
const endpoint = `fedex_registrations/${fedexAccountNumber}/pin/validate`;

Expand All @@ -72,7 +87,7 @@ export default (easypostClient) =>
* @param {Object} params - Map of parameters.
* @returns {Object}
*/
static async submitInvoice(fedexAccountNumber, params) {
static async submitInvoice(fedexAccountNumber: string, params: FedExParams): Promise<unknown> {
const wrappedParams = this._wrapInvoiceValidation(params);
const endpoint = `fedex_registrations/${fedexAccountNumber}/invoice`;

Expand All @@ -91,8 +106,8 @@ export default (easypostClient) =>
* @param {Object} params - The original parameters map.
* @returns {Object} - A new map with properly wrapped address_validation and easypost_details.
*/
static _wrapAddressValidation(params) {
const wrappedParams = {};
static _wrapAddressValidation(params: FedExParams): Record<string, unknown> {
const wrappedParams: Record<string, unknown> = {};

if (params.address_validation) {
const addressValidation = { ...params.address_validation };
Expand All @@ -114,8 +129,8 @@ export default (easypostClient) =>
* @param {Object} params - The original parameters map.
* @returns {Object} - A new map with properly wrapped pin_validation and easypost_details.
*/
static _wrapPinValidation(params) {
const wrappedParams = {};
static _wrapPinValidation(params: FedExParams): Record<string, unknown> {
const wrappedParams: Record<string, unknown> = {};

if (params.pin_validation) {
const pinValidation = { ...params.pin_validation };
Expand All @@ -137,8 +152,8 @@ export default (easypostClient) =>
* @param {Object} params - The original parameters map.
* @returns {Object} - A new map with properly wrapped invoice_validation and easypost_details.
*/
static _wrapInvoiceValidation(params) {
const wrappedParams = {};
static _wrapInvoiceValidation(params: FedExParams): Record<string, unknown> {
const wrappedParams: Record<string, unknown> = {};

if (params.invoice_validation) {
const invoiceValidation = { ...params.invoice_validation };
Expand All @@ -160,7 +175,7 @@ export default (easypostClient) =>
* @private
* @param {Object} map - The map to ensure the "name" field in.
*/
static _ensureNameField(map) {
static _ensureNameField(map: FedExValidationMap): void {
if (!map.name || map.name === null) {
const uuidValue = uuid().replace(/-/g, '');
map.name = uuidValue;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import baseService from './base_service';

type LumaParams = Record<string, unknown>;

export default (easypostClient) =>
/**
* The LumaService class provides methods for interacting with EasyPost Luma objects.
Expand All @@ -11,7 +13,7 @@ export default (easypostClient) =>
* @param {Object} params - The parameters to get a Luma promise with.
* @returns {Object} - An object containing the Luma promise.
*/
static async getPromise(params) {
static async getPromise(params: LumaParams): Promise<unknown> {
const url = `luma/promise`;

const wrappedParams = {
Expand Down
Loading