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
package/cjs/request/error.d.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
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 };
|
package/cjs/request/index.d.ts
DELETED
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
import { AxiosResponse, Method, AxiosRequestConfig } from 'axios';
|
|
2
|
-
import { StorageType } from '../cache/index.js';
|
|
3
|
-
export { default as RequestError, RequestErrorType } from './error.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* 错误处理器返回类型
|
|
7
|
-
* @template D 响应数据类型
|
|
8
|
-
*/
|
|
9
|
-
type ErrorHandlerReturnType<D> = {
|
|
10
|
-
/** 替换响应数据 */
|
|
11
|
-
replaceResData?: D;
|
|
12
|
-
/**
|
|
13
|
-
* 是否抛出错误
|
|
14
|
-
* - true: 强制抛出错误
|
|
15
|
-
* - false: 不抛出错误
|
|
16
|
-
* - 'default': 使用默认错误处理逻辑
|
|
17
|
-
*/
|
|
18
|
-
throwError?: boolean | "default";
|
|
19
|
-
};
|
|
20
|
-
/**
|
|
21
|
-
* 请求配置选项接口
|
|
22
|
-
* @template Params 请求参数类型
|
|
23
|
-
* @template Data 响应数据类型
|
|
24
|
-
*/
|
|
25
|
-
interface Options<Params = any, Data = any> {
|
|
26
|
-
/** 请求基础URL,默认为空字符串 */
|
|
27
|
-
baseURL?: string;
|
|
28
|
-
/**
|
|
29
|
-
* 是否抛出错误
|
|
30
|
-
* @default true
|
|
31
|
-
*/
|
|
32
|
-
throwError?: boolean;
|
|
33
|
-
/**
|
|
34
|
-
* 默认的消息展示函数
|
|
35
|
-
* @default window.alert
|
|
36
|
-
*/
|
|
37
|
-
defaultMessageShower?: (message: string) => void;
|
|
38
|
-
/**
|
|
39
|
-
* 是否启用缓存功能
|
|
40
|
-
* @default false
|
|
41
|
-
*/
|
|
42
|
-
enableCache?: boolean;
|
|
43
|
-
/**
|
|
44
|
-
* 缓存键比较函数
|
|
45
|
-
* @default defaultEquals 使用 JSON.stringify 进行比较
|
|
46
|
-
*/
|
|
47
|
-
cacheKeyEquals?: (prev: Params, next: Params) => boolean;
|
|
48
|
-
/**
|
|
49
|
-
* 是否将响应数据存入缓存
|
|
50
|
-
* @default false
|
|
51
|
-
*/
|
|
52
|
-
cacheData?: boolean;
|
|
53
|
-
/**
|
|
54
|
-
* 缓存时间(秒)
|
|
55
|
-
* @default 60
|
|
56
|
-
*/
|
|
57
|
-
cacheTime?: number;
|
|
58
|
-
/**
|
|
59
|
-
* 缓存数据的存储类型
|
|
60
|
-
* - localStorage: 使用浏览器本地存储,数据永久保存
|
|
61
|
-
* - sessionStorage: 使用会话存储,关闭浏览器后清除
|
|
62
|
-
* - indexedDB: 使用 IndexedDB 数据库存储
|
|
63
|
-
* - 不填则仅在内存中缓存,页面刷新后清除
|
|
64
|
-
*/
|
|
65
|
-
cacheDataInStorage?: StorageType;
|
|
66
|
-
/**
|
|
67
|
-
* 缓存数据的键名
|
|
68
|
-
* @default `${method}:${baseURL}${url}` 默认使用请求方法、基础URL和请求路径组合
|
|
69
|
-
*/
|
|
70
|
-
cacheDataKey?: string;
|
|
71
|
-
/**
|
|
72
|
-
* IndexedDB 数据库名称
|
|
73
|
-
* @default '__apiCacheDatabase__'
|
|
74
|
-
*/
|
|
75
|
-
indexDBName?: string;
|
|
76
|
-
/**
|
|
77
|
-
* 错误码在响应数据中的路径
|
|
78
|
-
* @default 'code'
|
|
79
|
-
*/
|
|
80
|
-
errorCodePath?: string;
|
|
81
|
-
/**
|
|
82
|
-
* 错误码映射表
|
|
83
|
-
* 可以配置错误码对应的错误信息或处理函数
|
|
84
|
-
* @default {} 空对象,使用默认处理函数
|
|
85
|
-
*/
|
|
86
|
-
errorCodeMap?: Record<string, string | ((code: string, data: Data, res: AxiosResponse<Data>, requestParam: RequestOptions<Params>) => ErrorHandlerReturnType<Data> | void)>;
|
|
87
|
-
/**
|
|
88
|
-
* 默认错误码处理函数
|
|
89
|
-
* 当错误码不在 errorCodeMap 中时调用
|
|
90
|
-
*/
|
|
91
|
-
defaultErrorCodeHandler?: (code: string, data: Data, res: AxiosResponse<Data>) => Promise<ErrorHandlerReturnType<Data> | void>;
|
|
92
|
-
/**
|
|
93
|
-
* 成功状态的错误码列表
|
|
94
|
-
* @default ['0', '200']
|
|
95
|
-
*/
|
|
96
|
-
successCodes?: string[];
|
|
97
|
-
/**
|
|
98
|
-
* HTTP 错误码映射表
|
|
99
|
-
* 可以配置 HTTP 状态码对应的错误信息或处理函数
|
|
100
|
-
* @default {} 空对象,使用默认处理函数
|
|
101
|
-
*/
|
|
102
|
-
httpErrorCodeMap?: Record<string, string | ((code: number, res: AxiosResponse<Data>, requestParam: RequestOptions<Params>) => Promise<ErrorHandlerReturnType<Data> | void>)>;
|
|
103
|
-
/**
|
|
104
|
-
* 默认 HTTP 错误码处理函数
|
|
105
|
-
* 当 HTTP 状态码不在 httpErrorCodeMap 中时调用
|
|
106
|
-
*/
|
|
107
|
-
defaultHttpErrorCodeHandler?: (code: number, error: any) => Promise<ErrorHandlerReturnType<Data> | void>;
|
|
108
|
-
/**
|
|
109
|
-
* 其他错误处理函数
|
|
110
|
-
* 处理非 HTTP 错误和非业务错误码的错误
|
|
111
|
-
*/
|
|
112
|
-
otherErrorHandler?: (error: any) => Promise<ErrorHandlerReturnType<Data> | void>;
|
|
113
|
-
axiosOptions?: Omit<AxiosRequestConfig<Params>, "method" | "url" | "params" | "data">;
|
|
114
|
-
requestMiddlewares?: ((options: Options<Params, Data>, requestOptions: RequestOptions<Params>) => Promise<{
|
|
115
|
-
axiosOptions?: Options<Params, Data>["axiosOptions"];
|
|
116
|
-
requestOptions?: RequestOptions<Params>;
|
|
117
|
-
}> | {
|
|
118
|
-
axiosOptions: Options<Params, Data>["axiosOptions"];
|
|
119
|
-
requestOptions?: RequestOptions<Params>;
|
|
120
|
-
})[];
|
|
121
|
-
}
|
|
122
|
-
/**
|
|
123
|
-
* 请求参数接口
|
|
124
|
-
* @template Param 请求参数类型
|
|
125
|
-
*/
|
|
126
|
-
interface RequestOptions<Param> {
|
|
127
|
-
/** HTTP 请求方法 */
|
|
128
|
-
method: Method;
|
|
129
|
-
/** 请求URL */
|
|
130
|
-
url: string;
|
|
131
|
-
/** POST/PUT 等请求的数据 */
|
|
132
|
-
data?: Param;
|
|
133
|
-
/** URL 查询参数 */
|
|
134
|
-
params?: Param;
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* 创建基础请求实例
|
|
138
|
-
* @param baseOptions 基础配置选项
|
|
139
|
-
* @returns 请求创建函数
|
|
140
|
-
*/
|
|
141
|
-
declare function createBaseRequest(baseOptions?: Options): <Param, Data extends Record<any, any>>(requestOptions: RequestOptions<Param>, createOptions?: Omit<Options<Param, Data>, "baseURL">) => {
|
|
142
|
-
(requestParam?: Omit<RequestOptions<Param>, "url" | "method">, options?: Omit<Options<Param, Data>, "baseURL" | "cacheDataKey" | "cacheDataInStorage" | "cacheKeyEquals">): Promise<Data>;
|
|
143
|
-
clearCache(): void;
|
|
144
|
-
};
|
|
145
|
-
|
|
146
|
-
export { createBaseRequest as default };
|
|
147
|
-
export type { ErrorHandlerReturnType, Options, RequestOptions };
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import createStateStore from '../createStateStore/index.js';
|
|
2
|
-
|
|
3
|
-
type StoreGetter<S = any> = {
|
|
4
|
-
[K in string]: (store: S) => any;
|
|
5
|
-
};
|
|
6
|
-
type GetterNameMap<G extends StoreGetter<any>> = {
|
|
7
|
-
[K in keyof G]: string;
|
|
8
|
-
};
|
|
9
|
-
type ReducedData<G extends StoreGetter<any>, M extends GetterNameMap<G>> = {
|
|
10
|
-
[K in keyof M as M[K]]: G[K extends keyof G ? K : never] extends (store: any) => infer R ? R : never;
|
|
11
|
-
};
|
|
12
|
-
/**
|
|
13
|
-
* 创建 store getter
|
|
14
|
-
* @param store store实例
|
|
15
|
-
* @param getters getter函数
|
|
16
|
-
* @param getterNameMaps 将 getter 函数和 getter 名称一一映射
|
|
17
|
-
* @returns getter object
|
|
18
|
-
*/
|
|
19
|
-
declare const createStoreGetter: <S, G extends StoreGetter<S>, M extends GetterNameMap<G>>(store: ReturnType<typeof createStateStore<S>>, getters: G, getterNameMaps: M) => ReducedData<G, M>;
|
|
20
|
-
/**
|
|
21
|
-
*
|
|
22
|
-
* @param store store实例
|
|
23
|
-
* @param getters getter函数
|
|
24
|
-
* @param getterNameMaps 将 getter 函数和 getter 名称一一映射
|
|
25
|
-
* @returns getter memo hook
|
|
26
|
-
*/
|
|
27
|
-
declare const createStoreGetterMemo: <S, G extends StoreGetter<S>, M extends GetterNameMap<G>>(store: ReturnType<typeof createStateStore<S>>, getters: G, getterNameMaps: M) => () => ReducedData<G, M>;
|
|
28
|
-
|
|
29
|
-
export { createStoreGetter, createStoreGetterMemo };
|
|
30
|
-
export type { GetterNameMap, ReducedData, StoreGetter };
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 状态初始化设置器类型
|
|
3
|
-
* 用于延迟初始化状态的函数类型
|
|
4
|
-
*/
|
|
5
|
-
type IHookStateInitialSetter<S> = () => S;
|
|
6
|
-
/**
|
|
7
|
-
* 状态初始化动作类型
|
|
8
|
-
* 可以是直接的状态值或初始化设置器
|
|
9
|
-
*/
|
|
10
|
-
type IHookStateInitAction<S> = S | IHookStateInitialSetter<S>;
|
|
11
|
-
/**
|
|
12
|
-
* 状态设置器类型
|
|
13
|
-
* 可以是接收前一个状态的函数或无参数的函数
|
|
14
|
-
*/
|
|
15
|
-
type IHookStateSetter<S> = ((prevState: S) => S) | (() => S);
|
|
16
|
-
/**
|
|
17
|
-
* 状态设置动作类型
|
|
18
|
-
* 可以是直接的状态值或状态设置器
|
|
19
|
-
*/
|
|
20
|
-
type IHookStateSetAction<S> = S | IHookStateSetter<S>;
|
|
21
|
-
/**
|
|
22
|
-
* 可解析的状态类型
|
|
23
|
-
* 包含所有可能的状态值或状态设置函数
|
|
24
|
-
*/
|
|
25
|
-
type IHookStateResolvable<S> = S | IHookStateInitialSetter<S> | IHookStateSetter<S>;
|
|
26
|
-
/**
|
|
27
|
-
* 创建状态存储
|
|
28
|
-
* 提供一个简单的状态管理解决方案,支持组件间状态共享
|
|
29
|
-
*
|
|
30
|
-
* @template S 状态类型
|
|
31
|
-
* @param initialState 初始状态值或初始化函数
|
|
32
|
-
* @returns 包含状态操作方法的对象
|
|
33
|
-
*/
|
|
34
|
-
declare function createStateStore<S>(initialState?: S): {
|
|
35
|
-
use: () => [S, (state: IHookStateSetAction<S>) => void];
|
|
36
|
-
get: () => S;
|
|
37
|
-
set: (state: IHookStateSetAction<S>) => void;
|
|
38
|
-
watch: (callback: (state: S) => S | void) => () => void;
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
export { createStateStore as default };
|
|
42
|
-
export type { IHookStateInitAction, IHookStateInitialSetter, IHookStateResolvable, IHookStateSetAction, IHookStateSetter };
|
package/cjs/store/index.d.ts
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
export { GetterNameMap, ReducedData, StoreGetter, createStoreGetter, createStoreGetterMemo } from './createGetter/index.js';
|
|
2
|
-
export { IHookStateInitAction, IHookStateInitialSetter, IHookStateResolvable, IHookStateSetAction, IHookStateSetter, default as createStateStore } from './createStateStore/index.js';
|
|
@@ -1,159 +0,0 @@
|
|
|
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 };
|
package/cjs/validator/index.d.ts
DELETED
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 验证器函数类型,接收任意值并返回验证结果
|
|
3
|
-
* @param val 需要验证的值
|
|
4
|
-
* @returns 包含验证状态和可选错误消息的对象
|
|
5
|
-
*/
|
|
6
|
-
type Validator = (val: any) => {
|
|
7
|
-
name: string;
|
|
8
|
-
status: boolean;
|
|
9
|
-
message?: string;
|
|
10
|
-
};
|
|
11
|
-
/**
|
|
12
|
-
* 验证器映射类型,用于存储字段名到验证器数组的映射
|
|
13
|
-
*/
|
|
14
|
-
type ValidatorMap = {
|
|
15
|
-
[key: string]: Validator[];
|
|
16
|
-
};
|
|
17
|
-
/**
|
|
18
|
-
* 基础验证器类
|
|
19
|
-
* 提供字段验证功能,可通过装饰器为类属性添加验证规则
|
|
20
|
-
*/
|
|
21
|
-
declare class BaseValidator {
|
|
22
|
-
/** 用于存储验证器映射的私有符号 */
|
|
23
|
-
private __keySymbol;
|
|
24
|
-
/** 用于存储验证器映射的索引签名 */
|
|
25
|
-
[key: symbol]: ValidatorMap;
|
|
26
|
-
/** 用于存储类属性的索引签名 */
|
|
27
|
-
[key: string]: any;
|
|
28
|
-
/**
|
|
29
|
-
* 构造函数
|
|
30
|
-
* 初始化验证器映射存储
|
|
31
|
-
*/
|
|
32
|
-
constructor();
|
|
33
|
-
/**
|
|
34
|
-
* 验证单个字段
|
|
35
|
-
* @param itemAll 是否验证所有规则,为true时会验证该字段的所有规则,为false时遇到第一个失败的规则就停止
|
|
36
|
-
* @param itemKey 要验证的字段名
|
|
37
|
-
* @returns 验证错误数组,如果没有错误则返回null
|
|
38
|
-
*/
|
|
39
|
-
validate(itemKey: string, itemAll?: boolean): string[] | null;
|
|
40
|
-
/**
|
|
41
|
-
* 验证多个或所有字段
|
|
42
|
-
* @param order 验证字段的顺序,可以指定验证的字段名数组及其顺序
|
|
43
|
-
* @param itemAll 是否验证每个字段的所有规则,为true时会验证字段的所有规则,为false时遇到第一个失败的规则就停止
|
|
44
|
-
* @param everyItem 是否验证所有字段,为true时会验证所有字段,为false时遇到第一个失败的字段就停止
|
|
45
|
-
* @returns 验证错误数组,如果没有错误则返回null
|
|
46
|
-
*/
|
|
47
|
-
validateAll(order?: string[], itemAll?: boolean, everyItem?: boolean): Record<string, string[]> | null;
|
|
48
|
-
/**
|
|
49
|
-
* 装饰器创建器
|
|
50
|
-
* 用于创建属性验证装饰器的工厂函数
|
|
51
|
-
*
|
|
52
|
-
* @param func 验证函数,接收属性值并返回布尔值表示验证结果
|
|
53
|
-
* @returns 返回一个接收错误消息的函数,该函数再返回实际的装饰器
|
|
54
|
-
*
|
|
55
|
-
* @example
|
|
56
|
-
* // 创建一个验证字符串的装饰器
|
|
57
|
-
* const VString = BaseValidator.decoratorCreator(
|
|
58
|
-
* (val) => typeof val === 'string' || val === undefined
|
|
59
|
-
* );
|
|
60
|
-
*
|
|
61
|
-
* // 创建一个验证必填项的装饰器
|
|
62
|
-
* const VRequired = BaseValidator.decoratorCreator(
|
|
63
|
-
* (val) => val !== undefined && val !== null && val !== ''
|
|
64
|
-
* );
|
|
65
|
-
*
|
|
66
|
-
* // 在类中使用这些装饰器
|
|
67
|
-
* class User extends BaseValidator {
|
|
68
|
-
* @VString('名称必须为字符串')
|
|
69
|
-
* @(VRequired()('名称必须填写'))
|
|
70
|
-
* name?: string;
|
|
71
|
-
*
|
|
72
|
-
* // 验证使用
|
|
73
|
-
* validateName() {
|
|
74
|
-
* return this.validate('name');
|
|
75
|
-
* }
|
|
76
|
-
* }
|
|
77
|
-
*
|
|
78
|
-
* const user = new User();
|
|
79
|
-
* console.log(user.validateName()); // 显示错误信息:名称必须填写
|
|
80
|
-
*/
|
|
81
|
-
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;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
export { BaseValidator };
|
package/es/_utils/deepAssign.mjs
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
function deepAssign(target) {
|
|
2
|
-
var sources = [];
|
|
3
|
-
for (var _i = 1; _i < arguments.length; _i++) {
|
|
4
|
-
sources[_i - 1] = arguments[_i];
|
|
5
|
-
}
|
|
6
|
-
for (var _a = 0, sources_1 = sources; _a < sources_1.length; _a++) {
|
|
7
|
-
var source = sources_1[_a];
|
|
8
|
-
for (var key in source) {
|
|
9
|
-
var val = source[key];
|
|
10
|
-
if (val &&
|
|
11
|
-
typeof val === 'object' &&
|
|
12
|
-
!Array.isArray(val)) {
|
|
13
|
-
target[key] = deepAssign(target[key] || {}, val);
|
|
14
|
-
}
|
|
15
|
-
else {
|
|
16
|
-
target[key] = val;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
return target;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export { deepAssign };
|