rxtutils 1.1.2-beta.1 → 1.1.2-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/README.md +144 -1
- package/cjs/index.cjs +28 -0
- package/{dist/types → cjs}/index.d.ts +2 -0
- package/cjs/request/index.cjs +150 -0
- package/{dist/types → cjs}/request/index.d.ts +11 -8
- package/cjs/validator/decorators.cjs +246 -0
- package/cjs/validator/decorators.d.ts +159 -0
- package/cjs/validator/validator.cjs +179 -0
- package/cjs/validator/validator.d.ts +89 -0
- package/es/cache/index.d.ts +135 -0
- package/es/cache/indexDB.d.ts +52 -0
- package/es/index.d.ts +7 -0
- package/{dist/es → es}/index.mjs +2 -0
- package/es/request/error.d.ts +31 -0
- package/es/request/index.d.ts +139 -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/validator/decorators.d.ts +159 -0
- package/es/validator/decorators.mjs +234 -0
- package/es/validator/validator.d.ts +89 -0
- package/es/validator/validator.mjs +177 -0
- package/package.json +9 -7
- package/dist/cjs/index.cjs +0 -14
- package/dist/cjs/request/index.cjs +0 -129
- package/dist/es/request/index.mjs +0 -127
- /package/{dist/cjs → cjs}/cache/index.cjs +0 -0
- /package/{dist/types → cjs}/cache/index.d.ts +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}/defaultEquals.cjs +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}/cache/index.mjs +0 -0
- /package/{dist/es → es}/cache/indexDB.mjs +0 -0
- /package/{dist/es → es}/defaultEquals.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
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { BaseValidator } from './validator.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 验证器装饰器模块
|
|
5
|
+
* 提供一系列用于数据验证的装饰器,可用于类属性的验证规则定义
|
|
6
|
+
* 这些装饰器基于 BaseValidator 的 decoratorCreator 方法创建
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 必填项验证装饰器
|
|
11
|
+
* 验证值是否存在且不在指定的无效值列表中
|
|
12
|
+
*
|
|
13
|
+
* @param noneVals 被视为无效的值数组,默认为 [undefined]
|
|
14
|
+
* @returns 装饰器工厂函数,可接收自定义错误消息
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* class User extends BaseValidator {
|
|
18
|
+
* @(VRequired()('用户名不能为空'))
|
|
19
|
+
* username?: string;
|
|
20
|
+
* }
|
|
21
|
+
*/
|
|
22
|
+
declare function VRequired(noneVals?: any[]): (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
23
|
+
/**
|
|
24
|
+
* 字符串类型验证装饰器
|
|
25
|
+
* 验证值是否为字符串类型
|
|
26
|
+
*
|
|
27
|
+
* @returns 装饰器工厂函数,可接收自定义错误消息
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* class User extends BaseValidator {
|
|
31
|
+
* @VString('用户名必须为字符串')
|
|
32
|
+
* username?: string;
|
|
33
|
+
* }
|
|
34
|
+
*/
|
|
35
|
+
declare const VString: (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
36
|
+
/**
|
|
37
|
+
* 数字类型验证装饰器
|
|
38
|
+
* 验证值是否为数字类型
|
|
39
|
+
*
|
|
40
|
+
* @returns 装饰器工厂函数,可接收自定义错误消息
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* class User extends BaseValidator {
|
|
44
|
+
* @VNumber('年龄必须为数字')
|
|
45
|
+
* age?: number;
|
|
46
|
+
* }
|
|
47
|
+
*/
|
|
48
|
+
declare const VNumber: (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
49
|
+
/**
|
|
50
|
+
* 数组类型验证装饰器
|
|
51
|
+
* 验证值是否为数组类型
|
|
52
|
+
*
|
|
53
|
+
* @returns 装饰器工厂函数,可接收自定义错误消息
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* class User extends BaseValidator {
|
|
57
|
+
* @VArray('标签必须为数组')
|
|
58
|
+
* tags?: string[];
|
|
59
|
+
* }
|
|
60
|
+
*/
|
|
61
|
+
declare const VArray: (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
62
|
+
/**
|
|
63
|
+
* 布尔类型验证装饰器
|
|
64
|
+
* 验证值是否为布尔类型
|
|
65
|
+
*
|
|
66
|
+
* @returns 装饰器工厂函数,可接收自定义错误消息
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* class User extends BaseValidator {
|
|
70
|
+
* @VBoolean('状态必须为布尔值')
|
|
71
|
+
* active?: boolean;
|
|
72
|
+
* }
|
|
73
|
+
*/
|
|
74
|
+
declare const VBoolean: (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
75
|
+
/**
|
|
76
|
+
* 最小值验证装饰器
|
|
77
|
+
* 验证数字是否大于或等于指定的最小值
|
|
78
|
+
*
|
|
79
|
+
* @param min 最小值
|
|
80
|
+
* @returns 装饰器工厂函数,可接收自定义错误消息
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* class User extends BaseValidator {
|
|
84
|
+
* @(VMin(18)('年龄必须大于或等于18岁'))
|
|
85
|
+
* age?: number;
|
|
86
|
+
* }
|
|
87
|
+
*/
|
|
88
|
+
declare const VMin: (min: number) => (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
89
|
+
/**
|
|
90
|
+
* 最大值验证装饰器
|
|
91
|
+
* 验证数字是否小于或等于指定的最大值
|
|
92
|
+
*
|
|
93
|
+
* @param max 最大值
|
|
94
|
+
* @returns 装饰器工厂函数,可接收自定义错误消息
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* class User extends BaseValidator {
|
|
98
|
+
* @(VMax(120)('年龄必须小于或等于120岁'))
|
|
99
|
+
* age?: number;
|
|
100
|
+
* }
|
|
101
|
+
*/
|
|
102
|
+
declare const VMax: (max: number) => (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
103
|
+
/**
|
|
104
|
+
* 最小长度验证装饰器
|
|
105
|
+
* 验证字符串或数组的长度是否大于或等于指定的最小长度
|
|
106
|
+
*
|
|
107
|
+
* @param minLen 最小长度
|
|
108
|
+
* @returns 装饰器工厂函数,可接收自定义错误消息
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* class User extends BaseValidator {
|
|
112
|
+
* @(VMinLength(6)('密码长度不能少于6位'))
|
|
113
|
+
* password?: string;
|
|
114
|
+
* }
|
|
115
|
+
*/
|
|
116
|
+
declare const VMinLength: (minLen: number) => (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
117
|
+
/**
|
|
118
|
+
* 最大长度验证装饰器
|
|
119
|
+
* 验证字符串或数组的长度是否小于或等于指定的最大长度
|
|
120
|
+
*
|
|
121
|
+
* @param maxLen 最大长度
|
|
122
|
+
* @returns 装饰器工厂函数,可接收自定义错误消息
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* class User extends BaseValidator {
|
|
126
|
+
* @(VMaxLength(20)('用户名长度不能超过20位'))
|
|
127
|
+
* username?: string;
|
|
128
|
+
* }
|
|
129
|
+
*/
|
|
130
|
+
declare const VMaxLength: (maxLen: number) => (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
131
|
+
/**
|
|
132
|
+
* 邮箱格式验证装饰器
|
|
133
|
+
* 验证字符串是否符合邮箱格式
|
|
134
|
+
*
|
|
135
|
+
* @returns 装饰器工厂函数,可接收自定义错误消息
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* class User extends BaseValidator {
|
|
139
|
+
* @VEmail('邮箱格式不正确')
|
|
140
|
+
* email?: string;
|
|
141
|
+
* }
|
|
142
|
+
*/
|
|
143
|
+
declare const VEmail: (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
144
|
+
/**
|
|
145
|
+
* 正则表达式验证装饰器
|
|
146
|
+
* 验证字符串是否匹配指定的正则表达式模式
|
|
147
|
+
*
|
|
148
|
+
* @param pattern 正则表达式
|
|
149
|
+
* @returns 装饰器工厂函数,可接收自定义错误消息
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* class User extends BaseValidator {
|
|
153
|
+
* @(VPattern(/^1[3-9]\d{9}$/)('手机号格式不正确'))
|
|
154
|
+
* phone?: string;
|
|
155
|
+
* }
|
|
156
|
+
*/
|
|
157
|
+
declare const VPattern: (pattern: RegExp) => (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
158
|
+
|
|
159
|
+
export { VArray, VBoolean, VEmail, VMax, VMaxLength, VMin, VMinLength, VNumber, VPattern, VRequired, VString };
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var tslib = require('tslib');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 基础验证器类
|
|
7
|
+
* 提供字段验证功能,可通过装饰器为类属性添加验证规则
|
|
8
|
+
*/
|
|
9
|
+
var BaseValidator = /** @class */ (function () {
|
|
10
|
+
/**
|
|
11
|
+
* 构造函数
|
|
12
|
+
* 初始化验证器映射存储
|
|
13
|
+
*/
|
|
14
|
+
function BaseValidator() {
|
|
15
|
+
/** 用于存储验证器映射的私有符号 */
|
|
16
|
+
this.__keySymbol = Symbol('key-description');
|
|
17
|
+
this.__keySymbol = Symbol('key-description');
|
|
18
|
+
this[this.__keySymbol] = {};
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* 验证单个字段
|
|
22
|
+
* @param itemKey 要验证的字段名
|
|
23
|
+
* @param itemAll 是否验证所有规则,为true时会验证该字段的所有规则,为false时遇到第一个失败的规则就停止
|
|
24
|
+
* @returns 验证错误数组,如果没有错误则返回null
|
|
25
|
+
*/
|
|
26
|
+
BaseValidator.prototype.validate = function (itemKey, itemAll) {
|
|
27
|
+
if (itemAll === void 0) { itemAll = false; }
|
|
28
|
+
var validatorMap = this[this.__keySymbol];
|
|
29
|
+
var errors = [];
|
|
30
|
+
// 校验每个 key
|
|
31
|
+
var validators = validatorMap[itemKey];
|
|
32
|
+
if (!validators) {
|
|
33
|
+
return errors;
|
|
34
|
+
}
|
|
35
|
+
for (var _i = 0, validators_1 = validators; _i < validators_1.length; _i++) {
|
|
36
|
+
var validator = validators_1[_i];
|
|
37
|
+
var res = validator(this[itemKey]);
|
|
38
|
+
if (!res.status) {
|
|
39
|
+
errors.push(res);
|
|
40
|
+
if (!itemAll)
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (errors.length) {
|
|
45
|
+
return errors;
|
|
46
|
+
}
|
|
47
|
+
return null;
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* 验证多个或所有字段
|
|
51
|
+
* @param itemAll 是否验证每个字段的所有规则,为true时会验证字段的所有规则,为false时遇到第一个失败的规则就停止
|
|
52
|
+
* @param everyItem 是否验证所有字段,为true时会验证所有字段,为false时遇到第一个失败的字段就停止
|
|
53
|
+
* @param order 验证字段的顺序,可以指定验证的字段名数组及其顺序
|
|
54
|
+
* @returns 验证错误数组,如果没有错误则返回null
|
|
55
|
+
*/
|
|
56
|
+
BaseValidator.prototype.validateAll = function (itemAll, everyItem, order) {
|
|
57
|
+
if (itemAll === void 0) { itemAll = false; }
|
|
58
|
+
if (everyItem === void 0) { everyItem = false; }
|
|
59
|
+
var validatorMap = this[this.__keySymbol];
|
|
60
|
+
var errors = [];
|
|
61
|
+
// 校验每个 key
|
|
62
|
+
var keys = order || Object.keys(validatorMap);
|
|
63
|
+
for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
|
|
64
|
+
var key = keys_1[_i];
|
|
65
|
+
var value = this[key];
|
|
66
|
+
var fns = validatorMap[key];
|
|
67
|
+
// 校验每个校验项目
|
|
68
|
+
for (var _a = 0, fns_1 = fns; _a < fns_1.length; _a++) {
|
|
69
|
+
var fn = fns_1[_a];
|
|
70
|
+
var res = fn(value);
|
|
71
|
+
if (!res.status) {
|
|
72
|
+
errors.push(res);
|
|
73
|
+
if (!itemAll)
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (!everyItem) {
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (errors.length) {
|
|
82
|
+
return errors;
|
|
83
|
+
}
|
|
84
|
+
return null;
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* 装饰器创建器
|
|
88
|
+
* 用于创建属性验证装饰器的工厂函数
|
|
89
|
+
*
|
|
90
|
+
* @param func 验证函数,接收属性值并返回布尔值表示验证结果
|
|
91
|
+
* @returns 返回一个接收错误消息的函数,该函数再返回实际的装饰器
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* // 创建一个验证字符串的装饰器
|
|
95
|
+
* const VString = BaseValidator.decoratorCreator(
|
|
96
|
+
* (val) => typeof val === 'string' || val === undefined
|
|
97
|
+
* );
|
|
98
|
+
*
|
|
99
|
+
* // 创建一个验证必填项的装饰器
|
|
100
|
+
* const VRequired = BaseValidator.decoratorCreator(
|
|
101
|
+
* (val) => val !== undefined && val !== null && val !== ''
|
|
102
|
+
* );
|
|
103
|
+
*
|
|
104
|
+
* // 在类中使用这些装饰器
|
|
105
|
+
* class User extends BaseValidator {
|
|
106
|
+
* @VString('名称必须为字符串')
|
|
107
|
+
* @(VRequired()('名称必须填写'))
|
|
108
|
+
* name?: string;
|
|
109
|
+
*
|
|
110
|
+
* // 验证使用
|
|
111
|
+
* validateName() {
|
|
112
|
+
* return this.validate('name');
|
|
113
|
+
* }
|
|
114
|
+
* }
|
|
115
|
+
*
|
|
116
|
+
* const user = new User();
|
|
117
|
+
* console.log(user.validateName()); // 显示错误信息:名称必须填写
|
|
118
|
+
*/
|
|
119
|
+
BaseValidator.decoratorCreator = function (func) {
|
|
120
|
+
return function (message) {
|
|
121
|
+
if (message === void 0) { message = function (val, value, context) { return "".concat(String(context.name), "\u683C\u5F0F\u9519\u8BEF"); }; }
|
|
122
|
+
return function (value, context) {
|
|
123
|
+
context.addInitializer(function () {
|
|
124
|
+
var validators = this[this.__keySymbol];
|
|
125
|
+
if (!validators) {
|
|
126
|
+
this[this.__keySymbol] = {};
|
|
127
|
+
validators = this[this.__keySymbol];
|
|
128
|
+
}
|
|
129
|
+
var name = context.name;
|
|
130
|
+
var validator = function (val) {
|
|
131
|
+
var validateStatus = func(val, value, context);
|
|
132
|
+
if (validateStatus) {
|
|
133
|
+
return { status: true };
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
var msg = '';
|
|
137
|
+
if (typeof message === 'function') {
|
|
138
|
+
msg = message(val, value, context);
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
msg = message;
|
|
142
|
+
}
|
|
143
|
+
return { status: false, message: msg };
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
if (validators[name]) {
|
|
147
|
+
validators[name] = tslib.__spreadArray(tslib.__spreadArray([], validators[name], true), [validator], false);
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
validators[name] = [validator];
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
};
|
|
154
|
+
};
|
|
155
|
+
};
|
|
156
|
+
return BaseValidator;
|
|
157
|
+
}());
|
|
158
|
+
// class User extends BaseValidator{
|
|
159
|
+
// @VString('名称必须为字符串')
|
|
160
|
+
// @(VRequired()('名称必须填写'))
|
|
161
|
+
// name?: string;
|
|
162
|
+
// @VNumber('年龄必须为数字')
|
|
163
|
+
// @(VRequired()('年龄必须填写'))
|
|
164
|
+
// age?: number;
|
|
165
|
+
// @VEmail('邮箱格式不正确')
|
|
166
|
+
// @(VRequired()('邮箱必须填写'))
|
|
167
|
+
// email?: string;
|
|
168
|
+
// @(VMinLength(6)('密码长度不能少于6位'))
|
|
169
|
+
// @(VRequired()('密码必须填写'))
|
|
170
|
+
// password?: string
|
|
171
|
+
// }
|
|
172
|
+
// const user = new User();
|
|
173
|
+
// user.name = '张三'
|
|
174
|
+
// user.email = ' asdfasdf'
|
|
175
|
+
// user.password = '12345'
|
|
176
|
+
// console.log(user)
|
|
177
|
+
// console.log(user.validateAll(false,true,['password','age','email']));
|
|
178
|
+
|
|
179
|
+
exports.BaseValidator = BaseValidator;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 验证器函数类型,接收任意值并返回验证结果
|
|
3
|
+
* @param val 需要验证的值
|
|
4
|
+
* @returns 包含验证状态和可选错误消息的对象
|
|
5
|
+
*/
|
|
6
|
+
type Validator = (val: any) => {
|
|
7
|
+
status: boolean;
|
|
8
|
+
message?: string;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* 验证器映射类型,用于存储字段名到验证器数组的映射
|
|
12
|
+
*/
|
|
13
|
+
type ValidatorMap = {
|
|
14
|
+
[key: string]: Validator[];
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* 基础验证器类
|
|
18
|
+
* 提供字段验证功能,可通过装饰器为类属性添加验证规则
|
|
19
|
+
*/
|
|
20
|
+
declare class BaseValidator {
|
|
21
|
+
/** 用于存储验证器映射的私有符号 */
|
|
22
|
+
private __keySymbol;
|
|
23
|
+
/** 用于存储验证器映射的索引签名 */
|
|
24
|
+
[key: symbol]: ValidatorMap;
|
|
25
|
+
/** 用于存储类属性的索引签名 */
|
|
26
|
+
[key: string]: any;
|
|
27
|
+
/**
|
|
28
|
+
* 构造函数
|
|
29
|
+
* 初始化验证器映射存储
|
|
30
|
+
*/
|
|
31
|
+
constructor();
|
|
32
|
+
/**
|
|
33
|
+
* 验证单个字段
|
|
34
|
+
* @param itemKey 要验证的字段名
|
|
35
|
+
* @param itemAll 是否验证所有规则,为true时会验证该字段的所有规则,为false时遇到第一个失败的规则就停止
|
|
36
|
+
* @returns 验证错误数组,如果没有错误则返回null
|
|
37
|
+
*/
|
|
38
|
+
validate(itemKey: string, itemAll?: boolean): {
|
|
39
|
+
status: boolean;
|
|
40
|
+
message?: string;
|
|
41
|
+
}[];
|
|
42
|
+
/**
|
|
43
|
+
* 验证多个或所有字段
|
|
44
|
+
* @param itemAll 是否验证每个字段的所有规则,为true时会验证字段的所有规则,为false时遇到第一个失败的规则就停止
|
|
45
|
+
* @param everyItem 是否验证所有字段,为true时会验证所有字段,为false时遇到第一个失败的字段就停止
|
|
46
|
+
* @param order 验证字段的顺序,可以指定验证的字段名数组及其顺序
|
|
47
|
+
* @returns 验证错误数组,如果没有错误则返回null
|
|
48
|
+
*/
|
|
49
|
+
validateAll(itemAll?: boolean, everyItem?: boolean, order?: string[]): {
|
|
50
|
+
status: boolean;
|
|
51
|
+
message?: string;
|
|
52
|
+
}[];
|
|
53
|
+
/**
|
|
54
|
+
* 装饰器创建器
|
|
55
|
+
* 用于创建属性验证装饰器的工厂函数
|
|
56
|
+
*
|
|
57
|
+
* @param func 验证函数,接收属性值并返回布尔值表示验证结果
|
|
58
|
+
* @returns 返回一个接收错误消息的函数,该函数再返回实际的装饰器
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* // 创建一个验证字符串的装饰器
|
|
62
|
+
* const VString = BaseValidator.decoratorCreator(
|
|
63
|
+
* (val) => typeof val === 'string' || val === undefined
|
|
64
|
+
* );
|
|
65
|
+
*
|
|
66
|
+
* // 创建一个验证必填项的装饰器
|
|
67
|
+
* const VRequired = BaseValidator.decoratorCreator(
|
|
68
|
+
* (val) => val !== undefined && val !== null && val !== ''
|
|
69
|
+
* );
|
|
70
|
+
*
|
|
71
|
+
* // 在类中使用这些装饰器
|
|
72
|
+
* class User extends BaseValidator {
|
|
73
|
+
* @VString('名称必须为字符串')
|
|
74
|
+
* @(VRequired()('名称必须填写'))
|
|
75
|
+
* name?: string;
|
|
76
|
+
*
|
|
77
|
+
* // 验证使用
|
|
78
|
+
* validateName() {
|
|
79
|
+
* return this.validate('name');
|
|
80
|
+
* }
|
|
81
|
+
* }
|
|
82
|
+
*
|
|
83
|
+
* const user = new User();
|
|
84
|
+
* console.log(user.validateName()); // 显示错误信息:名称必须填写
|
|
85
|
+
*/
|
|
86
|
+
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;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export { BaseValidator };
|
|
@@ -0,0 +1,135 @@
|
|
|
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
|
+
* @param cacheType 存储类型
|
|
95
|
+
* @param cacheKey 缓存键名
|
|
96
|
+
* @param cacheTime 缓存时间(秒)
|
|
97
|
+
* @param indexDBName IndexedDB 数据库名称,默认值为 '__apiCacheDatabase__'
|
|
98
|
+
* @param cacheKeyEquals 缓存键比较函数,默认使用 defaultEquals
|
|
99
|
+
*/
|
|
100
|
+
constructor(cacheType?: StorageType, cacheKey?: string, cacheTime?: number, indexDBName?: string, cacheKeyEquals?: (prev: Param, next: Param) => boolean);
|
|
101
|
+
/**
|
|
102
|
+
* 初始化缓存
|
|
103
|
+
* 从存储中加载已保存的缓存数据,并进行解析和过期处理
|
|
104
|
+
* @private
|
|
105
|
+
*/
|
|
106
|
+
private _init; /**
|
|
107
|
+
* 过滤掉已过期的缓存项
|
|
108
|
+
* 通过比较当前时间和过期时间,移除过期的缓存项
|
|
109
|
+
* @private
|
|
110
|
+
*/
|
|
111
|
+
private _filterExpired; /**
|
|
112
|
+
* 将当前缓存数据保存到存储中
|
|
113
|
+
* 如果设置了缓存键名且存储实例存在,则将缓存数据序列化后保存
|
|
114
|
+
* @private
|
|
115
|
+
*/
|
|
116
|
+
private _saveToStorage; /**
|
|
117
|
+
* 设置缓存数据
|
|
118
|
+
* @param params 缓存的参数
|
|
119
|
+
* @param data 要缓存的数据
|
|
120
|
+
* @param cacheOptions 可选的缓存配置,可以覆盖默认的缓存时间
|
|
121
|
+
*/
|
|
122
|
+
setCache(params: Param, data: Data, cacheOptions?: Omit<ICacheOptions<Param>, 'storageType' | 'cacheKey' | 'cacheKeyEquals'>): void; /**
|
|
123
|
+
* 获取缓存数据
|
|
124
|
+
* @param params 查询参数
|
|
125
|
+
* @returns 如果找到有效的缓存数据则返回数据,否则返回 null
|
|
126
|
+
*/
|
|
127
|
+
getCache(params: Param): Data; /**
|
|
128
|
+
* 清空所有缓存数据
|
|
129
|
+
* 清空内存中的缓存数组并同步到存储中
|
|
130
|
+
*/
|
|
131
|
+
clear(): void;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export { StorageMap, Cache as default };
|
|
135
|
+
export type { ICache, ICacheOptions, StorageType };
|
|
@@ -0,0 +1,52 @@
|
|
|
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/es/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
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 { default as RequestError, RequestErrorType } from './request/error.js';
|
package/{dist/es → es}/index.mjs
RENAMED
|
@@ -2,3 +2,5 @@ export { default as Cache } from './cache/index.mjs';
|
|
|
2
2
|
export { default as createBaseRequest } from './request/index.mjs';
|
|
3
3
|
export { createStoreGetter, createStoreGetterMemo } from './store/createGetter/index.mjs';
|
|
4
4
|
export { default as createStateStore } from './store/createStateStore/index.mjs';
|
|
5
|
+
export { BaseValidator } from './validator/validator.mjs';
|
|
6
|
+
export { VArray, VBoolean, VEmail, VMax, VMaxLength, VMin, VMinLength, VNumber, VPattern, VRequired, VString } from './validator/decorators.mjs';
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 请求错误类型
|
|
3
|
+
* - server: 服务端业务错误
|
|
4
|
+
* - http: HTTP 网络错误
|
|
5
|
+
*/
|
|
6
|
+
type RequestErrorType = 'server' | 'http';
|
|
7
|
+
/**
|
|
8
|
+
* 请求错误类
|
|
9
|
+
* 用于统一处理请求过程中的各种错误
|
|
10
|
+
*
|
|
11
|
+
* @template Data 错误数据类型
|
|
12
|
+
* @extends Error
|
|
13
|
+
*/
|
|
14
|
+
declare class RequestError<Data = any> extends Error {
|
|
15
|
+
/** 错误码 */
|
|
16
|
+
code: string;
|
|
17
|
+
/** 错误类型 */
|
|
18
|
+
type: RequestErrorType;
|
|
19
|
+
/** 错误相关的数据 */
|
|
20
|
+
data?: Data;
|
|
21
|
+
/**
|
|
22
|
+
* 构造函数
|
|
23
|
+
* @param message 错误消息
|
|
24
|
+
* @param type 错误类型
|
|
25
|
+
* @param data 错误相关的数据
|
|
26
|
+
*/
|
|
27
|
+
constructor(message: string, type: RequestErrorType, data?: Data);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export { RequestError as default };
|
|
31
|
+
export type { RequestErrorType };
|