rxtutils 1.1.2-beta.9 → 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.
Files changed (50) hide show
  1. package/README.md +2 -2
  2. package/{dist/cjs → cjs}/cache/index.cjs +28 -15
  3. package/{dist/types → cjs}/cache/index.d.ts +13 -7
  4. package/{dist/cjs → cjs}/request/index.cjs +3 -3
  5. package/{dist/types → cjs}/request/index.d.ts +1 -0
  6. package/cjs/store/index.cjs +10 -0
  7. package/cjs/store/index.d.ts +2 -0
  8. package/cjs/validator/index.cjs +19 -0
  9. package/cjs/validator/index.d.ts +2 -0
  10. package/{dist/cjs → cjs}/validator/validator.cjs +19 -14
  11. package/{dist/types → cjs}/validator/validator.d.ts +5 -10
  12. package/es/cache/index.d.ts +141 -0
  13. package/{dist/es → es}/cache/index.mjs +28 -15
  14. package/es/cache/indexDB.d.ts +52 -0
  15. package/es/index.d.ts +7 -0
  16. package/es/request/error.d.ts +31 -0
  17. package/es/request/index.d.ts +140 -0
  18. package/{dist/es → es}/request/index.mjs +2 -2
  19. package/es/store/createGetter/index.d.ts +30 -0
  20. package/es/store/createStateStore/index.d.ts +42 -0
  21. package/es/store/index.d.ts +2 -0
  22. package/es/store/index.mjs +2 -0
  23. package/es/validator/decorators.d.ts +159 -0
  24. package/es/validator/index.d.ts +2 -0
  25. package/es/validator/index.mjs +2 -0
  26. package/es/validator/validator.d.ts +84 -0
  27. package/{dist/es → es}/validator/validator.mjs +19 -14
  28. package/package.json +12 -9
  29. /package/{dist/cjs → cjs/_utils}/defaultEquals.cjs +0 -0
  30. /package/{dist/cjs → cjs}/cache/indexDB.cjs +0 -0
  31. /package/{dist/types → cjs}/cache/indexDB.d.ts +0 -0
  32. /package/{dist/cjs → cjs}/index.cjs +0 -0
  33. /package/{dist/types → cjs}/index.d.ts +0 -0
  34. /package/{dist/cjs → cjs}/request/defaultHandlers.cjs +0 -0
  35. /package/{dist/cjs → cjs}/request/error.cjs +0 -0
  36. /package/{dist/types → cjs}/request/error.d.ts +0 -0
  37. /package/{dist/cjs → cjs}/store/createGetter/index.cjs +0 -0
  38. /package/{dist/types → cjs}/store/createGetter/index.d.ts +0 -0
  39. /package/{dist/cjs → cjs}/store/createStateStore/index.cjs +0 -0
  40. /package/{dist/types → cjs}/store/createStateStore/index.d.ts +0 -0
  41. /package/{dist/cjs → cjs}/validator/decorators.cjs +0 -0
  42. /package/{dist/types → cjs}/validator/decorators.d.ts +0 -0
  43. /package/{dist/es → es/_utils}/defaultEquals.mjs +0 -0
  44. /package/{dist/es → es}/cache/indexDB.mjs +0 -0
  45. /package/{dist/es → es}/index.mjs +0 -0
  46. /package/{dist/es → es}/request/defaultHandlers.mjs +0 -0
  47. /package/{dist/es → es}/request/error.mjs +0 -0
  48. /package/{dist/es → es}/store/createGetter/index.mjs +0 -0
  49. /package/{dist/es → es}/store/createStateStore/index.mjs +0 -0
  50. /package/{dist/es → es}/validator/decorators.mjs +0 -0
package/README.md CHANGED
@@ -294,10 +294,10 @@ BaseValidator 类提供了两个主要的验证方法:
294
294
 
295
295
  ```typescript
296
296
  // 验证单个字段
297
- validate(itemKey: string, itemAll: boolean = false): { status: boolean; message?: string }[] | null;
297
+ validate(itemKey: string, itemAll: boolean = false): string[] | null;
298
298
 
299
299
  // 验证多个或所有字段
300
- validateAll(itemAll: boolean = false, everyItem: boolean = false, order?: string[]): { status: boolean; message?: string }[] | null;
300
+ validateAll(itemAll: boolean = false, everyItem: boolean = false, order?: string[]): string[] | null;
301
301
  ```
302
302
 
303
303
  参数说明:
@@ -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
  */
@@ -6,7 +6,7 @@ import { IndexedDBStorage } from './indexDB.js';
6
6
  * - localStorage: 本地存储,永久保存
7
7
  * - indexedDB: IndexedDB 数据库存储
8
8
  */
9
- type StorageType = 'sessionStorage' | 'localStorage' | 'indexedDB';
9
+ type StorageType = "sessionStorage" | "localStorage" | "indexedDB";
10
10
  /**
11
11
  * 缓存项接口定义
12
12
  * 定义了单个缓存项的数据结构
@@ -89,7 +89,8 @@ declare class Cache<Param, Data> {
89
89
  /** 缓存选项 */
90
90
  private cacheOptions;
91
91
  /** 存储实例 */
92
- storage?: Storage | IndexedDBStorage; /**
92
+ storage?: Storage | IndexedDBStorage;
93
+ /**
93
94
  * 构造函数
94
95
  * @param cacheType 存储类型
95
96
  * @param cacheKey 缓存键名
@@ -103,28 +104,33 @@ declare class Cache<Param, Data> {
103
104
  * 从存储中加载已保存的缓存数据,并进行解析和过期处理
104
105
  * @private
105
106
  */
106
- private _init; /**
107
+ private _init;
108
+ /**
107
109
  * 过滤掉已过期的缓存项
108
110
  * 通过比较当前时间和过期时间,移除过期的缓存项
109
111
  * @private
110
112
  */
111
- private _filterExpired; /**
113
+ private _filterExpired;
114
+ /**
112
115
  * 将当前缓存数据保存到存储中
113
116
  * 如果设置了缓存键名且存储实例存在,则将缓存数据序列化后保存
114
117
  * @private
115
118
  */
116
- private _saveToStorage; /**
119
+ private _saveToStorage;
120
+ /**
117
121
  * 设置缓存数据
118
122
  * @param params 缓存的参数
119
123
  * @param data 要缓存的数据
120
124
  * @param cacheOptions 可选的缓存配置,可以覆盖默认的缓存时间
121
125
  */
122
- setCache(params: Param, data: Data, cacheOptions?: Omit<ICacheOptions<Param>, 'storageType' | 'cacheKey' | 'cacheKeyEquals'>): void; /**
126
+ setCache(params: Param, data: Data, cacheOptions?: Omit<ICacheOptions<Param>, "storageType" | "cacheKey" | "cacheKeyEquals">): void;
127
+ /**
123
128
  * 获取缓存数据
124
129
  * @param params 查询参数
125
130
  * @returns 如果找到有效的缓存数据则返回数据,否则返回 null
126
131
  */
127
- getCache(params: Param): Data; /**
132
+ getCache(params: Param): Data;
133
+ /**
128
134
  * 清空所有缓存数据
129
135
  * 清空内存中的缓存数组并同步到存储中
130
136
  */
@@ -2,9 +2,9 @@
2
2
 
3
3
  var tslib = require('tslib');
4
4
  var axios = require('axios');
5
- var lodash = require('lodash');
5
+ var at = require('lodash-es/at');
6
6
  var defaultHandlers = require('./defaultHandlers.cjs');
7
- var defaultEquals = require('../defaultEquals.cjs');
7
+ var defaultEquals = require('../_utils/defaultEquals.cjs');
8
8
  var index = require('../cache/index.cjs');
9
9
  var error = require('./error.cjs');
10
10
 
@@ -54,7 +54,7 @@ function createBaseRequest(baseOptions) {
54
54
  return tslib.__generator(this, function (_h) {
55
55
  switch (_h.label) {
56
56
  case 0:
57
- errorCode = String(lodash.at(res.data, errorCodePath));
57
+ errorCode = String(at(res.data, errorCodePath));
58
58
  if (successCodes.includes(errorCode)) {
59
59
  if (cacheData) {
60
60
  cache.setCache(requestDataOrParams, res.data, { cacheTime: cacheTime });
@@ -1,5 +1,6 @@
1
1
  import { AxiosResponse, Method, AxiosRequestConfig } from 'axios';
2
2
  import { StorageType } from '../cache/index.js';
3
+ export { default as RequestError, RequestErrorType } from './error.js';
3
4
 
4
5
  /**
5
6
  * 错误处理器返回类型
@@ -0,0 +1,10 @@
1
+ 'use strict';
2
+
3
+ var index = require('./createGetter/index.cjs');
4
+ var index$1 = require('./createStateStore/index.cjs');
5
+
6
+
7
+
8
+ exports.createStoreGetter = index.createStoreGetter;
9
+ exports.createStoreGetterMemo = index.createStoreGetterMemo;
10
+ exports.createStateStore = index$1.default;
@@ -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,19 @@
1
+ 'use strict';
2
+
3
+ var validator = require('./validator.cjs');
4
+ var decorators = require('./decorators.cjs');
5
+
6
+
7
+
8
+ exports.BaseValidator = validator.BaseValidator;
9
+ exports.VArray = decorators.VArray;
10
+ exports.VBoolean = decorators.VBoolean;
11
+ exports.VEmail = decorators.VEmail;
12
+ exports.VMax = decorators.VMax;
13
+ exports.VMaxLength = decorators.VMaxLength;
14
+ exports.VMin = decorators.VMin;
15
+ exports.VMinLength = decorators.VMinLength;
16
+ exports.VNumber = decorators.VNumber;
17
+ exports.VPattern = decorators.VPattern;
18
+ exports.VRequired = decorators.VRequired;
19
+ exports.VString = decorators.VString;
@@ -0,0 +1,2 @@
1
+ export { BaseValidator } from './validator.js';
2
+ export { VArray, VBoolean, VEmail, VMax, VMaxLength, VMin, VMinLength, VNumber, VPattern, VRequired, VString } from './decorators.js';
@@ -13,14 +13,14 @@ var BaseValidator = /** @class */ (function () {
13
13
  */
14
14
  function BaseValidator() {
15
15
  /** 用于存储验证器映射的私有符号 */
16
- this.__keySymbol = Symbol('key-description');
17
- this.__keySymbol = Symbol('key-description');
16
+ this.__keySymbol = Symbol("key-description");
17
+ this.__keySymbol = Symbol("key-description");
18
18
  this[this.__keySymbol] = {};
19
19
  }
20
20
  /**
21
21
  * 验证单个字段
22
- * @param itemKey 要验证的字段名
23
22
  * @param itemAll 是否验证所有规则,为true时会验证该字段的所有规则,为false时遇到第一个失败的规则就停止
23
+ * @param itemKey 要验证的字段名
24
24
  * @returns 验证错误数组,如果没有错误则返回null
25
25
  */
26
26
  BaseValidator.prototype.validate = function (itemKey, itemAll) {
@@ -30,13 +30,13 @@ var BaseValidator = /** @class */ (function () {
30
30
  // 校验每个 key
31
31
  var validators = validatorMap[itemKey];
32
32
  if (!validators) {
33
- return errors;
33
+ return null;
34
34
  }
35
35
  for (var _i = 0, validators_1 = validators; _i < validators_1.length; _i++) {
36
36
  var validator = validators_1[_i];
37
37
  var res = validator(this[itemKey]);
38
38
  if (!res.status) {
39
- errors.push(res);
39
+ errors.push(res.message);
40
40
  if (!itemAll)
41
41
  break;
42
42
  }
@@ -48,16 +48,16 @@ var BaseValidator = /** @class */ (function () {
48
48
  };
49
49
  /**
50
50
  * 验证多个或所有字段
51
+ * @param order 验证字段的顺序,可以指定验证的字段名数组及其顺序
51
52
  * @param itemAll 是否验证每个字段的所有规则,为true时会验证字段的所有规则,为false时遇到第一个失败的规则就停止
52
53
  * @param everyItem 是否验证所有字段,为true时会验证所有字段,为false时遇到第一个失败的字段就停止
53
- * @param order 验证字段的顺序,可以指定验证的字段名数组及其顺序
54
54
  * @returns 验证错误数组,如果没有错误则返回null
55
55
  */
56
- BaseValidator.prototype.validateAll = function (itemAll, everyItem, order) {
56
+ BaseValidator.prototype.validateAll = function (order, itemAll, everyItem) {
57
57
  if (itemAll === void 0) { itemAll = false; }
58
58
  if (everyItem === void 0) { everyItem = false; }
59
59
  var validatorMap = this[this.__keySymbol];
60
- var errors = [];
60
+ var errors = {};
61
61
  // 校验每个 key
62
62
  var keys = order || Object.keys(validatorMap);
63
63
  for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
@@ -69,7 +69,12 @@ var BaseValidator = /** @class */ (function () {
69
69
  var fn = fns_1[_a];
70
70
  var res = fn(value);
71
71
  if (!res.status) {
72
- errors.push(res);
72
+ if (Array.isArray(errors[res.name])) {
73
+ errors[res.name].push(res.message);
74
+ }
75
+ else {
76
+ errors[res.name] = [res.message];
77
+ }
73
78
  if (!itemAll)
74
79
  break;
75
80
  }
@@ -78,7 +83,7 @@ var BaseValidator = /** @class */ (function () {
78
83
  break;
79
84
  }
80
85
  }
81
- if (errors.length) {
86
+ if (Object.keys(errors).length) {
82
87
  return errors;
83
88
  }
84
89
  return null;
@@ -130,17 +135,17 @@ var BaseValidator = /** @class */ (function () {
130
135
  var validator = function (val) {
131
136
  var validateStatus = func(val, value, context);
132
137
  if (validateStatus) {
133
- return { status: true };
138
+ return { name: name, status: true };
134
139
  }
135
140
  else {
136
- var msg = '';
137
- if (typeof message === 'function') {
141
+ var msg = "";
142
+ if (typeof message === "function") {
138
143
  msg = message(val, value, context);
139
144
  }
140
145
  else {
141
146
  msg = message;
142
147
  }
143
- return { status: false, message: msg };
148
+ return { name: name, status: false, message: msg };
144
149
  }
145
150
  };
146
151
  if (validators[name]) {
@@ -4,6 +4,7 @@
4
4
  * @returns 包含验证状态和可选错误消息的对象
5
5
  */
6
6
  type Validator = (val: any) => {
7
+ name: string;
7
8
  status: boolean;
8
9
  message?: string;
9
10
  };
@@ -31,25 +32,19 @@ declare class BaseValidator {
31
32
  constructor();
32
33
  /**
33
34
  * 验证单个字段
34
- * @param itemKey 要验证的字段名
35
35
  * @param itemAll 是否验证所有规则,为true时会验证该字段的所有规则,为false时遇到第一个失败的规则就停止
36
+ * @param itemKey 要验证的字段名
36
37
  * @returns 验证错误数组,如果没有错误则返回null
37
38
  */
38
- validate(itemKey: string, itemAll?: boolean): {
39
- status: boolean;
40
- message?: string;
41
- }[];
39
+ validate(itemKey: string, itemAll?: boolean): string[] | null;
42
40
  /**
43
41
  * 验证多个或所有字段
42
+ * @param order 验证字段的顺序,可以指定验证的字段名数组及其顺序
44
43
  * @param itemAll 是否验证每个字段的所有规则,为true时会验证字段的所有规则,为false时遇到第一个失败的规则就停止
45
44
  * @param everyItem 是否验证所有字段,为true时会验证所有字段,为false时遇到第一个失败的字段就停止
46
- * @param order 验证字段的顺序,可以指定验证的字段名数组及其顺序
47
45
  * @returns 验证错误数组,如果没有错误则返回null
48
46
  */
49
- validateAll(itemAll?: boolean, everyItem?: boolean, order?: string[]): {
50
- status: boolean;
51
- message?: string;
52
- }[];
47
+ validateAll(order?: string[], itemAll?: boolean, everyItem?: boolean): Record<string, string[]> | null;
53
48
  /**
54
49
  * 装饰器创建器
55
50
  * 用于创建属性验证装饰器的工厂函数
@@ -0,0 +1,141 @@
1
+ import { IndexedDBStorage } from './indexDB.js';
2
+
3
+ /**
4
+ * 缓存存储类型
5
+ * - sessionStorage: 会话存储,浏览器关闭后清除
6
+ * - localStorage: 本地存储,永久保存
7
+ * - indexedDB: IndexedDB 数据库存储
8
+ */
9
+ type StorageType = "sessionStorage" | "localStorage" | "indexedDB";
10
+ /**
11
+ * 缓存项接口定义
12
+ * 定义了单个缓存项的数据结构
13
+ *
14
+ * @template Param 缓存参数类型
15
+ * @template Data 缓存数据类型
16
+ */
17
+ interface ICache<Param, Data> {
18
+ /**
19
+ * 缓存的参数
20
+ * 用于标识和查找缓存项
21
+ */
22
+ params: Param;
23
+ /**
24
+ * 缓存的数据
25
+ * 实际存储的内容
26
+ */
27
+ data: Data;
28
+ /**
29
+ * 过期时间
30
+ * - ISO 8601 格式的字符串
31
+ * - 由 moment().add(cacheTime, 'seconds').toJSON() 生成
32
+ * - 示例:'2025-06-12T10:30:00.000Z'
33
+ */
34
+ expireTime: string;
35
+ }
36
+ /**
37
+ * 缓存选项接口
38
+ * @template Param 缓存参数类型
39
+ */
40
+ interface ICacheOptions<Param> {
41
+ /**
42
+ * 存储类型
43
+ * - 'sessionStorage': 会话存储,浏览器关闭后清除
44
+ * - 'localStorage': 本地存储,永久保存
45
+ * - 'indexedDB': IndexedDB 数据库存储
46
+ * - undefined: 仅在内存中缓存(默认值)
47
+ */
48
+ storageType?: StorageType;
49
+ /**
50
+ * 缓存键名
51
+ * - 当使用 localStorage/sessionStorage 时必须提供
52
+ * - 用于在存储中标识不同的缓存数据
53
+ * @default undefined 不使用持久化存储
54
+ */
55
+ cacheKey?: string;
56
+ /**
57
+ * 缓存时间(秒)
58
+ * - 超过这个时间的缓存项会被自动清除
59
+ * @default 60 一分钟
60
+ */
61
+ cacheTime?: number;
62
+ /**
63
+ * 缓存键比较函数
64
+ * - 用于判断两个缓存参数是否相等
65
+ * - 相等则认为是同一个缓存项
66
+ * @param prev 前一个参数
67
+ * @param next 后一个参数
68
+ * @returns 是否相等
69
+ * @default defaultEquals 使用 JSON.stringify 进行比较
70
+ */
71
+ cacheKeyEquals: (prev: Param, next: Param) => boolean;
72
+ /**
73
+ * IndexedDB 数据库名称
74
+ * - 仅在 storageType 为 'indexedDB' 时使用
75
+ * @default '__apiCacheDatabase__'
76
+ */
77
+ indexDBName?: string;
78
+ }
79
+ /** 存储类型映射表 */
80
+ declare const StorageMap: Record<StorageType | string, Storage>;
81
+ /**
82
+ * 缓存类
83
+ * @template Param 缓存参数类型
84
+ * @template Data 缓存数据类型
85
+ */
86
+ declare class Cache<Param, Data> {
87
+ /** 内存中的缓存数组 */
88
+ cache: ICache<Param, Data>[];
89
+ /** 缓存选项 */
90
+ private cacheOptions;
91
+ /** 存储实例 */
92
+ storage?: Storage | IndexedDBStorage;
93
+ /**
94
+ * 构造函数
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 };
@@ -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 = '__apiCacheDatabase__'; }
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 === 'indexedDB') {
30
- this.storage = new IndexedDBStorage(indexDBName, 'cacheStore');
37
+ if (cacheType === "indexedDB") {
38
+ this.storage = new IndexedDBStorage(indexDBName, "cacheStore");
31
39
  }
32
- else if (typeof cacheType === 'string') {
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 === 'string') {
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 === 'string') {
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, 'seconds').toJSON(),
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
  */