rxtutils 1.1.2-beta.2 → 1.1.2-beta.20
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 +144 -1
- package/{dist/cjs → cjs}/cache/index.cjs +28 -15
- package/{dist/types → cjs}/cache/index.d.ts +13 -7
- package/cjs/request/index.cjs +150 -0
- package/{dist/types → cjs}/request/index.d.ts +12 -8
- package/cjs/store/index.cjs +10 -0
- package/cjs/store/index.d.ts +2 -0
- package/cjs/validator/decorators.cjs +246 -0
- package/cjs/validator/decorators.d.ts +159 -0
- package/cjs/validator/index.cjs +19 -0
- package/cjs/validator/index.d.ts +2 -0
- package/{dist/cjs → cjs}/validator/validator.cjs +77 -12
- package/cjs/validator/validator.d.ts +84 -0
- package/es/cache/index.d.ts +141 -0
- package/{dist/es → es}/cache/index.mjs +28 -15
- package/es/cache/indexDB.d.ts +52 -0
- package/es/index.d.ts +7 -0
- package/es/request/error.d.ts +31 -0
- package/es/request/index.d.ts +140 -0
- package/es/request/index.mjs +148 -0
- package/es/store/createGetter/index.d.ts +30 -0
- package/es/store/createStateStore/index.d.ts +42 -0
- package/es/store/index.d.ts +2 -0
- package/es/store/index.mjs +2 -0
- package/es/validator/decorators.d.ts +159 -0
- package/es/validator/decorators.mjs +234 -0
- package/es/validator/index.d.ts +2 -0
- package/es/validator/index.mjs +2 -0
- package/es/validator/validator.d.ts +84 -0
- package/{dist/es → es}/validator/validator.mjs +77 -12
- package/package.json +12 -9
- package/dist/cjs/request/index.cjs +0 -129
- package/dist/cjs/validator/decorators.cjs +0 -104
- package/dist/es/request/index.mjs +0 -127
- package/dist/es/validator/decorators.mjs +0 -92
- package/dist/types/validator/decorators.d.ts +0 -15
- package/dist/types/validator/validator.d.ts +0 -24
- /package/{dist/cjs → cjs/_utils}/defaultEquals.cjs +0 -0
- /package/{dist/cjs → cjs}/cache/indexDB.cjs +0 -0
- /package/{dist/types → cjs}/cache/indexDB.d.ts +0 -0
- /package/{dist/cjs → cjs}/index.cjs +0 -0
- /package/{dist/types → cjs}/index.d.ts +0 -0
- /package/{dist/cjs → cjs}/request/defaultHandlers.cjs +0 -0
- /package/{dist/cjs → cjs}/request/error.cjs +0 -0
- /package/{dist/types → cjs}/request/error.d.ts +0 -0
- /package/{dist/cjs → cjs}/store/createGetter/index.cjs +0 -0
- /package/{dist/types → cjs}/store/createGetter/index.d.ts +0 -0
- /package/{dist/cjs → cjs}/store/createStateStore/index.cjs +0 -0
- /package/{dist/types → cjs}/store/createStateStore/index.d.ts +0 -0
- /package/{dist/es → es/_utils}/defaultEquals.mjs +0 -0
- /package/{dist/es → es}/cache/indexDB.mjs +0 -0
- /package/{dist/es → es}/index.mjs +0 -0
- /package/{dist/es → es}/request/defaultHandlers.mjs +0 -0
- /package/{dist/es → es}/request/error.mjs +0 -0
- /package/{dist/es → es}/store/createGetter/index.mjs +0 -0
- /package/{dist/es → es}/store/createStateStore/index.mjs +0 -0
package/package.json
CHANGED
|
@@ -1,20 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rxtutils",
|
|
3
|
-
"version": "1.1.2-beta.
|
|
3
|
+
"version": "1.1.2-beta.20",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"main": "
|
|
6
|
-
"module": "
|
|
7
|
-
"types": "
|
|
5
|
+
"main": "cjs/index.cjs",
|
|
6
|
+
"module": "es/index.mjs",
|
|
7
|
+
"types": "es/index.d.ts",
|
|
8
8
|
"files": [
|
|
9
|
-
"
|
|
9
|
+
"es",
|
|
10
|
+
"cjs"
|
|
10
11
|
],
|
|
11
12
|
"scripts": {
|
|
12
|
-
"build": "rollup -c",
|
|
13
|
-
"clean": "rm -rf
|
|
13
|
+
"build": "pnpm clean && rollup -c",
|
|
14
|
+
"clean": "rm -rf es cjs",
|
|
15
|
+
"publishNpm": "npm publish --registry https://registry.npmjs.org",
|
|
16
|
+
"login": "npm login --registry https://registry.npmjs.org"
|
|
14
17
|
},
|
|
15
18
|
"devDependencies": {
|
|
16
19
|
"@rollup/plugin-typescript": "^12.1.2",
|
|
17
|
-
"@types/lodash": "^4.17.
|
|
20
|
+
"@types/lodash-es": "^4.17.12",
|
|
18
21
|
"@types/node": "^22.15.29",
|
|
19
22
|
"@types/react": "^19.1.12",
|
|
20
23
|
"rollup": "^4.41.1",
|
|
@@ -27,7 +30,7 @@
|
|
|
27
30
|
},
|
|
28
31
|
"dependencies": {
|
|
29
32
|
"axios": "^1.9.0",
|
|
30
|
-
"lodash": "^4.17.21",
|
|
33
|
+
"lodash-es": "^4.17.21",
|
|
31
34
|
"moment": "^2.30.1",
|
|
32
35
|
"tslib": "^2.8.1"
|
|
33
36
|
}
|
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var tslib = require('tslib');
|
|
4
|
-
var axios = require('axios');
|
|
5
|
-
var lodash = require('lodash');
|
|
6
|
-
var defaultHandlers = require('./defaultHandlers.cjs');
|
|
7
|
-
var defaultEquals = require('../defaultEquals.cjs');
|
|
8
|
-
var index = require('../cache/index.cjs');
|
|
9
|
-
var error = require('./error.cjs');
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* 创建基础请求实例
|
|
13
|
-
* @param baseOptions 基础配置选项
|
|
14
|
-
* @returns 请求创建函数
|
|
15
|
-
*/
|
|
16
|
-
function createBaseRequest(baseOptions) {
|
|
17
|
-
var baseURL = Object(baseOptions).baseURL;
|
|
18
|
-
// 创建新的 Axios 实例并配置基础URL和跨域凭证
|
|
19
|
-
var instance = axios.create({
|
|
20
|
-
baseURL: baseURL,
|
|
21
|
-
withCredentials: true,
|
|
22
|
-
});
|
|
23
|
-
/**
|
|
24
|
-
* 创建请求函数
|
|
25
|
-
* @template Param 请求参数类型
|
|
26
|
-
* @template Data 响应数据类型,必须是对象类型
|
|
27
|
-
* @param requestOptions 请求配置
|
|
28
|
-
* @param createOptions 创建选项(覆盖基础配置)
|
|
29
|
-
* @returns 发送请求的函数
|
|
30
|
-
*/
|
|
31
|
-
return function createRequest(requestOptions, createOptions) {
|
|
32
|
-
var _a = tslib.__assign({}, requestOptions), method = _a.method, url = _a.url;
|
|
33
|
-
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
|
-
var cache = new index.default(cacheDataInStorage, cacheDataKey, cacheTime, indexDBName, cacheKeyEquals);
|
|
35
|
-
function request(requestParam, options) {
|
|
36
|
-
var _a = tslib.__assign(tslib.__assign({}, requestOptions), requestParam), method = _a.method, url = _a.url, _b = _a.data, data = _b === void 0 ? {} : _b, _c = _a.params, params = _c === void 0 ? {} : _c;
|
|
37
|
-
var requestDataOrParams = params;
|
|
38
|
-
if (method.toLowerCase() === 'post') {
|
|
39
|
-
requestDataOrParams = data;
|
|
40
|
-
}
|
|
41
|
-
var _d = tslib.__assign(tslib.__assign(tslib.__assign({}, baseOptions), createOptions), options).defaultMessageShower, defaultMessageShower = _d === void 0 ? console.error : _d;
|
|
42
|
-
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;
|
|
43
|
-
if (enableCache) {
|
|
44
|
-
var cacheItem = cache.getCache(requestDataOrParams);
|
|
45
|
-
if (cacheItem) {
|
|
46
|
-
return Promise.resolve(cacheItem);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
return instance
|
|
50
|
-
.request(tslib.__assign({ method: method, url: url, data: data, params: params }, axiosOptions))
|
|
51
|
-
.then(function (res) {
|
|
52
|
-
var errorCode = String(lodash.at(res.data, errorCodePath));
|
|
53
|
-
if (successCodes.includes(errorCode)) {
|
|
54
|
-
if (cacheData) {
|
|
55
|
-
cache.setCache(requestDataOrParams, res.data, { cacheTime: cacheTime });
|
|
56
|
-
}
|
|
57
|
-
return res.data;
|
|
58
|
-
}
|
|
59
|
-
var _a = errorCodeMap, _b = errorCode, _c = _a[_b], customHandler = _c === void 0 ? defaultErrorCodeHandler : _c;
|
|
60
|
-
var err = new error('服务端错误', 'server', res);
|
|
61
|
-
if (typeof customHandler === 'string') {
|
|
62
|
-
defaultMessageShower(customHandler);
|
|
63
|
-
}
|
|
64
|
-
else {
|
|
65
|
-
var _d = (Object(customHandler(errorCode, res.data, res, tslib.__assign(tslib.__assign({}, requestOptions), requestParam)))), _e = _d.replaceResData, replaceResData = _e === void 0 ? res.data : _e, _f = _d.throwError, handlerThrowError = _f === void 0 ? 'default' : _f;
|
|
66
|
-
res.data = replaceResData;
|
|
67
|
-
switch (handlerThrowError) {
|
|
68
|
-
case true:
|
|
69
|
-
throw err;
|
|
70
|
-
case false:
|
|
71
|
-
return res.data;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
if (throwError) {
|
|
75
|
-
throw err;
|
|
76
|
-
}
|
|
77
|
-
return res.data;
|
|
78
|
-
}, function (error$1) {
|
|
79
|
-
if (error$1.response) {
|
|
80
|
-
// 请求成功发出且服务器也响应了状态码,但状态代码超出了 2xx 的范围
|
|
81
|
-
var resData = error$1;
|
|
82
|
-
var _a = httpErrorCodeMap, _b = error$1.response.status, _c = _a[_b], customHandler = _c === void 0 ? defaultHttpErrorCodeHandler : _c;
|
|
83
|
-
var err = new error('服务端错误', 'http', error$1);
|
|
84
|
-
if (typeof customHandler === 'string') {
|
|
85
|
-
defaultMessageShower(customHandler);
|
|
86
|
-
}
|
|
87
|
-
else {
|
|
88
|
-
var _d = (Object(customHandler(error$1.response.status, error$1, tslib.__assign(tslib.__assign({}, requestOptions), requestParam)))), _e = _d.replaceResData, replaceResData = _e === void 0 ? error$1 : _e, _f = _d.throwError, handlerThrowError = _f === void 0 ? 'default' : _f;
|
|
89
|
-
resData = replaceResData;
|
|
90
|
-
switch (handlerThrowError) {
|
|
91
|
-
case true:
|
|
92
|
-
throw err;
|
|
93
|
-
case false:
|
|
94
|
-
return resData;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
if (throwError) {
|
|
98
|
-
throw err;
|
|
99
|
-
}
|
|
100
|
-
return resData;
|
|
101
|
-
}
|
|
102
|
-
else {
|
|
103
|
-
var resData = error$1;
|
|
104
|
-
var err = new error('服务端错误', 'http', error$1);
|
|
105
|
-
err.type = 'http';
|
|
106
|
-
err.data = error$1;
|
|
107
|
-
var _g = (Object(otherErrorHandler(error$1))), _h = _g.replaceResData, replaceResData = _h === void 0 ? error$1 : _h, _j = _g.throwError, handlerThrowError = _j === void 0 ? 'default' : _j;
|
|
108
|
-
resData = replaceResData;
|
|
109
|
-
switch (handlerThrowError) {
|
|
110
|
-
case true:
|
|
111
|
-
throw err;
|
|
112
|
-
case false:
|
|
113
|
-
return resData;
|
|
114
|
-
}
|
|
115
|
-
if (throwError) {
|
|
116
|
-
throw err;
|
|
117
|
-
}
|
|
118
|
-
return resData;
|
|
119
|
-
}
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
request.clearCache = function () {
|
|
123
|
-
cache.clear();
|
|
124
|
-
};
|
|
125
|
-
return request;
|
|
126
|
-
};
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
module.exports = createBaseRequest;
|
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var validator = require('./validator.cjs');
|
|
4
|
-
|
|
5
|
-
function VRequired(noneVals) {
|
|
6
|
-
if (noneVals === void 0) { noneVals = [undefined]; }
|
|
7
|
-
return validator.BaseValidator.decoratorCreator(function (val) {
|
|
8
|
-
if (noneVals.includes(val)) {
|
|
9
|
-
return false;
|
|
10
|
-
}
|
|
11
|
-
return true;
|
|
12
|
-
});
|
|
13
|
-
}
|
|
14
|
-
var VString = validator.BaseValidator.decoratorCreator(function (val) {
|
|
15
|
-
if (typeof val !== 'string') {
|
|
16
|
-
return false;
|
|
17
|
-
}
|
|
18
|
-
return true;
|
|
19
|
-
});
|
|
20
|
-
var VNumber = validator.BaseValidator.decoratorCreator(function (val) {
|
|
21
|
-
if (typeof val !== 'number') {
|
|
22
|
-
return false;
|
|
23
|
-
}
|
|
24
|
-
return true;
|
|
25
|
-
});
|
|
26
|
-
var VArray = validator.BaseValidator.decoratorCreator(function (val) {
|
|
27
|
-
if (!Array.isArray(val)) {
|
|
28
|
-
return false;
|
|
29
|
-
}
|
|
30
|
-
return true;
|
|
31
|
-
});
|
|
32
|
-
// 常用校验装饰器
|
|
33
|
-
var VBoolean = validator.BaseValidator.decoratorCreator(function (val) {
|
|
34
|
-
if (typeof val !== 'boolean') {
|
|
35
|
-
return false;
|
|
36
|
-
}
|
|
37
|
-
return true;
|
|
38
|
-
});
|
|
39
|
-
var VMin = function (min) {
|
|
40
|
-
return validator.BaseValidator.decoratorCreator(function (val) {
|
|
41
|
-
if (typeof val !== 'number' || val < min) {
|
|
42
|
-
return false;
|
|
43
|
-
}
|
|
44
|
-
return true;
|
|
45
|
-
});
|
|
46
|
-
};
|
|
47
|
-
var VMax = function (max) {
|
|
48
|
-
return validator.BaseValidator.decoratorCreator(function (val) {
|
|
49
|
-
if (typeof val !== 'number' || val > max) {
|
|
50
|
-
return false;
|
|
51
|
-
}
|
|
52
|
-
return true;
|
|
53
|
-
});
|
|
54
|
-
};
|
|
55
|
-
var VMinLength = function (minLen) {
|
|
56
|
-
return validator.BaseValidator.decoratorCreator(function (val) {
|
|
57
|
-
if (typeof val !== 'string' && !Array.isArray(val)) {
|
|
58
|
-
return false;
|
|
59
|
-
}
|
|
60
|
-
if (val.length < minLen) {
|
|
61
|
-
return false;
|
|
62
|
-
}
|
|
63
|
-
return true;
|
|
64
|
-
});
|
|
65
|
-
};
|
|
66
|
-
var VMaxLength = function (maxLen) {
|
|
67
|
-
return validator.BaseValidator.decoratorCreator(function (val) {
|
|
68
|
-
if (typeof val !== 'string' && !Array.isArray(val)) {
|
|
69
|
-
return false;
|
|
70
|
-
}
|
|
71
|
-
if (val.length > maxLen) {
|
|
72
|
-
return false;
|
|
73
|
-
}
|
|
74
|
-
return true;
|
|
75
|
-
});
|
|
76
|
-
};
|
|
77
|
-
var VEmail = validator.BaseValidator.decoratorCreator(function (val) {
|
|
78
|
-
if (typeof val !== 'string') {
|
|
79
|
-
return false;
|
|
80
|
-
}
|
|
81
|
-
// 简单邮箱正则
|
|
82
|
-
var emailReg = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
83
|
-
return emailReg.test(val);
|
|
84
|
-
});
|
|
85
|
-
var VPattern = function (pattern) {
|
|
86
|
-
return validator.BaseValidator.decoratorCreator(function (val) {
|
|
87
|
-
if (typeof val !== 'string') {
|
|
88
|
-
return false;
|
|
89
|
-
}
|
|
90
|
-
return pattern.test(val);
|
|
91
|
-
});
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
exports.VArray = VArray;
|
|
95
|
-
exports.VBoolean = VBoolean;
|
|
96
|
-
exports.VEmail = VEmail;
|
|
97
|
-
exports.VMax = VMax;
|
|
98
|
-
exports.VMaxLength = VMaxLength;
|
|
99
|
-
exports.VMin = VMin;
|
|
100
|
-
exports.VMinLength = VMinLength;
|
|
101
|
-
exports.VNumber = VNumber;
|
|
102
|
-
exports.VPattern = VPattern;
|
|
103
|
-
exports.VRequired = VRequired;
|
|
104
|
-
exports.VString = VString;
|
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
import { __assign } from 'tslib';
|
|
2
|
-
import axios from 'axios';
|
|
3
|
-
import { at } from 'lodash';
|
|
4
|
-
import { _defaultErrorCodeHandler, _defaultHttpErrorCodeHandler, _defaultOtherErrorCodeHandler } from './defaultHandlers.mjs';
|
|
5
|
-
import defaultEquals from '../defaultEquals.mjs';
|
|
6
|
-
import Cache from '../cache/index.mjs';
|
|
7
|
-
import RequestError from './error.mjs';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* 创建基础请求实例
|
|
11
|
-
* @param baseOptions 基础配置选项
|
|
12
|
-
* @returns 请求创建函数
|
|
13
|
-
*/
|
|
14
|
-
function createBaseRequest(baseOptions) {
|
|
15
|
-
var baseURL = Object(baseOptions).baseURL;
|
|
16
|
-
// 创建新的 Axios 实例并配置基础URL和跨域凭证
|
|
17
|
-
var instance = axios.create({
|
|
18
|
-
baseURL: baseURL,
|
|
19
|
-
withCredentials: true,
|
|
20
|
-
});
|
|
21
|
-
/**
|
|
22
|
-
* 创建请求函数
|
|
23
|
-
* @template Param 请求参数类型
|
|
24
|
-
* @template Data 响应数据类型,必须是对象类型
|
|
25
|
-
* @param requestOptions 请求配置
|
|
26
|
-
* @param createOptions 创建选项(覆盖基础配置)
|
|
27
|
-
* @returns 发送请求的函数
|
|
28
|
-
*/
|
|
29
|
-
return function createRequest(requestOptions, createOptions) {
|
|
30
|
-
var _a = __assign({}, requestOptions), method = _a.method, url = _a.url;
|
|
31
|
-
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
|
-
var cache = new Cache(cacheDataInStorage, cacheDataKey, cacheTime, indexDBName, cacheKeyEquals);
|
|
33
|
-
function request(requestParam, options) {
|
|
34
|
-
var _a = __assign(__assign({}, requestOptions), requestParam), method = _a.method, url = _a.url, _b = _a.data, data = _b === void 0 ? {} : _b, _c = _a.params, params = _c === void 0 ? {} : _c;
|
|
35
|
-
var requestDataOrParams = params;
|
|
36
|
-
if (method.toLowerCase() === 'post') {
|
|
37
|
-
requestDataOrParams = data;
|
|
38
|
-
}
|
|
39
|
-
var _d = __assign(__assign(__assign({}, baseOptions), createOptions), options).defaultMessageShower, defaultMessageShower = _d === void 0 ? console.error : _d;
|
|
40
|
-
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;
|
|
41
|
-
if (enableCache) {
|
|
42
|
-
var cacheItem = cache.getCache(requestDataOrParams);
|
|
43
|
-
if (cacheItem) {
|
|
44
|
-
return Promise.resolve(cacheItem);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
return instance
|
|
48
|
-
.request(__assign({ method: method, url: url, data: data, params: params }, axiosOptions))
|
|
49
|
-
.then(function (res) {
|
|
50
|
-
var errorCode = String(at(res.data, errorCodePath));
|
|
51
|
-
if (successCodes.includes(errorCode)) {
|
|
52
|
-
if (cacheData) {
|
|
53
|
-
cache.setCache(requestDataOrParams, res.data, { cacheTime: cacheTime });
|
|
54
|
-
}
|
|
55
|
-
return res.data;
|
|
56
|
-
}
|
|
57
|
-
var _a = errorCodeMap, _b = errorCode, _c = _a[_b], customHandler = _c === void 0 ? defaultErrorCodeHandler : _c;
|
|
58
|
-
var err = new RequestError('服务端错误', 'server', res);
|
|
59
|
-
if (typeof customHandler === 'string') {
|
|
60
|
-
defaultMessageShower(customHandler);
|
|
61
|
-
}
|
|
62
|
-
else {
|
|
63
|
-
var _d = (Object(customHandler(errorCode, res.data, res, __assign(__assign({}, requestOptions), requestParam)))), _e = _d.replaceResData, replaceResData = _e === void 0 ? res.data : _e, _f = _d.throwError, handlerThrowError = _f === void 0 ? 'default' : _f;
|
|
64
|
-
res.data = replaceResData;
|
|
65
|
-
switch (handlerThrowError) {
|
|
66
|
-
case true:
|
|
67
|
-
throw err;
|
|
68
|
-
case false:
|
|
69
|
-
return res.data;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
if (throwError) {
|
|
73
|
-
throw err;
|
|
74
|
-
}
|
|
75
|
-
return res.data;
|
|
76
|
-
}, function (error) {
|
|
77
|
-
if (error.response) {
|
|
78
|
-
// 请求成功发出且服务器也响应了状态码,但状态代码超出了 2xx 的范围
|
|
79
|
-
var resData = error;
|
|
80
|
-
var _a = httpErrorCodeMap, _b = error.response.status, _c = _a[_b], customHandler = _c === void 0 ? defaultHttpErrorCodeHandler : _c;
|
|
81
|
-
var err = new RequestError('服务端错误', 'http', error);
|
|
82
|
-
if (typeof customHandler === 'string') {
|
|
83
|
-
defaultMessageShower(customHandler);
|
|
84
|
-
}
|
|
85
|
-
else {
|
|
86
|
-
var _d = (Object(customHandler(error.response.status, error, __assign(__assign({}, requestOptions), requestParam)))), _e = _d.replaceResData, replaceResData = _e === void 0 ? error : _e, _f = _d.throwError, handlerThrowError = _f === void 0 ? 'default' : _f;
|
|
87
|
-
resData = replaceResData;
|
|
88
|
-
switch (handlerThrowError) {
|
|
89
|
-
case true:
|
|
90
|
-
throw err;
|
|
91
|
-
case false:
|
|
92
|
-
return resData;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
if (throwError) {
|
|
96
|
-
throw err;
|
|
97
|
-
}
|
|
98
|
-
return resData;
|
|
99
|
-
}
|
|
100
|
-
else {
|
|
101
|
-
var resData = error;
|
|
102
|
-
var err = new RequestError('服务端错误', 'http', error);
|
|
103
|
-
err.type = 'http';
|
|
104
|
-
err.data = error;
|
|
105
|
-
var _g = (Object(otherErrorHandler(error))), _h = _g.replaceResData, replaceResData = _h === void 0 ? error : _h, _j = _g.throwError, handlerThrowError = _j === void 0 ? 'default' : _j;
|
|
106
|
-
resData = replaceResData;
|
|
107
|
-
switch (handlerThrowError) {
|
|
108
|
-
case true:
|
|
109
|
-
throw err;
|
|
110
|
-
case false:
|
|
111
|
-
return resData;
|
|
112
|
-
}
|
|
113
|
-
if (throwError) {
|
|
114
|
-
throw err;
|
|
115
|
-
}
|
|
116
|
-
return resData;
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
request.clearCache = function () {
|
|
121
|
-
cache.clear();
|
|
122
|
-
};
|
|
123
|
-
return request;
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
export { createBaseRequest as default };
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import { BaseValidator } from './validator.mjs';
|
|
2
|
-
|
|
3
|
-
function VRequired(noneVals) {
|
|
4
|
-
if (noneVals === void 0) { noneVals = [undefined]; }
|
|
5
|
-
return BaseValidator.decoratorCreator(function (val) {
|
|
6
|
-
if (noneVals.includes(val)) {
|
|
7
|
-
return false;
|
|
8
|
-
}
|
|
9
|
-
return true;
|
|
10
|
-
});
|
|
11
|
-
}
|
|
12
|
-
var VString = BaseValidator.decoratorCreator(function (val) {
|
|
13
|
-
if (typeof val !== 'string') {
|
|
14
|
-
return false;
|
|
15
|
-
}
|
|
16
|
-
return true;
|
|
17
|
-
});
|
|
18
|
-
var VNumber = BaseValidator.decoratorCreator(function (val) {
|
|
19
|
-
if (typeof val !== 'number') {
|
|
20
|
-
return false;
|
|
21
|
-
}
|
|
22
|
-
return true;
|
|
23
|
-
});
|
|
24
|
-
var VArray = BaseValidator.decoratorCreator(function (val) {
|
|
25
|
-
if (!Array.isArray(val)) {
|
|
26
|
-
return false;
|
|
27
|
-
}
|
|
28
|
-
return true;
|
|
29
|
-
});
|
|
30
|
-
// 常用校验装饰器
|
|
31
|
-
var VBoolean = BaseValidator.decoratorCreator(function (val) {
|
|
32
|
-
if (typeof val !== 'boolean') {
|
|
33
|
-
return false;
|
|
34
|
-
}
|
|
35
|
-
return true;
|
|
36
|
-
});
|
|
37
|
-
var VMin = function (min) {
|
|
38
|
-
return BaseValidator.decoratorCreator(function (val) {
|
|
39
|
-
if (typeof val !== 'number' || val < min) {
|
|
40
|
-
return false;
|
|
41
|
-
}
|
|
42
|
-
return true;
|
|
43
|
-
});
|
|
44
|
-
};
|
|
45
|
-
var VMax = function (max) {
|
|
46
|
-
return BaseValidator.decoratorCreator(function (val) {
|
|
47
|
-
if (typeof val !== 'number' || val > max) {
|
|
48
|
-
return false;
|
|
49
|
-
}
|
|
50
|
-
return true;
|
|
51
|
-
});
|
|
52
|
-
};
|
|
53
|
-
var VMinLength = function (minLen) {
|
|
54
|
-
return BaseValidator.decoratorCreator(function (val) {
|
|
55
|
-
if (typeof val !== 'string' && !Array.isArray(val)) {
|
|
56
|
-
return false;
|
|
57
|
-
}
|
|
58
|
-
if (val.length < minLen) {
|
|
59
|
-
return false;
|
|
60
|
-
}
|
|
61
|
-
return true;
|
|
62
|
-
});
|
|
63
|
-
};
|
|
64
|
-
var VMaxLength = function (maxLen) {
|
|
65
|
-
return BaseValidator.decoratorCreator(function (val) {
|
|
66
|
-
if (typeof val !== 'string' && !Array.isArray(val)) {
|
|
67
|
-
return false;
|
|
68
|
-
}
|
|
69
|
-
if (val.length > maxLen) {
|
|
70
|
-
return false;
|
|
71
|
-
}
|
|
72
|
-
return true;
|
|
73
|
-
});
|
|
74
|
-
};
|
|
75
|
-
var VEmail = BaseValidator.decoratorCreator(function (val) {
|
|
76
|
-
if (typeof val !== 'string') {
|
|
77
|
-
return false;
|
|
78
|
-
}
|
|
79
|
-
// 简单邮箱正则
|
|
80
|
-
var emailReg = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
81
|
-
return emailReg.test(val);
|
|
82
|
-
});
|
|
83
|
-
var VPattern = function (pattern) {
|
|
84
|
-
return BaseValidator.decoratorCreator(function (val) {
|
|
85
|
-
if (typeof val !== 'string') {
|
|
86
|
-
return false;
|
|
87
|
-
}
|
|
88
|
-
return pattern.test(val);
|
|
89
|
-
});
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
export { VArray, VBoolean, VEmail, VMax, VMaxLength, VMin, VMinLength, VNumber, VPattern, VRequired, VString };
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { BaseValidator } from './validator.js';
|
|
2
|
-
|
|
3
|
-
declare function VRequired(noneVals?: any[]): (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
4
|
-
declare const VString: (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
5
|
-
declare const VNumber: (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
6
|
-
declare const VArray: (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
7
|
-
declare const VBoolean: (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
8
|
-
declare const VMin: (min: number) => (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
9
|
-
declare const VMax: (max: number) => (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
10
|
-
declare const VMinLength: (minLen: number) => (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
11
|
-
declare const VMaxLength: (maxLen: number) => (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
12
|
-
declare const VEmail: (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
13
|
-
declare const VPattern: (pattern: RegExp) => (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
14
|
-
|
|
15
|
-
export { VArray, VBoolean, VEmail, VMax, VMaxLength, VMin, VMinLength, VNumber, VPattern, VRequired, VString };
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
type Validator = (val: any) => {
|
|
2
|
-
status: boolean;
|
|
3
|
-
message?: string;
|
|
4
|
-
};
|
|
5
|
-
type ValidatorMap = {
|
|
6
|
-
[key: string]: Validator[];
|
|
7
|
-
};
|
|
8
|
-
declare class BaseValidator {
|
|
9
|
-
private __keySymbol;
|
|
10
|
-
[key: symbol]: ValidatorMap;
|
|
11
|
-
[key: string]: any;
|
|
12
|
-
constructor();
|
|
13
|
-
validate(itemKey: string, itemAll?: boolean): {
|
|
14
|
-
status: boolean;
|
|
15
|
-
message?: string;
|
|
16
|
-
}[];
|
|
17
|
-
validateAll(itemAll?: boolean, everyItem?: boolean, order?: string[]): {
|
|
18
|
-
status: boolean;
|
|
19
|
-
message?: string;
|
|
20
|
-
}[];
|
|
21
|
-
static decoratorCreator: (func: (val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => boolean) => (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export { BaseValidator };
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|