@biorate/axios
Version:
Axios OOP static interface
128 lines • 5.3 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
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 __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var _Axios_client;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Axios = void 0;
const lodash_1 = require("lodash");
const axios_1 = require("axios");
const axios_retry_1 = require("axios-retry");
const pathToUrl = require("path-to-url");
const singleton_1 = require("@biorate/singleton");
const stubs_1 = require("./stubs");
__exportStar(require("axios"), exports);
__exportStar(require("./interfaces"), exports);
const axiosRetryConfigKeys = [
'retries',
'retryDelay',
'shouldResetTimeout',
'retryCondition',
'onRetry',
];
const axiosExcludeKeys = ['path', 'config', 'retry'];
class Axios extends singleton_1.Singleton {
constructor() {
super(...arguments);
_Axios_client.set(this, void 0);
}
static get stubs() {
return stubs_1.Stubs.get(this);
}
static get options() {
return stubs_1.Stubs.get(this).options;
}
static fetch(options) {
return this._fetch(options);
}
static stub(params, persist = false) {
this.stubs.stub(this.instance(), params, persist);
}
static unstub() {
this.stubs.unstub();
}
static useMock() {
this.mocks.set(this, { value: true });
}
static get defaults() {
return axios_1.default.defaults;
}
static getMock(instance, options) {
return undefined;
}
static setMock(instance, result, options) { }
static async _fetch(options) {
var _a;
const instance = this.instance();
const useMock = (_a = this.mocks.get(this)) === null || _a === void 0 ? void 0 : _a.value;
if (useMock) {
const mock = this.getMock(instance, options);
if (mock)
return mock;
}
const result = await instance.fetch(options);
if (useMock)
this.setMock(instance, result, options);
return result;
}
async fetch(options) {
const settings = (0, lodash_1.merge)(Object.assign({}, this), options);
if (!__classPrivateFieldGet(this, _Axios_client, "f")) {
__classPrivateFieldSet(this, _Axios_client, axios_1.default.create(), "f");
if (settings.retry)
(0, axios_retry_1.default)(__classPrivateFieldGet(this, _Axios_client, "f"), (0, lodash_1.pick)(this, axiosRetryConfigKeys));
}
if (settings.baseURL && settings.path)
settings.baseURL = pathToUrl(settings.baseURL, settings.path);
if (settings.url && settings.path)
settings.url = pathToUrl(settings.url, settings.path);
const params = Object.assign({}, (0, lodash_1.omit)(settings, axiosRetryConfigKeys.concat(axiosExcludeKeys)));
await this.before(params);
const startTime = this.getStartTime();
try {
const result = await __classPrivateFieldGet(this, _Axios_client, "f").call(this, params);
await this.after(result, startTime, params);
return result;
}
catch (e) {
await this.catch(e, startTime, params);
throw e;
}
finally {
await this.finally(startTime, params);
}
}
getStartTime() {
return [Date.now(), 0];
}
async before(params) { }
async after(result, startTime, params) { }
async catch(e, startTime, params) { }
async finally(startTime, params) { }
}
_Axios_client = new WeakMap();
Axios.mocks = new WeakMap();
exports.Axios = Axios;
//# sourceMappingURL=index.js.map