UNPKG

@stand/ajax-base

Version:
396 lines (326 loc) 12.1 kB
import _regeneratorRuntime from '@babel/runtime-corejs3/regenerator'; import _classCallCheck from '@babel/runtime-corejs3/helpers/classCallCheck'; import _createClass from '@babel/runtime-corejs3/helpers/createClass'; import _possibleConstructorReturn from '@babel/runtime-corejs3/helpers/possibleConstructorReturn'; import _getPrototypeOf from '@babel/runtime-corejs3/helpers/getPrototypeOf'; import _inherits from '@babel/runtime-corejs3/helpers/inherits'; import _defineProperty from '@babel/runtime-corejs3/helpers/defineProperty'; import _Symbol from '@babel/runtime-corejs3/core-js-stable/symbol'; import axios from 'axios'; import StandBase from '@stand/base'; var abortableSymbol = _Symbol('abortable'); var ableSymbol = _Symbol('able'); var Ajax = /*#__PURE__*/ function (_StandBase) { _inherits(Ajax, _StandBase); function Ajax() { var _this; _classCallCheck(this, Ajax); _this = _possibleConstructorReturn(this, _getPrototypeOf(Ajax).call(this)); _this[ableSymbol] = true; _this.addSetAndGetMethods('config', 'getConfig'); _this.addSetAndGetMethods('params', 'getParams'); return _this; } _createClass(Ajax, [{ key: "method", value: function method(_method) { this.config('method', _method); return this; } }, { key: "url", value: function url(_url) { this.config('url', _url); return this; } }, { key: "abortable", value: function abortable() { var CancelToken = axios.CancelToken; var source = CancelToken.source(); this[abortableSymbol] = source; this.config('cancelToken', source.token); return this; } }, { key: "abort", value: function abort(message) { var source = this[abortableSymbol]; if (source) { source.cancel(message); } return this; } }, { key: "disable", value: function disable() { this[ableSymbol] = false; } }, { key: "enable", value: function enable() { this[ableSymbol] = true; } }, { key: "isDisabled", value: function isDisabled() { return this[ableSymbol] === false; } }, { key: "convertResponse", value: function convertResponse(res) { return _regeneratorRuntime.async(function convertResponse$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: return _context.abrupt("return", res); case 1: case "end": return _context.stop(); } } }); } }, { key: "convertData", value: function convertData(data) { return _regeneratorRuntime.async(function convertData$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: return _context2.abrupt("return", data); case 1: case "end": return _context2.stop(); } } }); } }, { key: "convertError", value: function convertError(error) { return _regeneratorRuntime.async(function convertError$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: return _context3.abrupt("return", error); case 1: case "end": return _context3.stop(); } } }); } }, { key: "fetch", value: function fetch() { var _this2 = this; var config, method; return _regeneratorRuntime.async(function fetch$(_context6) { while (1) { switch (_context6.prev = _context6.next) { case 0: if (this[ableSymbol]) { _context6.next = 2; break; } return _context6.abrupt("return", this); case 2: _context6.prev = 2; _context6.next = 5; return _regeneratorRuntime.awrap(this.emit('request')); case 5: _context6.next = 9; break; case 7: _context6.prev = 7; _context6.t0 = _context6["catch"](2); case 9: try { config = this.getConfig(); method = (config.method || 'get').toUpperCase(); if (method === 'PUT' || method === 'POST' || method === 'PATCH') { this.config({ data: this.getParams() }); } else { this.config({ params: this.getParams() }); } } catch (err) {} _context6.next = 12; return _regeneratorRuntime.awrap(axios.request(this.getConfig()).then(function _callee(raw) { var res; return _regeneratorRuntime.async(function _callee$(_context4) { while (1) { switch (_context4.prev = _context4.next) { case 0: res = raw; _context4.prev = 1; _context4.next = 4; return _regeneratorRuntime.awrap(_this2.convertResponse(res)); case 4: res = _context4.sent; _context4.next = 9; break; case 7: _context4.prev = 7; _context4.t0 = _context4["catch"](1); case 9: _context4.prev = 9; _context4.next = 12; return _regeneratorRuntime.awrap(_this2.convertData(res.data, res)); case 12: res.data = _context4.sent; _context4.next = 17; break; case 15: _context4.prev = 15; _context4.t1 = _context4["catch"](9); case 17: _context4.prev = 17; _context4.next = 20; return _regeneratorRuntime.awrap(_this2.emit('netComplete', true, res)); case 20: _context4.next = 24; break; case 22: _context4.prev = 22; _context4.t2 = _context4["catch"](17); case 24: _context4.prev = 24; _context4.next = 27; return _regeneratorRuntime.awrap(_this2.emit('netSuccess', res.data, res)); case 27: _context4.next = 31; break; case 29: _context4.prev = 29; _context4.t3 = _context4["catch"](24); case 31: _context4.prev = 31; _context4.next = 34; return _regeneratorRuntime.awrap(_this2.emit('netCompleted', true, res)); case 34: _context4.next = 38; break; case 36: _context4.prev = 36; _context4.t4 = _context4["catch"](31); case 38: case "end": return _context4.stop(); } } }, null, null, [[1, 7], [9, 15], [17, 22], [24, 29], [31, 36]]); })["catch"](function _callee2(rawError) { var error; return _regeneratorRuntime.async(function _callee2$(_context5) { while (1) { switch (_context5.prev = _context5.next) { case 0: error = rawError; _context5.prev = 1; _context5.next = 4; return _regeneratorRuntime.awrap(_this2.convertError(error)); case 4: error = _context5.sent; _context5.next = 9; break; case 7: _context5.prev = 7; _context5.t0 = _context5["catch"](1); case 9: _context5.prev = 9; _context5.next = 12; return _regeneratorRuntime.awrap(_this2.emit('netComplete', false, error)); case 12: _context5.next = 16; break; case 14: _context5.prev = 14; _context5.t1 = _context5["catch"](9); case 16: _context5.prev = 16; if (!error.response) { _context5.next = 22; break; } _context5.next = 20; return _regeneratorRuntime.awrap(_this2.emit('netResponseError', error.response, error)); case 20: _context5.next = 29; break; case 22: if (!error.request) { _context5.next = 27; break; } _context5.next = 25; return _regeneratorRuntime.awrap(_this2.emit('netRequestError', error.request, error)); case 25: _context5.next = 29; break; case 27: _context5.next = 29; return _regeneratorRuntime.awrap(_this2.emit('netUnknownError', error)); case 29: _context5.next = 33; break; case 31: _context5.prev = 31; _context5.t2 = _context5["catch"](16); case 33: _context5.prev = 33; _context5.next = 36; return _regeneratorRuntime.awrap(_this2.emit('netError', error)); case 36: _context5.next = 40; break; case 38: _context5.prev = 38; _context5.t3 = _context5["catch"](33); case 40: _context5.prev = 40; _context5.next = 43; return _regeneratorRuntime.awrap(_this2.emit('netCompleted', false, error)); case 43: _context5.next = 47; break; case 45: _context5.prev = 45; _context5.t4 = _context5["catch"](40); case 47: case "end": return _context5.stop(); } } }, null, null, [[1, 7], [9, 14], [16, 31], [33, 38], [40, 45]]); })); case 12: return _context6.abrupt("return", this); case 13: case "end": return _context6.stop(); } } }, null, this, [[2, 7]]); } }]); return Ajax; }(StandBase); _defineProperty(Ajax, "create", function (url, method) { var ajax = new this(); if (typeof url !== 'undefined') { ajax.url(url); } if (typeof method !== 'undefined') { ajax.method(method); } return ajax; }); Ajax.axios = axios; export default Ajax;