rxtutils 1.1.2-beta.1 → 1.1.2-beta.11

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.
Files changed (61) hide show
  1. package/README.md +144 -1
  2. package/{dist/cjs → cjs}/cache/index.cjs +28 -15
  3. package/cjs/defaultEquals.cjs +21 -0
  4. package/cjs/index.cjs +28 -0
  5. package/{dist/types → cjs}/index.d.ts +2 -0
  6. package/cjs/packages/cache/index.d.ts +141 -0
  7. package/cjs/packages/index.d.ts +7 -0
  8. package/cjs/packages/request/index.d.ts +140 -0
  9. package/cjs/packages/store/index.d.ts +2 -0
  10. package/cjs/packages/validator/index.d.ts +2 -0
  11. package/cjs/request/index.cjs +150 -0
  12. package/{dist/types → cjs}/request/index.d.ts +11 -8
  13. package/cjs/store/index.cjs +10 -0
  14. package/cjs/validator/decorators.cjs +246 -0
  15. package/cjs/validator/decorators.d.ts +159 -0
  16. package/cjs/validator/index.cjs +19 -0
  17. package/cjs/validator/validator.cjs +179 -0
  18. package/cjs/validator/validator.d.ts +89 -0
  19. package/es/cache/index.d.ts +135 -0
  20. package/{dist/es → es}/cache/index.mjs +28 -15
  21. package/es/cache/indexDB.d.ts +52 -0
  22. package/es/defaultEquals.mjs +19 -0
  23. package/es/index.d.ts +7 -0
  24. package/{dist/es → es}/index.mjs +2 -0
  25. package/es/packages/cache/index.d.ts +141 -0
  26. package/es/packages/index.d.ts +7 -0
  27. package/es/packages/request/index.d.ts +140 -0
  28. package/es/packages/store/index.d.ts +2 -0
  29. package/es/packages/validator/index.d.ts +2 -0
  30. package/es/request/error.d.ts +31 -0
  31. package/es/request/index.d.ts +139 -0
  32. package/es/request/index.mjs +148 -0
  33. package/es/store/createGetter/index.d.ts +30 -0
  34. package/es/store/createStateStore/index.d.ts +42 -0
  35. package/es/store/index.mjs +2 -0
  36. package/es/validator/decorators.d.ts +159 -0
  37. package/es/validator/decorators.mjs +234 -0
  38. package/es/validator/index.mjs +2 -0
  39. package/es/validator/validator.d.ts +89 -0
  40. package/es/validator/validator.mjs +177 -0
  41. package/package.json +9 -7
  42. package/dist/cjs/index.cjs +0 -14
  43. package/dist/cjs/request/index.cjs +0 -129
  44. package/dist/es/request/index.mjs +0 -127
  45. /package/{dist/cjs → cjs/_utils}/defaultEquals.cjs +0 -0
  46. /package/{dist/types → cjs}/cache/index.d.ts +0 -0
  47. /package/{dist/cjs → cjs}/cache/indexDB.cjs +0 -0
  48. /package/{dist/types → cjs}/cache/indexDB.d.ts +0 -0
  49. /package/{dist/cjs → cjs}/request/defaultHandlers.cjs +0 -0
  50. /package/{dist/cjs → cjs}/request/error.cjs +0 -0
  51. /package/{dist/types → cjs}/request/error.d.ts +0 -0
  52. /package/{dist/cjs → cjs}/store/createGetter/index.cjs +0 -0
  53. /package/{dist/types → cjs}/store/createGetter/index.d.ts +0 -0
  54. /package/{dist/cjs → cjs}/store/createStateStore/index.cjs +0 -0
  55. /package/{dist/types → cjs}/store/createStateStore/index.d.ts +0 -0
  56. /package/{dist/es → es/_utils}/defaultEquals.mjs +0 -0
  57. /package/{dist/es → es}/cache/indexDB.mjs +0 -0
  58. /package/{dist/es → es}/request/defaultHandlers.mjs +0 -0
  59. /package/{dist/es → es}/request/error.mjs +0 -0
  60. /package/{dist/es → es}/store/createGetter/index.mjs +0 -0
  61. /package/{dist/es → es}/store/createStateStore/index.mjs +0 -0
package/README.md CHANGED
@@ -17,6 +17,7 @@ pnpm add rxtutils
17
17
  - **缓存管理** - 支持内存、localStorage、sessionStorage 和 IndexedDB 多种存储方式
18
18
  - **HTTP 请求** - 基于 axios 的请求封装,支持错误处理和缓存
19
19
  - **状态管理** - 轻量级的状态管理解决方案,支持 React Hook
20
+ - **数据验证** - 基于装饰器的数据验证系统,支持多种验证规则
20
21
  - **TypeScript** - 完整的 TypeScript 类型支持
21
22
 
22
23
  ## 📚 模块介绍
@@ -198,7 +199,149 @@ const currentUser = userStore.get();
198
199
  userStore.set({ name: 'Jane Doe', email: 'jane@example.com', isLoggedIn: true });
199
200
  ```
200
201
 
201
- ### 4. 状态计算器 (createStoreGetter)
202
+ ### 4. 数据验证 (Validator)
203
+
204
+ 基于装饰器的数据验证系统,提供多种验证规则和自定义验证能力。
205
+
206
+ #### 基本用法
207
+
208
+ ```typescript
209
+ import { BaseValidator, VString, VNumber, VRequired, VEmail, VMinLength } from 'rxtutils';
210
+
211
+ // 创建验证模型
212
+ class User extends BaseValidator {
213
+ @VString('用户名必须为字符串')
214
+ @(VRequired()('用户名不能为空'))
215
+ name?: string;
216
+
217
+ @VNumber('年龄必须为数字')
218
+ @(VRequired()('年龄不能为空'))
219
+ age?: number;
220
+
221
+ @VEmail('邮箱格式不正确')
222
+ @(VRequired()('邮箱不能为空'))
223
+ email?: string;
224
+
225
+ @(VMinLength(6)('密码长度不能少于6位'))
226
+ @(VRequired()('密码不能为空'))
227
+ password?: string;
228
+ }
229
+
230
+ // 使用验证
231
+ const user = new User();
232
+ user.name = '张三';
233
+ user.age = 25;
234
+ user.email = 'invalid-email'; // 无效的邮箱格式
235
+ user.password = '123'; // 密码长度不足
236
+
237
+ // 验证单个字段
238
+ const emailErrors = user.validate('email');
239
+ console.log(emailErrors); // [{ status: false, message: '邮箱格式不正确' }]
240
+
241
+ // 验证所有字段
242
+ const allErrors = user.validateAll();
243
+ console.log(allErrors); // 返回所有验证错误
244
+ ```
245
+
246
+ #### 内置验证装饰器
247
+
248
+ ```typescript
249
+ // 基本类型验证
250
+ @VString('必须为字符串')
251
+ @VNumber('必须为数字')
252
+ @VBoolean('必须为布尔值')
253
+ @VArray('必须为数组')
254
+
255
+ // 必填验证
256
+ @(VRequired()('不能为空'))
257
+
258
+ // 范围验证
259
+ @(VMin(18)('必须大于等于18'))
260
+ @(VMax(100)('必须小于等于100'))
261
+
262
+ // 长度验证
263
+ @(VMinLength(6)('长度不能少于6位'))
264
+ @(VMaxLength(20)('长度不能超过20位'))
265
+
266
+ // 格式验证
267
+ @VEmail('邮箱格式不正确')
268
+ @(VPattern(/^1[3-9]\d{9}$/)('手机号格式不正确'))
269
+ ```
270
+
271
+ #### 自定义验证装饰器
272
+
273
+ ```typescript
274
+ import { BaseValidator } from 'rxtutils';
275
+
276
+ // 创建自定义验证装饰器
277
+ const VCustom = BaseValidator.decoratorCreator(
278
+ (val) => {
279
+ // 自定义验证逻辑
280
+ return typeof val === 'string' && val.startsWith('custom-');
281
+ }
282
+ );
283
+
284
+ // 使用自定义验证装饰器
285
+ class Product extends BaseValidator {
286
+ @VCustom('产品编码必须以 custom- 开头')
287
+ code?: string;
288
+ }
289
+ ```
290
+
291
+ #### 验证方法
292
+
293
+ BaseValidator 类提供了两个主要的验证方法:
294
+
295
+ ```typescript
296
+ // 验证单个字段
297
+ validate(itemKey: string, itemAll: boolean = false): { status: boolean; message?: string }[] | null;
298
+
299
+ // 验证多个或所有字段
300
+ validateAll(itemAll: boolean = false, everyItem: boolean = false, order?: string[]): { status: boolean; message?: string }[] | null;
301
+ ```
302
+
303
+ 参数说明:
304
+
305
+ - `itemKey`: 要验证的字段名
306
+ - `itemAll`: 是否验证该字段的所有规则,为 true 时会验证所有规则,为 false 时遇到第一个失败的规则就停止
307
+ - `everyItem`: 是否验证所有字段,为 true 时会验证所有字段,为 false 时遇到第一个失败的字段就停止
308
+ - `order`: 验证字段的顺序,可以指定验证的字段名数组及其顺序
309
+
310
+ 使用示例:
311
+
312
+ ```typescript
313
+ // 创建验证模型
314
+ class LoginForm extends BaseValidator {
315
+ @VString('用户名必须为字符串')
316
+ @(VRequired()('用户名不能为空'))
317
+ @(VMinLength(3)('用户名长度不能少于3位'))
318
+ @(VMaxLength(20)('用户名长度不能超过20位'))
319
+ username?: string;
320
+
321
+ @(VRequired()('密码不能为空'))
322
+ @(VMinLength(6)('密码长度不能少于6位'))
323
+ password?: string;
324
+ }
325
+
326
+ const form = new LoginForm();
327
+ form.username = 'ab'; // 长度不足
328
+ form.password = '123456';
329
+
330
+ // 验证单个字段的所有规则
331
+ const usernameErrors = form.validate('username', true);
332
+ console.log(usernameErrors);
333
+ // [{ status: false, message: '用户名长度不能少于3位' }]
334
+
335
+ // 验证所有字段,每个字段遇到第一个错误就停止
336
+ const allErrors = form.validateAll(false, true);
337
+ console.log(allErrors);
338
+
339
+ // 按指定顺序验证字段,并验证每个字段的所有规则
340
+ const orderedErrors = form.validateAll(true, true, ['password', 'username']);
341
+ console.log(orderedErrors);
342
+ ```
343
+
344
+ ### 5. 状态计算器 (createStoreGetter)
202
345
 
203
346
  为状态存储提供计算属性和派生状态。
204
347
 
@@ -5,7 +5,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var tslib = require('tslib');
6
6
  var moment = require('moment');
7
7
  var indexDB = require('./indexDB.cjs');
8
- var defaultEquals = require('../defaultEquals.cjs');
8
+ var defaultEquals = require('../_utils/defaultEquals.cjs');
9
9
 
10
10
  /** 存储类型映射表 */
11
11
  var StorageMap = {
@@ -18,8 +18,16 @@ var StorageMap = {
18
18
  * @template Data 缓存数据类型
19
19
  */
20
20
  var Cache = /** @class */ (function () {
21
+ /**
22
+ * 构造函数
23
+ * @param cacheType 存储类型
24
+ * @param cacheKey 缓存键名
25
+ * @param cacheTime 缓存时间(秒)
26
+ * @param indexDBName IndexedDB 数据库名称,默认值为 '__apiCacheDatabase__'
27
+ * @param cacheKeyEquals 缓存键比较函数,默认使用 defaultEquals
28
+ */
21
29
  function Cache(cacheType, cacheKey, cacheTime, indexDBName, cacheKeyEquals) {
22
- if (indexDBName === void 0) { indexDBName = '__apiCacheDatabase__'; }
30
+ if (indexDBName === void 0) { indexDBName = "__apiCacheDatabase__"; }
23
31
  if (cacheKeyEquals === void 0) { cacheKeyEquals = defaultEquals; }
24
32
  /** 内存中的缓存数组 */
25
33
  this.cache = [];
@@ -30,10 +38,10 @@ var Cache = /** @class */ (function () {
30
38
  indexDBName: indexDBName,
31
39
  cacheKeyEquals: cacheKeyEquals,
32
40
  };
33
- if (cacheType === 'indexedDB') {
34
- this.storage = new indexDB.IndexedDBStorage(indexDBName, 'cacheStore');
41
+ if (cacheType === "indexedDB") {
42
+ this.storage = new indexDB.IndexedDBStorage(indexDBName, "cacheStore");
35
43
  }
36
- else if (typeof cacheType === 'string') {
44
+ else if (typeof cacheType === "string") {
37
45
  this.storage = StorageMap[cacheType];
38
46
  }
39
47
  this._init();
@@ -55,15 +63,15 @@ var Cache = /** @class */ (function () {
55
63
  _d = (_c = JSON).parse;
56
64
  return [4 /*yield*/, this.storage.getItem(cacheKey)];
57
65
  case 1:
58
- _b.cache = _d.apply(_c, [(_e.sent()) || '[]']);
66
+ _b.cache = _d.apply(_c, [(_e.sent()) || "[]"]);
59
67
  return [3 /*break*/, 3];
60
68
  case 2:
61
69
  if (this.storage instanceof Storage) {
62
70
  this.storage = StorageMap[cacheType];
63
71
  if (this.storage) {
64
- if (typeof cacheKey === 'string') {
72
+ if (typeof cacheKey === "string") {
65
73
  try {
66
- this.cache = JSON.parse(this.storage.getItem(cacheKey) || '[]');
74
+ this.cache = JSON.parse(this.storage.getItem(cacheKey) || "[]");
67
75
  }
68
76
  catch (e) {
69
77
  this.cache = [];
@@ -80,7 +88,8 @@ var Cache = /** @class */ (function () {
80
88
  }
81
89
  });
82
90
  });
83
- }; /**
91
+ };
92
+ /**
84
93
  * 过滤掉已过期的缓存项
85
94
  * 通过比较当前时间和过期时间,移除过期的缓存项
86
95
  * @private
@@ -90,18 +99,20 @@ var Cache = /** @class */ (function () {
90
99
  return moment(item.expireTime).isAfter(moment());
91
100
  });
92
101
  this.cache = newCache;
93
- }; /**
102
+ };
103
+ /**
94
104
  * 将当前缓存数据保存到存储中
95
105
  * 如果设置了缓存键名且存储实例存在,则将缓存数据序列化后保存
96
106
  * @private
97
107
  */
98
108
  Cache.prototype._saveToStorage = function () {
99
109
  if (this.storage) {
100
- if (typeof this.cacheOptions.cacheKey === 'string') {
110
+ if (typeof this.cacheOptions.cacheKey === "string") {
101
111
  this.storage.setItem(this.cacheOptions.cacheKey, JSON.stringify(this.cache));
102
112
  }
103
113
  }
104
- }; /**
114
+ };
115
+ /**
105
116
  * 设置缓存数据
106
117
  * @param params 缓存的参数
107
118
  * @param data 要缓存的数据
@@ -118,10 +129,11 @@ var Cache = /** @class */ (function () {
118
129
  this.cache.push({
119
130
  params: params,
120
131
  data: data,
121
- expireTime: moment().add(cacheTime, 'seconds').toJSON(),
132
+ expireTime: moment().add(cacheTime, "seconds").toJSON(),
122
133
  });
123
134
  this._saveToStorage();
124
- }; /**
135
+ };
136
+ /**
125
137
  * 获取缓存数据
126
138
  * @param params 查询参数
127
139
  * @returns 如果找到有效的缓存数据则返回数据,否则返回 null
@@ -142,7 +154,8 @@ var Cache = /** @class */ (function () {
142
154
  }
143
155
  }
144
156
  return null;
145
- }; /**
157
+ };
158
+ /**
146
159
  * 清空所有缓存数据
147
160
  * 清空内存中的缓存数组并同步到存储中
148
161
  */
@@ -0,0 +1,21 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * 默认的相等性比较函数
5
+ * 通过将两个值序列化为 JSON 字符串来比较它们是否相等
6
+ *
7
+ * @template Param 比较值的类型,默认为 any
8
+ * @param prev 前一个值
9
+ * @param next 后一个值
10
+ * @returns {boolean} 如果两个值相等则返回 true,否则返回 false
11
+ *
12
+ * @remarks
13
+ * - 这个函数通过 JSON.stringify 进行比较,适用于大多数简单的数据结构
14
+ * - 不适用于包含函数、undefined、Symbol 等无法序列化的值
15
+ * - 对于循环引用的对象会抛出错误
16
+ */
17
+ function defaultEquals(prev, next) {
18
+ return JSON.stringify(prev) === JSON.stringify(next);
19
+ }
20
+
21
+ module.exports = defaultEquals;
package/cjs/index.cjs ADDED
@@ -0,0 +1,28 @@
1
+ 'use strict';
2
+
3
+ var index = require('./cache/index.cjs');
4
+ var index$1 = require('./request/index.cjs');
5
+ var index$3 = require('./store/createGetter/index.cjs');
6
+ var index$2 = require('./store/createStateStore/index.cjs');
7
+ var validator = require('./validator/validator.cjs');
8
+ var decorators = require('./validator/decorators.cjs');
9
+
10
+
11
+
12
+ exports.Cache = index.default;
13
+ exports.createBaseRequest = index$1;
14
+ exports.createStoreGetter = index$3.createStoreGetter;
15
+ exports.createStoreGetterMemo = index$3.createStoreGetterMemo;
16
+ exports.createStateStore = index$2.default;
17
+ exports.BaseValidator = validator.BaseValidator;
18
+ exports.VArray = decorators.VArray;
19
+ exports.VBoolean = decorators.VBoolean;
20
+ exports.VEmail = decorators.VEmail;
21
+ exports.VMax = decorators.VMax;
22
+ exports.VMaxLength = decorators.VMaxLength;
23
+ exports.VMin = decorators.VMin;
24
+ exports.VMinLength = decorators.VMinLength;
25
+ exports.VNumber = decorators.VNumber;
26
+ exports.VPattern = decorators.VPattern;
27
+ exports.VRequired = decorators.VRequired;
28
+ exports.VString = decorators.VString;
@@ -2,4 +2,6 @@ export { default as Cache, ICache, ICacheOptions, StorageMap, StorageType } from
2
2
  export { ErrorHandlerReturnType, Options, RequestOptions, default as createBaseRequest } from './request/index.js';
3
3
  export { createStoreGetter, createStoreGetterMemo } from './store/createGetter/index.js';
4
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';
5
7
  export { default as RequestError, RequestErrorType } from './request/error.js';
@@ -0,0 +1,141 @@
1
+ import { IndexedDBStorage } from '../../cache/indexDB.js';
2
+
3
+ /**
4
+ * 缓存存储类型
5
+ * - sessionStorage: 会话存储,浏览器关闭后清除
6
+ * - localStorage: 本地存储,永久保存
7
+ * - indexedDB: IndexedDB 数据库存储
8
+ */
9
+ type StorageType = "sessionStorage" | "localStorage" | "indexedDB";
10
+ /**
11
+ * 缓存项接口定义
12
+ * 定义了单个缓存项的数据结构
13
+ *
14
+ * @template Param 缓存参数类型
15
+ * @template Data 缓存数据类型
16
+ */
17
+ interface ICache<Param, Data> {
18
+ /**
19
+ * 缓存的参数
20
+ * 用于标识和查找缓存项
21
+ */
22
+ params: Param;
23
+ /**
24
+ * 缓存的数据
25
+ * 实际存储的内容
26
+ */
27
+ data: Data;
28
+ /**
29
+ * 过期时间
30
+ * - ISO 8601 格式的字符串
31
+ * - 由 moment().add(cacheTime, 'seconds').toJSON() 生成
32
+ * - 示例:'2025-06-12T10:30:00.000Z'
33
+ */
34
+ expireTime: string;
35
+ }
36
+ /**
37
+ * 缓存选项接口
38
+ * @template Param 缓存参数类型
39
+ */
40
+ interface ICacheOptions<Param> {
41
+ /**
42
+ * 存储类型
43
+ * - 'sessionStorage': 会话存储,浏览器关闭后清除
44
+ * - 'localStorage': 本地存储,永久保存
45
+ * - 'indexedDB': IndexedDB 数据库存储
46
+ * - undefined: 仅在内存中缓存(默认值)
47
+ */
48
+ storageType?: StorageType;
49
+ /**
50
+ * 缓存键名
51
+ * - 当使用 localStorage/sessionStorage 时必须提供
52
+ * - 用于在存储中标识不同的缓存数据
53
+ * @default undefined 不使用持久化存储
54
+ */
55
+ cacheKey?: string;
56
+ /**
57
+ * 缓存时间(秒)
58
+ * - 超过这个时间的缓存项会被自动清除
59
+ * @default 60 一分钟
60
+ */
61
+ cacheTime?: number;
62
+ /**
63
+ * 缓存键比较函数
64
+ * - 用于判断两个缓存参数是否相等
65
+ * - 相等则认为是同一个缓存项
66
+ * @param prev 前一个参数
67
+ * @param next 后一个参数
68
+ * @returns 是否相等
69
+ * @default defaultEquals 使用 JSON.stringify 进行比较
70
+ */
71
+ cacheKeyEquals: (prev: Param, next: Param) => boolean;
72
+ /**
73
+ * IndexedDB 数据库名称
74
+ * - 仅在 storageType 为 'indexedDB' 时使用
75
+ * @default '__apiCacheDatabase__'
76
+ */
77
+ indexDBName?: string;
78
+ }
79
+ /** 存储类型映射表 */
80
+ declare const StorageMap: Record<StorageType | string, Storage>;
81
+ /**
82
+ * 缓存类
83
+ * @template Param 缓存参数类型
84
+ * @template Data 缓存数据类型
85
+ */
86
+ declare class Cache<Param, Data> {
87
+ /** 内存中的缓存数组 */
88
+ cache: ICache<Param, Data>[];
89
+ /** 缓存选项 */
90
+ private cacheOptions;
91
+ /** 存储实例 */
92
+ storage?: Storage | IndexedDBStorage;
93
+ /**
94
+ * 构造函数
95
+ * @param cacheType 存储类型
96
+ * @param cacheKey 缓存键名
97
+ * @param cacheTime 缓存时间(秒)
98
+ * @param indexDBName IndexedDB 数据库名称,默认值为 '__apiCacheDatabase__'
99
+ * @param cacheKeyEquals 缓存键比较函数,默认使用 defaultEquals
100
+ */
101
+ constructor(cacheType?: StorageType, cacheKey?: string, cacheTime?: number, indexDBName?: string, cacheKeyEquals?: (prev: Param, next: Param) => boolean);
102
+ /**
103
+ * 初始化缓存
104
+ * 从存储中加载已保存的缓存数据,并进行解析和过期处理
105
+ * @private
106
+ */
107
+ private _init;
108
+ /**
109
+ * 过滤掉已过期的缓存项
110
+ * 通过比较当前时间和过期时间,移除过期的缓存项
111
+ * @private
112
+ */
113
+ private _filterExpired;
114
+ /**
115
+ * 将当前缓存数据保存到存储中
116
+ * 如果设置了缓存键名且存储实例存在,则将缓存数据序列化后保存
117
+ * @private
118
+ */
119
+ private _saveToStorage;
120
+ /**
121
+ * 设置缓存数据
122
+ * @param params 缓存的参数
123
+ * @param data 要缓存的数据
124
+ * @param cacheOptions 可选的缓存配置,可以覆盖默认的缓存时间
125
+ */
126
+ setCache(params: Param, data: Data, cacheOptions?: Omit<ICacheOptions<Param>, "storageType" | "cacheKey" | "cacheKeyEquals">): void;
127
+ /**
128
+ * 获取缓存数据
129
+ * @param params 查询参数
130
+ * @returns 如果找到有效的缓存数据则返回数据,否则返回 null
131
+ */
132
+ getCache(params: Param): Data;
133
+ /**
134
+ * 清空所有缓存数据
135
+ * 清空内存中的缓存数组并同步到存储中
136
+ */
137
+ clear(): void;
138
+ }
139
+
140
+ export { StorageMap, Cache as default };
141
+ export type { ICache, ICacheOptions, StorageType };
@@ -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,140 @@
1
+ import { AxiosResponse, Method, AxiosRequestConfig } from 'axios';
2
+ import { StorageType } from '../cache/index.js';
3
+ export { default as RequestError, RequestErrorType } from '../../request/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 };
@@ -0,0 +1,2 @@
1
+ export { GetterNameMap, ReducedData, StoreGetter, createStoreGetter, createStoreGetterMemo } from '../../store/createGetter/index.js';
2
+ export { IHookStateInitAction, IHookStateInitialSetter, IHookStateResolvable, IHookStateSetAction, IHookStateSetter, default as createStateStore } from '../../store/createStateStore/index.js';
@@ -0,0 +1,2 @@
1
+ export { BaseValidator } from '../../validator/validator.js';
2
+ export { VArray, VBoolean, VEmail, VMax, VMaxLength, VMin, VMinLength, VNumber, VPattern, VRequired, VString } from '../../validator/decorators.js';