UNPKG

@shipengine/connect

Version:

The official developer tooling for building ShipEngine connect apps

123 lines 5.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ApiClientErrors = void 0; const tslib_1 = require("tslib"); const axios_1 = tslib_1.__importDefault(require("axios")); const axios_retry_1 = tslib_1.__importDefault(require("axios-retry")); const ono_1 = tslib_1.__importDefault(require("@jsdevtools/ono")); const apps_1 = tslib_1.__importDefault(require("./resources/apps")); const deployments_1 = tslib_1.__importDefault(require("./resources/deployments")); const diagnostics_1 = tslib_1.__importDefault(require("./resources/diagnostics")); const sellers_1 = tslib_1.__importDefault(require("./resources/sellers")); const users_1 = tslib_1.__importDefault(require("./resources/users")); const configuration_1 = tslib_1.__importDefault(require("./resources/configuration")); const path_1 = tslib_1.__importDefault(require("path")); const fs_1 = tslib_1.__importDefault(require("fs")); const os_1 = tslib_1.__importDefault(require("os")); const documents_1 = tslib_1.__importDefault(require("./resources/documents")); var ApiClientErrors; (function (ApiClientErrors) { ApiClientErrors["BadRequest"] = "ERR_BAD_REQUEST"; ApiClientErrors["NotFound"] = "ERR_NOT_FOUND"; ApiClientErrors["Unauthorized"] = "ERR_UNAUTHORIZED"; ApiClientErrors["InternalServerError"] = "ERR_INTERNAL_SERVER"; ApiClientErrors["UnhandledError"] = "ERR_UNHANDLED"; })(ApiClientErrors || (exports.ApiClientErrors = ApiClientErrors = {})); const pjson = JSON.parse(fs_1.default.readFileSync(path_1.default.join(__dirname, '../../../package.json'), 'utf8')); /** * Create an instance of the APIClient. * @param {string} apiKey A valid API key. */ class APIClient { apps; configuration; deployments; diagnostics; sellers; users; documents; apiKey; debug; _apiAuthority = process.env.API_AUTHORITY ?? 'https://connect-api.shipengine.com/api'; constructor(apiKey, debug = false) { this.apiKey = apiKey; (0, axios_retry_1.default)(axios_1.default, { retries: 3, retryDelay: (retryCount) => { console.log(`retry attempt: ${retryCount}`); return retryCount * 2000; // time interval between retries }, retryCondition: (error) => { // if retry condition is not specified, by default idempotent requests are retried return (error.response?.status ?? 0) >= 500; }, }); this.apps = new apps_1.default(this); this.configuration = new configuration_1.default(this); this.deployments = new deployments_1.default(this); this.diagnostics = new diagnostics_1.default(this); this.sellers = new sellers_1.default(this); this.users = new users_1.default(this); this.documents = new documents_1.default(this); this.debug = debug; } async call({ endpoint, method, body = {}, headers = {}, isFileUpload = false, }) { const defaultHeaders = { 'content-type': 'application/json', 'api-key': this.apiKey, 'User-Agent': `${pjson.name}/${pjson.version} ${os_1.default.platform}-${os_1.default.arch} node-${process.version}`, }; if (this.debug) { axios_1.default.interceptors.request.use((request) => { console.log('Starting Request', request); return request; }); axios_1.default.interceptors.response.use((response) => { console.log('Response:', response); return response; }); } const mergedHeaders = { ...defaultHeaders, ...headers, }; const request = { headers: mergedHeaders, method: method, url: `${this._apiAuthority}/${endpoint}`, ...(isFileUpload && { maxContentLength: Infinity, maxBodyLength: Infinity, }), }; if (body) request.data = body; try { const response = await (0, axios_1.default)(request); return response.data; } catch (error) { const err = error; if (this.debug) { console.log('Response:', err.response); } switch (err.response?.status) { case 400: throw (0, ono_1.default)({ code: ApiClientErrors.BadRequest }, err.response.data.message || 'The request was invalid'); case 401: throw (0, ono_1.default)({ code: ApiClientErrors.Unauthorized }, 'The given API key is not valid'); case 404: throw (0, ono_1.default)({ code: ApiClientErrors.NotFound }, 'The record could not be found'); case 500: throw (0, ono_1.default)({ code: ApiClientErrors.InternalServerError }, 'The Connect API is experiencing issues'); default: if (err.code === 'ECONNREFUSED') { throw (0, ono_1.default)({ code: ApiClientErrors.InternalServerError }, 'The Connect API is experiencing issues'); } throw error; } } } } exports.default = APIClient; //# sourceMappingURL=index.js.map