rxtutils 1.1.2-beta.8 → 1.1.3
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 +2 -2
- package/{dist/cjs → cjs}/cache/index.cjs +28 -15
- package/{dist/types → cjs}/cache/index.d.ts +13 -7
- package/{dist/cjs → cjs}/request/index.cjs +3 -3
- package/{dist/types → cjs}/request/index.d.ts +2 -1
- package/cjs/store/index.cjs +10 -0
- package/cjs/store/index.d.ts +2 -0
- package/cjs/validator/index.cjs +19 -0
- package/cjs/validator/index.d.ts +2 -0
- package/{dist/cjs → cjs}/validator/validator.cjs +19 -14
- package/{dist/types → cjs}/validator/validator.d.ts +5 -10
- 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/{dist/es → es}/request/index.mjs +2 -2
- 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/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 +19 -14
- package/package.json +12 -9
- /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/cjs → cjs}/validator/decorators.cjs +0 -0
- /package/{dist/types → cjs}/validator/decorators.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/{dist/es → es}/validator/decorators.mjs +0 -0
|
@@ -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';
|
|
@@ -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 };
|
|
@@ -0,0 +1,140 @@
|
|
|
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
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* 请求参数接口
|
|
117
|
+
* @template Param 请求参数类型
|
|
118
|
+
*/
|
|
119
|
+
interface RequestOptions<Param> {
|
|
120
|
+
/** HTTP 请求方法 */
|
|
121
|
+
method: Method;
|
|
122
|
+
/** 请求URL */
|
|
123
|
+
url: string;
|
|
124
|
+
/** POST/PUT 等请求的数据 */
|
|
125
|
+
data?: Param;
|
|
126
|
+
/** URL 查询参数 */
|
|
127
|
+
params?: Param;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* 创建基础请求实例
|
|
131
|
+
* @param baseOptions 基础配置选项
|
|
132
|
+
* @returns 请求创建函数
|
|
133
|
+
*/
|
|
134
|
+
declare function createBaseRequest(baseOptions?: Options): <Param, Data extends Record<any, any>>(requestOptions: RequestOptions<Param>, createOptions?: Omit<Options<Param, Data>, "baseURL">) => {
|
|
135
|
+
(requestParam?: Omit<RequestOptions<Param>, "url" | "method">, options?: Omit<Options<Param, Data>, "baseURL" | "cacheDataKey" | "cacheDataInStorage" | "cacheKeyEquals">): Promise<Data>;
|
|
136
|
+
clearCache(): void;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
export { createBaseRequest as default };
|
|
140
|
+
export type { ErrorHandlerReturnType, Options, RequestOptions };
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { __assign, __awaiter, __generator } from 'tslib';
|
|
2
2
|
import axios from 'axios';
|
|
3
|
-
import
|
|
3
|
+
import at from 'lodash-es/at';
|
|
4
4
|
import { _defaultErrorCodeHandler, _defaultHttpErrorCodeHandler, _defaultOtherErrorCodeHandler } from './defaultHandlers.mjs';
|
|
5
|
-
import defaultEquals from '../defaultEquals.mjs';
|
|
5
|
+
import defaultEquals from '../_utils/defaultEquals.mjs';
|
|
6
6
|
import Cache from '../cache/index.mjs';
|
|
7
7
|
import RequestError from './error.mjs';
|
|
8
8
|
|
|
@@ -0,0 +1,30 @@
|
|
|
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 };
|
|
@@ -0,0 +1,42 @@
|
|
|
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 };
|
|
@@ -0,0 +1,2 @@
|
|
|
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';
|
|
@@ -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,84 @@
|
|
|
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 };
|
|
@@ -11,14 +11,14 @@ var BaseValidator = /** @class */ (function () {
|
|
|
11
11
|
*/
|
|
12
12
|
function BaseValidator() {
|
|
13
13
|
/** 用于存储验证器映射的私有符号 */
|
|
14
|
-
this.__keySymbol = Symbol(
|
|
15
|
-
this.__keySymbol = Symbol(
|
|
14
|
+
this.__keySymbol = Symbol("key-description");
|
|
15
|
+
this.__keySymbol = Symbol("key-description");
|
|
16
16
|
this[this.__keySymbol] = {};
|
|
17
17
|
}
|
|
18
18
|
/**
|
|
19
19
|
* 验证单个字段
|
|
20
|
-
* @param itemKey 要验证的字段名
|
|
21
20
|
* @param itemAll 是否验证所有规则,为true时会验证该字段的所有规则,为false时遇到第一个失败的规则就停止
|
|
21
|
+
* @param itemKey 要验证的字段名
|
|
22
22
|
* @returns 验证错误数组,如果没有错误则返回null
|
|
23
23
|
*/
|
|
24
24
|
BaseValidator.prototype.validate = function (itemKey, itemAll) {
|
|
@@ -28,13 +28,13 @@ var BaseValidator = /** @class */ (function () {
|
|
|
28
28
|
// 校验每个 key
|
|
29
29
|
var validators = validatorMap[itemKey];
|
|
30
30
|
if (!validators) {
|
|
31
|
-
return
|
|
31
|
+
return null;
|
|
32
32
|
}
|
|
33
33
|
for (var _i = 0, validators_1 = validators; _i < validators_1.length; _i++) {
|
|
34
34
|
var validator = validators_1[_i];
|
|
35
35
|
var res = validator(this[itemKey]);
|
|
36
36
|
if (!res.status) {
|
|
37
|
-
errors.push(res);
|
|
37
|
+
errors.push(res.message);
|
|
38
38
|
if (!itemAll)
|
|
39
39
|
break;
|
|
40
40
|
}
|
|
@@ -46,16 +46,16 @@ var BaseValidator = /** @class */ (function () {
|
|
|
46
46
|
};
|
|
47
47
|
/**
|
|
48
48
|
* 验证多个或所有字段
|
|
49
|
+
* @param order 验证字段的顺序,可以指定验证的字段名数组及其顺序
|
|
49
50
|
* @param itemAll 是否验证每个字段的所有规则,为true时会验证字段的所有规则,为false时遇到第一个失败的规则就停止
|
|
50
51
|
* @param everyItem 是否验证所有字段,为true时会验证所有字段,为false时遇到第一个失败的字段就停止
|
|
51
|
-
* @param order 验证字段的顺序,可以指定验证的字段名数组及其顺序
|
|
52
52
|
* @returns 验证错误数组,如果没有错误则返回null
|
|
53
53
|
*/
|
|
54
|
-
BaseValidator.prototype.validateAll = function (itemAll, everyItem
|
|
54
|
+
BaseValidator.prototype.validateAll = function (order, itemAll, everyItem) {
|
|
55
55
|
if (itemAll === void 0) { itemAll = false; }
|
|
56
56
|
if (everyItem === void 0) { everyItem = false; }
|
|
57
57
|
var validatorMap = this[this.__keySymbol];
|
|
58
|
-
var errors =
|
|
58
|
+
var errors = {};
|
|
59
59
|
// 校验每个 key
|
|
60
60
|
var keys = order || Object.keys(validatorMap);
|
|
61
61
|
for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
|
|
@@ -67,7 +67,12 @@ var BaseValidator = /** @class */ (function () {
|
|
|
67
67
|
var fn = fns_1[_a];
|
|
68
68
|
var res = fn(value);
|
|
69
69
|
if (!res.status) {
|
|
70
|
-
|
|
70
|
+
if (Array.isArray(errors[res.name])) {
|
|
71
|
+
errors[res.name].push(res.message);
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
errors[res.name] = [res.message];
|
|
75
|
+
}
|
|
71
76
|
if (!itemAll)
|
|
72
77
|
break;
|
|
73
78
|
}
|
|
@@ -76,7 +81,7 @@ var BaseValidator = /** @class */ (function () {
|
|
|
76
81
|
break;
|
|
77
82
|
}
|
|
78
83
|
}
|
|
79
|
-
if (errors.length) {
|
|
84
|
+
if (Object.keys(errors).length) {
|
|
80
85
|
return errors;
|
|
81
86
|
}
|
|
82
87
|
return null;
|
|
@@ -128,17 +133,17 @@ var BaseValidator = /** @class */ (function () {
|
|
|
128
133
|
var validator = function (val) {
|
|
129
134
|
var validateStatus = func(val, value, context);
|
|
130
135
|
if (validateStatus) {
|
|
131
|
-
return { status: true };
|
|
136
|
+
return { name: name, status: true };
|
|
132
137
|
}
|
|
133
138
|
else {
|
|
134
|
-
var msg =
|
|
135
|
-
if (typeof message ===
|
|
139
|
+
var msg = "";
|
|
140
|
+
if (typeof message === "function") {
|
|
136
141
|
msg = message(val, value, context);
|
|
137
142
|
}
|
|
138
143
|
else {
|
|
139
144
|
msg = message;
|
|
140
145
|
}
|
|
141
|
-
return { status: false, message: msg };
|
|
146
|
+
return { name: name, status: false, message: msg };
|
|
142
147
|
}
|
|
143
148
|
};
|
|
144
149
|
if (validators[name]) {
|