UNPKG

@soos-io/api-client

Version:

This is the SOOS API Client for registered clients leveraging the various integrations to the SOOS platform. Register for a free trial today at https://app.soos.io/register

71 lines (70 loc) 3.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const axios_1 = tslib_1.__importDefault(require("axios")); const SOOSLogger_1 = require("../logging/SOOSLogger"); class SOOSApiClient { static createHttpClient({ baseUri, apiKey, apiClientName, skipDebugResponseLogging, skipDebugRequestLogging, }) { const client = axios_1.default.create({ baseURL: baseUri, headers: { "x-soos-apikey": apiKey, "Content-Type": "application/json", }, maxBodyLength: 1024 * 5000 * 50, maxContentLength: 1024 * 5000 * 50, }); client.interceptors.request.use((request) => { if (request.data) { SOOSLogger_1.soosLogger.debug(apiClientName, `Request URL: ${request.method?.toLocaleUpperCase()} ${request.url}`); if (request.params) { SOOSLogger_1.soosLogger.debug(apiClientName, `Request Params: ${JSON.stringify(request.params)}`); } if (!skipDebugRequestLogging) { SOOSLogger_1.soosLogger.debug(apiClientName, `Request Body: ${JSON.stringify(request.data)}`); } } return request; }); client.interceptors.response.use((response) => { if (skipDebugResponseLogging !== true) { SOOSLogger_1.soosLogger.debug(apiClientName, `Response Body: ${JSON.stringify(response.data)}`); } return response; }, (error) => { if (axios_1.default.isAxiosError(error)) { const { config, response } = error; if (config && response) { switch (response.status) { case 401: throw new Error(`Please Verify your API Key and Client ID. (Unauthorized - ${apiClientName} - ${config.method} ${config.url})`); case 403: throw new Error(`Your request may have been blocked. (Forbidden - ${apiClientName} - ${config.method} ${config.url})`); case 429: throw new Error(`You have been rate limited. (TooManyRequests - ${apiClientName} - ${config.method} ${config.url})`); case 502: throw new Error(`Unable to connect to SOOS. Please verify your connection and try again in a few minutes. (BadGateway - ${apiClientName} - ${config.method} ${config.url})`); case 503: throw new Error(`We are down for maintenance. Please try again in a few minutes. (ServiceUnavailable - ${apiClientName} - ${config.method} ${config.url})`); } if (response.data && response.data.code && response.data.message) { throw new Error(`${response.data.message} (${response.status} ${response.data.code} - ${apiClientName} - ${config.method} ${config.url})`, error); } throw new Error(`Unexpected error response. (${response.status} - ${apiClientName} - ${config.method} ${config.url})`); } } else if (error.code && error.message) { throw new Error(`An unexpected coded error occurred: ${error.code} ${error.message}`); } else if (error.message) { throw new Error(`An unexpected error occurred: ${error.message}`, error); } return Promise.reject(error); }); return client; } static create(params) { return this.createHttpClient(params); } } exports.default = SOOSApiClient;