UNPKG

easy-pix

Version:

Pix payments made easy for developers build arround payment gateways

85 lines (84 loc) 2.8 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.AxiosHttpClient = void 0; const axios_1 = __importDefault(require("axios")); const http_status_codes_1 = require("http-status-codes"); const errors_1 = require("./errors"); class AxiosHttpClient { constructor(config = { timeout: 2 * 60 * 1000, // 2 minutes headers: { accept: 'application/json', 'content-type': 'application/json' } }) { Object.defineProperty(this, "fetcher", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.fetcher = axios_1.default.create(config); } handleResponseError(error) { const { isAxiosError = false, response } = error; if (isAxiosError) { throw new errors_1.HttpClientError(`error calling ${response?.config.url}`, response?.data || {}, (0, http_status_codes_1.getReasonPhrase)(response?.status || '') || 'REQUEST_TIMEOUT'); } } async put(url, data, config) { try { const result = await this.fetcher.put(url, data, config); return { statusCode: result.status, body: result.data }; } catch (error) { this.handleResponseError(error); throw error; } } async patch(url, data, config) { try { const result = await this.fetcher.patch(url, data, config); return { statusCode: result.status, body: result.data }; } catch (error) { this.handleResponseError(error); throw error; } } async delete(url, config) { try { const result = await this.fetcher.delete(url, config); return { statusCode: result.status, body: result.data }; } catch (error) { this.handleResponseError(error); throw error; } } async get(url, config) { try { const result = await this.fetcher.get(url, config); return { statusCode: result.status, body: result.data }; } catch (error) { this.handleResponseError(error); throw error; } } async post(url, data, config) { try { const result = await this.fetcher.post(url, data, config); return { statusCode: result.status, body: result.data }; } catch (error) { this.handleResponseError(error); throw error; } } } exports.AxiosHttpClient = AxiosHttpClient; exports.default = new AxiosHttpClient();