rxtutils 1.1.2-beta.9 → 1.1.4-beta.1
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/cjs/hooks/index.cjs +7 -0
- package/cjs/hooks/index.d.ts +1 -0
- package/cjs/hooks/useCombineControlValue.cjs +32 -0
- package/cjs/hooks/useCombineControlValue.d.ts +18 -0
- package/{dist/cjs → cjs}/request/index.cjs +3 -3
- package/{dist/types → cjs}/request/index.d.ts +1 -0
- 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/hooks/index.d.ts +1 -0
- package/es/hooks/index.mjs +1 -0
- package/es/hooks/useCombineControlValue.d.ts +18 -0
- package/es/hooks/useCombineControlValue.mjs +30 -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
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { __awaiter, __generator, __assign } from 'tslib';
|
|
2
2
|
import moment from 'moment';
|
|
3
3
|
import { IndexedDBStorage } from './indexDB.mjs';
|
|
4
|
-
import defaultEquals from '../defaultEquals.mjs';
|
|
4
|
+
import defaultEquals from '../_utils/defaultEquals.mjs';
|
|
5
5
|
|
|
6
6
|
/** 存储类型映射表 */
|
|
7
7
|
var StorageMap = {
|
|
@@ -14,8 +14,16 @@ var StorageMap = {
|
|
|
14
14
|
* @template Data 缓存数据类型
|
|
15
15
|
*/
|
|
16
16
|
var Cache = /** @class */ (function () {
|
|
17
|
+
/**
|
|
18
|
+
* 构造函数
|
|
19
|
+
* @param cacheType 存储类型
|
|
20
|
+
* @param cacheKey 缓存键名
|
|
21
|
+
* @param cacheTime 缓存时间(秒)
|
|
22
|
+
* @param indexDBName IndexedDB 数据库名称,默认值为 '__apiCacheDatabase__'
|
|
23
|
+
* @param cacheKeyEquals 缓存键比较函数,默认使用 defaultEquals
|
|
24
|
+
*/
|
|
17
25
|
function Cache(cacheType, cacheKey, cacheTime, indexDBName, cacheKeyEquals) {
|
|
18
|
-
if (indexDBName === void 0) { indexDBName =
|
|
26
|
+
if (indexDBName === void 0) { indexDBName = "__apiCacheDatabase__"; }
|
|
19
27
|
if (cacheKeyEquals === void 0) { cacheKeyEquals = defaultEquals; }
|
|
20
28
|
/** 内存中的缓存数组 */
|
|
21
29
|
this.cache = [];
|
|
@@ -26,10 +34,10 @@ var Cache = /** @class */ (function () {
|
|
|
26
34
|
indexDBName: indexDBName,
|
|
27
35
|
cacheKeyEquals: cacheKeyEquals,
|
|
28
36
|
};
|
|
29
|
-
if (cacheType ===
|
|
30
|
-
this.storage = new IndexedDBStorage(indexDBName,
|
|
37
|
+
if (cacheType === "indexedDB") {
|
|
38
|
+
this.storage = new IndexedDBStorage(indexDBName, "cacheStore");
|
|
31
39
|
}
|
|
32
|
-
else if (typeof cacheType ===
|
|
40
|
+
else if (typeof cacheType === "string") {
|
|
33
41
|
this.storage = StorageMap[cacheType];
|
|
34
42
|
}
|
|
35
43
|
this._init();
|
|
@@ -51,15 +59,15 @@ var Cache = /** @class */ (function () {
|
|
|
51
59
|
_d = (_c = JSON).parse;
|
|
52
60
|
return [4 /*yield*/, this.storage.getItem(cacheKey)];
|
|
53
61
|
case 1:
|
|
54
|
-
_b.cache = _d.apply(_c, [(_e.sent()) ||
|
|
62
|
+
_b.cache = _d.apply(_c, [(_e.sent()) || "[]"]);
|
|
55
63
|
return [3 /*break*/, 3];
|
|
56
64
|
case 2:
|
|
57
65
|
if (this.storage instanceof Storage) {
|
|
58
66
|
this.storage = StorageMap[cacheType];
|
|
59
67
|
if (this.storage) {
|
|
60
|
-
if (typeof cacheKey ===
|
|
68
|
+
if (typeof cacheKey === "string") {
|
|
61
69
|
try {
|
|
62
|
-
this.cache = JSON.parse(this.storage.getItem(cacheKey) ||
|
|
70
|
+
this.cache = JSON.parse(this.storage.getItem(cacheKey) || "[]");
|
|
63
71
|
}
|
|
64
72
|
catch (e) {
|
|
65
73
|
this.cache = [];
|
|
@@ -76,7 +84,8 @@ var Cache = /** @class */ (function () {
|
|
|
76
84
|
}
|
|
77
85
|
});
|
|
78
86
|
});
|
|
79
|
-
};
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
80
89
|
* 过滤掉已过期的缓存项
|
|
81
90
|
* 通过比较当前时间和过期时间,移除过期的缓存项
|
|
82
91
|
* @private
|
|
@@ -86,18 +95,20 @@ var Cache = /** @class */ (function () {
|
|
|
86
95
|
return moment(item.expireTime).isAfter(moment());
|
|
87
96
|
});
|
|
88
97
|
this.cache = newCache;
|
|
89
|
-
};
|
|
98
|
+
};
|
|
99
|
+
/**
|
|
90
100
|
* 将当前缓存数据保存到存储中
|
|
91
101
|
* 如果设置了缓存键名且存储实例存在,则将缓存数据序列化后保存
|
|
92
102
|
* @private
|
|
93
103
|
*/
|
|
94
104
|
Cache.prototype._saveToStorage = function () {
|
|
95
105
|
if (this.storage) {
|
|
96
|
-
if (typeof this.cacheOptions.cacheKey ===
|
|
106
|
+
if (typeof this.cacheOptions.cacheKey === "string") {
|
|
97
107
|
this.storage.setItem(this.cacheOptions.cacheKey, JSON.stringify(this.cache));
|
|
98
108
|
}
|
|
99
109
|
}
|
|
100
|
-
};
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
101
112
|
* 设置缓存数据
|
|
102
113
|
* @param params 缓存的参数
|
|
103
114
|
* @param data 要缓存的数据
|
|
@@ -114,10 +125,11 @@ var Cache = /** @class */ (function () {
|
|
|
114
125
|
this.cache.push({
|
|
115
126
|
params: params,
|
|
116
127
|
data: data,
|
|
117
|
-
expireTime: moment().add(cacheTime,
|
|
128
|
+
expireTime: moment().add(cacheTime, "seconds").toJSON(),
|
|
118
129
|
});
|
|
119
130
|
this._saveToStorage();
|
|
120
|
-
};
|
|
131
|
+
};
|
|
132
|
+
/**
|
|
121
133
|
* 获取缓存数据
|
|
122
134
|
* @param params 查询参数
|
|
123
135
|
* @returns 如果找到有效的缓存数据则返回数据,否则返回 null
|
|
@@ -138,7 +150,8 @@ var Cache = /** @class */ (function () {
|
|
|
138
150
|
}
|
|
139
151
|
}
|
|
140
152
|
return null;
|
|
141
|
-
};
|
|
153
|
+
};
|
|
154
|
+
/**
|
|
142
155
|
* 清空所有缓存数据
|
|
143
156
|
* 清空内存中的缓存数组并同步到存储中
|
|
144
157
|
*/
|
|
@@ -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 };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useCombineControlValue } from './useCombineControlValue.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useCombineControlValue } from './useCombineControlValue.mjs';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param param props 组件属性
|
|
3
|
+
* @param param valueKey 组件值的key,默认value, undefined 被认为是有值,只有当 key 不存在时,转换为非受控模式
|
|
4
|
+
* @param param defaultValue 默认值
|
|
5
|
+
* @param param onChange 值改变时的回调
|
|
6
|
+
* @returns value: 组件应该采用的值,onChange:值改变时的回调,处理非受控值与向父组件传递值的逻辑
|
|
7
|
+
*/
|
|
8
|
+
declare const useCombineControlValue: <V>({ props, valueKey, defaultValue, onChange }: {
|
|
9
|
+
props: Record<string, any>;
|
|
10
|
+
valueKey?: string;
|
|
11
|
+
defaultValue?: V;
|
|
12
|
+
onChange?: (val: V) => void;
|
|
13
|
+
}) => {
|
|
14
|
+
value: any;
|
|
15
|
+
onChange: (nextVal: V) => void;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export { useCombineControlValue };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { useState, useCallback, useMemo } from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @param param props 组件属性
|
|
5
|
+
* @param param valueKey 组件值的key,默认value, undefined 被认为是有值,只有当 key 不存在时,转换为非受控模式
|
|
6
|
+
* @param param defaultValue 默认值
|
|
7
|
+
* @param param onChange 值改变时的回调
|
|
8
|
+
* @returns value: 组件应该采用的值,onChange:值改变时的回调,处理非受控值与向父组件传递值的逻辑
|
|
9
|
+
*/
|
|
10
|
+
var useCombineControlValue = function (_a) {
|
|
11
|
+
var props = _a.props, _b = _a.valueKey, valueKey = _b === void 0 ? 'value' : _b, defaultValue = _a.defaultValue, onChange = _a.onChange;
|
|
12
|
+
var _c = props, _d = valueKey, value = _c[_d];
|
|
13
|
+
var hasValue = props.hasOwnProperty(valueKey);
|
|
14
|
+
var _e = useState(value !== null && value !== void 0 ? value : defaultValue), internalValue = _e[0], setInternalValue = _e[1];
|
|
15
|
+
var handleChange = useCallback(function (nextVal) {
|
|
16
|
+
setInternalValue(nextVal);
|
|
17
|
+
onChange === null || onChange === void 0 ? void 0 : onChange(nextVal);
|
|
18
|
+
}, [onChange]);
|
|
19
|
+
var finalValue = useMemo(function () {
|
|
20
|
+
if (hasValue)
|
|
21
|
+
return value;
|
|
22
|
+
return internalValue;
|
|
23
|
+
}, [hasValue, internalValue, value]);
|
|
24
|
+
return {
|
|
25
|
+
value: finalValue,
|
|
26
|
+
onChange: handleChange
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export { useCombineControlValue };
|
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 };
|