UNPKG

minh-custom-hooks-release

Version:

My custom hooks for working easier while developing react web app

109 lines 5.14 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /* eslint-disable @typescript-eslint/no-explicit-any */ var axios_1 = __importDefault(require("axios")); var rxjs_1 = require("rxjs"); var innerFrom_1 = require("rxjs/internal/observable/innerFrom"); var AxiosObservable = /** @class */ (function () { function AxiosObservable(config) { this.requestInterceptors = new Map(); this.responseInterceptors = new Map(); this.axiosInstance = axios_1.default.create(config); } AxiosObservable.prototype.getRequestInterceptors = function () { return this.requestInterceptors.entries(); }; AxiosObservable.prototype.addRequestInterceptor = function (key) { var _a; var handler = []; for (var _i = 1; _i < arguments.length; _i++) { handler[_i - 1] = arguments[_i]; } this.removeRequestInterceptor(key); this.requestInterceptors.set(key, (_a = this.axiosInstance.interceptors.request).use.apply(_a, handler)); }; AxiosObservable.prototype.removeRequestInterceptor = function (key) { var findSameInterceptorKey = this.requestInterceptors.get(key); if (findSameInterceptorKey) { this.axiosInstance.interceptors.request.eject(findSameInterceptorKey); } }; AxiosObservable.prototype.getResponseInterceptors = function () { return this.responseInterceptors.entries(); }; AxiosObservable.prototype.addResponseInterceptor = function (key) { var _a; var handler = []; for (var _i = 1; _i < arguments.length; _i++) { handler[_i - 1] = arguments[_i]; } this.removeResponseInterceptor(key); this.responseInterceptors.set(key, (_a = this.axiosInstance.interceptors.response).use.apply(_a, handler)); }; AxiosObservable.prototype.removeResponseInterceptor = function (key) { var findSameInterceptorKey = this.responseInterceptors.get(key); if (findSameInterceptorKey) { this.axiosInstance.interceptors.response.eject(findSameInterceptorKey); } }; AxiosObservable.prototype.get = function (url, config) { var _this = this; return (0, rxjs_1.defer)(function () { return (0, innerFrom_1.fromPromise)(_this.axiosInstance.get(url, config)).pipe((0, rxjs_1.map)(function (r) { return r.data; })); }); }; AxiosObservable.prototype.post = function (url, data, config) { var _this = this; return (0, rxjs_1.defer)(function () { return (0, innerFrom_1.fromPromise)(_this.axiosInstance.post(url, data, config)).pipe((0, rxjs_1.map)(function (r) { return r.data; })); }); }; AxiosObservable.prototype.postForm = function (url, data, config) { var _this = this; return (0, rxjs_1.defer)(function () { return (0, innerFrom_1.fromPromise)(_this.axiosInstance.postForm(url, data, config)).pipe((0, rxjs_1.map)(function (r) { return r.data; })); }); }; AxiosObservable.prototype.delete = function (url, config) { var _this = this; return (0, rxjs_1.defer)(function () { return (0, innerFrom_1.fromPromise)(_this.axiosInstance.delete(url, config)).pipe((0, rxjs_1.map)(function (r) { return r.data; })); }); }; AxiosObservable.prototype.head = function (url, config) { var _this = this; return (0, rxjs_1.defer)(function () { return (0, innerFrom_1.fromPromise)(_this.axiosInstance.head(url, config)).pipe((0, rxjs_1.map)(function (r) { return r.data; })); }); }; AxiosObservable.prototype.options = function (url, config) { var _this = this; return (0, rxjs_1.defer)(function () { return (0, innerFrom_1.fromPromise)(_this.axiosInstance.options(url, config)).pipe((0, rxjs_1.map)(function (r) { return r.data; })); }); }; AxiosObservable.prototype.put = function (url, data, config) { var _this = this; return (0, rxjs_1.defer)(function () { return (0, innerFrom_1.fromPromise)(_this.axiosInstance.put(url, data, config)).pipe((0, rxjs_1.map)(function (r) { return r.data; })); }); }; AxiosObservable.prototype.patch = function (url, data, config) { var _this = this; return (0, rxjs_1.defer)(function () { return (0, innerFrom_1.fromPromise)(_this.axiosInstance.patch(url, data, config)).pipe((0, rxjs_1.map)(function (r) { return r.data; })); }); }; AxiosObservable.prototype.putForm = function (url, data, config) { var _this = this; return (0, rxjs_1.defer)(function () { return (0, innerFrom_1.fromPromise)(_this.axiosInstance.putForm(url, data, config)).pipe((0, rxjs_1.map)(function (r) { return r.data; })); }); }; return AxiosObservable; }()); exports.default = AxiosObservable; //# sourceMappingURL=AxiosObservable.js.map