UNPKG

react-antd-admin-panel

Version:

Easy prototyping admin panel using React and Antd

117 lines 4.43 kB
import Axios from "./Axios"; import axios from 'axios'; export default class Get extends Axios { constructor() { var _a; super(); this._cache = false; this._env = JSON.parse((_a = window.localStorage.getItem('env')) !== null && _a !== void 0 ? _a : '{}'); } key(v) { this._key = v; return this; } main(v) { this._main = v; return this; } cache(v) { this._cache = v; return this; } parent(v) { this._parent = v; if (this._parent._main) this._main = this._parent._main; return this; } mock(v) { this._mock = v; return this; } data() { if (this._mock) return this._mock; return this._data; } copy() { let get = new Get(); get.main(this._main); get.target(this._target); return get; } finalize(response, args, callback) { var _a, _b, _c, _d; if (this._mock && this._data) { console.log("The request succeeded and you're mocking your data, so you can only get your mocked data trough data(). Remove your mock() data to disable this functionality."); } this._data = this._alter ? this._alter(response.data, args) : response.data; this._success = true; this._onThen(response, this._data, args); this._onComplete(response, this._data, args); if (this._parent) { (_b = (_a = this._parent)._onThen) === null || _b === void 0 ? void 0 : _b.call(_a, response, this._data, args); (_d = (_c = this._parent)._onComplete) === null || _d === void 0 ? void 0 : _d.call(_c, response, this._data, args); } callback === null || callback === void 0 ? void 0 : callback(response); } get(args = undefined, callback = undefined) { if (this._doIf && !this._doIf()) return; let target = this.targetBuild(args); let header = typeof this._header === 'function' ? this._header(args) : this._header; let params = undefined; let auth = undefined; let method = 'get'; if (target.method) method = target.method.toLowerCase(); if (target.params) params = target.params; if (target.target) target = target.target; if (target.auth) auth = target.auth; let settings = {}; if (!!auth) settings['auth'] = auth; if (!!params) settings['params'] = params; if (!!header) settings['headers'] = header; if (this._env.cacheGet) { let store = window.localStorage.getItem(target); if (store) { this.finalize(JSON.parse(store), args, callback); return; } } axios[method](target, settings).then((response) => { try { if (this._env.cacheGet) window.localStorage.setItem(target, JSON.stringify(response)); } catch (e) { } this.finalize(response, args, callback); }) .catch((e) => { var _a, _b, _c, _d, _e; if (!this._mock) { if (((_a = e.response) === null || _a === void 0 ? void 0 : _a.status) === 403 && this._main) (_c = (_b = this._main.$config.config.access) === null || _b === void 0 ? void 0 : _b.accessViolationApi) === null || _c === void 0 ? void 0 : _c.call(_b, this._main); if (this._fail) { console.log('Get: failed - using default.', this._fail); this.finalize({ data: this._fail }); } else { this._error = true; (_d = this._onCatch) === null || _d === void 0 ? void 0 : _d.call(this, e, args); (_e = this._onError) === null || _e === void 0 ? void 0 : _e.call(this, e, args); } } else { console.log("The request failed and you're mocking your data, so axios.catch() is ignored. Remove your mock() data to disable this functionality."); } }); } } //# sourceMappingURL=Get.js.map