rxtutils 1.1.4-beta.14 → 1.1.4-beta.16
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/cache/index.cjs +48 -66
- package/cjs/cache/index.d.ts +1 -1
- package/cjs/cache/indexDB.cjs +36 -43
- package/cjs/hooks/useCombineControlValue.cjs +9 -14
- package/cjs/request/defaultHandlers.cjs +3 -3
- package/cjs/request/error.cjs +7 -12
- package/cjs/request/index.cjs +144 -158
- package/cjs/request/index.d.ts +14 -0
- package/cjs/store/createGetter/index.cjs +11 -11
- package/cjs/store/createStateStore/index.cjs +22 -22
- package/cjs/validator/decorators.cjs +44 -55
- package/cjs/validator/validator.cjs +87 -97
- package/es/cache/index.d.ts +1 -1
- package/es/cache/index.mjs +48 -66
- package/es/cache/indexDB.mjs +36 -43
- package/es/hooks/useCombineControlValue.mjs +9 -14
- package/es/request/defaultHandlers.mjs +3 -3
- package/es/request/error.mjs +7 -12
- package/es/request/index.d.ts +14 -0
- package/es/request/index.mjs +144 -158
- package/es/store/createGetter/index.mjs +11 -11
- package/es/store/createStateStore/index.mjs +22 -22
- package/es/validator/decorators.mjs +44 -55
- package/es/validator/validator.mjs +87 -97
- package/package.json +7 -2
package/cjs/cache/index.cjs
CHANGED
|
@@ -2,13 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var tslib = require('tslib');
|
|
6
5
|
var moment = require('moment');
|
|
7
6
|
var indexDB = require('./indexDB.cjs');
|
|
8
7
|
var defaultEquals = require('../_utils/defaultEquals.cjs');
|
|
9
8
|
|
|
10
9
|
/** 存储类型映射表 */
|
|
11
|
-
|
|
10
|
+
const StorageMap = {
|
|
12
11
|
localStorage: localStorage,
|
|
13
12
|
sessionStorage: sessionStorage,
|
|
14
13
|
};
|
|
@@ -17,7 +16,7 @@ var StorageMap = {
|
|
|
17
16
|
* @template Param 缓存参数类型
|
|
18
17
|
* @template Data 缓存数据类型
|
|
19
18
|
*/
|
|
20
|
-
|
|
19
|
+
class Cache {
|
|
21
20
|
/**
|
|
22
21
|
* 构造函数
|
|
23
22
|
* @param cacheType 存储类型
|
|
@@ -26,17 +25,15 @@ var Cache = /** @class */ (function () {
|
|
|
26
25
|
* @param indexDBName IndexedDB 数据库名称,默认值为 '__apiCacheDatabase__'
|
|
27
26
|
* @param cacheKeyEquals 缓存键比较函数,默认使用 defaultEquals
|
|
28
27
|
*/
|
|
29
|
-
|
|
30
|
-
if (indexDBName === void 0) { indexDBName = "__apiCacheDatabase__"; }
|
|
31
|
-
if (cacheKeyEquals === void 0) { cacheKeyEquals = defaultEquals; }
|
|
28
|
+
constructor(cacheType, cacheKey, cacheTime, indexDBName = "__apiCacheDatabase__", cacheKeyEquals = defaultEquals) {
|
|
32
29
|
/** 内存中的缓存数组 */
|
|
33
30
|
this.cache = [];
|
|
34
31
|
this.cacheOptions = {
|
|
35
32
|
storageType: cacheType,
|
|
36
33
|
cacheKey: cacheKey,
|
|
37
34
|
cacheTime: cacheTime,
|
|
38
|
-
indexDBName
|
|
39
|
-
cacheKeyEquals
|
|
35
|
+
indexDBName,
|
|
36
|
+
cacheKeyEquals,
|
|
40
37
|
};
|
|
41
38
|
if (cacheType === "indexedDB") {
|
|
42
39
|
this.storage = new indexDB.IndexedDBStorage(indexDBName, "cacheStore");
|
|
@@ -51,99 +48,85 @@ var Cache = /** @class */ (function () {
|
|
|
51
48
|
* 从存储中加载已保存的缓存数据,并进行解析和过期处理
|
|
52
49
|
* @private
|
|
53
50
|
*/
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
this.storage = StorageMap[cacheType];
|
|
71
|
-
if (this.storage) {
|
|
72
|
-
if (typeof cacheKey === "string") {
|
|
73
|
-
try {
|
|
74
|
-
this.cache = JSON.parse(this.storage.getItem(cacheKey) || "[]");
|
|
75
|
-
}
|
|
76
|
-
catch (e) {
|
|
77
|
-
this.cache = [];
|
|
78
|
-
console.error("\u7F13\u5B58\u6570\u636E\u89E3\u6790\u5931\u8D25\uFF0Ckey:".concat(cacheKey));
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
_e.label = 3;
|
|
84
|
-
case 3:
|
|
85
|
-
this._filterExpired();
|
|
86
|
-
this._saveToStorage();
|
|
87
|
-
return [2 /*return*/];
|
|
51
|
+
async _init() {
|
|
52
|
+
const { storageType: cacheType, cacheKey: cacheKey } = this.cacheOptions;
|
|
53
|
+
if (this.storage instanceof indexDB.IndexedDBStorage) {
|
|
54
|
+
this.cache = JSON.parse((await this.storage.getItem(cacheKey)) || "[]");
|
|
55
|
+
}
|
|
56
|
+
else if (this.storage instanceof Storage) {
|
|
57
|
+
this.storage = StorageMap[cacheType];
|
|
58
|
+
if (this.storage) {
|
|
59
|
+
if (typeof cacheKey === "string") {
|
|
60
|
+
try {
|
|
61
|
+
this.cache = JSON.parse(this.storage.getItem(cacheKey) || "[]");
|
|
62
|
+
}
|
|
63
|
+
catch (e) {
|
|
64
|
+
this.cache = [];
|
|
65
|
+
console.error(`缓存数据解析失败,key:${cacheKey}`);
|
|
66
|
+
}
|
|
88
67
|
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
this._filterExpired();
|
|
71
|
+
this._saveToStorage();
|
|
72
|
+
}
|
|
92
73
|
/**
|
|
93
74
|
* 过滤掉已过期的缓存项
|
|
94
75
|
* 通过比较当前时间和过期时间,移除过期的缓存项
|
|
95
76
|
* @private
|
|
96
77
|
*/
|
|
97
|
-
|
|
98
|
-
|
|
78
|
+
_filterExpired() {
|
|
79
|
+
const newCache = this.cache.filter((item) => {
|
|
99
80
|
return moment(item.expireTime).isAfter(moment());
|
|
100
81
|
});
|
|
101
82
|
this.cache = newCache;
|
|
102
|
-
}
|
|
83
|
+
}
|
|
103
84
|
/**
|
|
104
85
|
* 将当前缓存数据保存到存储中
|
|
105
86
|
* 如果设置了缓存键名且存储实例存在,则将缓存数据序列化后保存
|
|
106
87
|
* @private
|
|
107
88
|
*/
|
|
108
|
-
|
|
89
|
+
_saveToStorage() {
|
|
109
90
|
if (this.storage) {
|
|
110
91
|
if (typeof this.cacheOptions.cacheKey === "string") {
|
|
111
92
|
this.storage.setItem(this.cacheOptions.cacheKey, JSON.stringify(this.cache));
|
|
112
93
|
}
|
|
113
94
|
}
|
|
114
|
-
}
|
|
95
|
+
}
|
|
115
96
|
/**
|
|
116
97
|
* 设置缓存数据
|
|
117
98
|
* @param params 缓存的参数
|
|
118
99
|
* @param data 要缓存的数据
|
|
119
100
|
* @param cacheOptions 可选的缓存配置,可以覆盖默认的缓存时间
|
|
120
101
|
*/
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
102
|
+
setCache(params, data, cacheOptions) {
|
|
103
|
+
const { cacheTime, cacheKeyEquals = defaultEquals } = {
|
|
104
|
+
...this.cacheOptions,
|
|
105
|
+
...cacheOptions,
|
|
106
|
+
};
|
|
107
|
+
const cacheItemIndex = this.cache.findIndex((item) => {
|
|
124
108
|
return cacheKeyEquals(item.params, params);
|
|
125
109
|
});
|
|
126
110
|
if (cacheItemIndex > -1) {
|
|
127
111
|
this.cache.splice(cacheItemIndex, 1);
|
|
128
112
|
}
|
|
129
113
|
this.cache.push({
|
|
130
|
-
params
|
|
131
|
-
data
|
|
114
|
+
params,
|
|
115
|
+
data,
|
|
132
116
|
expireTime: moment().add(cacheTime, "seconds").toJSON(),
|
|
133
117
|
});
|
|
134
118
|
this._saveToStorage();
|
|
135
|
-
}
|
|
119
|
+
}
|
|
136
120
|
/**
|
|
137
121
|
* 获取缓存数据
|
|
138
122
|
* @param params 查询参数
|
|
139
123
|
* @returns 如果找到有效的缓存数据则返回数据,否则返回 null
|
|
140
124
|
*/
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
return _this.cacheOptions.cacheKeyEquals(item.params, params);
|
|
125
|
+
getCache(params) {
|
|
126
|
+
const itemIndex = this.cache.findIndex((item) => {
|
|
127
|
+
return this.cacheOptions.cacheKeyEquals(item.params, params);
|
|
145
128
|
});
|
|
146
|
-
|
|
129
|
+
const item = this.cache[itemIndex];
|
|
147
130
|
if (item) {
|
|
148
131
|
if (moment(item.expireTime).isAfter(moment())) {
|
|
149
132
|
return item.data;
|
|
@@ -154,17 +137,16 @@ var Cache = /** @class */ (function () {
|
|
|
154
137
|
}
|
|
155
138
|
}
|
|
156
139
|
return null;
|
|
157
|
-
}
|
|
140
|
+
}
|
|
158
141
|
/**
|
|
159
142
|
* 清空所有缓存数据
|
|
160
143
|
* 清空内存中的缓存数组并同步到存储中
|
|
161
144
|
*/
|
|
162
|
-
|
|
145
|
+
clear() {
|
|
163
146
|
this.cache = [];
|
|
164
147
|
this._saveToStorage();
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
}());
|
|
148
|
+
}
|
|
149
|
+
}
|
|
168
150
|
|
|
169
151
|
exports.StorageMap = StorageMap;
|
|
170
152
|
exports.default = Cache;
|
package/cjs/cache/index.d.ts
CHANGED
package/cjs/cache/indexDB.cjs
CHANGED
|
@@ -4,13 +4,13 @@
|
|
|
4
4
|
* IndexedDB 存储类
|
|
5
5
|
* 提供了对 IndexedDB 数据库操作的简单封装
|
|
6
6
|
*/
|
|
7
|
-
|
|
7
|
+
class IndexedDBStorage {
|
|
8
8
|
/**
|
|
9
9
|
* 构造函数
|
|
10
10
|
* @param dbName 数据库名称
|
|
11
11
|
* @param storeName 存储对象名称
|
|
12
12
|
*/
|
|
13
|
-
|
|
13
|
+
constructor(dbName, storeName) {
|
|
14
14
|
/** 数据库连接实例 */
|
|
15
15
|
this.db = null;
|
|
16
16
|
this.dbName = dbName;
|
|
@@ -22,28 +22,26 @@ var IndexedDBStorage = /** @class */ (function () {
|
|
|
22
22
|
* @private
|
|
23
23
|
* @returns Promise<IDBDatabase> 数据库连接实例
|
|
24
24
|
*/
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
resolve(_this.db);
|
|
25
|
+
_open() {
|
|
26
|
+
return new Promise((resolve, reject) => {
|
|
27
|
+
if (this.db) {
|
|
28
|
+
resolve(this.db);
|
|
30
29
|
return;
|
|
31
30
|
}
|
|
32
|
-
|
|
33
|
-
request.onupgradeneeded =
|
|
34
|
-
|
|
35
|
-
db.createObjectStore(
|
|
31
|
+
const request = indexedDB.open(this.dbName);
|
|
32
|
+
request.onupgradeneeded = (event) => {
|
|
33
|
+
const db = event.target.result;
|
|
34
|
+
db.createObjectStore(this.storeName, { keyPath: 'key' });
|
|
36
35
|
};
|
|
37
|
-
request.onerror =
|
|
38
|
-
|
|
39
|
-
reject("IndexedDB open request error: ".concat((_a = request.error) === null || _a === void 0 ? void 0 : _a.message));
|
|
36
|
+
request.onerror = () => {
|
|
37
|
+
reject(`IndexedDB open request error: ${request.error?.message}`);
|
|
40
38
|
};
|
|
41
|
-
request.onsuccess =
|
|
42
|
-
|
|
43
|
-
resolve(
|
|
39
|
+
request.onsuccess = (event) => {
|
|
40
|
+
this.db = event.target.result;
|
|
41
|
+
resolve(this.db);
|
|
44
42
|
};
|
|
45
43
|
});
|
|
46
|
-
}
|
|
44
|
+
}
|
|
47
45
|
/**
|
|
48
46
|
* 获取存储对象
|
|
49
47
|
* @param mode 事务模式,默认为只读模式
|
|
@@ -52,14 +50,12 @@ var IndexedDBStorage = /** @class */ (function () {
|
|
|
52
50
|
* @private
|
|
53
51
|
* @returns Promise<IDBObjectStore> 存储对象实例
|
|
54
52
|
*/
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
var transaction = db.transaction(_this.storeName, mode);
|
|
60
|
-
return transaction.objectStore(_this.storeName);
|
|
53
|
+
_getStore(mode = 'readonly') {
|
|
54
|
+
return this._open().then((db) => {
|
|
55
|
+
const transaction = db.transaction(this.storeName, mode);
|
|
56
|
+
return transaction.objectStore(this.storeName);
|
|
61
57
|
});
|
|
62
|
-
}
|
|
58
|
+
}
|
|
63
59
|
/**
|
|
64
60
|
* 设置键值对
|
|
65
61
|
* @param key 键名
|
|
@@ -67,15 +63,15 @@ var IndexedDBStorage = /** @class */ (function () {
|
|
|
67
63
|
* @returns Promise<void> 存储操作的结果
|
|
68
64
|
* @throws 当存储操作失败时抛出错误
|
|
69
65
|
*/
|
|
70
|
-
|
|
71
|
-
return this._getStore('readwrite').then(
|
|
72
|
-
return new Promise(
|
|
73
|
-
|
|
74
|
-
request.onsuccess =
|
|
75
|
-
request.onerror =
|
|
66
|
+
setItem(key, value) {
|
|
67
|
+
return this._getStore('readwrite').then((store) => {
|
|
68
|
+
return new Promise((resolve, reject) => {
|
|
69
|
+
const request = store.put({ key, value });
|
|
70
|
+
request.onsuccess = () => resolve();
|
|
71
|
+
request.onerror = () => reject(`Could not set the item: ${request.error?.message}`);
|
|
76
72
|
});
|
|
77
73
|
});
|
|
78
|
-
}
|
|
74
|
+
}
|
|
79
75
|
/**
|
|
80
76
|
* 获取键对应的值
|
|
81
77
|
* @param key 要获取的键名
|
|
@@ -83,18 +79,15 @@ var IndexedDBStorage = /** @class */ (function () {
|
|
|
83
79
|
* @throws 当获取操作失败时抛出错误
|
|
84
80
|
* @template T 存储值的类型,默认为 any
|
|
85
81
|
*/
|
|
86
|
-
|
|
87
|
-
return this._getStore().then(
|
|
88
|
-
return new Promise(
|
|
89
|
-
|
|
90
|
-
request.onsuccess =
|
|
91
|
-
|
|
92
|
-
};
|
|
93
|
-
request.onerror = function () { var _a; return reject("Could not get the item: ".concat((_a = request.error) === null || _a === void 0 ? void 0 : _a.message)); };
|
|
82
|
+
getItem(key) {
|
|
83
|
+
return this._getStore().then((store) => {
|
|
84
|
+
return new Promise((resolve, reject) => {
|
|
85
|
+
const request = store.get(key);
|
|
86
|
+
request.onsuccess = () => resolve(request.result ? request.result.value : undefined);
|
|
87
|
+
request.onerror = () => reject(`Could not get the item: ${request.error?.message}`);
|
|
94
88
|
});
|
|
95
89
|
});
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
}());
|
|
90
|
+
}
|
|
91
|
+
}
|
|
99
92
|
|
|
100
93
|
exports.IndexedDBStorage = IndexedDBStorage;
|
|
@@ -9,27 +9,22 @@ var react = require('react');
|
|
|
9
9
|
* @param param onChange 值改变时的回调
|
|
10
10
|
* @returns value: 组件应该采用的值,onChange:值改变时的回调,处理非受控值与向父组件传递值的逻辑
|
|
11
11
|
*/
|
|
12
|
-
function useCombineControlValue(
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
var params = [];
|
|
19
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
20
|
-
params[_i] = arguments[_i];
|
|
21
|
-
}
|
|
22
|
-
var realNextVal;
|
|
12
|
+
function useCombineControlValue({ props, valueKey = 'value', defaultValue, onChange }, resolveFn) {
|
|
13
|
+
const { [valueKey]: value } = props;
|
|
14
|
+
const hasValue = Object.prototype.hasOwnProperty.call(props, valueKey);
|
|
15
|
+
const [internalValue, setInternalValue] = react.useState(value ?? defaultValue);
|
|
16
|
+
const handleChange = react.useCallback((...params) => {
|
|
17
|
+
let realNextVal;
|
|
23
18
|
if (typeof resolveFn === 'function') {
|
|
24
|
-
realNextVal = resolveFn
|
|
19
|
+
realNextVal = resolveFn(...params);
|
|
25
20
|
}
|
|
26
21
|
else {
|
|
27
22
|
realNextVal = params[0];
|
|
28
23
|
}
|
|
29
24
|
setInternalValue(realNextVal);
|
|
30
|
-
onChange
|
|
25
|
+
onChange?.(...params);
|
|
31
26
|
}, [onChange, resolveFn]);
|
|
32
|
-
|
|
27
|
+
const finalValue = react.useMemo(() => {
|
|
33
28
|
if (hasValue)
|
|
34
29
|
return value;
|
|
35
30
|
return internalValue;
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* @param code 业务错误码
|
|
9
9
|
*/
|
|
10
10
|
function _defaultErrorCodeHandler(defaultMessageShower, code) {
|
|
11
|
-
defaultMessageShower(
|
|
11
|
+
defaultMessageShower(`请求出错,错误码:${code},请稍后再试`);
|
|
12
12
|
}
|
|
13
13
|
/**
|
|
14
14
|
* 默认 HTTP 错误码处理函数
|
|
@@ -18,7 +18,7 @@ function _defaultErrorCodeHandler(defaultMessageShower, code) {
|
|
|
18
18
|
* @param code HTTP 状态码
|
|
19
19
|
*/
|
|
20
20
|
function _defaultHttpErrorCodeHandler(defaultMessageShower, code) {
|
|
21
|
-
defaultMessageShower(
|
|
21
|
+
defaultMessageShower(`服务端请求出错,Http错误码:${String(code)},请稍后再试`);
|
|
22
22
|
}
|
|
23
23
|
/**
|
|
24
24
|
* 默认其他错误处理函数
|
|
@@ -28,7 +28,7 @@ function _defaultHttpErrorCodeHandler(defaultMessageShower, code) {
|
|
|
28
28
|
* @param error 错误对象
|
|
29
29
|
*/
|
|
30
30
|
function _defaultOtherErrorCodeHandler(defaultMessageShower, error) {
|
|
31
|
-
defaultMessageShower(
|
|
31
|
+
defaultMessageShower(`未知请求出错,请稍后再试`);
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
exports._defaultErrorCodeHandler = _defaultErrorCodeHandler;
|
package/cjs/request/error.cjs
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var tslib = require('tslib');
|
|
4
|
-
|
|
5
3
|
/**
|
|
6
4
|
* 请求错误类
|
|
7
5
|
* 用于统一处理请求过程中的各种错误
|
|
@@ -9,22 +7,19 @@ var tslib = require('tslib');
|
|
|
9
7
|
* @template Data 错误数据类型
|
|
10
8
|
* @extends Error
|
|
11
9
|
*/
|
|
12
|
-
|
|
13
|
-
tslib.__extends(RequestError, _super);
|
|
10
|
+
class RequestError extends Error {
|
|
14
11
|
/**
|
|
15
12
|
* 构造函数
|
|
16
13
|
* @param message 错误消息
|
|
17
14
|
* @param type 错误类型
|
|
18
15
|
* @param data 错误相关的数据
|
|
19
16
|
*/
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
return _this;
|
|
17
|
+
constructor(message, type, data) {
|
|
18
|
+
super(message);
|
|
19
|
+
this.name = 'RequestError';
|
|
20
|
+
this.type = type;
|
|
21
|
+
this.data = data;
|
|
26
22
|
}
|
|
27
|
-
|
|
28
|
-
}(Error));
|
|
23
|
+
}
|
|
29
24
|
|
|
30
25
|
module.exports = RequestError;
|