rxtutils 1.1.3 → 1.1.4-beta.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/cjs/_utils/deepAssign.cjs +25 -0
- package/cjs/hooks/index.cjs +7 -0
- package/cjs/hooks/index.d.ts +1 -0
- package/cjs/hooks/useCombineControlValue.cjs +43 -0
- package/cjs/hooks/useCombineControlValue.d.ts +21 -0
- package/cjs/index.cjs +2 -0
- package/cjs/index.d.ts +1 -0
- package/cjs/request/index.cjs +132 -97
- package/cjs/request/index.d.ts +8 -0
- package/cjs/validator/validator.cjs +2 -2
- package/es/_utils/deepAssign.mjs +23 -0
- package/es/hooks/index.d.ts +1 -0
- package/es/hooks/index.mjs +1 -0
- package/es/hooks/useCombineControlValue.d.ts +21 -0
- package/es/hooks/useCombineControlValue.mjs +41 -0
- package/es/index.d.ts +1 -0
- package/es/index.mjs +1 -0
- package/es/request/index.d.ts +8 -0
- package/es/request/index.mjs +132 -97
- package/es/validator/validator.mjs +2 -2
- package/package.json +2 -2
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
function deepAssign(target) {
|
|
4
|
+
var sources = [];
|
|
5
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
6
|
+
sources[_i - 1] = arguments[_i];
|
|
7
|
+
}
|
|
8
|
+
for (var _a = 0, sources_1 = sources; _a < sources_1.length; _a++) {
|
|
9
|
+
var source = sources_1[_a];
|
|
10
|
+
for (var key in source) {
|
|
11
|
+
var val = source[key];
|
|
12
|
+
if (val &&
|
|
13
|
+
typeof val === 'object' &&
|
|
14
|
+
!Array.isArray(val)) {
|
|
15
|
+
target[key] = deepAssign(target[key] || {}, val);
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
target[key] = val;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return target;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
exports.deepAssign = deepAssign;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useCombineControlValue } from './useCombineControlValue.js';
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var react = require('react');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @param param props 组件属性
|
|
7
|
+
* @param param valueKey 组件值的key,默认value, undefined 被认为是有值,只有当 key 不存在时,转换为非受控模式
|
|
8
|
+
* @param param defaultValue 默认值,当 value 和 defaultValue 同时存在时,以 value 为默认值
|
|
9
|
+
* @param param onChange 值改变时的回调
|
|
10
|
+
* @returns value: 组件应该采用的值,onChange:值改变时的回调,处理非受控值与向父组件传递值的逻辑
|
|
11
|
+
*/
|
|
12
|
+
function useCombineControlValue(_a, resolveFn) {
|
|
13
|
+
var props = _a.props, _b = _a.valueKey, valueKey = _b === void 0 ? 'value' : _b, defaultValue = _a.defaultValue, onChange = _a.onChange;
|
|
14
|
+
var _c = props, _d = valueKey, value = _c[_d];
|
|
15
|
+
var hasValue = Object.prototype.hasOwnProperty.call(props, valueKey);
|
|
16
|
+
var _e = react.useState(value !== null && value !== void 0 ? value : defaultValue), internalValue = _e[0], setInternalValue = _e[1];
|
|
17
|
+
var handleChange = react.useCallback(function () {
|
|
18
|
+
var params = [];
|
|
19
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
20
|
+
params[_i] = arguments[_i];
|
|
21
|
+
}
|
|
22
|
+
var realNextVal;
|
|
23
|
+
if (typeof resolveFn === 'function') {
|
|
24
|
+
realNextVal = resolveFn.apply(void 0, params);
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
realNextVal = params[0];
|
|
28
|
+
}
|
|
29
|
+
setInternalValue(realNextVal);
|
|
30
|
+
onChange === null || onChange === void 0 ? void 0 : onChange.apply(void 0, params);
|
|
31
|
+
}, [onChange, resolveFn]);
|
|
32
|
+
var finalValue = react.useMemo(function () {
|
|
33
|
+
if (hasValue)
|
|
34
|
+
return value;
|
|
35
|
+
return internalValue;
|
|
36
|
+
}, [hasValue, internalValue, value]);
|
|
37
|
+
return {
|
|
38
|
+
value: finalValue,
|
|
39
|
+
onChange: handleChange
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
exports.useCombineControlValue = useCombineControlValue;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
type UseCombineControlValueOptions<V> = {
|
|
2
|
+
props: Record<string, any>;
|
|
3
|
+
valueKey?: string;
|
|
4
|
+
defaultValue?: V;
|
|
5
|
+
onChange?: (val: V) => void;
|
|
6
|
+
};
|
|
7
|
+
type UseCombineControlValueEasyResult<V> = {
|
|
8
|
+
value: V;
|
|
9
|
+
onChange: (nextVal: V) => void;
|
|
10
|
+
};
|
|
11
|
+
type UseCombineControlValueResolveResult<V, R extends (...args: any[]) => any> = {
|
|
12
|
+
value: V;
|
|
13
|
+
onChange: (...args: Parameters<R>) => void;
|
|
14
|
+
};
|
|
15
|
+
declare function useCombineControlValue<V>(options: UseCombineControlValueOptions<V>): UseCombineControlValueEasyResult<V>;
|
|
16
|
+
declare function useCombineControlValue<V, R extends (...args: any[]) => any>(options: Omit<UseCombineControlValueOptions<V>, 'onChange'> & {
|
|
17
|
+
onChange?: (...args: Parameters<R>) => void;
|
|
18
|
+
}, resolveFn: (...args: Parameters<R>) => V): UseCombineControlValueResolveResult<V, R>;
|
|
19
|
+
|
|
20
|
+
export { useCombineControlValue };
|
|
21
|
+
export type { UseCombineControlValueEasyResult, UseCombineControlValueOptions, UseCombineControlValueResolveResult };
|
package/cjs/index.cjs
CHANGED
|
@@ -6,6 +6,7 @@ var index$3 = require('./store/createGetter/index.cjs');
|
|
|
6
6
|
var index$2 = require('./store/createStateStore/index.cjs');
|
|
7
7
|
var validator = require('./validator/validator.cjs');
|
|
8
8
|
var decorators = require('./validator/decorators.cjs');
|
|
9
|
+
var useCombineControlValue = require('./hooks/useCombineControlValue.cjs');
|
|
9
10
|
|
|
10
11
|
|
|
11
12
|
|
|
@@ -26,3 +27,4 @@ exports.VNumber = decorators.VNumber;
|
|
|
26
27
|
exports.VPattern = decorators.VPattern;
|
|
27
28
|
exports.VRequired = decorators.VRequired;
|
|
28
29
|
exports.VString = decorators.VString;
|
|
30
|
+
exports.useCombineControlValue = useCombineControlValue.useCombineControlValue;
|
package/cjs/index.d.ts
CHANGED
|
@@ -4,4 +4,5 @@ export { createStoreGetter, createStoreGetterMemo } from './store/createGetter/i
|
|
|
4
4
|
export { IHookStateInitAction, IHookStateInitialSetter, IHookStateResolvable, IHookStateSetAction, IHookStateSetter, default as createStateStore } from './store/createStateStore/index.js';
|
|
5
5
|
export { BaseValidator } from './validator/validator.js';
|
|
6
6
|
export { VArray, VBoolean, VEmail, VMax, VMaxLength, VMin, VMinLength, VNumber, VPattern, VRequired, VString } from './validator/decorators.js';
|
|
7
|
+
export { useCombineControlValue } from './hooks/useCombineControlValue.js';
|
|
7
8
|
export { default as RequestError, RequestErrorType } from './request/error.js';
|
package/cjs/request/index.cjs
CHANGED
|
@@ -7,6 +7,7 @@ var defaultHandlers = require('./defaultHandlers.cjs');
|
|
|
7
7
|
var defaultEquals = require('../_utils/defaultEquals.cjs');
|
|
8
8
|
var index = require('../cache/index.cjs');
|
|
9
9
|
var error = require('./error.cjs');
|
|
10
|
+
var deepAssign = require('../_utils/deepAssign.cjs');
|
|
10
11
|
|
|
11
12
|
/**
|
|
12
13
|
* 创建基础请求实例
|
|
@@ -33,112 +34,146 @@ function createBaseRequest(baseOptions) {
|
|
|
33
34
|
var _b = tslib.__assign(tslib.__assign({}, baseOptions), createOptions), baseURL = _b.baseURL, _c = _b.cacheDataKey, cacheDataKey = _c === void 0 ? "".concat(method, ":").concat(baseURL).concat(url) : _c, cacheDataInStorage = _b.cacheDataInStorage, _d = _b.cacheKeyEquals, cacheKeyEquals = _d === void 0 ? defaultEquals : _d, cacheTime = _b.cacheTime, _e = _b.indexDBName, indexDBName = _e === void 0 ? "__apiCacheDatabase__" : _e;
|
|
34
35
|
var cache = new index.default(cacheDataInStorage, cacheDataKey, cacheTime, indexDBName, cacheKeyEquals);
|
|
35
36
|
function request(requestParam, options) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
var _e = tslib.__assign(tslib.__assign(tslib.__assign({}, baseOptions), createOptions), options), _f = _e.enableCache, enableCache = _f === void 0 ? false : _f, _g = _e.cacheData, cacheData = _g === void 0 ? false : _g, _h = _e.defaultErrorCodeHandler, defaultErrorCodeHandler = _h === void 0 ? defaultHandlers._defaultErrorCodeHandler.bind(null, defaultMessageShower) : _h, _j = _e.defaultHttpErrorCodeHandler, defaultHttpErrorCodeHandler = _j === void 0 ? defaultHandlers._defaultHttpErrorCodeHandler.bind(null, defaultMessageShower) : _j, _k = _e.otherErrorHandler, otherErrorHandler = _k === void 0 ? defaultHandlers._defaultOtherErrorCodeHandler.bind(null, defaultMessageShower) : _k, _l = _e.errorCodePath, errorCodePath = _l === void 0 ? "code" : _l, _m = _e.cacheTime, cacheTime = _m === void 0 ? 60 : _m, _o = _e.errorCodeMap, errorCodeMap = _o === void 0 ? {} : _o, _p = _e.successCodes, successCodes = _p === void 0 ? ["0", "200"] : _p, _q = _e.httpErrorCodeMap, httpErrorCodeMap = _q === void 0 ? {} : _q, _r = _e.axiosOptions, axiosOptions = _r === void 0 ? {} : _r, _s = _e.throwError, throwError = _s === void 0 ? true : _s;
|
|
44
|
-
if (enableCache) {
|
|
45
|
-
var cacheItem = cache.getCache(requestDataOrParams);
|
|
46
|
-
if (cacheItem) {
|
|
47
|
-
return Promise.resolve(cacheItem);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
return instance
|
|
51
|
-
.request(tslib.__assign({ method: method, url: url, data: data, params: params }, axiosOptions))
|
|
52
|
-
.then(function (res) { return tslib.__awaiter(_this, void 0, void 0, function () {
|
|
53
|
-
var errorCode, _a, _b, _c, customHandler, err, _d, _e, replaceResData, _f, handlerThrowError, _g;
|
|
54
|
-
return tslib.__generator(this, function (_h) {
|
|
55
|
-
switch (_h.label) {
|
|
37
|
+
return tslib.__awaiter(this, void 0, void 0, function () {
|
|
38
|
+
var _a, _b, requestMiddlewares, _c, finalAxiosOptions, finalRequestOptions, _i, requestMiddlewares_1, middleware, _d, nextAxiosOptions, nextRequestOptions, method, url, _e, data, _f, params, requestDataOrParams, _g, defaultMessageShower, _h, _j, enableCache, _k, cacheData, _l, defaultErrorCodeHandler, _m, defaultHttpErrorCodeHandler, _o, otherErrorHandler, _p, errorCodePath, _q, cacheTime, _r, errorCodeMap, _s, successCodes, _t, httpErrorCodeMap, _u,
|
|
39
|
+
// axiosOptions = {},
|
|
40
|
+
throwError, _v, retryTimes, cacheItem;
|
|
41
|
+
var _this = this;
|
|
42
|
+
return tslib.__generator(this, function (_w) {
|
|
43
|
+
switch (_w.label) {
|
|
56
44
|
case 0:
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
}
|
|
62
|
-
return [2 /*return*/, res.data];
|
|
63
|
-
}
|
|
64
|
-
_a = errorCodeMap, _b = errorCode, _c = _a[_b], customHandler = _c === void 0 ? defaultErrorCodeHandler : _c;
|
|
65
|
-
err = new error("服务端错误", "server", res);
|
|
66
|
-
if (!(typeof customHandler === "string")) return [3 /*break*/, 1];
|
|
67
|
-
defaultMessageShower(customHandler);
|
|
68
|
-
return [3 /*break*/, 3];
|
|
45
|
+
_a = tslib.__assign(tslib.__assign(tslib.__assign({}, baseOptions), createOptions), options), _b = _a.requestMiddlewares, requestMiddlewares = _b === void 0 ? [] : _b, _c = _a.axiosOptions, finalAxiosOptions = _c === void 0 ? {} : _c;
|
|
46
|
+
finalRequestOptions = tslib.__assign(tslib.__assign({}, requestOptions), requestParam);
|
|
47
|
+
_i = 0, requestMiddlewares_1 = requestMiddlewares;
|
|
48
|
+
_w.label = 1;
|
|
69
49
|
case 1:
|
|
70
|
-
|
|
71
|
-
|
|
50
|
+
if (!(_i < requestMiddlewares_1.length)) return [3 /*break*/, 4];
|
|
51
|
+
middleware = requestMiddlewares_1[_i];
|
|
52
|
+
return [4 /*yield*/, middleware(finalAxiosOptions, finalRequestOptions)];
|
|
72
53
|
case 2:
|
|
73
|
-
_d =
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
throw err;
|
|
78
|
-
case false:
|
|
79
|
-
return [2 /*return*/, res.data];
|
|
80
|
-
}
|
|
81
|
-
_h.label = 3;
|
|
54
|
+
_d = _w.sent(), nextAxiosOptions = _d.axiosOptions, nextRequestOptions = _d.requestOptions;
|
|
55
|
+
deepAssign.deepAssign({}, finalAxiosOptions, nextAxiosOptions);
|
|
56
|
+
deepAssign.deepAssign({}, finalRequestOptions, nextRequestOptions);
|
|
57
|
+
_w.label = 3;
|
|
82
58
|
case 3:
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
}
|
|
86
|
-
return [2 /*return*/, res.data];
|
|
87
|
-
}
|
|
88
|
-
});
|
|
89
|
-
}); }, function (error$1) { return tslib.__awaiter(_this, void 0, void 0, function () {
|
|
90
|
-
var resData, _a, _b, _c, customHandler, err, _d, _e, replaceResData, _f, handlerThrowError, _g, resData, err, _h, _j, replaceResData, _k, handlerThrowError, _l;
|
|
91
|
-
return tslib.__generator(this, function (_m) {
|
|
92
|
-
switch (_m.label) {
|
|
93
|
-
case 0:
|
|
94
|
-
if (!error$1.response) return [3 /*break*/, 4];
|
|
95
|
-
resData = error$1;
|
|
96
|
-
_a = httpErrorCodeMap, _b = error$1.response.status, _c = _a[_b], customHandler = _c === void 0 ? defaultHttpErrorCodeHandler : _c;
|
|
97
|
-
err = new error("服务端错误", "http", error$1);
|
|
98
|
-
if (!(typeof customHandler === "string")) return [3 /*break*/, 1];
|
|
99
|
-
defaultMessageShower(customHandler);
|
|
100
|
-
return [3 /*break*/, 3];
|
|
101
|
-
case 1:
|
|
102
|
-
_g = Object;
|
|
103
|
-
return [4 /*yield*/, customHandler(error$1.response.status, error$1, tslib.__assign(tslib.__assign({}, requestOptions), requestParam))];
|
|
104
|
-
case 2:
|
|
105
|
-
_d = _g.apply(void 0, [(_m.sent())]), _e = _d.replaceResData, replaceResData = _e === void 0 ? error$1 : _e, _f = _d.throwError, handlerThrowError = _f === void 0 ? "default" : _f;
|
|
106
|
-
resData = replaceResData;
|
|
107
|
-
switch (handlerThrowError) {
|
|
108
|
-
case true:
|
|
109
|
-
throw err;
|
|
110
|
-
case false:
|
|
111
|
-
return [2 /*return*/, resData];
|
|
112
|
-
}
|
|
113
|
-
_m.label = 3;
|
|
114
|
-
case 3:
|
|
115
|
-
if (throwError) {
|
|
116
|
-
throw err;
|
|
117
|
-
}
|
|
118
|
-
return [2 /*return*/, resData];
|
|
59
|
+
_i++;
|
|
60
|
+
return [3 /*break*/, 1];
|
|
119
61
|
case 4:
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
_l = Object;
|
|
125
|
-
return [4 /*yield*/, otherErrorHandler(error$1)];
|
|
126
|
-
case 5:
|
|
127
|
-
_h = (_l.apply(void 0, [(_m.sent())])), _j = _h.replaceResData, replaceResData = _j === void 0 ? error$1 : _j, _k = _h.throwError, handlerThrowError = _k === void 0 ? "default" : _k;
|
|
128
|
-
resData = replaceResData;
|
|
129
|
-
switch (handlerThrowError) {
|
|
130
|
-
case true:
|
|
131
|
-
throw err;
|
|
132
|
-
case false:
|
|
133
|
-
return [2 /*return*/, resData];
|
|
62
|
+
method = finalRequestOptions.method, url = finalRequestOptions.url, _e = finalRequestOptions.data, data = _e === void 0 ? {} : _e, _f = finalRequestOptions.params, params = _f === void 0 ? {} : _f;
|
|
63
|
+
requestDataOrParams = params;
|
|
64
|
+
if (method.toLowerCase() === "post") {
|
|
65
|
+
requestDataOrParams = data;
|
|
134
66
|
}
|
|
135
|
-
|
|
136
|
-
|
|
67
|
+
_g = tslib.__assign(tslib.__assign(tslib.__assign({}, baseOptions), createOptions), options).defaultMessageShower, defaultMessageShower = _g === void 0 ? console.error : _g;
|
|
68
|
+
_h = tslib.__assign(tslib.__assign(tslib.__assign({}, baseOptions), createOptions), options), _j = _h.enableCache, enableCache = _j === void 0 ? false : _j, _k = _h.cacheData, cacheData = _k === void 0 ? false : _k, _l = _h.defaultErrorCodeHandler, defaultErrorCodeHandler = _l === void 0 ? defaultHandlers._defaultErrorCodeHandler.bind(null, defaultMessageShower) : _l, _m = _h.defaultHttpErrorCodeHandler, defaultHttpErrorCodeHandler = _m === void 0 ? defaultHandlers._defaultHttpErrorCodeHandler.bind(null, defaultMessageShower) : _m, _o = _h.otherErrorHandler, otherErrorHandler = _o === void 0 ? defaultHandlers._defaultOtherErrorCodeHandler.bind(null, defaultMessageShower) : _o, _p = _h.errorCodePath, errorCodePath = _p === void 0 ? "code" : _p, _q = _h.cacheTime, cacheTime = _q === void 0 ? 60 : _q, _r = _h.errorCodeMap, errorCodeMap = _r === void 0 ? {} : _r, _s = _h.successCodes, successCodes = _s === void 0 ? ["0", "200"] : _s, _t = _h.httpErrorCodeMap, httpErrorCodeMap = _t === void 0 ? {} : _t, _u = _h.throwError, throwError = _u === void 0 ? true : _u, _v = _h.retryTimes, retryTimes = _v === void 0 ? 0 : _v;
|
|
69
|
+
if (enableCache) {
|
|
70
|
+
cacheItem = cache.getCache(requestDataOrParams);
|
|
71
|
+
if (cacheItem) {
|
|
72
|
+
return [2 /*return*/, Promise.resolve(cacheItem)];
|
|
73
|
+
}
|
|
137
74
|
}
|
|
138
|
-
return [2 /*return*/,
|
|
75
|
+
return [2 /*return*/, instance
|
|
76
|
+
.request(tslib.__assign({ method: method, url: url, data: data, params: params }, finalAxiosOptions))
|
|
77
|
+
.then(function (res) { return tslib.__awaiter(_this, void 0, void 0, function () {
|
|
78
|
+
var errorCode, _a, _b, _c, customHandler, err, _d, _e, replaceResData, _f, handlerThrowError, _g;
|
|
79
|
+
return tslib.__generator(this, function (_h) {
|
|
80
|
+
switch (_h.label) {
|
|
81
|
+
case 0:
|
|
82
|
+
errorCode = String(at(res.data, errorCodePath));
|
|
83
|
+
if (successCodes.includes(errorCode)) {
|
|
84
|
+
if (cacheData) {
|
|
85
|
+
cache.setCache(requestDataOrParams, res.data, { cacheTime: cacheTime });
|
|
86
|
+
}
|
|
87
|
+
return [2 /*return*/, res.data];
|
|
88
|
+
}
|
|
89
|
+
// 不在成功 code 中,意味着请求失败
|
|
90
|
+
if (retryTimes > 0) {
|
|
91
|
+
return [2 /*return*/, request(requestParam, tslib.__assign(tslib.__assign({}, options), { retryTimes: retryTimes - 1 }))];
|
|
92
|
+
}
|
|
93
|
+
_a = errorCodeMap, _b = errorCode, _c = _a[_b], customHandler = _c === void 0 ? defaultErrorCodeHandler : _c;
|
|
94
|
+
err = new error("服务端错误", "server", res);
|
|
95
|
+
if (!(typeof customHandler === "string")) return [3 /*break*/, 1];
|
|
96
|
+
defaultMessageShower(customHandler);
|
|
97
|
+
return [3 /*break*/, 3];
|
|
98
|
+
case 1:
|
|
99
|
+
_g = Object;
|
|
100
|
+
return [4 /*yield*/, customHandler(errorCode, res.data, res, tslib.__assign(tslib.__assign({}, requestOptions), requestParam))];
|
|
101
|
+
case 2:
|
|
102
|
+
_d = _g.apply(void 0, [(_h.sent())]), _e = _d.replaceResData, replaceResData = _e === void 0 ? res.data : _e, _f = _d.throwError, handlerThrowError = _f === void 0 ? "default" : _f;
|
|
103
|
+
res.data = replaceResData;
|
|
104
|
+
switch (handlerThrowError) {
|
|
105
|
+
case true:
|
|
106
|
+
throw err;
|
|
107
|
+
case false:
|
|
108
|
+
return [2 /*return*/, res.data];
|
|
109
|
+
}
|
|
110
|
+
_h.label = 3;
|
|
111
|
+
case 3:
|
|
112
|
+
if (throwError) {
|
|
113
|
+
throw err;
|
|
114
|
+
}
|
|
115
|
+
return [2 /*return*/, res.data];
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}); }, function (error$1) { return tslib.__awaiter(_this, void 0, void 0, function () {
|
|
119
|
+
var resData, _a, _b, _c, customHandler, err, _d, _e, replaceResData, _f, handlerThrowError, _g, resData, err, _h, _j, replaceResData, _k, handlerThrowError, _l;
|
|
120
|
+
return tslib.__generator(this, function (_m) {
|
|
121
|
+
switch (_m.label) {
|
|
122
|
+
case 0:
|
|
123
|
+
if (retryTimes > 0) {
|
|
124
|
+
return [2 /*return*/, request(requestParam, tslib.__assign(tslib.__assign({}, options), { retryTimes: retryTimes - 1 }))];
|
|
125
|
+
}
|
|
126
|
+
if (!error$1.response) return [3 /*break*/, 4];
|
|
127
|
+
resData = error$1;
|
|
128
|
+
_a = httpErrorCodeMap, _b = error$1.response.status, _c = _a[_b], customHandler = _c === void 0 ? defaultHttpErrorCodeHandler : _c;
|
|
129
|
+
err = new error("服务端错误", "http", error$1);
|
|
130
|
+
if (!(typeof customHandler === "string")) return [3 /*break*/, 1];
|
|
131
|
+
defaultMessageShower(customHandler);
|
|
132
|
+
return [3 /*break*/, 3];
|
|
133
|
+
case 1:
|
|
134
|
+
_g = Object;
|
|
135
|
+
return [4 /*yield*/, customHandler(error$1.response.status, error$1, tslib.__assign(tslib.__assign({}, requestOptions), requestParam))];
|
|
136
|
+
case 2:
|
|
137
|
+
_d = _g.apply(void 0, [(_m.sent())]), _e = _d.replaceResData, replaceResData = _e === void 0 ? error$1 : _e, _f = _d.throwError, handlerThrowError = _f === void 0 ? "default" : _f;
|
|
138
|
+
resData = replaceResData;
|
|
139
|
+
switch (handlerThrowError) {
|
|
140
|
+
case true:
|
|
141
|
+
throw err;
|
|
142
|
+
case false:
|
|
143
|
+
return [2 /*return*/, resData];
|
|
144
|
+
}
|
|
145
|
+
_m.label = 3;
|
|
146
|
+
case 3:
|
|
147
|
+
if (throwError) {
|
|
148
|
+
throw err;
|
|
149
|
+
}
|
|
150
|
+
return [2 /*return*/, resData];
|
|
151
|
+
case 4:
|
|
152
|
+
resData = error$1;
|
|
153
|
+
err = new error("服务端错误", "http", error$1);
|
|
154
|
+
err.type = "http";
|
|
155
|
+
err.data = error$1;
|
|
156
|
+
_l = Object;
|
|
157
|
+
return [4 /*yield*/, otherErrorHandler(error$1)];
|
|
158
|
+
case 5:
|
|
159
|
+
_h = (_l.apply(void 0, [(_m.sent())])), _j = _h.replaceResData, replaceResData = _j === void 0 ? error$1 : _j, _k = _h.throwError, handlerThrowError = _k === void 0 ? "default" : _k;
|
|
160
|
+
resData = replaceResData;
|
|
161
|
+
switch (handlerThrowError) {
|
|
162
|
+
case true:
|
|
163
|
+
throw err;
|
|
164
|
+
case false:
|
|
165
|
+
return [2 /*return*/, resData];
|
|
166
|
+
}
|
|
167
|
+
if (throwError) {
|
|
168
|
+
throw err;
|
|
169
|
+
}
|
|
170
|
+
return [2 /*return*/, resData];
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
}); })];
|
|
139
174
|
}
|
|
140
175
|
});
|
|
141
|
-
});
|
|
176
|
+
});
|
|
142
177
|
}
|
|
143
178
|
request.clearCache = function () {
|
|
144
179
|
cache.clear();
|
package/cjs/request/index.d.ts
CHANGED
|
@@ -111,6 +111,14 @@ interface Options<Params = any, Data = any> {
|
|
|
111
111
|
*/
|
|
112
112
|
otherErrorHandler?: (error: any) => Promise<ErrorHandlerReturnType<Data> | void>;
|
|
113
113
|
axiosOptions?: Omit<AxiosRequestConfig<Params>, "method" | "url" | "params" | "data">;
|
|
114
|
+
requestMiddlewares?: ((options: Options<Params, Data>, requestOptions: RequestOptions<Params>) => Promise<{
|
|
115
|
+
axiosOptions?: Options<Params, Data>["axiosOptions"];
|
|
116
|
+
requestOptions?: RequestOptions<Params>;
|
|
117
|
+
}> | {
|
|
118
|
+
axiosOptions: Options<Params, Data>["axiosOptions"];
|
|
119
|
+
requestOptions?: RequestOptions<Params>;
|
|
120
|
+
})[];
|
|
121
|
+
retryTimes?: number;
|
|
114
122
|
}
|
|
115
123
|
/**
|
|
116
124
|
* 请求参数接口
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
var tslib = require('tslib');
|
|
4
4
|
|
|
5
|
+
var KEY_SYMBOL = Symbol("key-description");
|
|
5
6
|
/**
|
|
6
7
|
* 基础验证器类
|
|
7
8
|
* 提供字段验证功能,可通过装饰器为类属性添加验证规则
|
|
@@ -13,8 +14,7 @@ var BaseValidator = /** @class */ (function () {
|
|
|
13
14
|
*/
|
|
14
15
|
function BaseValidator() {
|
|
15
16
|
/** 用于存储验证器映射的私有符号 */
|
|
16
|
-
this.__keySymbol =
|
|
17
|
-
this.__keySymbol = Symbol("key-description");
|
|
17
|
+
this.__keySymbol = KEY_SYMBOL;
|
|
18
18
|
this[this.__keySymbol] = {};
|
|
19
19
|
}
|
|
20
20
|
/**
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
function deepAssign(target) {
|
|
2
|
+
var sources = [];
|
|
3
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
4
|
+
sources[_i - 1] = arguments[_i];
|
|
5
|
+
}
|
|
6
|
+
for (var _a = 0, sources_1 = sources; _a < sources_1.length; _a++) {
|
|
7
|
+
var source = sources_1[_a];
|
|
8
|
+
for (var key in source) {
|
|
9
|
+
var val = source[key];
|
|
10
|
+
if (val &&
|
|
11
|
+
typeof val === 'object' &&
|
|
12
|
+
!Array.isArray(val)) {
|
|
13
|
+
target[key] = deepAssign(target[key] || {}, val);
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
target[key] = val;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return target;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export { deepAssign };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useCombineControlValue } from './useCombineControlValue.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useCombineControlValue } from './useCombineControlValue.mjs';
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
type UseCombineControlValueOptions<V> = {
|
|
2
|
+
props: Record<string, any>;
|
|
3
|
+
valueKey?: string;
|
|
4
|
+
defaultValue?: V;
|
|
5
|
+
onChange?: (val: V) => void;
|
|
6
|
+
};
|
|
7
|
+
type UseCombineControlValueEasyResult<V> = {
|
|
8
|
+
value: V;
|
|
9
|
+
onChange: (nextVal: V) => void;
|
|
10
|
+
};
|
|
11
|
+
type UseCombineControlValueResolveResult<V, R extends (...args: any[]) => any> = {
|
|
12
|
+
value: V;
|
|
13
|
+
onChange: (...args: Parameters<R>) => void;
|
|
14
|
+
};
|
|
15
|
+
declare function useCombineControlValue<V>(options: UseCombineControlValueOptions<V>): UseCombineControlValueEasyResult<V>;
|
|
16
|
+
declare function useCombineControlValue<V, R extends (...args: any[]) => any>(options: Omit<UseCombineControlValueOptions<V>, 'onChange'> & {
|
|
17
|
+
onChange?: (...args: Parameters<R>) => void;
|
|
18
|
+
}, resolveFn: (...args: Parameters<R>) => V): UseCombineControlValueResolveResult<V, R>;
|
|
19
|
+
|
|
20
|
+
export { useCombineControlValue };
|
|
21
|
+
export type { UseCombineControlValueEasyResult, UseCombineControlValueOptions, UseCombineControlValueResolveResult };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { useState, useCallback, useMemo } from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @param param props 组件属性
|
|
5
|
+
* @param param valueKey 组件值的key,默认value, undefined 被认为是有值,只有当 key 不存在时,转换为非受控模式
|
|
6
|
+
* @param param defaultValue 默认值,当 value 和 defaultValue 同时存在时,以 value 为默认值
|
|
7
|
+
* @param param onChange 值改变时的回调
|
|
8
|
+
* @returns value: 组件应该采用的值,onChange:值改变时的回调,处理非受控值与向父组件传递值的逻辑
|
|
9
|
+
*/
|
|
10
|
+
function useCombineControlValue(_a, resolveFn) {
|
|
11
|
+
var props = _a.props, _b = _a.valueKey, valueKey = _b === void 0 ? 'value' : _b, defaultValue = _a.defaultValue, onChange = _a.onChange;
|
|
12
|
+
var _c = props, _d = valueKey, value = _c[_d];
|
|
13
|
+
var hasValue = Object.prototype.hasOwnProperty.call(props, valueKey);
|
|
14
|
+
var _e = useState(value !== null && value !== void 0 ? value : defaultValue), internalValue = _e[0], setInternalValue = _e[1];
|
|
15
|
+
var handleChange = useCallback(function () {
|
|
16
|
+
var params = [];
|
|
17
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
18
|
+
params[_i] = arguments[_i];
|
|
19
|
+
}
|
|
20
|
+
var realNextVal;
|
|
21
|
+
if (typeof resolveFn === 'function') {
|
|
22
|
+
realNextVal = resolveFn.apply(void 0, params);
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
realNextVal = params[0];
|
|
26
|
+
}
|
|
27
|
+
setInternalValue(realNextVal);
|
|
28
|
+
onChange === null || onChange === void 0 ? void 0 : onChange.apply(void 0, params);
|
|
29
|
+
}, [onChange, resolveFn]);
|
|
30
|
+
var finalValue = useMemo(function () {
|
|
31
|
+
if (hasValue)
|
|
32
|
+
return value;
|
|
33
|
+
return internalValue;
|
|
34
|
+
}, [hasValue, internalValue, value]);
|
|
35
|
+
return {
|
|
36
|
+
value: finalValue,
|
|
37
|
+
onChange: handleChange
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export { useCombineControlValue };
|
package/es/index.d.ts
CHANGED
|
@@ -4,4 +4,5 @@ export { createStoreGetter, createStoreGetterMemo } from './store/createGetter/i
|
|
|
4
4
|
export { IHookStateInitAction, IHookStateInitialSetter, IHookStateResolvable, IHookStateSetAction, IHookStateSetter, default as createStateStore } from './store/createStateStore/index.js';
|
|
5
5
|
export { BaseValidator } from './validator/validator.js';
|
|
6
6
|
export { VArray, VBoolean, VEmail, VMax, VMaxLength, VMin, VMinLength, VNumber, VPattern, VRequired, VString } from './validator/decorators.js';
|
|
7
|
+
export { useCombineControlValue } from './hooks/useCombineControlValue.js';
|
|
7
8
|
export { default as RequestError, RequestErrorType } from './request/error.js';
|
package/es/index.mjs
CHANGED
|
@@ -4,3 +4,4 @@ export { createStoreGetter, createStoreGetterMemo } from './store/createGetter/i
|
|
|
4
4
|
export { default as createStateStore } from './store/createStateStore/index.mjs';
|
|
5
5
|
export { BaseValidator } from './validator/validator.mjs';
|
|
6
6
|
export { VArray, VBoolean, VEmail, VMax, VMaxLength, VMin, VMinLength, VNumber, VPattern, VRequired, VString } from './validator/decorators.mjs';
|
|
7
|
+
export { useCombineControlValue } from './hooks/useCombineControlValue.mjs';
|
package/es/request/index.d.ts
CHANGED
|
@@ -111,6 +111,14 @@ interface Options<Params = any, Data = any> {
|
|
|
111
111
|
*/
|
|
112
112
|
otherErrorHandler?: (error: any) => Promise<ErrorHandlerReturnType<Data> | void>;
|
|
113
113
|
axiosOptions?: Omit<AxiosRequestConfig<Params>, "method" | "url" | "params" | "data">;
|
|
114
|
+
requestMiddlewares?: ((options: Options<Params, Data>, requestOptions: RequestOptions<Params>) => Promise<{
|
|
115
|
+
axiosOptions?: Options<Params, Data>["axiosOptions"];
|
|
116
|
+
requestOptions?: RequestOptions<Params>;
|
|
117
|
+
}> | {
|
|
118
|
+
axiosOptions: Options<Params, Data>["axiosOptions"];
|
|
119
|
+
requestOptions?: RequestOptions<Params>;
|
|
120
|
+
})[];
|
|
121
|
+
retryTimes?: number;
|
|
114
122
|
}
|
|
115
123
|
/**
|
|
116
124
|
* 请求参数接口
|
package/es/request/index.mjs
CHANGED
|
@@ -5,6 +5,7 @@ import { _defaultErrorCodeHandler, _defaultHttpErrorCodeHandler, _defaultOtherEr
|
|
|
5
5
|
import defaultEquals from '../_utils/defaultEquals.mjs';
|
|
6
6
|
import Cache from '../cache/index.mjs';
|
|
7
7
|
import RequestError from './error.mjs';
|
|
8
|
+
import { deepAssign } from '../_utils/deepAssign.mjs';
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
11
|
* 创建基础请求实例
|
|
@@ -31,112 +32,146 @@ function createBaseRequest(baseOptions) {
|
|
|
31
32
|
var _b = __assign(__assign({}, baseOptions), createOptions), baseURL = _b.baseURL, _c = _b.cacheDataKey, cacheDataKey = _c === void 0 ? "".concat(method, ":").concat(baseURL).concat(url) : _c, cacheDataInStorage = _b.cacheDataInStorage, _d = _b.cacheKeyEquals, cacheKeyEquals = _d === void 0 ? defaultEquals : _d, cacheTime = _b.cacheTime, _e = _b.indexDBName, indexDBName = _e === void 0 ? "__apiCacheDatabase__" : _e;
|
|
32
33
|
var cache = new Cache(cacheDataInStorage, cacheDataKey, cacheTime, indexDBName, cacheKeyEquals);
|
|
33
34
|
function request(requestParam, options) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
var _e = __assign(__assign(__assign({}, baseOptions), createOptions), options), _f = _e.enableCache, enableCache = _f === void 0 ? false : _f, _g = _e.cacheData, cacheData = _g === void 0 ? false : _g, _h = _e.defaultErrorCodeHandler, defaultErrorCodeHandler = _h === void 0 ? _defaultErrorCodeHandler.bind(null, defaultMessageShower) : _h, _j = _e.defaultHttpErrorCodeHandler, defaultHttpErrorCodeHandler = _j === void 0 ? _defaultHttpErrorCodeHandler.bind(null, defaultMessageShower) : _j, _k = _e.otherErrorHandler, otherErrorHandler = _k === void 0 ? _defaultOtherErrorCodeHandler.bind(null, defaultMessageShower) : _k, _l = _e.errorCodePath, errorCodePath = _l === void 0 ? "code" : _l, _m = _e.cacheTime, cacheTime = _m === void 0 ? 60 : _m, _o = _e.errorCodeMap, errorCodeMap = _o === void 0 ? {} : _o, _p = _e.successCodes, successCodes = _p === void 0 ? ["0", "200"] : _p, _q = _e.httpErrorCodeMap, httpErrorCodeMap = _q === void 0 ? {} : _q, _r = _e.axiosOptions, axiosOptions = _r === void 0 ? {} : _r, _s = _e.throwError, throwError = _s === void 0 ? true : _s;
|
|
42
|
-
if (enableCache) {
|
|
43
|
-
var cacheItem = cache.getCache(requestDataOrParams);
|
|
44
|
-
if (cacheItem) {
|
|
45
|
-
return Promise.resolve(cacheItem);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
return instance
|
|
49
|
-
.request(__assign({ method: method, url: url, data: data, params: params }, axiosOptions))
|
|
50
|
-
.then(function (res) { return __awaiter(_this, void 0, void 0, function () {
|
|
51
|
-
var errorCode, _a, _b, _c, customHandler, err, _d, _e, replaceResData, _f, handlerThrowError, _g;
|
|
52
|
-
return __generator(this, function (_h) {
|
|
53
|
-
switch (_h.label) {
|
|
35
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
36
|
+
var _a, _b, requestMiddlewares, _c, finalAxiosOptions, finalRequestOptions, _i, requestMiddlewares_1, middleware, _d, nextAxiosOptions, nextRequestOptions, method, url, _e, data, _f, params, requestDataOrParams, _g, defaultMessageShower, _h, _j, enableCache, _k, cacheData, _l, defaultErrorCodeHandler, _m, defaultHttpErrorCodeHandler, _o, otherErrorHandler, _p, errorCodePath, _q, cacheTime, _r, errorCodeMap, _s, successCodes, _t, httpErrorCodeMap, _u,
|
|
37
|
+
// axiosOptions = {},
|
|
38
|
+
throwError, _v, retryTimes, cacheItem;
|
|
39
|
+
var _this = this;
|
|
40
|
+
return __generator(this, function (_w) {
|
|
41
|
+
switch (_w.label) {
|
|
54
42
|
case 0:
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
-
return [2 /*return*/, res.data];
|
|
61
|
-
}
|
|
62
|
-
_a = errorCodeMap, _b = errorCode, _c = _a[_b], customHandler = _c === void 0 ? defaultErrorCodeHandler : _c;
|
|
63
|
-
err = new RequestError("服务端错误", "server", res);
|
|
64
|
-
if (!(typeof customHandler === "string")) return [3 /*break*/, 1];
|
|
65
|
-
defaultMessageShower(customHandler);
|
|
66
|
-
return [3 /*break*/, 3];
|
|
43
|
+
_a = __assign(__assign(__assign({}, baseOptions), createOptions), options), _b = _a.requestMiddlewares, requestMiddlewares = _b === void 0 ? [] : _b, _c = _a.axiosOptions, finalAxiosOptions = _c === void 0 ? {} : _c;
|
|
44
|
+
finalRequestOptions = __assign(__assign({}, requestOptions), requestParam);
|
|
45
|
+
_i = 0, requestMiddlewares_1 = requestMiddlewares;
|
|
46
|
+
_w.label = 1;
|
|
67
47
|
case 1:
|
|
68
|
-
|
|
69
|
-
|
|
48
|
+
if (!(_i < requestMiddlewares_1.length)) return [3 /*break*/, 4];
|
|
49
|
+
middleware = requestMiddlewares_1[_i];
|
|
50
|
+
return [4 /*yield*/, middleware(finalAxiosOptions, finalRequestOptions)];
|
|
70
51
|
case 2:
|
|
71
|
-
_d =
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
throw err;
|
|
76
|
-
case false:
|
|
77
|
-
return [2 /*return*/, res.data];
|
|
78
|
-
}
|
|
79
|
-
_h.label = 3;
|
|
52
|
+
_d = _w.sent(), nextAxiosOptions = _d.axiosOptions, nextRequestOptions = _d.requestOptions;
|
|
53
|
+
deepAssign({}, finalAxiosOptions, nextAxiosOptions);
|
|
54
|
+
deepAssign({}, finalRequestOptions, nextRequestOptions);
|
|
55
|
+
_w.label = 3;
|
|
80
56
|
case 3:
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
}
|
|
84
|
-
return [2 /*return*/, res.data];
|
|
85
|
-
}
|
|
86
|
-
});
|
|
87
|
-
}); }, function (error) { return __awaiter(_this, void 0, void 0, function () {
|
|
88
|
-
var resData, _a, _b, _c, customHandler, err, _d, _e, replaceResData, _f, handlerThrowError, _g, resData, err, _h, _j, replaceResData, _k, handlerThrowError, _l;
|
|
89
|
-
return __generator(this, function (_m) {
|
|
90
|
-
switch (_m.label) {
|
|
91
|
-
case 0:
|
|
92
|
-
if (!error.response) return [3 /*break*/, 4];
|
|
93
|
-
resData = error;
|
|
94
|
-
_a = httpErrorCodeMap, _b = error.response.status, _c = _a[_b], customHandler = _c === void 0 ? defaultHttpErrorCodeHandler : _c;
|
|
95
|
-
err = new RequestError("服务端错误", "http", error);
|
|
96
|
-
if (!(typeof customHandler === "string")) return [3 /*break*/, 1];
|
|
97
|
-
defaultMessageShower(customHandler);
|
|
98
|
-
return [3 /*break*/, 3];
|
|
99
|
-
case 1:
|
|
100
|
-
_g = Object;
|
|
101
|
-
return [4 /*yield*/, customHandler(error.response.status, error, __assign(__assign({}, requestOptions), requestParam))];
|
|
102
|
-
case 2:
|
|
103
|
-
_d = _g.apply(void 0, [(_m.sent())]), _e = _d.replaceResData, replaceResData = _e === void 0 ? error : _e, _f = _d.throwError, handlerThrowError = _f === void 0 ? "default" : _f;
|
|
104
|
-
resData = replaceResData;
|
|
105
|
-
switch (handlerThrowError) {
|
|
106
|
-
case true:
|
|
107
|
-
throw err;
|
|
108
|
-
case false:
|
|
109
|
-
return [2 /*return*/, resData];
|
|
110
|
-
}
|
|
111
|
-
_m.label = 3;
|
|
112
|
-
case 3:
|
|
113
|
-
if (throwError) {
|
|
114
|
-
throw err;
|
|
115
|
-
}
|
|
116
|
-
return [2 /*return*/, resData];
|
|
57
|
+
_i++;
|
|
58
|
+
return [3 /*break*/, 1];
|
|
117
59
|
case 4:
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
_l = Object;
|
|
123
|
-
return [4 /*yield*/, otherErrorHandler(error)];
|
|
124
|
-
case 5:
|
|
125
|
-
_h = (_l.apply(void 0, [(_m.sent())])), _j = _h.replaceResData, replaceResData = _j === void 0 ? error : _j, _k = _h.throwError, handlerThrowError = _k === void 0 ? "default" : _k;
|
|
126
|
-
resData = replaceResData;
|
|
127
|
-
switch (handlerThrowError) {
|
|
128
|
-
case true:
|
|
129
|
-
throw err;
|
|
130
|
-
case false:
|
|
131
|
-
return [2 /*return*/, resData];
|
|
60
|
+
method = finalRequestOptions.method, url = finalRequestOptions.url, _e = finalRequestOptions.data, data = _e === void 0 ? {} : _e, _f = finalRequestOptions.params, params = _f === void 0 ? {} : _f;
|
|
61
|
+
requestDataOrParams = params;
|
|
62
|
+
if (method.toLowerCase() === "post") {
|
|
63
|
+
requestDataOrParams = data;
|
|
132
64
|
}
|
|
133
|
-
|
|
134
|
-
|
|
65
|
+
_g = __assign(__assign(__assign({}, baseOptions), createOptions), options).defaultMessageShower, defaultMessageShower = _g === void 0 ? console.error : _g;
|
|
66
|
+
_h = __assign(__assign(__assign({}, baseOptions), createOptions), options), _j = _h.enableCache, enableCache = _j === void 0 ? false : _j, _k = _h.cacheData, cacheData = _k === void 0 ? false : _k, _l = _h.defaultErrorCodeHandler, defaultErrorCodeHandler = _l === void 0 ? _defaultErrorCodeHandler.bind(null, defaultMessageShower) : _l, _m = _h.defaultHttpErrorCodeHandler, defaultHttpErrorCodeHandler = _m === void 0 ? _defaultHttpErrorCodeHandler.bind(null, defaultMessageShower) : _m, _o = _h.otherErrorHandler, otherErrorHandler = _o === void 0 ? _defaultOtherErrorCodeHandler.bind(null, defaultMessageShower) : _o, _p = _h.errorCodePath, errorCodePath = _p === void 0 ? "code" : _p, _q = _h.cacheTime, cacheTime = _q === void 0 ? 60 : _q, _r = _h.errorCodeMap, errorCodeMap = _r === void 0 ? {} : _r, _s = _h.successCodes, successCodes = _s === void 0 ? ["0", "200"] : _s, _t = _h.httpErrorCodeMap, httpErrorCodeMap = _t === void 0 ? {} : _t, _u = _h.throwError, throwError = _u === void 0 ? true : _u, _v = _h.retryTimes, retryTimes = _v === void 0 ? 0 : _v;
|
|
67
|
+
if (enableCache) {
|
|
68
|
+
cacheItem = cache.getCache(requestDataOrParams);
|
|
69
|
+
if (cacheItem) {
|
|
70
|
+
return [2 /*return*/, Promise.resolve(cacheItem)];
|
|
71
|
+
}
|
|
135
72
|
}
|
|
136
|
-
return [2 /*return*/,
|
|
73
|
+
return [2 /*return*/, instance
|
|
74
|
+
.request(__assign({ method: method, url: url, data: data, params: params }, finalAxiosOptions))
|
|
75
|
+
.then(function (res) { return __awaiter(_this, void 0, void 0, function () {
|
|
76
|
+
var errorCode, _a, _b, _c, customHandler, err, _d, _e, replaceResData, _f, handlerThrowError, _g;
|
|
77
|
+
return __generator(this, function (_h) {
|
|
78
|
+
switch (_h.label) {
|
|
79
|
+
case 0:
|
|
80
|
+
errorCode = String(at(res.data, errorCodePath));
|
|
81
|
+
if (successCodes.includes(errorCode)) {
|
|
82
|
+
if (cacheData) {
|
|
83
|
+
cache.setCache(requestDataOrParams, res.data, { cacheTime: cacheTime });
|
|
84
|
+
}
|
|
85
|
+
return [2 /*return*/, res.data];
|
|
86
|
+
}
|
|
87
|
+
// 不在成功 code 中,意味着请求失败
|
|
88
|
+
if (retryTimes > 0) {
|
|
89
|
+
return [2 /*return*/, request(requestParam, __assign(__assign({}, options), { retryTimes: retryTimes - 1 }))];
|
|
90
|
+
}
|
|
91
|
+
_a = errorCodeMap, _b = errorCode, _c = _a[_b], customHandler = _c === void 0 ? defaultErrorCodeHandler : _c;
|
|
92
|
+
err = new RequestError("服务端错误", "server", res);
|
|
93
|
+
if (!(typeof customHandler === "string")) return [3 /*break*/, 1];
|
|
94
|
+
defaultMessageShower(customHandler);
|
|
95
|
+
return [3 /*break*/, 3];
|
|
96
|
+
case 1:
|
|
97
|
+
_g = Object;
|
|
98
|
+
return [4 /*yield*/, customHandler(errorCode, res.data, res, __assign(__assign({}, requestOptions), requestParam))];
|
|
99
|
+
case 2:
|
|
100
|
+
_d = _g.apply(void 0, [(_h.sent())]), _e = _d.replaceResData, replaceResData = _e === void 0 ? res.data : _e, _f = _d.throwError, handlerThrowError = _f === void 0 ? "default" : _f;
|
|
101
|
+
res.data = replaceResData;
|
|
102
|
+
switch (handlerThrowError) {
|
|
103
|
+
case true:
|
|
104
|
+
throw err;
|
|
105
|
+
case false:
|
|
106
|
+
return [2 /*return*/, res.data];
|
|
107
|
+
}
|
|
108
|
+
_h.label = 3;
|
|
109
|
+
case 3:
|
|
110
|
+
if (throwError) {
|
|
111
|
+
throw err;
|
|
112
|
+
}
|
|
113
|
+
return [2 /*return*/, res.data];
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
}); }, function (error) { return __awaiter(_this, void 0, void 0, function () {
|
|
117
|
+
var resData, _a, _b, _c, customHandler, err, _d, _e, replaceResData, _f, handlerThrowError, _g, resData, err, _h, _j, replaceResData, _k, handlerThrowError, _l;
|
|
118
|
+
return __generator(this, function (_m) {
|
|
119
|
+
switch (_m.label) {
|
|
120
|
+
case 0:
|
|
121
|
+
if (retryTimes > 0) {
|
|
122
|
+
return [2 /*return*/, request(requestParam, __assign(__assign({}, options), { retryTimes: retryTimes - 1 }))];
|
|
123
|
+
}
|
|
124
|
+
if (!error.response) return [3 /*break*/, 4];
|
|
125
|
+
resData = error;
|
|
126
|
+
_a = httpErrorCodeMap, _b = error.response.status, _c = _a[_b], customHandler = _c === void 0 ? defaultHttpErrorCodeHandler : _c;
|
|
127
|
+
err = new RequestError("服务端错误", "http", error);
|
|
128
|
+
if (!(typeof customHandler === "string")) return [3 /*break*/, 1];
|
|
129
|
+
defaultMessageShower(customHandler);
|
|
130
|
+
return [3 /*break*/, 3];
|
|
131
|
+
case 1:
|
|
132
|
+
_g = Object;
|
|
133
|
+
return [4 /*yield*/, customHandler(error.response.status, error, __assign(__assign({}, requestOptions), requestParam))];
|
|
134
|
+
case 2:
|
|
135
|
+
_d = _g.apply(void 0, [(_m.sent())]), _e = _d.replaceResData, replaceResData = _e === void 0 ? error : _e, _f = _d.throwError, handlerThrowError = _f === void 0 ? "default" : _f;
|
|
136
|
+
resData = replaceResData;
|
|
137
|
+
switch (handlerThrowError) {
|
|
138
|
+
case true:
|
|
139
|
+
throw err;
|
|
140
|
+
case false:
|
|
141
|
+
return [2 /*return*/, resData];
|
|
142
|
+
}
|
|
143
|
+
_m.label = 3;
|
|
144
|
+
case 3:
|
|
145
|
+
if (throwError) {
|
|
146
|
+
throw err;
|
|
147
|
+
}
|
|
148
|
+
return [2 /*return*/, resData];
|
|
149
|
+
case 4:
|
|
150
|
+
resData = error;
|
|
151
|
+
err = new RequestError("服务端错误", "http", error);
|
|
152
|
+
err.type = "http";
|
|
153
|
+
err.data = error;
|
|
154
|
+
_l = Object;
|
|
155
|
+
return [4 /*yield*/, otherErrorHandler(error)];
|
|
156
|
+
case 5:
|
|
157
|
+
_h = (_l.apply(void 0, [(_m.sent())])), _j = _h.replaceResData, replaceResData = _j === void 0 ? error : _j, _k = _h.throwError, handlerThrowError = _k === void 0 ? "default" : _k;
|
|
158
|
+
resData = replaceResData;
|
|
159
|
+
switch (handlerThrowError) {
|
|
160
|
+
case true:
|
|
161
|
+
throw err;
|
|
162
|
+
case false:
|
|
163
|
+
return [2 /*return*/, resData];
|
|
164
|
+
}
|
|
165
|
+
if (throwError) {
|
|
166
|
+
throw err;
|
|
167
|
+
}
|
|
168
|
+
return [2 /*return*/, resData];
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
}); })];
|
|
137
172
|
}
|
|
138
173
|
});
|
|
139
|
-
});
|
|
174
|
+
});
|
|
140
175
|
}
|
|
141
176
|
request.clearCache = function () {
|
|
142
177
|
cache.clear();
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { __spreadArray } from 'tslib';
|
|
2
2
|
|
|
3
|
+
var KEY_SYMBOL = Symbol("key-description");
|
|
3
4
|
/**
|
|
4
5
|
* 基础验证器类
|
|
5
6
|
* 提供字段验证功能,可通过装饰器为类属性添加验证规则
|
|
@@ -11,8 +12,7 @@ var BaseValidator = /** @class */ (function () {
|
|
|
11
12
|
*/
|
|
12
13
|
function BaseValidator() {
|
|
13
14
|
/** 用于存储验证器映射的私有符号 */
|
|
14
|
-
this.__keySymbol =
|
|
15
|
-
this.__keySymbol = Symbol("key-description");
|
|
15
|
+
this.__keySymbol = KEY_SYMBOL;
|
|
16
16
|
this[this.__keySymbol] = {};
|
|
17
17
|
}
|
|
18
18
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rxtutils",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.4-beta.10",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "cjs/index.cjs",
|
|
6
6
|
"module": "es/index.mjs",
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"build": "pnpm clean && rollup -c",
|
|
14
14
|
"clean": "rm -rf es cjs",
|
|
15
15
|
"publishNpm": "npm publish --registry https://registry.npmjs.org",
|
|
16
|
-
"
|
|
16
|
+
"loginNpm": "npm login --registry https://registry.npmjs.org"
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": {
|
|
19
19
|
"@rollup/plugin-typescript": "^12.1.2",
|