UNPKG

minh-custom-hooks-release

Version:

My custom hooks for working easier while developing react web app

104 lines 4.53 kB
/* eslint-disable @typescript-eslint/no-explicit-any */ import axios from 'axios'; import { defer, map } from 'rxjs'; import { fromPromise } from 'rxjs/internal/observable/innerFrom'; var AxiosObservable = /** @class */ (function () { function AxiosObservable(config) { this.requestInterceptors = new Map(); this.responseInterceptors = new Map(); this.axiosInstance = axios.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 defer(function () { return fromPromise(_this.axiosInstance.get(url, config)).pipe(map(function (r) { return r.data; })); }); }; AxiosObservable.prototype.post = function (url, data, config) { var _this = this; return defer(function () { return fromPromise(_this.axiosInstance.post(url, data, config)).pipe(map(function (r) { return r.data; })); }); }; AxiosObservable.prototype.postForm = function (url, data, config) { var _this = this; return defer(function () { return fromPromise(_this.axiosInstance.postForm(url, data, config)).pipe(map(function (r) { return r.data; })); }); }; AxiosObservable.prototype.delete = function (url, config) { var _this = this; return defer(function () { return fromPromise(_this.axiosInstance.delete(url, config)).pipe(map(function (r) { return r.data; })); }); }; AxiosObservable.prototype.head = function (url, config) { var _this = this; return defer(function () { return fromPromise(_this.axiosInstance.head(url, config)).pipe(map(function (r) { return r.data; })); }); }; AxiosObservable.prototype.options = function (url, config) { var _this = this; return defer(function () { return fromPromise(_this.axiosInstance.options(url, config)).pipe(map(function (r) { return r.data; })); }); }; AxiosObservable.prototype.put = function (url, data, config) { var _this = this; return defer(function () { return fromPromise(_this.axiosInstance.put(url, data, config)).pipe(map(function (r) { return r.data; })); }); }; AxiosObservable.prototype.patch = function (url, data, config) { var _this = this; return defer(function () { return fromPromise(_this.axiosInstance.patch(url, data, config)).pipe(map(function (r) { return r.data; })); }); }; AxiosObservable.prototype.putForm = function (url, data, config) { var _this = this; return defer(function () { return fromPromise(_this.axiosInstance.putForm(url, data, config)).pipe(map(function (r) { return r.data; })); }); }; return AxiosObservable; }()); export default AxiosObservable; //# sourceMappingURL=AxiosObservable.js.map