UNPKG

@micro.ts/core

Version:

Microservice framework with Typescript

84 lines (83 loc) 3.11 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.HttpClient = void 0; const axios_1 = __importDefault(require("axios")); const __1 = require(".."); class HttpClient { constructor(baseUrl) { this.request = axios_1.default.create({ baseURL: baseUrl }); } get(url, config) { return __awaiter(this, void 0, void 0, function* () { return this.request .get(url, config) .then((r) => r.data) .catch((t) => { throw this.handleAxiosError(t); }); }); } post(url, data, config) { return __awaiter(this, void 0, void 0, function* () { return this.request .post(url, data, config) .then((r) => r.data) .catch((t) => { throw this.handleAxiosError(t); }); }); } put(url, data, config) { return __awaiter(this, void 0, void 0, function* () { return this.request .put(url, data, config) .then((r) => r.data) .catch((t) => { throw this.handleAxiosError(t); }); }); } patch(url, data, config) { return __awaiter(this, void 0, void 0, function* () { return this.request .patch(url, data, config) .then((r) => r.data) .catch((t) => { throw this.handleAxiosError(t); }); }); } delete(url, config) { return __awaiter(this, void 0, void 0, function* () { return this.request .delete(url, config) .then((r) => r.data) .catch((t) => { throw this.handleAxiosError(t); }); }); } handleAxiosError(error) { if (error.response && error.response.data) { const err = new __1.MainAppError(); err.message = error.response.data.message || error.message; err.statusCode = error.response.status; err.data = error.response.data; return err; } return new __1.ServerError(error.message); } } exports.HttpClient = HttpClient;