UNPKG

@biorate/axios

Version:

Axios OOP static interface

55 lines 2.88 kB
"use strict"; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _a, _Stubs_cache, _Stubs_halt; Object.defineProperty(exports, "__esModule", { value: true }); exports.Stubs = void 0; const axios_1 = require("axios"); const http_status_1 = require("http-status"); const options_1 = require("./options"); const fake_response_1 = require("./fake-response"); class Stubs { static get(Class) { if (!__classPrivateFieldGet(this, _a, "f", _Stubs_cache).has(Class)) __classPrivateFieldGet(this, _a, "f", _Stubs_cache).set(Class, new this(Class)); return __classPrivateFieldGet(this, _a, "f", _Stubs_cache).get(Class); } constructor(Class) { this.Class = Class; _Stubs_halt.set(this, (statusText, status, params, response) => { throw new axios_1.AxiosError(statusText, String(status), params.config, params.headers, response); }); this.options = new options_1.Options(); this.fetch = new WeakMap(); } stub(instance, params, persist = false) { var _b, _c, _d, _e; const status = (_b = params === null || params === void 0 ? void 0 : params.status) !== null && _b !== void 0 ? _b : 200; const statusText = String((_c = http_status_1.status[status]) !== null && _c !== void 0 ? _c : http_status_1.status[500]); const response = new fake_response_1.FakeResponse(params.data, status, statusText, (_d = params.headers) !== null && _d !== void 0 ? _d : {}, (_e = params.config) !== null && _e !== void 0 ? _e : {}); this.fetch.set(this.Class, this.Class.fetch); this.Class.fetch = async (options) => { var _b; this.options.push(options); if (!persist) this.unstub(); if ((_b = instance === null || instance === void 0 ? void 0 : instance.validateStatus) === null || _b === void 0 ? void 0 : _b.call(instance, status)) return response; if (['4', '5'].includes(String(status)[0])) __classPrivateFieldGet(this, _Stubs_halt, "f").call(this, statusText, status, params, response); return response; }; } unstub() { if (!this.fetch.has(this.Class)) return; this.Class.fetch = this.fetch.get(this.Class); } } _a = Stubs, _Stubs_halt = new WeakMap(); _Stubs_cache = { value: new WeakMap() }; exports.Stubs = Stubs; //# sourceMappingURL=stubs.js.map