UNPKG

@atomic-utils/clients

Version:
103 lines 4.24 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseClient = void 0; const axios_1 = __importDefault(require("axios")); const API_PREFIX = 'api/v1'; const DEFAULT_TIMEOUT = 20000; /** * Oracle client */ class BaseClient { constructor(opts) { this.handleError = (error) => { if (error.code === 'ECONNREFUSED') { this.opts.logger.debug('Make sure the Service is running and that you are connecting to the correct port'); throw Error(`Could not connect to Service ${this.opts.uri}`); } else if (error.code === 'EPIPE') { throw Error(`EPIPE error ${this.opts.uri}`); } else if (error.code === 'ERR_NETWORK') { if (error.config) { const { method, baseURL, url, params } = error.config; throw Error(`Network Error ${method} ${baseURL}${url} with params ${JSON.stringify(params)}`); } else { throw new Error(`Network Error ${this.opts.uri} ${JSON.stringify(error)}`); } } else if (error.response) { if (error.response.data) { if (typeof error.response.data === 'string') { throw new Error(error.response.data); } else if (typeof error.response.data.error === 'string') { throw new Error(error.response.data.error); } else { throw new Error(JSON.stringify(error.response.data)); } } else { throw new Error(JSON.stringify(error.response)); } } else { throw new Error(error.message); } }; this.opts = opts; } request(method, endpoint, params = {}, data = {}, headers = {}) { return __awaiter(this, void 0, void 0, function* () { const config = { baseURL: `${this.opts.uri}/${this.opts.prefix || API_PREFIX}/`, url: endpoint, timeout: this.opts.timeout || DEFAULT_TIMEOUT, method, params, data: method === 'GET' ? undefined : data, responseType: 'json', }; if (headers) { config.headers = headers; } if (this.opts.apiKey) { const auth = { username: 'admin', password: this.opts.apiKey, }; config.auth = auth; } return this.axiosCall(config) .then((response) => response.data) .catch(this.handleError); }); } axiosCall(config) { return (0, axios_1.default)(config); } get(endpoint, params = {}, headers = {}) { return this.request('GET', endpoint, params, {}, headers); } post(endpoint, params = {}, headers = {}) { return this.request('POST', endpoint, {}, params, headers); } put(endpoint, params = {}, headers = {}) { return this.request('PUT', endpoint, {}, params, headers); } } exports.BaseClient = BaseClient; //# sourceMappingURL=baseClient.js.map