@wapri/ghermes 0.0.0-stage → 3.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -3
- package/dist/base.d.ts +46 -0
- package/dist/base.js +165 -0
- package/dist/constants/domain.d.ts +44 -0
- package/dist/constants/domain.js +80 -0
- package/dist/constants/index.d.ts +2 -0
- package/dist/constants/index.js +2 -0
- package/dist/constants/resultCode.d.ts +13 -0
- package/dist/constants/resultCode.js +14 -0
- package/dist/demo/custom-interceptor.d.ts +3 -0
- package/dist/demo/custom-interceptor.js +69 -0
- package/dist/demo/index.d.ts +3 -0
- package/dist/demo/index.js +65 -0
- package/dist/hermes.d.ts +25 -0
- package/dist/hermes.js +107 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/interceptors/index.d.ts +4 -0
- package/dist/interceptors/index.js +298 -0
- package/dist/interceptors/interceptorAddSnowflake.d.ts +7 -0
- package/dist/interceptors/interceptorAddSnowflake.js +35 -0
- package/dist/interceptors/interceptorAddToken.d.ts +7 -0
- package/dist/interceptors/interceptorAddToken.js +46 -0
- package/dist/interceptors/interceptorAddTrace.d.ts +8 -0
- package/dist/interceptors/interceptorAddTrace.js +34 -0
- package/dist/interceptors/interceptorRepeatSubmit.d.ts +8 -0
- package/dist/interceptors/interceptorRepeatSubmit.js +78 -0
- package/dist/modules/auth/dto/account.d.ts +56 -0
- package/dist/modules/auth/dto/account.js +1 -0
- package/dist/modules/auth/dto/ticket.d.ts +13 -0
- package/dist/modules/auth/dto/ticket.js +1 -0
- package/dist/modules/auth/index.d.ts +36 -0
- package/dist/modules/auth/index.js +121 -0
- package/dist/modules/file/dto/tos.d.ts +6 -0
- package/dist/modules/file/dto/tos.js +1 -0
- package/dist/modules/file/index.d.ts +8 -0
- package/dist/modules/file/index.js +28 -0
- package/dist/types/common.d.ts +170 -0
- package/dist/types/common.js +38 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.js +2 -0
- package/dist/types/interceptor.d.ts +13 -0
- package/dist/types/interceptor.js +1 -0
- package/dist/utils/error.d.ts +40 -0
- package/dist/utils/error.js +66 -0
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.js +2 -0
- package/dist/utils/snowflake.d.ts +26 -0
- package/dist/utils/snowflake.js +79 -0
- package/package.json +25 -3
package/dist/hermes.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
2
|
+
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
|
|
3
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
4
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
5
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
6
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
7
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
8
|
+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
|
|
9
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
10
|
+
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
|
|
11
|
+
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
|
12
|
+
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
|
|
13
|
+
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
|
|
14
|
+
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
|
15
|
+
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
|
16
|
+
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
|
17
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
18
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
19
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
20
|
+
import registerRequestInterceptors from "./interceptors";
|
|
21
|
+
import HermesBase from "./base";
|
|
22
|
+
import { markNotifiedError, normalizeErrorPayload } from "./utils/error";
|
|
23
|
+
import { HermesModuleAuth } from "./modules/auth";
|
|
24
|
+
import { HermesModuleFile } from "./modules/file";
|
|
25
|
+
export var Hermes = /*#__PURE__*/function (_HermesBase) {
|
|
26
|
+
_inherits(Hermes, _HermesBase);
|
|
27
|
+
var _super = _createSuper(Hermes);
|
|
28
|
+
function Hermes(config) {
|
|
29
|
+
var _this;
|
|
30
|
+
_classCallCheck(this, Hermes);
|
|
31
|
+
_this = _super.call(this, config);
|
|
32
|
+
|
|
33
|
+
// 注册拦截器
|
|
34
|
+
_defineProperty(_assertThisInitialized(_this), "modules", {
|
|
35
|
+
auth: new HermesModuleAuth(_assertThisInitialized(_this)),
|
|
36
|
+
file: new HermesModuleFile(_assertThisInitialized(_this))
|
|
37
|
+
});
|
|
38
|
+
registerRequestInterceptors(_assertThisInitialized(_this), {
|
|
39
|
+
onError: _this.onError,
|
|
40
|
+
onUnauthorized: _this.onUnauthorized,
|
|
41
|
+
removeToken: _this.removeToken
|
|
42
|
+
});
|
|
43
|
+
return _this;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 以 blob 方式下载文件,返回 Blob 由调用方保存(不引入 file-saver,保持无 UI 依赖)。
|
|
48
|
+
* 后端以 JSON 返回业务错误时,按错误码解析并 reject(错误已带 __notified 标记)。
|
|
49
|
+
*
|
|
50
|
+
* @param url 下载接口地址
|
|
51
|
+
* @param params 请求参数(表单)
|
|
52
|
+
* @param config 透传给 axios 的额外配置
|
|
53
|
+
*/
|
|
54
|
+
_createClass(Hermes, [{
|
|
55
|
+
key: "download",
|
|
56
|
+
value: (function () {
|
|
57
|
+
var _download = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(url, params, config) {
|
|
58
|
+
var res, data, text, payload, message, code, traceId;
|
|
59
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
60
|
+
while (1) switch (_context.prev = _context.next) {
|
|
61
|
+
case 0:
|
|
62
|
+
_context.next = 2;
|
|
63
|
+
return this.instance.post(url, params, _objectSpread(_objectSpread({}, config || {}), {}, {
|
|
64
|
+
responseType: 'blob'
|
|
65
|
+
}));
|
|
66
|
+
case 2:
|
|
67
|
+
res = _context.sent;
|
|
68
|
+
data = res; // 后端把业务错误以 JSON 返回时,blob.type 会被标记为 application/json
|
|
69
|
+
if (!(data && data.type === 'application/json')) {
|
|
70
|
+
_context.next = 13;
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
_context.next = 7;
|
|
74
|
+
return data.text();
|
|
75
|
+
case 7:
|
|
76
|
+
text = _context.sent;
|
|
77
|
+
payload = normalizeErrorPayload(text);
|
|
78
|
+
message = payload && String(payload.message || payload.msg) || '下载失败';
|
|
79
|
+
code = payload === null || payload === void 0 ? void 0 : payload.code;
|
|
80
|
+
traceId = payload === null || payload === void 0 ? void 0 : payload.traceId;
|
|
81
|
+
return _context.abrupt("return", Promise.reject(markNotifiedError(message, {
|
|
82
|
+
code: typeof code === 'string' || typeof code === 'number' ? code : undefined,
|
|
83
|
+
traceId: typeof traceId === 'string' ? traceId : undefined
|
|
84
|
+
})));
|
|
85
|
+
case 13:
|
|
86
|
+
return _context.abrupt("return", data);
|
|
87
|
+
case 14:
|
|
88
|
+
case "end":
|
|
89
|
+
return _context.stop();
|
|
90
|
+
}
|
|
91
|
+
}, _callee, this);
|
|
92
|
+
}));
|
|
93
|
+
function download(_x, _x2, _x3) {
|
|
94
|
+
return _download.apply(this, arguments);
|
|
95
|
+
}
|
|
96
|
+
return download;
|
|
97
|
+
}())
|
|
98
|
+
}]);
|
|
99
|
+
return Hermes;
|
|
100
|
+
}(HermesBase);
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* 创建 Hermes 请求实例,直接返回 Axios 实例。
|
|
104
|
+
*/
|
|
105
|
+
export var createHermes = function createHermes(config) {
|
|
106
|
+
return new Hermes(config).instance;
|
|
107
|
+
};
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { HermesBaseConfig } from '../types';
|
|
2
|
+
import { Hermes } from '../hermes';
|
|
3
|
+
declare const registerRequestInterceptors: (hermes: Hermes, config: Pick<HermesBaseConfig, 'onError' | 'onUnauthorized' | 'removeToken'>) => void;
|
|
4
|
+
export default registerRequestInterceptors;
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
2
|
+
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
3
|
+
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
4
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
5
|
+
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
6
|
+
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
7
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
8
|
+
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
|
|
9
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
10
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
11
|
+
import interceptorAddToken from "./interceptorAddToken";
|
|
12
|
+
import { removeToken, sleep } from '@wapri/gbase';
|
|
13
|
+
import interceptorAddTrace from "./interceptorAddTrace";
|
|
14
|
+
import interceptorAddSnowflake from "./interceptorAddSnowflake";
|
|
15
|
+
import interceptorRepeatSubmit from "./interceptorRepeatSubmit";
|
|
16
|
+
import { ResultCode } from "../constants/resultCode";
|
|
17
|
+
import { markNotifiedError, normalizeErrorPayload } from "../utils/error";
|
|
18
|
+
var pluginInterceptorsMap = {
|
|
19
|
+
trace: interceptorAddTrace,
|
|
20
|
+
snowflake: interceptorAddSnowflake,
|
|
21
|
+
repeatSubmit: interceptorRepeatSubmit
|
|
22
|
+
};
|
|
23
|
+
var registerRequestInterceptors = function registerRequestInterceptors(hermes, config) {
|
|
24
|
+
var interceptor = hermes === null || hermes === void 0 ? void 0 : hermes.interceptor;
|
|
25
|
+
var request = hermes.instance;
|
|
26
|
+
var retry = hermes.retry;
|
|
27
|
+
var onError = config === null || config === void 0 ? void 0 : config.onError;
|
|
28
|
+
var _ref = interceptor || {},
|
|
29
|
+
plugin = _ref.plugin,
|
|
30
|
+
customRequest = _ref.request,
|
|
31
|
+
customResponse = _ref.response,
|
|
32
|
+
customError = _ref.error;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 自定义错误回调在 Hermes 核心错误流程完成后执行:
|
|
36
|
+
* - 未提供回调,保留 Hermes 统一错误;
|
|
37
|
+
* - 回调正常返回,保留 Hermes 统一错误;
|
|
38
|
+
* - 回调以 Promise.reject(value) 或 throw(value) 返回,使用 value 作为最终 reject 值。
|
|
39
|
+
*/
|
|
40
|
+
var resolveCustomError = /*#__PURE__*/function () {
|
|
41
|
+
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(error) {
|
|
42
|
+
var result;
|
|
43
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
44
|
+
while (1) switch (_context.prev = _context.next) {
|
|
45
|
+
case 0:
|
|
46
|
+
_context.prev = 0;
|
|
47
|
+
_context.next = 3;
|
|
48
|
+
return customError === null || customError === void 0 ? void 0 : customError(error, {
|
|
49
|
+
hermes: hermes
|
|
50
|
+
});
|
|
51
|
+
case 3:
|
|
52
|
+
result = _context.sent;
|
|
53
|
+
return _context.abrupt("return", result === undefined ? error : result);
|
|
54
|
+
case 7:
|
|
55
|
+
_context.prev = 7;
|
|
56
|
+
_context.t0 = _context["catch"](0);
|
|
57
|
+
return _context.abrupt("return", _context.t0);
|
|
58
|
+
case 10:
|
|
59
|
+
case "end":
|
|
60
|
+
return _context.stop();
|
|
61
|
+
}
|
|
62
|
+
}, _callee, null, [[0, 7]]);
|
|
63
|
+
}));
|
|
64
|
+
return function resolveCustomError(_x) {
|
|
65
|
+
return _ref2.apply(this, arguments);
|
|
66
|
+
};
|
|
67
|
+
}();
|
|
68
|
+
var rejectWithCustomError = /*#__PURE__*/function () {
|
|
69
|
+
var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(error) {
|
|
70
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
71
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
72
|
+
case 0:
|
|
73
|
+
_context2.t0 = Promise;
|
|
74
|
+
_context2.next = 3;
|
|
75
|
+
return resolveCustomError(error);
|
|
76
|
+
case 3:
|
|
77
|
+
_context2.t1 = _context2.sent;
|
|
78
|
+
return _context2.abrupt("return", _context2.t0.reject.call(_context2.t0, _context2.t1));
|
|
79
|
+
case 5:
|
|
80
|
+
case "end":
|
|
81
|
+
return _context2.stop();
|
|
82
|
+
}
|
|
83
|
+
}, _callee2);
|
|
84
|
+
}));
|
|
85
|
+
return function rejectWithCustomError(_x2) {
|
|
86
|
+
return _ref3.apply(this, arguments);
|
|
87
|
+
};
|
|
88
|
+
}();
|
|
89
|
+
|
|
90
|
+
// 根据plugin依次注册plugin,请注意次序问题,不要两个plugin里有做交叉的事情
|
|
91
|
+
request.interceptors.request.use( /*#__PURE__*/function () {
|
|
92
|
+
var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(config) {
|
|
93
|
+
var specificInterceptors;
|
|
94
|
+
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
|
|
95
|
+
while (1) switch (_context3.prev = _context3.next) {
|
|
96
|
+
case 0:
|
|
97
|
+
_context3.next = 2;
|
|
98
|
+
return interceptorAddToken(config, {
|
|
99
|
+
hermes: hermes
|
|
100
|
+
});
|
|
101
|
+
case 2:
|
|
102
|
+
specificInterceptors = plugin === null || plugin === void 0 ? void 0 : plugin.reduce(function (result, interceptorKey) {
|
|
103
|
+
var specificInterceptor = pluginInterceptorsMap === null || pluginInterceptorsMap === void 0 ? void 0 : pluginInterceptorsMap[interceptorKey];
|
|
104
|
+
return specificInterceptor ? [].concat(_toConsumableArray(result), [specificInterceptor]) : result;
|
|
105
|
+
}, []);
|
|
106
|
+
/**
|
|
107
|
+
* 并行遍历插件集,执行对应的拦截器
|
|
108
|
+
* 在不同插件中修改同一属性的场景暂时没想到
|
|
109
|
+
* 不用串行了,用并行就ok
|
|
110
|
+
* */
|
|
111
|
+
if (!specificInterceptors) {
|
|
112
|
+
_context3.next = 6;
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
_context3.next = 6;
|
|
116
|
+
return Promise.all(specificInterceptors.map(function (fn) {
|
|
117
|
+
// 用catch包一下,防止插件执行错误导致代码无法继续运行
|
|
118
|
+
return fn(config, {
|
|
119
|
+
hermes: hermes
|
|
120
|
+
}).catch(function () {
|
|
121
|
+
return Promise.resolve();
|
|
122
|
+
});
|
|
123
|
+
}));
|
|
124
|
+
case 6:
|
|
125
|
+
_context3.next = 8;
|
|
126
|
+
return customRequest === null || customRequest === void 0 ? void 0 : customRequest(config, {
|
|
127
|
+
hermes: hermes
|
|
128
|
+
});
|
|
129
|
+
case 8:
|
|
130
|
+
return _context3.abrupt("return", config);
|
|
131
|
+
case 9:
|
|
132
|
+
case "end":
|
|
133
|
+
return _context3.stop();
|
|
134
|
+
}
|
|
135
|
+
}, _callee3);
|
|
136
|
+
}));
|
|
137
|
+
return function (_x3) {
|
|
138
|
+
return _ref4.apply(this, arguments);
|
|
139
|
+
};
|
|
140
|
+
}(), function (error) {
|
|
141
|
+
return Promise.reject(error);
|
|
142
|
+
});
|
|
143
|
+
request.interceptors.response.use( /*#__PURE__*/function () {
|
|
144
|
+
var _ref5 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(res) {
|
|
145
|
+
var _res$config;
|
|
146
|
+
var _ref6, data, responseType, rawCode, code, success, msg, traceId, bizError, _config$onUnauthorize, cfg, currentCount, maxRetry;
|
|
147
|
+
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
|
|
148
|
+
while (1) switch (_context4.prev = _context4.next) {
|
|
149
|
+
case 0:
|
|
150
|
+
_context4.next = 2;
|
|
151
|
+
return customResponse === null || customResponse === void 0 ? void 0 : customResponse(res, {
|
|
152
|
+
hermes: hermes
|
|
153
|
+
});
|
|
154
|
+
case 2:
|
|
155
|
+
_ref6 = res || {}, data = _ref6.data; // blob / arraybuffer 下载直通,不走业务 JSON 解析
|
|
156
|
+
responseType = res === null || res === void 0 || (_res$config = res.config) === null || _res$config === void 0 ? void 0 : _res$config.responseType;
|
|
157
|
+
if (!(responseType === 'blob' || responseType === 'arraybuffer')) {
|
|
158
|
+
_context4.next = 6;
|
|
159
|
+
break;
|
|
160
|
+
}
|
|
161
|
+
return _context4.abrupt("return", Promise.resolve(data));
|
|
162
|
+
case 6:
|
|
163
|
+
if (!(!data || _typeof(data) !== 'object')) {
|
|
164
|
+
_context4.next = 8;
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
167
|
+
return _context4.abrupt("return", Promise.resolve(data));
|
|
168
|
+
case 8:
|
|
169
|
+
rawCode = data === null || data === void 0 ? void 0 : data.code;
|
|
170
|
+
code = typeof rawCode === 'string' || typeof rawCode === 'number' ? rawCode : undefined; // 兼容两种成功判定:标准 code(0 / 200),或网关 success 标记
|
|
171
|
+
success = (data === null || data === void 0 ? void 0 : data.success) === true || code === ResultCode.Success || code === ResultCode.Success200;
|
|
172
|
+
if (!success) {
|
|
173
|
+
_context4.next = 13;
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
return _context4.abrupt("return", Promise.resolve(hermes.unwrapResponse ? data === null || data === void 0 ? void 0 : data.data : data));
|
|
177
|
+
case 13:
|
|
178
|
+
// 业务错误统一包装,便于业务 catch 识别并跳过重复提示
|
|
179
|
+
msg = (data === null || data === void 0 ? void 0 : data.msg) || (data === null || data === void 0 ? void 0 : data.message) || '请求失败';
|
|
180
|
+
traceId = typeof (data === null || data === void 0 ? void 0 : data.traceId) === 'string' ? data.traceId : undefined;
|
|
181
|
+
bizError = markNotifiedError(String(msg), {
|
|
182
|
+
code: code,
|
|
183
|
+
traceId: traceId
|
|
184
|
+
});
|
|
185
|
+
bizError.response = res;
|
|
186
|
+
|
|
187
|
+
// 业务 401:网关后多实例权限中心部分节点未兼容 JWT 时,先瞬时重试
|
|
188
|
+
if (!(code === ResultCode.LoginError || code === String(ResultCode.LoginError))) {
|
|
189
|
+
_context4.next = 28;
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
cfg = res.config;
|
|
193
|
+
currentCount = cfg.__hermes_401_retry_count__ || 0;
|
|
194
|
+
maxRetry = hermes.unauthorizedRetry;
|
|
195
|
+
if (!(maxRetry > 0 && currentCount < maxRetry)) {
|
|
196
|
+
_context4.next = 24;
|
|
197
|
+
break;
|
|
198
|
+
}
|
|
199
|
+
cfg.__hermes_401_retry_count__ = currentCount + 1;
|
|
200
|
+
return _context4.abrupt("return", request(cfg));
|
|
201
|
+
case 24:
|
|
202
|
+
onError === null || onError === void 0 || onError(bizError);
|
|
203
|
+
config === null || config === void 0 || (_config$onUnauthorize = config.onUnauthorized) === null || _config$onUnauthorize === void 0 || _config$onUnauthorize.call(config);
|
|
204
|
+
if ((config === null || config === void 0 ? void 0 : config.removeToken) !== false) {
|
|
205
|
+
removeToken();
|
|
206
|
+
}
|
|
207
|
+
return _context4.abrupt("return", rejectWithCustomError(bizError));
|
|
208
|
+
case 28:
|
|
209
|
+
onError === null || onError === void 0 || onError(bizError);
|
|
210
|
+
return _context4.abrupt("return", rejectWithCustomError(bizError));
|
|
211
|
+
case 30:
|
|
212
|
+
case "end":
|
|
213
|
+
return _context4.stop();
|
|
214
|
+
}
|
|
215
|
+
}, _callee4);
|
|
216
|
+
}));
|
|
217
|
+
return function (_x4) {
|
|
218
|
+
return _ref5.apply(this, arguments);
|
|
219
|
+
};
|
|
220
|
+
}(), /*#__PURE__*/function () {
|
|
221
|
+
var _ref7 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(error) {
|
|
222
|
+
var _error$response, _error$response2, _error$config;
|
|
223
|
+
var status, unverified, _config$onUnauthorize2, num, delay, codes, errorCode, currentErrorAllowRetry, _errorConfig$__ktc_re, errorConfig, currentRetryCount, retryDelay, payload, bizMsg, message, httpError;
|
|
224
|
+
return _regeneratorRuntime().wrap(function _callee5$(_context5) {
|
|
225
|
+
while (1) switch (_context5.prev = _context5.next) {
|
|
226
|
+
case 0:
|
|
227
|
+
status = (error === null || error === void 0 ? void 0 : error.status) || (error === null || error === void 0 || (_error$response = error.response) === null || _error$response === void 0 ? void 0 : _error$response.status);
|
|
228
|
+
unverified = Number(status) === ResultCode.LoginError || status === ResultCode.AdminStatus;
|
|
229
|
+
if (!unverified) {
|
|
230
|
+
_context5.next = 7;
|
|
231
|
+
break;
|
|
232
|
+
}
|
|
233
|
+
// 登录态失效:先回调业务层重登录钩子,再按配置清理 token
|
|
234
|
+
onError === null || onError === void 0 || onError(error);
|
|
235
|
+
config === null || config === void 0 || (_config$onUnauthorize2 = config.onUnauthorized) === null || _config$onUnauthorize2 === void 0 || _config$onUnauthorize2.call(config);
|
|
236
|
+
if ((config === null || config === void 0 ? void 0 : config.removeToken) !== false) {
|
|
237
|
+
removeToken();
|
|
238
|
+
}
|
|
239
|
+
return _context5.abrupt("return", rejectWithCustomError(error));
|
|
240
|
+
case 7:
|
|
241
|
+
if (!(retry !== null && retry !== void 0 && retry.open)) {
|
|
242
|
+
_context5.next = 20;
|
|
243
|
+
break;
|
|
244
|
+
}
|
|
245
|
+
num = retry.num, delay = retry.delay, codes = retry.codes;
|
|
246
|
+
errorCode = error === null || error === void 0 ? void 0 : error.code;
|
|
247
|
+
currentErrorAllowRetry = codes === null || codes === void 0 ? void 0 : codes.includes(errorCode);
|
|
248
|
+
if (!currentErrorAllowRetry) {
|
|
249
|
+
_context5.next = 20;
|
|
250
|
+
break;
|
|
251
|
+
}
|
|
252
|
+
errorConfig = error.config;
|
|
253
|
+
currentRetryCount = (_errorConfig$__ktc_re = errorConfig === null || errorConfig === void 0 ? void 0 : errorConfig.__ktc_request_retry_count__) !== null && _errorConfig$__ktc_re !== void 0 ? _errorConfig$__ktc_re : 0;
|
|
254
|
+
if (!(currentRetryCount < num)) {
|
|
255
|
+
_context5.next = 20;
|
|
256
|
+
break;
|
|
257
|
+
}
|
|
258
|
+
errorConfig.__ktc_request_retry_count__ = currentRetryCount + 1;
|
|
259
|
+
retryDelay = Math.pow(2, currentRetryCount) * delay;
|
|
260
|
+
_context5.next = 19;
|
|
261
|
+
return sleep(retryDelay);
|
|
262
|
+
case 19:
|
|
263
|
+
return _context5.abrupt("return", request(errorConfig));
|
|
264
|
+
case 20:
|
|
265
|
+
// 网络 / HTTP 错误:提取业务文案或生成泛化提示
|
|
266
|
+
payload = normalizeErrorPayload((_error$response2 = error.response) === null || _error$response2 === void 0 ? void 0 : _error$response2.data);
|
|
267
|
+
bizMsg = payload && (payload.message || payload.msg);
|
|
268
|
+
message = bizMsg && String(bizMsg) || (error === null || error === void 0 ? void 0 : error.message) || '请求失败';
|
|
269
|
+
if (!bizMsg) {
|
|
270
|
+
if (message === 'Network Error') {
|
|
271
|
+
message = '后端接口连接异常';
|
|
272
|
+
} else if (message.includes('timeout')) {
|
|
273
|
+
message = '系统接口请求超时';
|
|
274
|
+
} else if (message.includes('Request failed with status code')) {
|
|
275
|
+
message = '系统接口' + message.slice(-3) + '异常';
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
httpError = markNotifiedError(message, {
|
|
279
|
+
code: typeof (payload === null || payload === void 0 ? void 0 : payload.code) === 'string' || typeof (payload === null || payload === void 0 ? void 0 : payload.code) === 'number' ? payload.code : undefined,
|
|
280
|
+
traceId: typeof (payload === null || payload === void 0 ? void 0 : payload.traceId) === 'string' ? payload.traceId : undefined
|
|
281
|
+
});
|
|
282
|
+
httpError.response = error.response;
|
|
283
|
+
if (!((_error$config = error.config) !== null && _error$config !== void 0 && _error$config.silentError)) {
|
|
284
|
+
onError === null || onError === void 0 || onError(httpError);
|
|
285
|
+
}
|
|
286
|
+
return _context5.abrupt("return", rejectWithCustomError(httpError));
|
|
287
|
+
case 28:
|
|
288
|
+
case "end":
|
|
289
|
+
return _context5.stop();
|
|
290
|
+
}
|
|
291
|
+
}, _callee5);
|
|
292
|
+
}));
|
|
293
|
+
return function (_x5) {
|
|
294
|
+
return _ref7.apply(this, arguments);
|
|
295
|
+
};
|
|
296
|
+
}());
|
|
297
|
+
};
|
|
298
|
+
export default registerRequestInterceptors;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
|
|
2
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
3
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
4
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
5
|
+
import { stringifySnowflakeFields } from "../utils/snowflake";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 雪花请求拦截器:
|
|
9
|
+
* 发出前把可能丢精度的雪花 ID 字段强制转为字符串。
|
|
10
|
+
*/
|
|
11
|
+
var interceptorAddSnowflake = /*#__PURE__*/function () {
|
|
12
|
+
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(config) {
|
|
13
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
14
|
+
while (1) switch (_context.prev = _context.next) {
|
|
15
|
+
case 0:
|
|
16
|
+
if (config) {
|
|
17
|
+
_context.next = 2;
|
|
18
|
+
break;
|
|
19
|
+
}
|
|
20
|
+
return _context.abrupt("return");
|
|
21
|
+
case 2:
|
|
22
|
+
if (config.data && _typeof(config.data) === 'object' && !(config.data instanceof FormData)) {
|
|
23
|
+
config.data = stringifySnowflakeFields(config.data);
|
|
24
|
+
}
|
|
25
|
+
case 3:
|
|
26
|
+
case "end":
|
|
27
|
+
return _context.stop();
|
|
28
|
+
}
|
|
29
|
+
}, _callee);
|
|
30
|
+
}));
|
|
31
|
+
return function interceptorAddSnowflake(_x) {
|
|
32
|
+
return _ref.apply(this, arguments);
|
|
33
|
+
};
|
|
34
|
+
}();
|
|
35
|
+
export default interceptorAddSnowflake;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
2
|
+
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
|
|
3
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
4
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
5
|
+
/**
|
|
6
|
+
* 公参请求拦截器:
|
|
7
|
+
* 为所有HTTP请求添加 token 身份凭证
|
|
8
|
+
*/
|
|
9
|
+
var interceptorAddToken = /*#__PURE__*/function () {
|
|
10
|
+
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(config, other) {
|
|
11
|
+
var headers, hermes, token;
|
|
12
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
13
|
+
while (1) switch (_context.prev = _context.next) {
|
|
14
|
+
case 0:
|
|
15
|
+
if (config) {
|
|
16
|
+
_context.next = 2;
|
|
17
|
+
break;
|
|
18
|
+
}
|
|
19
|
+
return _context.abrupt("return");
|
|
20
|
+
case 2:
|
|
21
|
+
headers = config.headers || {}; // 兼容 admin-frontend:单个请求可声明不需要 token
|
|
22
|
+
if (!(headers.isToken === false)) {
|
|
23
|
+
_context.next = 5;
|
|
24
|
+
break;
|
|
25
|
+
}
|
|
26
|
+
return _context.abrupt("return");
|
|
27
|
+
case 5:
|
|
28
|
+
hermes = other === null || other === void 0 ? void 0 : other.hermes;
|
|
29
|
+
_context.next = 8;
|
|
30
|
+
return hermes === null || hermes === void 0 ? void 0 : hermes.getToken();
|
|
31
|
+
case 8:
|
|
32
|
+
token = _context.sent;
|
|
33
|
+
if (token) {
|
|
34
|
+
headers['Authorization'] = 'Bearer ' + token;
|
|
35
|
+
}
|
|
36
|
+
case 10:
|
|
37
|
+
case "end":
|
|
38
|
+
return _context.stop();
|
|
39
|
+
}
|
|
40
|
+
}, _callee);
|
|
41
|
+
}));
|
|
42
|
+
return function interceptorAddToken(_x, _x2) {
|
|
43
|
+
return _ref.apply(this, arguments);
|
|
44
|
+
};
|
|
45
|
+
}();
|
|
46
|
+
export default interceptorAddToken;
|