@wapri/ghermes 0.0.0-stage → 2.0.10
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 +44 -0
- package/dist/base.js +160 -0
- package/dist/constants/domain.d.ts +34 -0
- package/dist/constants/domain.js +56 -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 +68 -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 +126 -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 +165 -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/README.md
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# @wapri/ghermes
|
|
2
|
+
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
请求包
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
1: 维护不同服务的请求,会根据模块名、env决定他的请求域名
|
|
10
|
+
2: 维护各个业务板块的请求,避免各种项目间重复定义接口请求
|
|
11
|
+
3: 统一维护服务端返回的数据格式类型,避免各个项目间调用时数据类型定义不统一的问题
|
|
12
|
+
4: 插拔式插件开发,进行拦截器等插件开发,项目中可按需开启功能配置
|
|
13
|
+
5: 可以实现简单的bff,当然并不推荐在拿到返回后做这些处理
|
|
14
|
+
|
|
15
|
+
当然,他能做的不止如此
|
|
16
|
+
```
|
package/dist/base.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file HermesBase
|
|
3
|
+
* - request的基础类
|
|
4
|
+
* - 主要负责生成axios实例
|
|
5
|
+
* - 同时维护内部配置
|
|
6
|
+
* */
|
|
7
|
+
import { AxiosInstance } from 'axios';
|
|
8
|
+
import { HermesBaseConfig, HermesBaseInterceptor, HermesRetry, RepeatSubmitConfig } from './types';
|
|
9
|
+
import { Env } from '@wapri/gbase';
|
|
10
|
+
export default class HermesBase {
|
|
11
|
+
readonly env: Env;
|
|
12
|
+
private readonly baseUrl;
|
|
13
|
+
/** axios实例 */
|
|
14
|
+
readonly instance: AxiosInstance;
|
|
15
|
+
/** 拦截器配置 */
|
|
16
|
+
readonly interceptor?: HermesBaseInterceptor;
|
|
17
|
+
/** 重试配置 */
|
|
18
|
+
readonly retry: Required<HermesRetry> & {
|
|
19
|
+
open: boolean;
|
|
20
|
+
};
|
|
21
|
+
protected onError?: HermesBaseConfig['onError'];
|
|
22
|
+
protected onUnauthorized?: HermesBaseConfig['onUnauthorized'];
|
|
23
|
+
/** 防重复提交配置 */
|
|
24
|
+
readonly repeatSubmit: Required<RepeatSubmitConfig> & {
|
|
25
|
+
open: boolean;
|
|
26
|
+
};
|
|
27
|
+
/** 雪花 Long 精度保护开关(响应侧 transformResponse) */
|
|
28
|
+
readonly snowflake: boolean;
|
|
29
|
+
/** 业务 401 瞬时重试次数 */
|
|
30
|
+
readonly unauthorizedRetry: number;
|
|
31
|
+
/** 登录态失效时是否自动移除 token */
|
|
32
|
+
readonly removeToken: boolean;
|
|
33
|
+
/** 是否自动 unwrap 业务 data */
|
|
34
|
+
readonly unwrapResponse: boolean;
|
|
35
|
+
constructor(config: HermesBaseConfig);
|
|
36
|
+
private initialBaseUrl;
|
|
37
|
+
/** 设置重试配置 */
|
|
38
|
+
private initialRetry;
|
|
39
|
+
/** 设置防重复提交配置 */
|
|
40
|
+
private initialRepeatSubmit;
|
|
41
|
+
/** 设置雪花保护开关,默认开启 */
|
|
42
|
+
private initialSnowflake;
|
|
43
|
+
getToken(): Promise<string>;
|
|
44
|
+
}
|
package/dist/base.js
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
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 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); } }
|
|
3
|
+
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); }); }; }
|
|
4
|
+
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); }
|
|
5
|
+
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; }
|
|
6
|
+
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; }
|
|
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 _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; }
|
|
11
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
12
|
+
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); }
|
|
13
|
+
/**
|
|
14
|
+
* @file HermesBase
|
|
15
|
+
* - request的基础类
|
|
16
|
+
* - 主要负责生成axios实例
|
|
17
|
+
* - 同时维护内部配置
|
|
18
|
+
* */
|
|
19
|
+
|
|
20
|
+
import axios from 'axios';
|
|
21
|
+
import { isEmpty, Env, getToken as _getToken } from '@wapri/gbase';
|
|
22
|
+
import { serverDomains } from "./constants/domain";
|
|
23
|
+
import { axiosTransformSafeLong } from "./utils/snowflake";
|
|
24
|
+
var HermesBase = /*#__PURE__*/function () {
|
|
25
|
+
function HermesBase(config) {
|
|
26
|
+
_classCallCheck(this, HermesBase);
|
|
27
|
+
_defineProperty(this, "env", void 0);
|
|
28
|
+
_defineProperty(this, "baseUrl", '');
|
|
29
|
+
/** axios实例 */
|
|
30
|
+
_defineProperty(this, "instance", void 0);
|
|
31
|
+
/** 拦截器配置 */
|
|
32
|
+
_defineProperty(this, "interceptor", void 0);
|
|
33
|
+
/** 重试配置 */
|
|
34
|
+
_defineProperty(this, "retry", void 0);
|
|
35
|
+
_defineProperty(this, "onError", void 0);
|
|
36
|
+
_defineProperty(this, "onUnauthorized", void 0);
|
|
37
|
+
/** 防重复提交配置 */
|
|
38
|
+
_defineProperty(this, "repeatSubmit", void 0);
|
|
39
|
+
/** 雪花 Long 精度保护开关(响应侧 transformResponse) */
|
|
40
|
+
_defineProperty(this, "snowflake", void 0);
|
|
41
|
+
/** 业务 401 瞬时重试次数 */
|
|
42
|
+
_defineProperty(this, "unauthorizedRetry", void 0);
|
|
43
|
+
/** 登录态失效时是否自动移除 token */
|
|
44
|
+
_defineProperty(this, "removeToken", void 0);
|
|
45
|
+
/** 是否自动 unwrap 业务 data */
|
|
46
|
+
_defineProperty(this, "unwrapResponse", void 0);
|
|
47
|
+
var _config$axiosConfig = config.axiosConfig,
|
|
48
|
+
axiosConfig = _config$axiosConfig === void 0 ? {} : _config$axiosConfig,
|
|
49
|
+
retry = config.retry,
|
|
50
|
+
_config$interceptor = config.interceptor,
|
|
51
|
+
interceptor = _config$interceptor === void 0 ? {
|
|
52
|
+
plugin: ['trace']
|
|
53
|
+
} : _config$interceptor,
|
|
54
|
+
_config$env = config.env,
|
|
55
|
+
env = _config$env === void 0 ? Env.PROD : _config$env,
|
|
56
|
+
_config$repeatSubmit = config.repeatSubmit,
|
|
57
|
+
repeatSubmit = _config$repeatSubmit === void 0 ? true : _config$repeatSubmit,
|
|
58
|
+
snowflake = config.snowflake,
|
|
59
|
+
unauthorizedRetry = config.unauthorizedRetry,
|
|
60
|
+
removeToken = config.removeToken,
|
|
61
|
+
unwrapResponse = config.unwrapResponse,
|
|
62
|
+
onError = config.onError,
|
|
63
|
+
onUnauthorized = config.onUnauthorized;
|
|
64
|
+
this.env = env;
|
|
65
|
+
this.baseUrl = this.initialBaseUrl(config);
|
|
66
|
+
this.interceptor = interceptor;
|
|
67
|
+
this.retry = this.initialRetry(retry);
|
|
68
|
+
this.repeatSubmit = this.initialRepeatSubmit(repeatSubmit);
|
|
69
|
+
this.snowflake = this.initialSnowflake(snowflake);
|
|
70
|
+
this.unauthorizedRetry = unauthorizedRetry !== null && unauthorizedRetry !== void 0 ? unauthorizedRetry : 2;
|
|
71
|
+
this.removeToken = removeToken !== false;
|
|
72
|
+
this.unwrapResponse = unwrapResponse !== false;
|
|
73
|
+
this.onError = onError;
|
|
74
|
+
this.onUnauthorized = onUnauthorized;
|
|
75
|
+
this.instance = axios.create(_objectSpread(_objectSpread({
|
|
76
|
+
baseURL: this.baseUrl,
|
|
77
|
+
headers: {}
|
|
78
|
+
}, axiosConfig || {}), {}, {
|
|
79
|
+
timeout: (axiosConfig === null || axiosConfig === void 0 ? void 0 : axiosConfig.timeout) || 20 * 1000
|
|
80
|
+
}, this.snowflake ? {
|
|
81
|
+
transformResponse: [axiosTransformSafeLong]
|
|
82
|
+
} : {}));
|
|
83
|
+
}
|
|
84
|
+
_createClass(HermesBase, [{
|
|
85
|
+
key: "initialBaseUrl",
|
|
86
|
+
value: function initialBaseUrl(config) {
|
|
87
|
+
var _serverDomains$module;
|
|
88
|
+
var _config$env2 = config.env,
|
|
89
|
+
env = _config$env2 === void 0 ? Env.PROD : _config$env2,
|
|
90
|
+
baseUrl = config.baseUrl,
|
|
91
|
+
_config$module = config.module,
|
|
92
|
+
module = _config$module === void 0 ? 'store' : _config$module;
|
|
93
|
+
return baseUrl || ((_serverDomains$module = serverDomains[module]) === null || _serverDomains$module === void 0 ? void 0 : _serverDomains$module[env]);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** 设置重试配置 */
|
|
97
|
+
}, {
|
|
98
|
+
key: "initialRetry",
|
|
99
|
+
value: function initialRetry(retry) {
|
|
100
|
+
var _retry$num, _retry$delay;
|
|
101
|
+
/**
|
|
102
|
+
* 什么时候呢我会认为你不需要重试,以下条件均为或的关系
|
|
103
|
+
* 1. 你的retry配置中num不为真
|
|
104
|
+
* 2. 你的retry配置中错误码场景为空
|
|
105
|
+
*/
|
|
106
|
+
var num = (_retry$num = retry === null || retry === void 0 ? void 0 : retry.num) !== null && _retry$num !== void 0 ? _retry$num : 2;
|
|
107
|
+
var codes = (retry === null || retry === void 0 ? void 0 : retry.codes) || ['ECONNABORTED', 'ERR_NETWORK'];
|
|
108
|
+
var delay = (_retry$delay = retry === null || retry === void 0 ? void 0 : retry.delay) !== null && _retry$delay !== void 0 ? _retry$delay : 1000;
|
|
109
|
+
var noRetry = Number(num) < 1 || isEmpty(codes);
|
|
110
|
+
var retryConfig = {
|
|
111
|
+
open: !noRetry,
|
|
112
|
+
num: num,
|
|
113
|
+
delay: delay,
|
|
114
|
+
codes: codes
|
|
115
|
+
};
|
|
116
|
+
return retryConfig;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** 设置防重复提交配置 */
|
|
120
|
+
}, {
|
|
121
|
+
key: "initialRepeatSubmit",
|
|
122
|
+
value: function initialRepeatSubmit(repeatSubmit) {
|
|
123
|
+
var _ref;
|
|
124
|
+
var open = repeatSubmit === true || _typeof(repeatSubmit) === 'object';
|
|
125
|
+
var interval = (_ref = _typeof(repeatSubmit) === 'object' ? repeatSubmit === null || repeatSubmit === void 0 ? void 0 : repeatSubmit.interval : undefined) !== null && _ref !== void 0 ? _ref : 1000;
|
|
126
|
+
return {
|
|
127
|
+
open: open,
|
|
128
|
+
interval: interval
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** 设置雪花保护开关,默认开启 */
|
|
133
|
+
}, {
|
|
134
|
+
key: "initialSnowflake",
|
|
135
|
+
value: function initialSnowflake(snowflake) {
|
|
136
|
+
return snowflake !== false;
|
|
137
|
+
}
|
|
138
|
+
}, {
|
|
139
|
+
key: "getToken",
|
|
140
|
+
value: function () {
|
|
141
|
+
var _getToken2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
142
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
143
|
+
while (1) switch (_context.prev = _context.next) {
|
|
144
|
+
case 0:
|
|
145
|
+
return _context.abrupt("return", _getToken() || '');
|
|
146
|
+
case 1:
|
|
147
|
+
case "end":
|
|
148
|
+
return _context.stop();
|
|
149
|
+
}
|
|
150
|
+
}, _callee);
|
|
151
|
+
}));
|
|
152
|
+
function getToken() {
|
|
153
|
+
return _getToken2.apply(this, arguments);
|
|
154
|
+
}
|
|
155
|
+
return getToken;
|
|
156
|
+
}()
|
|
157
|
+
}]);
|
|
158
|
+
return HermesBase;
|
|
159
|
+
}();
|
|
160
|
+
export { HermesBase as default };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Env } from '@wapri/gbase';
|
|
2
|
+
/**
|
|
3
|
+
* 服务端模块(域名维度):
|
|
4
|
+
* - store / admin 为终端维度(门店端 / 平台端),直接挂到网关根
|
|
5
|
+
* - 其余为业务中心维度,走「网关根 + 服务前缀」
|
|
6
|
+
* - gateway 为网关管控台独立实例(不经过业务网关根)
|
|
7
|
+
*/
|
|
8
|
+
export type ServerDomainKey = 'store' | 'admin' | 'permission' | 'merchant' | 'goods' | 'marketing' | 'pay' | 'order' | 'express' | 'file' | 'capability' | 'message' | 'cart' | 'gateway' | 'aftersale' | 'invoice' | 'auth';
|
|
9
|
+
/** 各业务中心的网关注册前缀(固定值,不随环境变化) */
|
|
10
|
+
export declare const SERVICE_PREFIX: Record<Exclude<ServerDomainKey, 'store' | 'admin' | 'gateway'>, string>;
|
|
11
|
+
export declare const serverDomains: {
|
|
12
|
+
store: Record<Env, string>;
|
|
13
|
+
admin: Record<Env, string>;
|
|
14
|
+
permission: Record<Env, string>;
|
|
15
|
+
merchant: Record<Env, string>;
|
|
16
|
+
goods: Record<Env, string>;
|
|
17
|
+
marketing: Record<Env, string>;
|
|
18
|
+
pay: Record<Env, string>;
|
|
19
|
+
order: Record<Env, string>;
|
|
20
|
+
express: Record<Env, string>;
|
|
21
|
+
file: Record<Env, string>;
|
|
22
|
+
capability: Record<Env, string>;
|
|
23
|
+
message: Record<Env, string>;
|
|
24
|
+
cart: Record<Env, string>;
|
|
25
|
+
gateway: {
|
|
26
|
+
dev: string;
|
|
27
|
+
test: string;
|
|
28
|
+
pre: string;
|
|
29
|
+
prod: string;
|
|
30
|
+
};
|
|
31
|
+
aftersale: Record<Env, string>;
|
|
32
|
+
invoice: Record<Env, string>;
|
|
33
|
+
auth: Record<Env, string>;
|
|
34
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
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 _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; }
|
|
3
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
4
|
+
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); }
|
|
5
|
+
import { Env } from '@wapri/gbase';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 服务端模块(域名维度):
|
|
9
|
+
* - store / admin 为终端维度(门店端 / 平台端),直接挂到网关根
|
|
10
|
+
* - 其余为业务中心维度,走「网关根 + 服务前缀」
|
|
11
|
+
* - gateway 为网关管控台独立实例(不经过业务网关根)
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** 网关根路径(终端维度直接使用) */
|
|
15
|
+
var GATEWAY_ROOT = _defineProperty(_defineProperty(_defineProperty(_defineProperty({}, Env.DEV, '/api'), Env.TEST, '/api'), Env.PRE, '/api'), Env.PROD, '/api');
|
|
16
|
+
|
|
17
|
+
/** 各业务中心的网关注册前缀(固定值,不随环境变化) */
|
|
18
|
+
export var SERVICE_PREFIX = {
|
|
19
|
+
permission: '/kaimai-permission-center',
|
|
20
|
+
merchant: '/kaimai-merchant',
|
|
21
|
+
goods: '/kaimai-goods-center',
|
|
22
|
+
marketing: '/kaimai-marketing-center',
|
|
23
|
+
pay: '/kaimai-pay-center',
|
|
24
|
+
order: '/kaimai-order-center',
|
|
25
|
+
express: '/kaimai-express-center',
|
|
26
|
+
file: '/kaimai-file',
|
|
27
|
+
capability: '/kaimai-capability',
|
|
28
|
+
message: '/kaimai-message',
|
|
29
|
+
cart: '/kaimai-cart-center',
|
|
30
|
+
aftersale: '/kaimai-after-sale-center',
|
|
31
|
+
invoice: '/invoice/v1',
|
|
32
|
+
auth: '/auth'
|
|
33
|
+
};
|
|
34
|
+
function buildServiceDomains(prefix) {
|
|
35
|
+
return _defineProperty(_defineProperty(_defineProperty(_defineProperty({}, Env.DEV, "".concat(GATEWAY_ROOT[Env.DEV]).concat(prefix)), Env.TEST, "".concat(GATEWAY_ROOT[Env.TEST]).concat(prefix)), Env.PRE, "".concat(GATEWAY_ROOT[Env.PRE]).concat(prefix)), Env.PROD, "".concat(GATEWAY_ROOT[Env.PROD]).concat(prefix));
|
|
36
|
+
}
|
|
37
|
+
export var serverDomains = {
|
|
38
|
+
store: GATEWAY_ROOT,
|
|
39
|
+
admin: GATEWAY_ROOT,
|
|
40
|
+
permission: buildServiceDomains(SERVICE_PREFIX.permission),
|
|
41
|
+
merchant: buildServiceDomains(SERVICE_PREFIX.merchant),
|
|
42
|
+
goods: buildServiceDomains(SERVICE_PREFIX.goods),
|
|
43
|
+
marketing: buildServiceDomains(SERVICE_PREFIX.marketing),
|
|
44
|
+
pay: buildServiceDomains(SERVICE_PREFIX.pay),
|
|
45
|
+
order: buildServiceDomains(SERVICE_PREFIX.order),
|
|
46
|
+
express: buildServiceDomains(SERVICE_PREFIX.express),
|
|
47
|
+
file: buildServiceDomains(SERVICE_PREFIX.file),
|
|
48
|
+
capability: buildServiceDomains(SERVICE_PREFIX.capability),
|
|
49
|
+
message: buildServiceDomains(SERVICE_PREFIX.message),
|
|
50
|
+
cart: buildServiceDomains(SERVICE_PREFIX.cart),
|
|
51
|
+
// 网关管控台独立实例,各环境同路径;聚合文档可 new Hermes({ baseUrl: '/docs' }) 覆盖
|
|
52
|
+
gateway: _defineProperty(_defineProperty(_defineProperty(_defineProperty({}, Env.DEV, '/gateway/admin'), Env.TEST, '/gateway/admin'), Env.PRE, '/gateway/admin'), Env.PROD, '/gateway/admin'),
|
|
53
|
+
aftersale: buildServiceDomains(SERVICE_PREFIX.aftersale),
|
|
54
|
+
invoice: buildServiceDomains(SERVICE_PREFIX.invoice),
|
|
55
|
+
auth: buildServiceDomains(SERVICE_PREFIX.auth)
|
|
56
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare enum ResultCode {
|
|
2
|
+
Success = 0,
|
|
3
|
+
Success200 = 200,
|
|
4
|
+
LoginError = 401,
|
|
5
|
+
AdminStatus = "ADMIN_401",
|
|
6
|
+
NoPermission = 403,
|
|
7
|
+
NotFount = 404,
|
|
8
|
+
ServerError = 500,
|
|
9
|
+
Warning = 601,
|
|
10
|
+
NotAccessPermission = 40301,
|
|
11
|
+
Mismatch = 40425,
|
|
12
|
+
MismatchNotEdit = 40426
|
|
13
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export var ResultCode = /*#__PURE__*/function (ResultCode) {
|
|
2
|
+
ResultCode[ResultCode["Success"] = 0] = "Success";
|
|
3
|
+
ResultCode[ResultCode["Success200"] = 200] = "Success200";
|
|
4
|
+
ResultCode[ResultCode["LoginError"] = 401] = "LoginError";
|
|
5
|
+
ResultCode["AdminStatus"] = "ADMIN_401";
|
|
6
|
+
ResultCode[ResultCode["NoPermission"] = 403] = "NoPermission";
|
|
7
|
+
ResultCode[ResultCode["NotFount"] = 404] = "NotFount";
|
|
8
|
+
ResultCode[ResultCode["ServerError"] = 500] = "ServerError";
|
|
9
|
+
ResultCode[ResultCode["Warning"] = 601] = "Warning";
|
|
10
|
+
ResultCode[ResultCode["NotAccessPermission"] = 40301] = "NotAccessPermission";
|
|
11
|
+
ResultCode[ResultCode["Mismatch"] = 40425] = "Mismatch";
|
|
12
|
+
ResultCode[ResultCode["MismatchNotEdit"] = 40426] = "MismatchNotEdit";
|
|
13
|
+
return ResultCode;
|
|
14
|
+
}({});
|
|
@@ -0,0 +1,68 @@
|
|
|
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
|
+
import { Env } from '@wapri/gbase';
|
|
6
|
+
import { Hermes } from "./..";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 业务自定义拦截器示例:
|
|
10
|
+
* 在内置拦截器(token + plugin)的基础上,追加 request / response / error 三个自定义回调。
|
|
11
|
+
*/
|
|
12
|
+
var hermes = new Hermes({
|
|
13
|
+
env: Env.DEV,
|
|
14
|
+
module: 'merchant',
|
|
15
|
+
baseUrl: 'http://admin-test.kaimaitong.com/api',
|
|
16
|
+
interceptor: {
|
|
17
|
+
plugin: ['trace'],
|
|
18
|
+
// 自定义请求拦截器:内置拦截器执行后追加,可继续修改 config
|
|
19
|
+
request: function request(config) {
|
|
20
|
+
config.headers['X-Biz-Trace'] = 'custom-interceptor';
|
|
21
|
+
},
|
|
22
|
+
// 自定义响应拦截器:在响应拦截器顶层最先执行,在 blob 判定、业务码解析之前
|
|
23
|
+
response: function response(res) {},
|
|
24
|
+
// 自定义错误拦截器:内置错误处理(业务/网络错误包装 + onError)后追加
|
|
25
|
+
error: function error(err) {}
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
var Demo = function Demo() {
|
|
29
|
+
var send = /*#__PURE__*/function () {
|
|
30
|
+
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
31
|
+
var res;
|
|
32
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
33
|
+
while (1) switch (_context.prev = _context.next) {
|
|
34
|
+
case 0:
|
|
35
|
+
_context.prev = 0;
|
|
36
|
+
_context.next = 3;
|
|
37
|
+
return hermes.instance.request({
|
|
38
|
+
method: 'get',
|
|
39
|
+
url: '/kaimai-permission-center/system/menu/list'
|
|
40
|
+
});
|
|
41
|
+
case 3:
|
|
42
|
+
res = _context.sent;
|
|
43
|
+
_context.next = 8;
|
|
44
|
+
break;
|
|
45
|
+
case 6:
|
|
46
|
+
_context.prev = 6;
|
|
47
|
+
_context.t0 = _context["catch"](0);
|
|
48
|
+
case 8:
|
|
49
|
+
case "end":
|
|
50
|
+
return _context.stop();
|
|
51
|
+
}
|
|
52
|
+
}, _callee, null, [[0, 6]]);
|
|
53
|
+
}));
|
|
54
|
+
return function send() {
|
|
55
|
+
return _ref.apply(this, arguments);
|
|
56
|
+
};
|
|
57
|
+
}();
|
|
58
|
+
return /*#__PURE__*/React.createElement("div", {
|
|
59
|
+
style: {
|
|
60
|
+
margin: '12px 0',
|
|
61
|
+
gap: '0 12px',
|
|
62
|
+
display: 'flex'
|
|
63
|
+
}
|
|
64
|
+
}, /*#__PURE__*/React.createElement("button", {
|
|
65
|
+
onClick: send
|
|
66
|
+
}, "\u53D1\u9001\u8BF7\u6C42\uFF08\u6253\u5F00\u63A7\u5236\u53F0\u67E5\u770B\u62E6\u622A\u5668\u8F93\u51FA\uFF09"));
|
|
67
|
+
};
|
|
68
|
+
export default Demo;
|
|
@@ -0,0 +1,65 @@
|
|
|
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
|
+
import { Env, storage } from '@wapri/gbase';
|
|
6
|
+
import { Hermes } from "./..";
|
|
7
|
+
var hermes = new Hermes({
|
|
8
|
+
env: Env.DEV,
|
|
9
|
+
retry: {
|
|
10
|
+
num: 2
|
|
11
|
+
},
|
|
12
|
+
onError: function onError(error) {}
|
|
13
|
+
});
|
|
14
|
+
var Demo = function Demo() {
|
|
15
|
+
var getTicket = /*#__PURE__*/function () {
|
|
16
|
+
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
17
|
+
var res;
|
|
18
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
19
|
+
while (1) switch (_context.prev = _context.next) {
|
|
20
|
+
case 0:
|
|
21
|
+
_context.next = 2;
|
|
22
|
+
return hermes.modules.auth.getTicket();
|
|
23
|
+
case 2:
|
|
24
|
+
res = _context.sent;
|
|
25
|
+
window.ticket = res === null || res === void 0 ? void 0 : res.ticket;
|
|
26
|
+
case 4:
|
|
27
|
+
case "end":
|
|
28
|
+
return _context.stop();
|
|
29
|
+
}
|
|
30
|
+
}, _callee);
|
|
31
|
+
}));
|
|
32
|
+
return function getTicket() {
|
|
33
|
+
return _ref.apply(this, arguments);
|
|
34
|
+
};
|
|
35
|
+
}();
|
|
36
|
+
var ticketGetToken = function ticketGetToken() {
|
|
37
|
+
hermes.modules.auth.ticketGetToken({
|
|
38
|
+
ticket: window.ticket
|
|
39
|
+
});
|
|
40
|
+
};
|
|
41
|
+
var tosGetSts = function tosGetSts() {
|
|
42
|
+
hermes.modules.file.tosGetSts();
|
|
43
|
+
};
|
|
44
|
+
return /*#__PURE__*/React.createElement("div", {
|
|
45
|
+
style: {
|
|
46
|
+
margin: '12px 0',
|
|
47
|
+
gap: '0 12px',
|
|
48
|
+
display: 'flex'
|
|
49
|
+
}
|
|
50
|
+
}, "token: ", /*#__PURE__*/React.createElement("textarea", {
|
|
51
|
+
id: "demo-token"
|
|
52
|
+
}), /*#__PURE__*/React.createElement("button", {
|
|
53
|
+
onClick: function onClick() {
|
|
54
|
+
var _document$querySelect;
|
|
55
|
+
storage.setItem('Admin-Token', (_document$querySelect = document.querySelector('#demo-token')) === null || _document$querySelect === void 0 ? void 0 : _document$querySelect.value);
|
|
56
|
+
}
|
|
57
|
+
}, "\u8BBE\u7F6Etoken"), /*#__PURE__*/React.createElement("button", {
|
|
58
|
+
onClick: getTicket
|
|
59
|
+
}, "\u83B7\u53D6ticket"), /*#__PURE__*/React.createElement("button", {
|
|
60
|
+
onClick: ticketGetToken
|
|
61
|
+
}, "\u83B7\u53D6token"), /*#__PURE__*/React.createElement("button", {
|
|
62
|
+
onClick: tosGetSts
|
|
63
|
+
}, "\u83B7\u53D6tos sts"));
|
|
64
|
+
};
|
|
65
|
+
export default Demo;
|
package/dist/hermes.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { AxiosInstance, AxiosRequestConfig } from 'axios';
|
|
2
|
+
import { HermesBaseConfig } from './types';
|
|
3
|
+
import HermesBase from './base';
|
|
4
|
+
import { HermesModuleAuth } from './modules/auth';
|
|
5
|
+
import { HermesModuleFile } from './modules/file';
|
|
6
|
+
export declare class Hermes extends HermesBase {
|
|
7
|
+
modules: {
|
|
8
|
+
auth: HermesModuleAuth;
|
|
9
|
+
file: HermesModuleFile;
|
|
10
|
+
};
|
|
11
|
+
constructor(config: HermesBaseConfig);
|
|
12
|
+
/**
|
|
13
|
+
* 以 blob 方式下载文件,返回 Blob 由调用方保存(不引入 file-saver,保持无 UI 依赖)。
|
|
14
|
+
* 后端以 JSON 返回业务错误时,按错误码解析并 reject(错误已带 __notified 标记)。
|
|
15
|
+
*
|
|
16
|
+
* @param url 下载接口地址
|
|
17
|
+
* @param params 请求参数(表单)
|
|
18
|
+
* @param config 透传给 axios 的额外配置
|
|
19
|
+
*/
|
|
20
|
+
download(url: string, params?: Record<string, unknown>, config?: AxiosRequestConfig): Promise<Blob>;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* 创建 Hermes 请求实例,直接返回 Axios 实例。
|
|
24
|
+
*/
|
|
25
|
+
export declare const createHermes: (config: HermesBaseConfig) => AxiosInstance;
|