rxtutils 1.1.4-beta.9 → 1.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +117 -49
- package/cjs/_utils/defaultEquals.cjs +4 -19
- package/cjs/cache/index.cjs +130 -163
- package/cjs/cache/indexDB.cjs +86 -99
- package/cjs/downloadBlob.cjs +14 -0
- package/cjs/hooks/index.cjs +3 -6
- package/cjs/hooks/useCombineControlValue.cjs +33 -41
- package/cjs/index.cjs +16 -17
- package/cjs/request/defaultHandlers.cjs +5 -27
- package/cjs/request/error.cjs +18 -30
- package/cjs/request/index.cjs +194 -174
- package/cjs/store/createGetter/index.cjs +26 -40
- package/cjs/store/createStateStore/index.cjs +47 -90
- package/cjs/store/index.cjs +4 -7
- package/cjs/validator/decorators.cjs +71 -228
- package/cjs/validator/index.cjs +4 -7
- package/cjs/validator/validator.cjs +101 -177
- package/es/_utils/defaultEquals.mjs +4 -17
- package/es/cache/index.d.ts +9 -13
- package/es/cache/index.mjs +132 -160
- package/es/cache/indexDB.d.ts +1 -3
- package/es/cache/indexDB.mjs +87 -98
- package/es/downloadBlob.d.ts +8 -0
- package/es/downloadBlob.mjs +14 -0
- package/es/hooks/index.d.ts +1 -1
- package/es/hooks/index.mjs +4 -1
- package/es/hooks/useCombineControlValue.d.ts +5 -8
- package/es/hooks/useCombineControlValue.mjs +34 -40
- package/es/index.d.ts +28 -8
- package/es/index.mjs +29 -7
- package/es/request/defaultHandlers.d.ts +24 -0
- package/es/request/defaultHandlers.mjs +8 -26
- package/es/request/error.d.ts +3 -6
- package/es/request/error.mjs +18 -28
- package/es/request/index.d.ts +32 -20
- package/es/request/index.mjs +194 -172
- package/es/store/createGetter/index.d.ts +6 -10
- package/es/store/createGetter/index.mjs +28 -39
- package/es/store/createStateStore/index.d.ts +9 -9
- package/es/store/createStateStore/index.mjs +49 -87
- package/es/store/index.d.ts +4 -2
- package/es/store/index.mjs +7 -2
- package/es/validator/decorators.d.ts +12 -21
- package/es/validator/decorators.mjs +81 -226
- package/es/validator/index.d.ts +2 -2
- package/es/validator/index.mjs +16 -2
- package/es/validator/validator.d.ts +5 -6
- package/es/validator/validator.mjs +102 -176
- package/package.json +85 -15
- package/cjs/_utils/deepAssign.cjs +0 -25
- package/cjs/cache/index.d.ts +0 -141
- package/cjs/cache/indexDB.d.ts +0 -52
- package/cjs/hooks/index.d.ts +0 -1
- package/cjs/hooks/useCombineControlValue.d.ts +0 -21
- package/cjs/index.d.ts +0 -8
- package/cjs/request/error.d.ts +0 -31
- package/cjs/request/index.d.ts +0 -147
- package/cjs/store/createGetter/index.d.ts +0 -30
- package/cjs/store/createStateStore/index.d.ts +0 -42
- package/cjs/store/index.d.ts +0 -2
- package/cjs/validator/decorators.d.ts +0 -159
- package/cjs/validator/index.d.ts +0 -2
- package/cjs/validator/validator.d.ts +0 -84
- package/es/_utils/deepAssign.mjs +0 -23
package/es/cache/index.d.ts
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import { IndexedDBStorage } from './indexDB
|
|
2
|
-
|
|
1
|
+
import { IndexedDBStorage } from './indexDB';
|
|
3
2
|
/**
|
|
4
3
|
* 缓存存储类型
|
|
5
4
|
* - sessionStorage: 会话存储,浏览器关闭后清除
|
|
6
5
|
* - localStorage: 本地存储,永久保存
|
|
7
6
|
* - indexedDB: IndexedDB 数据库存储
|
|
8
7
|
*/
|
|
9
|
-
type StorageType =
|
|
8
|
+
export type StorageType = 'sessionStorage' | 'localStorage' | 'indexedDB';
|
|
10
9
|
/**
|
|
11
10
|
* 缓存项接口定义
|
|
12
11
|
* 定义了单个缓存项的数据结构
|
|
@@ -14,7 +13,7 @@ type StorageType = "sessionStorage" | "localStorage" | "indexedDB";
|
|
|
14
13
|
* @template Param 缓存参数类型
|
|
15
14
|
* @template Data 缓存数据类型
|
|
16
15
|
*/
|
|
17
|
-
interface ICache<Param, Data> {
|
|
16
|
+
export interface ICache<Param, Data> {
|
|
18
17
|
/**
|
|
19
18
|
* 缓存的参数
|
|
20
19
|
* 用于标识和查找缓存项
|
|
@@ -28,7 +27,7 @@ interface ICache<Param, Data> {
|
|
|
28
27
|
/**
|
|
29
28
|
* 过期时间
|
|
30
29
|
* - ISO 8601 格式的字符串
|
|
31
|
-
* - 由
|
|
30
|
+
* - 由 addSeconds(new Date(), cacheTime).toISOString() 生成
|
|
32
31
|
* - 示例:'2025-06-12T10:30:00.000Z'
|
|
33
32
|
*/
|
|
34
33
|
expireTime: string;
|
|
@@ -37,7 +36,7 @@ interface ICache<Param, Data> {
|
|
|
37
36
|
* 缓存选项接口
|
|
38
37
|
* @template Param 缓存参数类型
|
|
39
38
|
*/
|
|
40
|
-
interface ICacheOptions<Param> {
|
|
39
|
+
export interface ICacheOptions<Param> {
|
|
41
40
|
/**
|
|
42
41
|
* 存储类型
|
|
43
42
|
* - 'sessionStorage': 会话存储,浏览器关闭后清除
|
|
@@ -77,13 +76,13 @@ interface ICacheOptions<Param> {
|
|
|
77
76
|
indexDBName?: string;
|
|
78
77
|
}
|
|
79
78
|
/** 存储类型映射表 */
|
|
80
|
-
declare const StorageMap: Record<StorageType | string, Storage>;
|
|
79
|
+
export declare const StorageMap: Record<StorageType | string, Storage>;
|
|
81
80
|
/**
|
|
82
81
|
* 缓存类
|
|
83
82
|
* @template Param 缓存参数类型
|
|
84
83
|
* @template Data 缓存数据类型
|
|
85
84
|
*/
|
|
86
|
-
|
|
85
|
+
export default class Cache<Param, Data> {
|
|
87
86
|
/** 内存中的缓存数组 */
|
|
88
87
|
cache: ICache<Param, Data>[];
|
|
89
88
|
/** 缓存选项 */
|
|
@@ -123,19 +122,16 @@ declare class Cache<Param, Data> {
|
|
|
123
122
|
* @param data 要缓存的数据
|
|
124
123
|
* @param cacheOptions 可选的缓存配置,可以覆盖默认的缓存时间
|
|
125
124
|
*/
|
|
126
|
-
setCache(params: Param, data: Data, cacheOptions?: Omit<ICacheOptions<Param>,
|
|
125
|
+
setCache(params: Param, data: Data, cacheOptions?: Omit<ICacheOptions<Param>, 'storageType' | 'cacheKey' | 'cacheKeyEquals'>): void;
|
|
127
126
|
/**
|
|
128
127
|
* 获取缓存数据
|
|
129
128
|
* @param params 查询参数
|
|
130
129
|
* @returns 如果找到有效的缓存数据则返回数据,否则返回 null
|
|
131
130
|
*/
|
|
132
|
-
getCache(params: Param): Data;
|
|
131
|
+
getCache(params: Param): Data | null;
|
|
133
132
|
/**
|
|
134
133
|
* 清空所有缓存数据
|
|
135
134
|
* 清空内存中的缓存数组并同步到存储中
|
|
136
135
|
*/
|
|
137
136
|
clear(): void;
|
|
138
137
|
}
|
|
139
|
-
|
|
140
|
-
export { StorageMap, Cache as default };
|
|
141
|
-
export type { ICache, ICacheOptions, StorageType };
|
package/es/cache/index.mjs
CHANGED
|
@@ -1,165 +1,137 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
var StorageMap = {
|
|
8
|
-
localStorage: localStorage,
|
|
9
|
-
sessionStorage: sessionStorage,
|
|
1
|
+
import { isAfter, parseISO, addSeconds } from "date-fns";
|
|
2
|
+
import { IndexedDBStorage } from "./indexDB.mjs";
|
|
3
|
+
import defaultEquals from "../_utils/defaultEquals.mjs";
|
|
4
|
+
const StorageMap = {
|
|
5
|
+
localStorage,
|
|
6
|
+
sessionStorage
|
|
10
7
|
};
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
/** 内存中的缓存数组 */
|
|
29
|
-
this.cache = [];
|
|
30
|
-
this.cacheOptions = {
|
|
31
|
-
storageType: cacheType,
|
|
32
|
-
cacheKey: cacheKey,
|
|
33
|
-
cacheTime: cacheTime,
|
|
34
|
-
indexDBName: indexDBName,
|
|
35
|
-
cacheKeyEquals: cacheKeyEquals,
|
|
36
|
-
};
|
|
37
|
-
if (cacheType === "indexedDB") {
|
|
38
|
-
this.storage = new IndexedDBStorage(indexDBName, "cacheStore");
|
|
39
|
-
}
|
|
40
|
-
else if (typeof cacheType === "string") {
|
|
41
|
-
this.storage = StorageMap[cacheType];
|
|
42
|
-
}
|
|
43
|
-
this._init();
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* 初始化缓存
|
|
47
|
-
* 从存储中加载已保存的缓存数据,并进行解析和过期处理
|
|
48
|
-
* @private
|
|
49
|
-
*/
|
|
50
|
-
Cache.prototype._init = function () {
|
|
51
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
52
|
-
var _a, cacheType, cacheKey, _b, _c, _d;
|
|
53
|
-
return __generator(this, function (_e) {
|
|
54
|
-
switch (_e.label) {
|
|
55
|
-
case 0:
|
|
56
|
-
_a = this.cacheOptions, cacheType = _a.storageType, cacheKey = _a.cacheKey;
|
|
57
|
-
if (!(this.storage instanceof IndexedDBStorage)) return [3 /*break*/, 2];
|
|
58
|
-
_b = this;
|
|
59
|
-
_d = (_c = JSON).parse;
|
|
60
|
-
return [4 /*yield*/, this.storage.getItem(cacheKey)];
|
|
61
|
-
case 1:
|
|
62
|
-
_b.cache = _d.apply(_c, [(_e.sent()) || "[]"]);
|
|
63
|
-
return [3 /*break*/, 3];
|
|
64
|
-
case 2:
|
|
65
|
-
if (this.storage instanceof Storage) {
|
|
66
|
-
this.storage = StorageMap[cacheType];
|
|
67
|
-
if (this.storage) {
|
|
68
|
-
if (typeof cacheKey === "string") {
|
|
69
|
-
try {
|
|
70
|
-
this.cache = JSON.parse(this.storage.getItem(cacheKey) || "[]");
|
|
71
|
-
}
|
|
72
|
-
catch (e) {
|
|
73
|
-
this.cache = [];
|
|
74
|
-
console.error("\u7F13\u5B58\u6570\u636E\u89E3\u6790\u5931\u8D25\uFF0Ckey:".concat(cacheKey));
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
_e.label = 3;
|
|
80
|
-
case 3:
|
|
81
|
-
this._filterExpired();
|
|
82
|
-
this._saveToStorage();
|
|
83
|
-
return [2 /*return*/];
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
});
|
|
87
|
-
};
|
|
88
|
-
/**
|
|
89
|
-
* 过滤掉已过期的缓存项
|
|
90
|
-
* 通过比较当前时间和过期时间,移除过期的缓存项
|
|
91
|
-
* @private
|
|
92
|
-
*/
|
|
93
|
-
Cache.prototype._filterExpired = function () {
|
|
94
|
-
var newCache = this.cache.filter(function (item) {
|
|
95
|
-
return moment(item.expireTime).isAfter(moment());
|
|
96
|
-
});
|
|
97
|
-
this.cache = newCache;
|
|
98
|
-
};
|
|
99
|
-
/**
|
|
100
|
-
* 将当前缓存数据保存到存储中
|
|
101
|
-
* 如果设置了缓存键名且存储实例存在,则将缓存数据序列化后保存
|
|
102
|
-
* @private
|
|
103
|
-
*/
|
|
104
|
-
Cache.prototype._saveToStorage = function () {
|
|
105
|
-
if (this.storage) {
|
|
106
|
-
if (typeof this.cacheOptions.cacheKey === "string") {
|
|
107
|
-
this.storage.setItem(this.cacheOptions.cacheKey, JSON.stringify(this.cache));
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
};
|
|
111
|
-
/**
|
|
112
|
-
* 设置缓存数据
|
|
113
|
-
* @param params 缓存的参数
|
|
114
|
-
* @param data 要缓存的数据
|
|
115
|
-
* @param cacheOptions 可选的缓存配置,可以覆盖默认的缓存时间
|
|
116
|
-
*/
|
|
117
|
-
Cache.prototype.setCache = function (params, data, cacheOptions) {
|
|
118
|
-
var _a = __assign(__assign({}, this.cacheOptions), cacheOptions), cacheTime = _a.cacheTime, _b = _a.cacheKeyEquals, cacheKeyEquals = _b === void 0 ? defaultEquals : _b;
|
|
119
|
-
var cacheItemIndex = this.cache.findIndex(function (item) {
|
|
120
|
-
return cacheKeyEquals(item.params, params);
|
|
121
|
-
});
|
|
122
|
-
if (cacheItemIndex > -1) {
|
|
123
|
-
this.cache.splice(cacheItemIndex, 1);
|
|
124
|
-
}
|
|
125
|
-
this.cache.push({
|
|
126
|
-
params: params,
|
|
127
|
-
data: data,
|
|
128
|
-
expireTime: moment().add(cacheTime, "seconds").toJSON(),
|
|
129
|
-
});
|
|
130
|
-
this._saveToStorage();
|
|
8
|
+
class Cache {
|
|
9
|
+
/**
|
|
10
|
+
* 构造函数
|
|
11
|
+
* @param cacheType 存储类型
|
|
12
|
+
* @param cacheKey 缓存键名
|
|
13
|
+
* @param cacheTime 缓存时间(秒)
|
|
14
|
+
* @param indexDBName IndexedDB 数据库名称,默认值为 '__apiCacheDatabase__'
|
|
15
|
+
* @param cacheKeyEquals 缓存键比较函数,默认使用 defaultEquals
|
|
16
|
+
*/
|
|
17
|
+
constructor(cacheType, cacheKey, cacheTime, indexDBName = "__apiCacheDatabase__", cacheKeyEquals = defaultEquals) {
|
|
18
|
+
this.cache = [];
|
|
19
|
+
this.cacheOptions = {
|
|
20
|
+
storageType: cacheType,
|
|
21
|
+
cacheKey,
|
|
22
|
+
cacheTime,
|
|
23
|
+
indexDBName,
|
|
24
|
+
cacheKeyEquals
|
|
131
25
|
};
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
26
|
+
if (cacheType === "indexedDB") {
|
|
27
|
+
this.storage = new IndexedDBStorage(indexDBName, "cacheStore");
|
|
28
|
+
} else if (typeof cacheType === "string") {
|
|
29
|
+
this.storage = StorageMap[cacheType];
|
|
30
|
+
}
|
|
31
|
+
this._init();
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* 初始化缓存
|
|
35
|
+
* 从存储中加载已保存的缓存数据,并进行解析和过期处理
|
|
36
|
+
* @private
|
|
37
|
+
*/
|
|
38
|
+
async _init() {
|
|
39
|
+
const { storageType: cacheType, cacheKey } = this.cacheOptions;
|
|
40
|
+
if (this.storage instanceof IndexedDBStorage) {
|
|
41
|
+
this.cache = JSON.parse(await this.storage.getItem(cacheKey) || "[]");
|
|
42
|
+
} else if (this.storage instanceof Storage) {
|
|
43
|
+
this.storage = StorageMap[cacheType];
|
|
44
|
+
if (this.storage) {
|
|
45
|
+
if (typeof cacheKey === "string") {
|
|
46
|
+
try {
|
|
47
|
+
this.cache = JSON.parse(this.storage.getItem(cacheKey) || "[]");
|
|
48
|
+
} catch (e) {
|
|
49
|
+
this.cache = [];
|
|
50
|
+
console.error(`缓存数据解析失败,key:${cacheKey}`);
|
|
51
|
+
}
|
|
151
52
|
}
|
|
152
|
-
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
this._filterExpired();
|
|
56
|
+
this._saveToStorage();
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* 过滤掉已过期的缓存项
|
|
60
|
+
* 通过比较当前时间和过期时间,移除过期的缓存项
|
|
61
|
+
* @private
|
|
62
|
+
*/
|
|
63
|
+
_filterExpired() {
|
|
64
|
+
const newCache = this.cache.filter((item) => {
|
|
65
|
+
return isAfter(parseISO(item.expireTime), /* @__PURE__ */ new Date());
|
|
66
|
+
});
|
|
67
|
+
this.cache = newCache;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* 将当前缓存数据保存到存储中
|
|
71
|
+
* 如果设置了缓存键名且存储实例存在,则将缓存数据序列化后保存
|
|
72
|
+
* @private
|
|
73
|
+
*/
|
|
74
|
+
_saveToStorage() {
|
|
75
|
+
if (this.storage) {
|
|
76
|
+
if (typeof this.cacheOptions.cacheKey === "string") {
|
|
77
|
+
this.storage.setItem(this.cacheOptions.cacheKey, JSON.stringify(this.cache));
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* 设置缓存数据
|
|
83
|
+
* @param params 缓存的参数
|
|
84
|
+
* @param data 要缓存的数据
|
|
85
|
+
* @param cacheOptions 可选的缓存配置,可以覆盖默认的缓存时间
|
|
86
|
+
*/
|
|
87
|
+
setCache(params, data, cacheOptions) {
|
|
88
|
+
const { cacheTime, cacheKeyEquals = defaultEquals } = {
|
|
89
|
+
...this.cacheOptions,
|
|
90
|
+
...cacheOptions
|
|
153
91
|
};
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
92
|
+
const cacheItemIndex = this.cache.findIndex((item) => {
|
|
93
|
+
return cacheKeyEquals(item.params, params);
|
|
94
|
+
});
|
|
95
|
+
if (cacheItemIndex > -1) {
|
|
96
|
+
this.cache.splice(cacheItemIndex, 1);
|
|
97
|
+
}
|
|
98
|
+
this.cache.push({
|
|
99
|
+
params,
|
|
100
|
+
data,
|
|
101
|
+
expireTime: addSeconds(/* @__PURE__ */ new Date(), cacheTime ?? 0).toISOString()
|
|
102
|
+
});
|
|
103
|
+
this._saveToStorage();
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* 获取缓存数据
|
|
107
|
+
* @param params 查询参数
|
|
108
|
+
* @returns 如果找到有效的缓存数据则返回数据,否则返回 null
|
|
109
|
+
*/
|
|
110
|
+
getCache(params) {
|
|
111
|
+
const itemIndex = this.cache.findIndex((item2) => {
|
|
112
|
+
return this.cacheOptions.cacheKeyEquals(item2.params, params);
|
|
113
|
+
});
|
|
114
|
+
const item = this.cache[itemIndex];
|
|
115
|
+
if (item) {
|
|
116
|
+
if (isAfter(parseISO(item.expireTime), /* @__PURE__ */ new Date())) {
|
|
117
|
+
return item.data;
|
|
118
|
+
} else {
|
|
119
|
+
this.cache.splice(itemIndex, 1);
|
|
160
120
|
this._saveToStorage();
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* 清空所有缓存数据
|
|
127
|
+
* 清空内存中的缓存数组并同步到存储中
|
|
128
|
+
*/
|
|
129
|
+
clear() {
|
|
130
|
+
this.cache = [];
|
|
131
|
+
this._saveToStorage();
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
export {
|
|
135
|
+
StorageMap,
|
|
136
|
+
Cache as default
|
|
137
|
+
};
|
package/es/cache/indexDB.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* IndexedDB 存储类
|
|
3
3
|
* 提供了对 IndexedDB 数据库操作的简单封装
|
|
4
4
|
*/
|
|
5
|
-
declare class IndexedDBStorage {
|
|
5
|
+
export declare class IndexedDBStorage {
|
|
6
6
|
/** 数据库名称 */
|
|
7
7
|
private dbName;
|
|
8
8
|
/** 存储对象名称 */
|
|
@@ -48,5 +48,3 @@ declare class IndexedDBStorage {
|
|
|
48
48
|
*/
|
|
49
49
|
getItem<T = any>(key: string): Promise<T>;
|
|
50
50
|
}
|
|
51
|
-
|
|
52
|
-
export { IndexedDBStorage };
|
package/es/cache/indexDB.mjs
CHANGED
|
@@ -1,98 +1,87 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
request.onsuccess = function () {
|
|
89
|
-
return resolve(request.result ? request.result.value : undefined);
|
|
90
|
-
};
|
|
91
|
-
request.onerror = function () { var _a; return reject("Could not get the item: ".concat((_a = request.error) === null || _a === void 0 ? void 0 : _a.message)); };
|
|
92
|
-
});
|
|
93
|
-
});
|
|
94
|
-
};
|
|
95
|
-
return IndexedDBStorage;
|
|
96
|
-
}());
|
|
97
|
-
|
|
98
|
-
export { IndexedDBStorage };
|
|
1
|
+
class IndexedDBStorage {
|
|
2
|
+
/**
|
|
3
|
+
* 构造函数
|
|
4
|
+
* @param dbName 数据库名称
|
|
5
|
+
* @param storeName 存储对象名称
|
|
6
|
+
*/
|
|
7
|
+
constructor(dbName, storeName) {
|
|
8
|
+
this.db = null;
|
|
9
|
+
this.dbName = dbName;
|
|
10
|
+
this.storeName = storeName;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* 打开数据库连接
|
|
14
|
+
* 如果数据库不存在则创建新的数据库和存储对象
|
|
15
|
+
* @private
|
|
16
|
+
* @returns Promise<IDBDatabase> 数据库连接实例
|
|
17
|
+
*/
|
|
18
|
+
_open() {
|
|
19
|
+
return new Promise((resolve, reject) => {
|
|
20
|
+
if (this.db) {
|
|
21
|
+
resolve(this.db);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const request = indexedDB.open(this.dbName);
|
|
25
|
+
request.onupgradeneeded = (event) => {
|
|
26
|
+
const db = event.target.result;
|
|
27
|
+
db.createObjectStore(this.storeName, { keyPath: "key" });
|
|
28
|
+
};
|
|
29
|
+
request.onerror = () => {
|
|
30
|
+
reject(`IndexedDB open request error: ${request.error?.message}`);
|
|
31
|
+
};
|
|
32
|
+
request.onsuccess = (event) => {
|
|
33
|
+
this.db = event.target.result;
|
|
34
|
+
resolve(this.db);
|
|
35
|
+
};
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* 获取存储对象
|
|
40
|
+
* @param mode 事务模式,默认为只读模式
|
|
41
|
+
* - readonly: 只读模式
|
|
42
|
+
* - readwrite: 读写模式
|
|
43
|
+
* @private
|
|
44
|
+
* @returns Promise<IDBObjectStore> 存储对象实例
|
|
45
|
+
*/
|
|
46
|
+
_getStore(mode = "readonly") {
|
|
47
|
+
return this._open().then((db) => {
|
|
48
|
+
const transaction = db.transaction(this.storeName, mode);
|
|
49
|
+
return transaction.objectStore(this.storeName);
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* 设置键值对
|
|
54
|
+
* @param key 键名
|
|
55
|
+
* @param value 要存储的值
|
|
56
|
+
* @returns Promise<void> 存储操作的结果
|
|
57
|
+
* @throws 当存储操作失败时抛出错误
|
|
58
|
+
*/
|
|
59
|
+
setItem(key, value) {
|
|
60
|
+
return this._getStore("readwrite").then((store) => {
|
|
61
|
+
return new Promise((resolve, reject) => {
|
|
62
|
+
const request = store.put({ key, value });
|
|
63
|
+
request.onsuccess = () => resolve();
|
|
64
|
+
request.onerror = () => reject(`Could not set the item: ${request.error?.message}`);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* 获取键对应的值
|
|
70
|
+
* @param key 要获取的键名
|
|
71
|
+
* @returns Promise<T> 返回存储的值,如果不存在则返回 undefined
|
|
72
|
+
* @throws 当获取操作失败时抛出错误
|
|
73
|
+
* @template T 存储值的类型,默认为 any
|
|
74
|
+
*/
|
|
75
|
+
getItem(key) {
|
|
76
|
+
return this._getStore().then((store) => {
|
|
77
|
+
return new Promise((resolve, reject) => {
|
|
78
|
+
const request = store.get(key);
|
|
79
|
+
request.onsuccess = () => resolve(request.result ? request.result.value : void 0);
|
|
80
|
+
request.onerror = () => reject(`Could not get the item: ${request.error?.message}`);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
export {
|
|
86
|
+
IndexedDBStorage
|
|
87
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const downloadBlob = (blob, fileName, autoRevoke = true) => {
|
|
2
|
+
const url = URL.createObjectURL(blob);
|
|
3
|
+
const a = document.createElement("a");
|
|
4
|
+
a.href = url;
|
|
5
|
+
a.download = fileName;
|
|
6
|
+
a.click();
|
|
7
|
+
if (autoRevoke) {
|
|
8
|
+
URL.revokeObjectURL(url);
|
|
9
|
+
}
|
|
10
|
+
return url;
|
|
11
|
+
};
|
|
12
|
+
export {
|
|
13
|
+
downloadBlob
|
|
14
|
+
};
|
package/es/hooks/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { useCombineControlValue } from './useCombineControlValue
|
|
1
|
+
export { useCombineControlValue } from './useCombineControlValue';
|
package/es/hooks/index.mjs
CHANGED
|
@@ -1,21 +1,18 @@
|
|
|
1
|
-
type UseCombineControlValueOptions<V> = {
|
|
1
|
+
export type UseCombineControlValueOptions<V> = {
|
|
2
2
|
props: Record<string, any>;
|
|
3
3
|
valueKey?: string;
|
|
4
4
|
defaultValue?: V;
|
|
5
5
|
onChange?: (val: V) => void;
|
|
6
6
|
};
|
|
7
|
-
type UseCombineControlValueEasyResult<V> = {
|
|
7
|
+
export type UseCombineControlValueEasyResult<V> = {
|
|
8
8
|
value: V;
|
|
9
9
|
onChange: (nextVal: V) => void;
|
|
10
10
|
};
|
|
11
|
-
type UseCombineControlValueResolveResult<V, R extends (...args: any[]) => any> = {
|
|
11
|
+
export type UseCombineControlValueResolveResult<V, R extends (...args: any[]) => any> = {
|
|
12
12
|
value: V;
|
|
13
13
|
onChange: (...args: Parameters<R>) => void;
|
|
14
14
|
};
|
|
15
|
-
declare function useCombineControlValue<V>(options: UseCombineControlValueOptions<V>): UseCombineControlValueEasyResult<V>;
|
|
16
|
-
declare function useCombineControlValue<V, R extends (...args: any[]) => any>(options: Omit<UseCombineControlValueOptions<V>, 'onChange'> & {
|
|
15
|
+
export declare function useCombineControlValue<V>(options: UseCombineControlValueOptions<V>): UseCombineControlValueEasyResult<V>;
|
|
16
|
+
export declare function useCombineControlValue<V, R extends (...args: any[]) => any>(options: Omit<UseCombineControlValueOptions<V>, 'onChange'> & {
|
|
17
17
|
onChange?: (...args: Parameters<R>) => void;
|
|
18
18
|
}, resolveFn: (...args: Parameters<R>) => V): UseCombineControlValueResolveResult<V, R>;
|
|
19
|
-
|
|
20
|
-
export { useCombineControlValue };
|
|
21
|
-
export type { UseCombineControlValueEasyResult, UseCombineControlValueOptions, UseCombineControlValueResolveResult };
|