rxtutils 1.1.4-beta.9 → 1.1.6
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 +117 -49
- package/cjs/_utils/defaultEquals.cjs +4 -19
- package/cjs/cache/index.cjs +130 -163
- package/cjs/cache/indexDB.cjs +86 -99
- package/cjs/downloadBlob.cjs +14 -0
- package/cjs/hooks/index.cjs +3 -6
- package/cjs/hooks/useCombineControlValue.cjs +33 -41
- package/cjs/index.cjs +16 -17
- package/cjs/request/defaultHandlers.cjs +5 -27
- package/cjs/request/error.cjs +18 -30
- package/cjs/request/index.cjs +194 -174
- package/cjs/store/createGetter/index.cjs +26 -40
- package/cjs/store/createStateStore/index.cjs +47 -90
- package/cjs/store/index.cjs +4 -7
- package/cjs/validator/decorators.cjs +71 -228
- package/cjs/validator/index.cjs +4 -7
- package/cjs/validator/validator.cjs +101 -177
- package/es/_utils/defaultEquals.mjs +4 -17
- package/es/cache/index.d.ts +9 -13
- package/es/cache/index.mjs +132 -160
- package/es/cache/indexDB.d.ts +1 -3
- package/es/cache/indexDB.mjs +87 -98
- package/es/downloadBlob.d.ts +8 -0
- package/es/downloadBlob.mjs +14 -0
- package/es/hooks/index.d.ts +1 -1
- package/es/hooks/index.mjs +4 -1
- package/es/hooks/useCombineControlValue.d.ts +5 -8
- package/es/hooks/useCombineControlValue.mjs +34 -40
- package/es/index.d.ts +28 -8
- package/es/index.mjs +29 -7
- package/es/request/defaultHandlers.d.ts +24 -0
- package/es/request/defaultHandlers.mjs +8 -26
- package/es/request/error.d.ts +3 -6
- package/es/request/error.mjs +18 -28
- package/es/request/index.d.ts +32 -20
- package/es/request/index.mjs +194 -172
- package/es/store/createGetter/index.d.ts +6 -10
- package/es/store/createGetter/index.mjs +28 -39
- package/es/store/createStateStore/index.d.ts +9 -9
- package/es/store/createStateStore/index.mjs +49 -87
- package/es/store/index.d.ts +4 -2
- package/es/store/index.mjs +7 -2
- package/es/validator/decorators.d.ts +12 -21
- package/es/validator/decorators.mjs +81 -226
- package/es/validator/index.d.ts +2 -2
- package/es/validator/index.mjs +16 -2
- package/es/validator/validator.d.ts +5 -6
- package/es/validator/validator.mjs +102 -176
- package/package.json +85 -15
- package/cjs/_utils/deepAssign.cjs +0 -25
- package/cjs/cache/index.d.ts +0 -141
- package/cjs/cache/indexDB.d.ts +0 -52
- package/cjs/hooks/index.d.ts +0 -1
- package/cjs/hooks/useCombineControlValue.d.ts +0 -21
- package/cjs/index.d.ts +0 -8
- package/cjs/request/error.d.ts +0 -31
- package/cjs/request/index.d.ts +0 -147
- package/cjs/store/createGetter/index.d.ts +0 -30
- package/cjs/store/createStateStore/index.d.ts +0 -42
- package/cjs/store/index.d.ts +0 -2
- package/cjs/validator/decorators.d.ts +0 -159
- package/cjs/validator/index.d.ts +0 -2
- package/cjs/validator/validator.d.ts +0 -84
- package/es/_utils/deepAssign.mjs +0 -23
|
@@ -1,182 +1,108 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
1
|
+
const KEY_SYMBOL = /* @__PURE__ */ Symbol("key-description");
|
|
2
|
+
const _BaseValidator = class _BaseValidator {
|
|
3
|
+
/**
|
|
4
|
+
* 构造函数
|
|
5
|
+
* 初始化验证器映射存储
|
|
6
|
+
*/
|
|
7
|
+
constructor() {
|
|
8
|
+
this.__keySymbol = KEY_SYMBOL;
|
|
9
|
+
this[this.__keySymbol] = {};
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* 验证单个字段
|
|
13
|
+
* @param itemAll 是否验证所有规则,为true时会验证该字段的所有规则,为false时遇到第一个失败的规则就停止
|
|
14
|
+
* @param itemKey 要验证的字段名
|
|
15
|
+
* @returns 验证错误数组,如果没有错误则返回null
|
|
16
|
+
*/
|
|
17
|
+
validate(itemKey, itemAll = false) {
|
|
18
|
+
const validatorMap = this[this.__keySymbol];
|
|
19
|
+
const errors = [];
|
|
20
|
+
const validators = validatorMap[itemKey];
|
|
21
|
+
if (!validators) {
|
|
22
|
+
return null;
|
|
17
23
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
24
|
+
for (const validator of validators) {
|
|
25
|
+
const res = validator(this[itemKey]);
|
|
26
|
+
if (!res.status) {
|
|
27
|
+
errors.push(res.message || `${itemKey} error`);
|
|
28
|
+
if (!itemAll) break;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
if (errors.length) {
|
|
32
|
+
return errors;
|
|
33
|
+
}
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* 验证多个或所有字段
|
|
38
|
+
* @param order 验证字段的顺序,可以指定验证的字段名数组及其顺序,默认验证所有字段,按对象定义顺序
|
|
39
|
+
* @param itemAll 是否验证每个字段的所有规则,为true时会验证字段的所有规则,为false时遇到第一个失败的规则就停止,默认为 false
|
|
40
|
+
* @param everyItem 是否验证所有字段,为true时会验证所有字段,为false时遇到第一个失败的字段就停止,默认为 false
|
|
41
|
+
* @returns 验证错误数组,如果没有错误则返回null
|
|
42
|
+
*/
|
|
43
|
+
validateAll(order, itemAll = false, everyItem = false) {
|
|
44
|
+
const validatorMap = this[this.__keySymbol];
|
|
45
|
+
const errors = {};
|
|
46
|
+
const keys = order || Object.keys(validatorMap);
|
|
47
|
+
for (const key of keys) {
|
|
48
|
+
const value = this[key];
|
|
49
|
+
const fns = validatorMap[key];
|
|
50
|
+
for (const fn of fns) {
|
|
51
|
+
const res = fn(value);
|
|
52
|
+
if (!res.status) {
|
|
53
|
+
const msg = res.message || `${key} error`;
|
|
54
|
+
if (Array.isArray(errors[key])) {
|
|
55
|
+
errors[key].push(msg);
|
|
56
|
+
} else {
|
|
57
|
+
errors[key] = [msg];
|
|
58
|
+
}
|
|
59
|
+
if (!itemAll) break;
|
|
41
60
|
}
|
|
42
|
-
|
|
43
|
-
|
|
61
|
+
}
|
|
62
|
+
if (errors[key] && !everyItem) {
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (Object.keys(errors).length) {
|
|
67
|
+
return errors;
|
|
68
|
+
}
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
_BaseValidator.decoratorCreator = (func) => {
|
|
73
|
+
return (message = (val, value, context) => `${String(context.name)}格式错误`) => {
|
|
74
|
+
return function(value, context) {
|
|
75
|
+
context.addInitializer(function() {
|
|
76
|
+
let validators = this[this.__keySymbol];
|
|
77
|
+
if (!validators) {
|
|
78
|
+
this[this.__keySymbol] = {};
|
|
79
|
+
validators = this[this.__keySymbol];
|
|
44
80
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
if (everyItem === void 0) { everyItem = false; }
|
|
57
|
-
var validatorMap = this[this.__keySymbol];
|
|
58
|
-
var errors = {};
|
|
59
|
-
// 校验每个 key
|
|
60
|
-
var keys = order || Object.keys(validatorMap);
|
|
61
|
-
for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
|
|
62
|
-
var key = keys_1[_i];
|
|
63
|
-
var value = this[key];
|
|
64
|
-
var fns = validatorMap[key];
|
|
65
|
-
// 校验每个校验项目
|
|
66
|
-
for (var _a = 0, fns_1 = fns; _a < fns_1.length; _a++) {
|
|
67
|
-
var fn = fns_1[_a];
|
|
68
|
-
var res = fn(value);
|
|
69
|
-
if (!res.status) {
|
|
70
|
-
if (Array.isArray(errors[res.name])) {
|
|
71
|
-
errors[res.name].push(res.message);
|
|
72
|
-
}
|
|
73
|
-
else {
|
|
74
|
-
errors[res.name] = [res.message];
|
|
75
|
-
}
|
|
76
|
-
if (!itemAll)
|
|
77
|
-
break;
|
|
78
|
-
}
|
|
81
|
+
const name = context.name;
|
|
82
|
+
const validator = (val) => {
|
|
83
|
+
const validateStatus = func(val, value, context);
|
|
84
|
+
if (validateStatus) {
|
|
85
|
+
return { name, status: true };
|
|
86
|
+
} else {
|
|
87
|
+
let msg = "";
|
|
88
|
+
if (typeof message === "function") {
|
|
89
|
+
msg = message(val, value, context);
|
|
90
|
+
} else {
|
|
91
|
+
msg = message;
|
|
79
92
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
if (Object.keys(errors).length) {
|
|
85
|
-
return errors;
|
|
86
|
-
}
|
|
87
|
-
return null;
|
|
88
|
-
};
|
|
89
|
-
/**
|
|
90
|
-
* 装饰器创建器
|
|
91
|
-
* 用于创建属性验证装饰器的工厂函数
|
|
92
|
-
*
|
|
93
|
-
* @param func 验证函数,接收属性值并返回布尔值表示验证结果
|
|
94
|
-
* @returns 返回一个接收错误消息的函数,该函数再返回实际的装饰器
|
|
95
|
-
*
|
|
96
|
-
* @example
|
|
97
|
-
* // 创建一个验证字符串的装饰器
|
|
98
|
-
* const VString = BaseValidator.decoratorCreator(
|
|
99
|
-
* (val) => typeof val === 'string' || val === undefined
|
|
100
|
-
* );
|
|
101
|
-
*
|
|
102
|
-
* // 创建一个验证必填项的装饰器
|
|
103
|
-
* const VRequired = BaseValidator.decoratorCreator(
|
|
104
|
-
* (val) => val !== undefined && val !== null && val !== ''
|
|
105
|
-
* );
|
|
106
|
-
*
|
|
107
|
-
* // 在类中使用这些装饰器
|
|
108
|
-
* class User extends BaseValidator {
|
|
109
|
-
* @VString('名称必须为字符串')
|
|
110
|
-
* @(VRequired()('名称必须填写'))
|
|
111
|
-
* name?: string;
|
|
112
|
-
*
|
|
113
|
-
* // 验证使用
|
|
114
|
-
* validateName() {
|
|
115
|
-
* return this.validate('name');
|
|
116
|
-
* }
|
|
117
|
-
* }
|
|
118
|
-
*
|
|
119
|
-
* const user = new User();
|
|
120
|
-
* console.log(user.validateName()); // 显示错误信息:名称必须填写
|
|
121
|
-
*/
|
|
122
|
-
BaseValidator.decoratorCreator = function (func) {
|
|
123
|
-
return function (message) {
|
|
124
|
-
if (message === void 0) { message = function (val, value, context) { return "".concat(String(context.name), "\u683C\u5F0F\u9519\u8BEF"); }; }
|
|
125
|
-
return function (value, context) {
|
|
126
|
-
context.addInitializer(function () {
|
|
127
|
-
var validators = this[this.__keySymbol];
|
|
128
|
-
if (!validators) {
|
|
129
|
-
this[this.__keySymbol] = {};
|
|
130
|
-
validators = this[this.__keySymbol];
|
|
131
|
-
}
|
|
132
|
-
var name = context.name;
|
|
133
|
-
var validator = function (val) {
|
|
134
|
-
var validateStatus = func(val, value, context);
|
|
135
|
-
if (validateStatus) {
|
|
136
|
-
return { name: name, status: true };
|
|
137
|
-
}
|
|
138
|
-
else {
|
|
139
|
-
var msg = "";
|
|
140
|
-
if (typeof message === "function") {
|
|
141
|
-
msg = message(val, value, context);
|
|
142
|
-
}
|
|
143
|
-
else {
|
|
144
|
-
msg = message;
|
|
145
|
-
}
|
|
146
|
-
return { name: name, status: false, message: msg };
|
|
147
|
-
}
|
|
148
|
-
};
|
|
149
|
-
if (validators[name]) {
|
|
150
|
-
validators[name] = __spreadArray(__spreadArray([], validators[name], true), [validator], false);
|
|
151
|
-
}
|
|
152
|
-
else {
|
|
153
|
-
validators[name] = [validator];
|
|
154
|
-
}
|
|
155
|
-
});
|
|
156
|
-
};
|
|
93
|
+
return { name, status: false, message: msg };
|
|
94
|
+
}
|
|
157
95
|
};
|
|
96
|
+
if (validators[name]) {
|
|
97
|
+
validators[name] = [...validators[name], validator];
|
|
98
|
+
} else {
|
|
99
|
+
validators[name] = [validator];
|
|
100
|
+
}
|
|
101
|
+
});
|
|
158
102
|
};
|
|
159
|
-
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
// @VNumber('年龄必须为数字')
|
|
166
|
-
// @(VRequired()('年龄必须填写'))
|
|
167
|
-
// age?: number;
|
|
168
|
-
// @VEmail('邮箱格式不正确')
|
|
169
|
-
// @(VRequired()('邮箱必须填写'))
|
|
170
|
-
// email?: string;
|
|
171
|
-
// @(VMinLength(6)('密码长度不能少于6位'))
|
|
172
|
-
// @(VRequired()('密码必须填写'))
|
|
173
|
-
// password?: string
|
|
174
|
-
// }
|
|
175
|
-
// const user = new User();
|
|
176
|
-
// user.name = '张三'
|
|
177
|
-
// user.email = ' asdfasdf'
|
|
178
|
-
// user.password = '12345'
|
|
179
|
-
// console.log(user)
|
|
180
|
-
// console.log(user.validateAll(false,true,['password','age','email']));
|
|
181
|
-
|
|
182
|
-
export { BaseValidator };
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
let BaseValidator = _BaseValidator;
|
|
106
|
+
export {
|
|
107
|
+
BaseValidator
|
|
108
|
+
};
|
package/package.json
CHANGED
|
@@ -1,37 +1,107 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rxtutils",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.6",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "cjs/index.cjs",
|
|
6
6
|
"module": "es/index.mjs",
|
|
7
7
|
"types": "es/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./es/index.d.ts",
|
|
11
|
+
"import": "./es/index.mjs",
|
|
12
|
+
"require": "./cjs/index.cjs"
|
|
13
|
+
},
|
|
14
|
+
"./cache": {
|
|
15
|
+
"types": "./es/cache/index.d.ts",
|
|
16
|
+
"import": "./es/cache/index.mjs",
|
|
17
|
+
"require": "./cjs/cache/index.cjs"
|
|
18
|
+
},
|
|
19
|
+
"./hooks": {
|
|
20
|
+
"types": "./es/hooks/index.d.ts",
|
|
21
|
+
"import": "./es/hooks/index.mjs",
|
|
22
|
+
"require": "./cjs/hooks/index.cjs"
|
|
23
|
+
},
|
|
24
|
+
"./request": {
|
|
25
|
+
"types": "./es/request/index.d.ts",
|
|
26
|
+
"import": "./es/request/index.mjs",
|
|
27
|
+
"require": "./cjs/request/index.cjs"
|
|
28
|
+
},
|
|
29
|
+
"./store": {
|
|
30
|
+
"types": "./es/store/index.d.ts",
|
|
31
|
+
"import": "./es/store/index.mjs",
|
|
32
|
+
"require": "./cjs/store/index.cjs"
|
|
33
|
+
},
|
|
34
|
+
"./validator": {
|
|
35
|
+
"types": "./es/validator/index.d.ts",
|
|
36
|
+
"import": "./es/validator/index.mjs",
|
|
37
|
+
"require": "./cjs/validator/index.cjs"
|
|
38
|
+
},
|
|
39
|
+
"./downloadBlob": {
|
|
40
|
+
"types": "./es/downloadBlob.d.ts",
|
|
41
|
+
"import": "./es/downloadBlob.mjs",
|
|
42
|
+
"require": "./cjs/downloadBlob.cjs"
|
|
43
|
+
},
|
|
44
|
+
"./package.json": "./package.json"
|
|
45
|
+
},
|
|
46
|
+
"sideEffects": false,
|
|
8
47
|
"files": [
|
|
9
48
|
"es",
|
|
10
49
|
"cjs"
|
|
11
50
|
],
|
|
12
51
|
"scripts": {
|
|
13
|
-
"
|
|
14
|
-
"
|
|
52
|
+
"dev": "vite serve playground --config playground/vite.config.ts",
|
|
53
|
+
"build": "pnpm clean && vite build",
|
|
54
|
+
"clean": "rimraf es cjs dist",
|
|
55
|
+
"format": "prettier . --write",
|
|
56
|
+
"format:check": "prettier . --check",
|
|
57
|
+
"lint": "eslint . --max-warnings=0",
|
|
58
|
+
"lint:fix": "eslint . --fix",
|
|
59
|
+
"test": "vitest run",
|
|
60
|
+
"test:watch": "vitest",
|
|
61
|
+
"typecheck": "tsc -p tsconfig.test.json --noEmit",
|
|
15
62
|
"publishNpm": "npm publish --registry https://registry.npmjs.org",
|
|
16
63
|
"loginNpm": "npm login --registry https://registry.npmjs.org"
|
|
17
64
|
},
|
|
18
65
|
"devDependencies": {
|
|
19
|
-
"@
|
|
66
|
+
"@babel/plugin-proposal-class-properties": "^7.18.6",
|
|
67
|
+
"@babel/plugin-proposal-decorators": "^7.28.6",
|
|
68
|
+
"@babel/plugin-transform-class-static-block": "^7.28.6",
|
|
69
|
+
"@eslint/js": "^9.39.2",
|
|
70
|
+
"@rollup/plugin-typescript": "^12.3.0",
|
|
71
|
+
"@testing-library/react": "^16.3.2",
|
|
20
72
|
"@types/lodash-es": "^4.17.12",
|
|
21
|
-
"@types/node": "^22.
|
|
22
|
-
"@types/react": "^19.
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"
|
|
73
|
+
"@types/node": "^22.19.5",
|
|
74
|
+
"@types/react": "^19.2.8",
|
|
75
|
+
"@types/react-dom": "^19.2.3",
|
|
76
|
+
"@vitejs/plugin-react": "^5.1.2",
|
|
77
|
+
"babel-plugin-react-compiler": "^1.0.0",
|
|
78
|
+
"eslint": "^9.39.2",
|
|
79
|
+
"eslint-config-prettier": "^10.1.8",
|
|
80
|
+
"eslint-plugin-prettier": "^5.5.5",
|
|
81
|
+
"eslint-plugin-react-hooks": "^7.0.1",
|
|
82
|
+
"eslint-plugin-vitest": "^0.5.4",
|
|
83
|
+
"fake-indexeddb": "^6.2.2",
|
|
84
|
+
"globals": "^17.3.0",
|
|
85
|
+
"jsdom": "^28.0.0",
|
|
86
|
+
"prettier": "^3.8.1",
|
|
87
|
+
"react": "^19.2.4",
|
|
88
|
+
"react-dom": "^19.2.4",
|
|
89
|
+
"rimraf": "^6.1.2",
|
|
90
|
+
"rollup": "^4.55.1",
|
|
91
|
+
"rollup-plugin-dts": "^6.3.0",
|
|
92
|
+
"typescript": "^5.9.3",
|
|
93
|
+
"typescript-eslint": "^8.54.0",
|
|
94
|
+
"vite": "^7.3.1",
|
|
95
|
+
"vite-plugin-dts": "^4.5.4",
|
|
96
|
+
"vitest": "^4.0.18"
|
|
27
97
|
},
|
|
28
98
|
"peerDependencies": {
|
|
29
|
-
"react": "^19.
|
|
99
|
+
"react": "^19.2.3"
|
|
30
100
|
},
|
|
31
101
|
"dependencies": {
|
|
32
|
-
"axios": "^1.
|
|
33
|
-
"
|
|
34
|
-
"
|
|
102
|
+
"axios": "^1.13.2",
|
|
103
|
+
"date-fns": "^4.1.0",
|
|
104
|
+
"lodash-es": "^4.17.22",
|
|
35
105
|
"tslib": "^2.8.1"
|
|
36
106
|
}
|
|
37
|
-
}
|
|
107
|
+
}
|
|
@@ -1,25 +0,0 @@
|
|
|
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;
|
package/cjs/cache/index.d.ts
DELETED
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
import { IndexedDBStorage } from './indexDB.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 缓存存储类型
|
|
5
|
-
* - sessionStorage: 会话存储,浏览器关闭后清除
|
|
6
|
-
* - localStorage: 本地存储,永久保存
|
|
7
|
-
* - indexedDB: IndexedDB 数据库存储
|
|
8
|
-
*/
|
|
9
|
-
type StorageType = "sessionStorage" | "localStorage" | "indexedDB";
|
|
10
|
-
/**
|
|
11
|
-
* 缓存项接口定义
|
|
12
|
-
* 定义了单个缓存项的数据结构
|
|
13
|
-
*
|
|
14
|
-
* @template Param 缓存参数类型
|
|
15
|
-
* @template Data 缓存数据类型
|
|
16
|
-
*/
|
|
17
|
-
interface ICache<Param, Data> {
|
|
18
|
-
/**
|
|
19
|
-
* 缓存的参数
|
|
20
|
-
* 用于标识和查找缓存项
|
|
21
|
-
*/
|
|
22
|
-
params: Param;
|
|
23
|
-
/**
|
|
24
|
-
* 缓存的数据
|
|
25
|
-
* 实际存储的内容
|
|
26
|
-
*/
|
|
27
|
-
data: Data;
|
|
28
|
-
/**
|
|
29
|
-
* 过期时间
|
|
30
|
-
* - ISO 8601 格式的字符串
|
|
31
|
-
* - 由 moment().add(cacheTime, 'seconds').toJSON() 生成
|
|
32
|
-
* - 示例:'2025-06-12T10:30:00.000Z'
|
|
33
|
-
*/
|
|
34
|
-
expireTime: string;
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* 缓存选项接口
|
|
38
|
-
* @template Param 缓存参数类型
|
|
39
|
-
*/
|
|
40
|
-
interface ICacheOptions<Param> {
|
|
41
|
-
/**
|
|
42
|
-
* 存储类型
|
|
43
|
-
* - 'sessionStorage': 会话存储,浏览器关闭后清除
|
|
44
|
-
* - 'localStorage': 本地存储,永久保存
|
|
45
|
-
* - 'indexedDB': IndexedDB 数据库存储
|
|
46
|
-
* - undefined: 仅在内存中缓存(默认值)
|
|
47
|
-
*/
|
|
48
|
-
storageType?: StorageType;
|
|
49
|
-
/**
|
|
50
|
-
* 缓存键名
|
|
51
|
-
* - 当使用 localStorage/sessionStorage 时必须提供
|
|
52
|
-
* - 用于在存储中标识不同的缓存数据
|
|
53
|
-
* @default undefined 不使用持久化存储
|
|
54
|
-
*/
|
|
55
|
-
cacheKey?: string;
|
|
56
|
-
/**
|
|
57
|
-
* 缓存时间(秒)
|
|
58
|
-
* - 超过这个时间的缓存项会被自动清除
|
|
59
|
-
* @default 60 一分钟
|
|
60
|
-
*/
|
|
61
|
-
cacheTime?: number;
|
|
62
|
-
/**
|
|
63
|
-
* 缓存键比较函数
|
|
64
|
-
* - 用于判断两个缓存参数是否相等
|
|
65
|
-
* - 相等则认为是同一个缓存项
|
|
66
|
-
* @param prev 前一个参数
|
|
67
|
-
* @param next 后一个参数
|
|
68
|
-
* @returns 是否相等
|
|
69
|
-
* @default defaultEquals 使用 JSON.stringify 进行比较
|
|
70
|
-
*/
|
|
71
|
-
cacheKeyEquals: (prev: Param, next: Param) => boolean;
|
|
72
|
-
/**
|
|
73
|
-
* IndexedDB 数据库名称
|
|
74
|
-
* - 仅在 storageType 为 'indexedDB' 时使用
|
|
75
|
-
* @default '__apiCacheDatabase__'
|
|
76
|
-
*/
|
|
77
|
-
indexDBName?: string;
|
|
78
|
-
}
|
|
79
|
-
/** 存储类型映射表 */
|
|
80
|
-
declare const StorageMap: Record<StorageType | string, Storage>;
|
|
81
|
-
/**
|
|
82
|
-
* 缓存类
|
|
83
|
-
* @template Param 缓存参数类型
|
|
84
|
-
* @template Data 缓存数据类型
|
|
85
|
-
*/
|
|
86
|
-
declare class Cache<Param, Data> {
|
|
87
|
-
/** 内存中的缓存数组 */
|
|
88
|
-
cache: ICache<Param, Data>[];
|
|
89
|
-
/** 缓存选项 */
|
|
90
|
-
private cacheOptions;
|
|
91
|
-
/** 存储实例 */
|
|
92
|
-
storage?: Storage | IndexedDBStorage;
|
|
93
|
-
/**
|
|
94
|
-
* 构造函数
|
|
95
|
-
* @param cacheType 存储类型
|
|
96
|
-
* @param cacheKey 缓存键名
|
|
97
|
-
* @param cacheTime 缓存时间(秒)
|
|
98
|
-
* @param indexDBName IndexedDB 数据库名称,默认值为 '__apiCacheDatabase__'
|
|
99
|
-
* @param cacheKeyEquals 缓存键比较函数,默认使用 defaultEquals
|
|
100
|
-
*/
|
|
101
|
-
constructor(cacheType?: StorageType, cacheKey?: string, cacheTime?: number, indexDBName?: string, cacheKeyEquals?: (prev: Param, next: Param) => boolean);
|
|
102
|
-
/**
|
|
103
|
-
* 初始化缓存
|
|
104
|
-
* 从存储中加载已保存的缓存数据,并进行解析和过期处理
|
|
105
|
-
* @private
|
|
106
|
-
*/
|
|
107
|
-
private _init;
|
|
108
|
-
/**
|
|
109
|
-
* 过滤掉已过期的缓存项
|
|
110
|
-
* 通过比较当前时间和过期时间,移除过期的缓存项
|
|
111
|
-
* @private
|
|
112
|
-
*/
|
|
113
|
-
private _filterExpired;
|
|
114
|
-
/**
|
|
115
|
-
* 将当前缓存数据保存到存储中
|
|
116
|
-
* 如果设置了缓存键名且存储实例存在,则将缓存数据序列化后保存
|
|
117
|
-
* @private
|
|
118
|
-
*/
|
|
119
|
-
private _saveToStorage;
|
|
120
|
-
/**
|
|
121
|
-
* 设置缓存数据
|
|
122
|
-
* @param params 缓存的参数
|
|
123
|
-
* @param data 要缓存的数据
|
|
124
|
-
* @param cacheOptions 可选的缓存配置,可以覆盖默认的缓存时间
|
|
125
|
-
*/
|
|
126
|
-
setCache(params: Param, data: Data, cacheOptions?: Omit<ICacheOptions<Param>, "storageType" | "cacheKey" | "cacheKeyEquals">): void;
|
|
127
|
-
/**
|
|
128
|
-
* 获取缓存数据
|
|
129
|
-
* @param params 查询参数
|
|
130
|
-
* @returns 如果找到有效的缓存数据则返回数据,否则返回 null
|
|
131
|
-
*/
|
|
132
|
-
getCache(params: Param): Data;
|
|
133
|
-
/**
|
|
134
|
-
* 清空所有缓存数据
|
|
135
|
-
* 清空内存中的缓存数组并同步到存储中
|
|
136
|
-
*/
|
|
137
|
-
clear(): void;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
export { StorageMap, Cache as default };
|
|
141
|
-
export type { ICache, ICacheOptions, StorageType };
|
package/cjs/cache/indexDB.d.ts
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* IndexedDB 存储类
|
|
3
|
-
* 提供了对 IndexedDB 数据库操作的简单封装
|
|
4
|
-
*/
|
|
5
|
-
declare class IndexedDBStorage {
|
|
6
|
-
/** 数据库名称 */
|
|
7
|
-
private dbName;
|
|
8
|
-
/** 存储对象名称 */
|
|
9
|
-
private storeName;
|
|
10
|
-
/** 数据库连接实例 */
|
|
11
|
-
private db;
|
|
12
|
-
/**
|
|
13
|
-
* 构造函数
|
|
14
|
-
* @param dbName 数据库名称
|
|
15
|
-
* @param storeName 存储对象名称
|
|
16
|
-
*/
|
|
17
|
-
constructor(dbName: string, storeName: string);
|
|
18
|
-
/**
|
|
19
|
-
* 打开数据库连接
|
|
20
|
-
* 如果数据库不存在则创建新的数据库和存储对象
|
|
21
|
-
* @private
|
|
22
|
-
* @returns Promise<IDBDatabase> 数据库连接实例
|
|
23
|
-
*/
|
|
24
|
-
private _open;
|
|
25
|
-
/**
|
|
26
|
-
* 获取存储对象
|
|
27
|
-
* @param mode 事务模式,默认为只读模式
|
|
28
|
-
* - readonly: 只读模式
|
|
29
|
-
* - readwrite: 读写模式
|
|
30
|
-
* @private
|
|
31
|
-
* @returns Promise<IDBObjectStore> 存储对象实例
|
|
32
|
-
*/
|
|
33
|
-
private _getStore;
|
|
34
|
-
/**
|
|
35
|
-
* 设置键值对
|
|
36
|
-
* @param key 键名
|
|
37
|
-
* @param value 要存储的值
|
|
38
|
-
* @returns Promise<void> 存储操作的结果
|
|
39
|
-
* @throws 当存储操作失败时抛出错误
|
|
40
|
-
*/
|
|
41
|
-
setItem<T>(key: string, value: T): Promise<void>;
|
|
42
|
-
/**
|
|
43
|
-
* 获取键对应的值
|
|
44
|
-
* @param key 要获取的键名
|
|
45
|
-
* @returns Promise<T> 返回存储的值,如果不存在则返回 undefined
|
|
46
|
-
* @throws 当获取操作失败时抛出错误
|
|
47
|
-
* @template T 存储值的类型,默认为 any
|
|
48
|
-
*/
|
|
49
|
-
getItem<T = any>(key: string): Promise<T>;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export { IndexedDBStorage };
|
package/cjs/hooks/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { useCombineControlValue } from './useCombineControlValue.js';
|
|
@@ -1,21 +0,0 @@
|
|
|
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.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
export { default as Cache, ICache, ICacheOptions, StorageMap, StorageType } from './cache/index.js';
|
|
2
|
-
export { ErrorHandlerReturnType, Options, RequestOptions, default as createBaseRequest } from './request/index.js';
|
|
3
|
-
export { createStoreGetter, createStoreGetterMemo } from './store/createGetter/index.js';
|
|
4
|
-
export { IHookStateInitAction, IHookStateInitialSetter, IHookStateResolvable, IHookStateSetAction, IHookStateSetter, default as createStateStore } from './store/createStateStore/index.js';
|
|
5
|
-
export { BaseValidator } from './validator/validator.js';
|
|
6
|
-
export { VArray, VBoolean, VEmail, VMax, VMaxLength, VMin, VMinLength, VNumber, VPattern, VRequired, VString } from './validator/decorators.js';
|
|
7
|
-
export { useCombineControlValue } from './hooks/useCombineControlValue.js';
|
|
8
|
-
export { default as RequestError, RequestErrorType } from './request/error.js';
|