@net-vert/core 1.0.0 → 1.2.0
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/dist/index.d.ts +53 -165
- package/dist/index.js +493 -264
- package/dist/index.umd.cjs +1 -1
- package/package.json +2 -5
package/dist/index.d.ts
CHANGED
|
@@ -1,67 +1,11 @@
|
|
|
1
|
+
import { AnyRecord } from 'store-vert';
|
|
2
|
+
import { EnhancedStore } from 'store-vert';
|
|
3
|
+
import { Key as Key_2 } from 'store-vert';
|
|
4
|
+
import { Store } from 'store-vert';
|
|
5
|
+
import { StoreFactory } from 'store-vert';
|
|
6
|
+
import { StoreKey } from 'store-vert';
|
|
1
7
|
import { TaskQueue } from 'id-queue';
|
|
2
8
|
|
|
3
|
-
declare type AnyRecord = Record<string | number | symbol, any>;
|
|
4
|
-
|
|
5
|
-
declare type AnyRecord_2 = Record<string, any>;
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* 缓存存储基类
|
|
9
|
-
* 提供针对缓存场景的快捷方法,自动处理 ExpirableValue 的封装和解封
|
|
10
|
-
*/
|
|
11
|
-
declare abstract class BaseCacheStorage<K extends string | number | symbol, R> {
|
|
12
|
-
abstract getItem(key: K): ExpirableValue<R> | undefined;
|
|
13
|
-
abstract setItem(key: K, value: ExpirableValue<R>): ExpirableValue<R>;
|
|
14
|
-
abstract removeItem(key: K): void;
|
|
15
|
-
abstract clear(): void;
|
|
16
|
-
abstract length(): number;
|
|
17
|
-
abstract keys(): K[];
|
|
18
|
-
abstract iterate(iteratee: (value: ExpirableValue<R>, key: K, iterationNumber: number) => void): void;
|
|
19
|
-
/**
|
|
20
|
-
* 获取缓存值(自动处理过期检查和解包)
|
|
21
|
-
* @param key 缓存键
|
|
22
|
-
* @returns 缓存的原始值,如果不存在或已过期返回 undefined
|
|
23
|
-
*/
|
|
24
|
-
get(key: K): R | undefined;
|
|
25
|
-
/**
|
|
26
|
-
* 设置缓存值(自动包装为 ExpirableValue)
|
|
27
|
-
* @param key 缓存键
|
|
28
|
-
* @param value 要缓存的值
|
|
29
|
-
* @param duration 有效期(毫秒),默认 24 小时
|
|
30
|
-
*/
|
|
31
|
-
set(key: K, value: R, duration?: number): void;
|
|
32
|
-
/**
|
|
33
|
-
* 删除指定缓存(别名方法,等同于 removeItem)
|
|
34
|
-
* @param key 缓存键
|
|
35
|
-
*/
|
|
36
|
-
delete(key: K): void;
|
|
37
|
-
/**
|
|
38
|
-
* 检查缓存是否存在且未过期
|
|
39
|
-
* @param key 缓存键
|
|
40
|
-
*/
|
|
41
|
-
has(key: K): boolean;
|
|
42
|
-
/**
|
|
43
|
-
* 获取缓存数量(别名方法,等同于 length)
|
|
44
|
-
*/
|
|
45
|
-
size(): number;
|
|
46
|
-
/**
|
|
47
|
-
* 遍历所有有效的缓存(自动跳过已过期的,并解包值)
|
|
48
|
-
* @param callback 回调函数,接收解包后的值、键和索引
|
|
49
|
-
*/
|
|
50
|
-
forEach(callback: (value: R, key: K, index: number) => void): void;
|
|
51
|
-
/**
|
|
52
|
-
* 清理所有过期的缓存
|
|
53
|
-
* @returns 清理的缓存数量
|
|
54
|
-
*/
|
|
55
|
-
clearExpired(): number;
|
|
56
|
-
/**
|
|
57
|
-
* 更新缓存的过期时间(不改变值)
|
|
58
|
-
* @param key 缓存键
|
|
59
|
-
* @param duration 新的有效期(毫秒)
|
|
60
|
-
* @returns 是否更新成功
|
|
61
|
-
*/
|
|
62
|
-
touch(key: K, duration: number): boolean;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
9
|
export declare type BaseRequestor = (config: RequestConfig<any>) => any;
|
|
66
10
|
|
|
67
11
|
/**
|
|
@@ -70,7 +14,7 @@ export declare type BaseRequestor = (config: RequestConfig<any>) => any;
|
|
|
70
14
|
* - 自定义缓存 key 生成
|
|
71
15
|
* - 自定义缓存有效期(固定时长或动态计算)
|
|
72
16
|
* - 自定义缓存有效性校验
|
|
73
|
-
* -
|
|
17
|
+
* - 自定义存储介质
|
|
74
18
|
*/
|
|
75
19
|
export declare const cache: <D = any, R = any>(options?: Partial<CacheOptions<D, R>>) => CacheMiddleware<D, R>;
|
|
76
20
|
|
|
@@ -78,7 +22,7 @@ export declare const cache: <D = any, R = any>(options?: Partial<CacheOptions<D,
|
|
|
78
22
|
declare interface CacheCheckContext<D = any, R = any> {
|
|
79
23
|
key: CacheKey;
|
|
80
24
|
config: RequestConfig<D>;
|
|
81
|
-
cachedData?:
|
|
25
|
+
cachedData?: R;
|
|
82
26
|
}
|
|
83
27
|
|
|
84
28
|
/**
|
|
@@ -90,46 +34,16 @@ IdempotencyOptions<D>
|
|
|
90
34
|
]>;
|
|
91
35
|
|
|
92
36
|
/** 缓存 key 类型 */
|
|
93
|
-
declare type CacheKey = string | number | symbol;
|
|
37
|
+
export declare type CacheKey = string | number | symbol;
|
|
94
38
|
|
|
95
39
|
/** 请求前上下文:生成缓存 key */
|
|
96
40
|
declare interface CacheKeyContext<D = any> {
|
|
97
41
|
config: RequestConfig<D>;
|
|
98
42
|
}
|
|
99
43
|
|
|
100
|
-
/**
|
|
101
|
-
* 基于 LocalStorage 的缓存存储
|
|
102
|
-
* 继承 LocalStorage 并扩展缓存快捷方法
|
|
103
|
-
*/
|
|
104
|
-
declare class CacheLocalStorage<K extends string | number | symbol = string, R = any> extends LocalStorage<Record<K, ExpirableValue<R>>> implements BaseCacheStorage<K, R> {
|
|
105
|
-
get: (key: any) => any;
|
|
106
|
-
set: (key: any, value: any, duration?: number) => void;
|
|
107
|
-
delete: (key: any) => void;
|
|
108
|
-
has: (key: any) => boolean;
|
|
109
|
-
size: () => number;
|
|
110
|
-
forEach: (callback: (value: any, key: any, index: number) => void) => void;
|
|
111
|
-
clearExpired: () => number;
|
|
112
|
-
touch: (key: any, duration: number) => boolean;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* 基于内存的缓存存储
|
|
117
|
-
* 继承 MemoryStorage 并扩展缓存快捷方法
|
|
118
|
-
*/
|
|
119
|
-
declare class CacheMemoryStorage<K extends string | number | symbol = string, R = any> extends MemoryStorage<Record<K, ExpirableValue<R>>> implements BaseCacheStorage<K, R> {
|
|
120
|
-
get: (key: any) => any;
|
|
121
|
-
set: (key: any, value: any, duration?: number) => void;
|
|
122
|
-
delete: (key: any) => void;
|
|
123
|
-
has: (key: any) => boolean;
|
|
124
|
-
size: () => number;
|
|
125
|
-
forEach: (callback: (value: any, key: any, index: number) => void) => void;
|
|
126
|
-
clearExpired: () => number;
|
|
127
|
-
touch: (key: any, duration: number) => boolean;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
44
|
/** 缓存中间件类型(带 storage 实例)*/
|
|
131
45
|
declare type CacheMiddleware<D = any, R = any> = TypedMiddleware<MIDDLEWARE_TYPE.CACHE, false, D, R> & {
|
|
132
|
-
storage:
|
|
46
|
+
storage: CacheStorageInstance<R>;
|
|
133
47
|
};
|
|
134
48
|
|
|
135
49
|
/** 缓存模块配置 */
|
|
@@ -151,21 +65,19 @@ declare interface CacheOptions<D = any, R = any> {
|
|
|
151
65
|
* - 返回 boolean 或 Promise<boolean>
|
|
152
66
|
*/
|
|
153
67
|
isValid: (ctx: CacheCheckContext<D, R>) => boolean | Promise<boolean>;
|
|
154
|
-
/**
|
|
155
|
-
|
|
68
|
+
/** 缓存介质 */
|
|
69
|
+
store: StoreDescriptor;
|
|
156
70
|
}
|
|
157
71
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
*/
|
|
162
|
-
declare type CacheStorage_2<K extends string | number | symbol = string, R = any> = CacheMemoryStorage<K, R> | CacheLocalStorage<K, R>;
|
|
72
|
+
declare type CacheSchema<R> = Record<CacheKey, R>;
|
|
73
|
+
|
|
74
|
+
declare type CacheStorageInstance<R = any> = ExpirableCacheStorage<R> & EnhancedStore<ExpirableSchema<CacheSchema<R>>>;
|
|
163
75
|
|
|
164
76
|
/** 请求后上下文:更新缓存 */
|
|
165
77
|
declare interface CacheUpdateContext<D = any, R = any> {
|
|
166
78
|
key: CacheKey;
|
|
167
79
|
config: RequestConfig<D>;
|
|
168
|
-
cachedData?:
|
|
80
|
+
cachedData?: R;
|
|
169
81
|
response: R;
|
|
170
82
|
}
|
|
171
83
|
|
|
@@ -235,6 +147,38 @@ export declare interface CreateRequestorConfig<Extensions extends readonly Middl
|
|
|
235
147
|
instanceKey?: Key;
|
|
236
148
|
}
|
|
237
149
|
|
|
150
|
+
declare class ExpirableCacheStorage<R = any> {
|
|
151
|
+
store: EnhancedStore<ExpirableSchema<CacheSchema<R>>>;
|
|
152
|
+
constructor(store: StoreDescriptor);
|
|
153
|
+
/**
|
|
154
|
+
* 设置缓存(自动包装成 ExpirableValue)
|
|
155
|
+
* @param key 缓存 key
|
|
156
|
+
* @param value 要缓存的值
|
|
157
|
+
* @param duration 过期时长(毫秒),默认 24 小时
|
|
158
|
+
*/
|
|
159
|
+
setCache(key: CacheKey, value: R, duration?: number): void;
|
|
160
|
+
/**
|
|
161
|
+
* 获取缓存值(检查过期,如果过期返回 undefined)
|
|
162
|
+
* @param key 缓存 key
|
|
163
|
+
* @returns 未过期返回值,已过期返回 undefined
|
|
164
|
+
*/
|
|
165
|
+
getCache(key: CacheKey): Promise<R | undefined>;
|
|
166
|
+
/**
|
|
167
|
+
* 检查是否有有效的缓存(未过期)
|
|
168
|
+
* @param key 缓存 key
|
|
169
|
+
* @returns true 表示有有效缓存,false 表示无缓存或已过期
|
|
170
|
+
*/
|
|
171
|
+
hasValidCache(key: CacheKey): Promise<boolean>;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* 将 Schema 的所有值类型映射为 ExpirableValue 包装的版本
|
|
176
|
+
* 用于缓存存储的类型转换
|
|
177
|
+
*/
|
|
178
|
+
export declare type ExpirableSchema<Schema extends AnyRecord> = {
|
|
179
|
+
[K in keyof Schema]: ExpirableValue<Schema[K]>;
|
|
180
|
+
};
|
|
181
|
+
|
|
238
182
|
/**
|
|
239
183
|
* 带过期时间的值类型(通用型)
|
|
240
184
|
* 可用于缓存、幂等、同步等需要时间控制的场景
|
|
@@ -288,71 +232,10 @@ declare type IdempotentMiddleware<D = any, R = any> = TypedMiddleware<MIDDLEWARE
|
|
|
288
232
|
|
|
289
233
|
export declare const inject: (requestor: BaseRequestor, instanceKey?: string) => void;
|
|
290
234
|
|
|
291
|
-
/**
|
|
292
|
-
* Storage 接口
|
|
293
|
-
* 定义存储器的基本操作
|
|
294
|
-
*/
|
|
295
|
-
export declare interface IStorage<Schema extends Record<string, any> = Record<string, any>> {
|
|
296
|
-
/**
|
|
297
|
-
* 获取存储项
|
|
298
|
-
*/
|
|
299
|
-
getItem<K extends keyof Schema>(key: K): Schema[K] | undefined;
|
|
300
|
-
/**
|
|
301
|
-
* 设置存储项
|
|
302
|
-
*/
|
|
303
|
-
setItem<K extends keyof Schema>(key: K, value: Schema[K]): Schema[K];
|
|
304
|
-
/**
|
|
305
|
-
* 删除存储项
|
|
306
|
-
*/
|
|
307
|
-
removeItem<K extends keyof Schema>(key: K): void;
|
|
308
|
-
/**
|
|
309
|
-
* 清空所有存储项
|
|
310
|
-
*/
|
|
311
|
-
clear(): void;
|
|
312
|
-
/**
|
|
313
|
-
* 获取存储项数量
|
|
314
|
-
*/
|
|
315
|
-
length(): number;
|
|
316
|
-
/**
|
|
317
|
-
* 获取所有 key
|
|
318
|
-
*/
|
|
319
|
-
keys(): (keyof Schema)[];
|
|
320
|
-
/**
|
|
321
|
-
* 遍历所有存储项
|
|
322
|
-
*/
|
|
323
|
-
iterate(iteratee: <K extends keyof Schema>(value: Schema[K], key: K, iterationNumber: number) => void): void;
|
|
324
|
-
}
|
|
325
|
-
|
|
326
235
|
export declare type Key = string | symbol | number;
|
|
327
236
|
|
|
328
|
-
/**
|
|
329
|
-
* LocalStorage 存储类
|
|
330
|
-
* 基于浏览器的 localStorage 实现
|
|
331
|
-
*/
|
|
332
|
-
declare class LocalStorage<Schema extends AnyRecord_2 = AnyRecord_2> implements IStorage<Schema> {
|
|
333
|
-
constructor();
|
|
334
|
-
getItem<K extends keyof Schema>(key: K): Schema[K] | undefined;
|
|
335
|
-
setItem<K extends keyof Schema>(key: K, value: Schema[K]): Schema[K];
|
|
336
|
-
removeItem<K extends keyof Schema>(key: K): void;
|
|
337
|
-
clear(): void;
|
|
338
|
-
length(): number;
|
|
339
|
-
keys(): (keyof Schema)[];
|
|
340
|
-
iterate(iteratee: <K extends keyof Schema>(value: Schema[K], key: K, iterationNumber: number) => void): void;
|
|
341
|
-
}
|
|
342
|
-
|
|
343
237
|
export declare type MaybePromise<IsSync, R> = IsSync extends true ? R : IsSync extends false ? Promise<R> : R | Promise<R>;
|
|
344
238
|
|
|
345
|
-
declare class MemoryStorage<Schema extends AnyRecord = AnyRecord> implements IStorage<Schema> {
|
|
346
|
-
private store;
|
|
347
|
-
getItem<K extends keyof Schema>(key: K): Schema[K] | undefined;
|
|
348
|
-
setItem<K extends keyof Schema>(key: K, value: Schema[K]): Schema[K];
|
|
349
|
-
removeItem<K extends keyof Schema>(key: K): void;
|
|
350
|
-
clear(): void;
|
|
351
|
-
length(): number;
|
|
352
|
-
keys(): (keyof Schema)[];
|
|
353
|
-
iterate(iteratee: <K extends keyof Schema>(value: Schema[K], key: K, iterationNumber: number) => void): void;
|
|
354
|
-
}
|
|
355
|
-
|
|
356
239
|
export declare type Middleware<IsSync extends boolean = false, D = any, R = any> = (context: {
|
|
357
240
|
config: RequestConfig<D>;
|
|
358
241
|
next: () => MaybePromise<IsSync, R>;
|
|
@@ -413,6 +296,11 @@ declare type RetryOptions<D = any> = {
|
|
|
413
296
|
|
|
414
297
|
declare type SafeKey = string | number | symbol;
|
|
415
298
|
|
|
299
|
+
export declare type StoreDescriptor = StoreKey | {
|
|
300
|
+
key: Key_2;
|
|
301
|
+
factory: StoreFactory<Store<AnyRecord>, any[]>;
|
|
302
|
+
};
|
|
303
|
+
|
|
416
304
|
/**
|
|
417
305
|
* 缓存中有数据则直接返回。
|
|
418
306
|
* suspense 为 true 时,会抛出一个 Promise(而不是普通错误),Promise resolve 后得到数据。
|
package/dist/index.js
CHANGED
|
@@ -1,72 +1,72 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
import { TaskQueue as
|
|
5
|
-
var
|
|
6
|
-
const
|
|
1
|
+
var D = Object.defineProperty;
|
|
2
|
+
var F = (r, e, t) => e in r ? D(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t;
|
|
3
|
+
var l = (r, e, t) => F(r, typeof e != "symbol" ? e + "" : e, t);
|
|
4
|
+
import { TaskQueue as $ } from "id-queue";
|
|
5
|
+
var w = /* @__PURE__ */ ((r) => (r.GET = "get", r.POST = "post", r.PUT = "put", r.DELETE = "delete", r))(w || {}), y = /* @__PURE__ */ ((r) => (r.CACHE = "cache", r.RETRY = "retry", r.IDEMPOTENT = "idempotent", r.CONCURRENT = "concurrent", r.SYNC = "sync", r))(y || {});
|
|
6
|
+
const v = "default", E = {
|
|
7
7
|
retries: 3,
|
|
8
8
|
delay: 0,
|
|
9
9
|
retryCondition: () => !0
|
|
10
|
-
},
|
|
11
|
-
const e = { ...
|
|
12
|
-
return Object.assign(async ({ config:
|
|
10
|
+
}, T = (r) => {
|
|
11
|
+
const e = { ...E, ...r };
|
|
12
|
+
return Object.assign(async ({ config: n, next: s }) => {
|
|
13
13
|
let o = 0, i;
|
|
14
14
|
for (; o <= e.retries; )
|
|
15
15
|
try {
|
|
16
|
-
return await
|
|
16
|
+
return await s();
|
|
17
17
|
} catch (a) {
|
|
18
18
|
if (i = a, o === e.retries)
|
|
19
19
|
throw a;
|
|
20
|
-
const
|
|
21
|
-
config:
|
|
20
|
+
const c = {
|
|
21
|
+
config: n,
|
|
22
22
|
lastResponse: a,
|
|
23
23
|
attempt: o
|
|
24
24
|
};
|
|
25
|
-
if (!e.retryCondition(
|
|
25
|
+
if (!e.retryCondition(c))
|
|
26
26
|
throw a;
|
|
27
27
|
o++;
|
|
28
|
-
const
|
|
29
|
-
|
|
28
|
+
const u = typeof e.delay == "function" ? e.delay(c) : e.delay;
|
|
29
|
+
u > 0 && await new Promise((h) => setTimeout(h, u));
|
|
30
30
|
}
|
|
31
31
|
throw i;
|
|
32
|
-
}, { __middlewareType:
|
|
33
|
-
},
|
|
32
|
+
}, { __middlewareType: y.RETRY });
|
|
33
|
+
}, q = () => {
|
|
34
34
|
const r = /* @__PURE__ */ new Map();
|
|
35
35
|
return { getPromise: (o) => r.get(o), setPromise: (o, i) => {
|
|
36
36
|
r.set(o, i), i.finally(() => r.delete(o));
|
|
37
37
|
}, delPromise: (o) => r.delete(o), clearCache: () => r.clear() };
|
|
38
|
-
},
|
|
39
|
-
const { config: e } = r, { method: t, url:
|
|
40
|
-
return [t,
|
|
41
|
-
},
|
|
42
|
-
key:
|
|
43
|
-
},
|
|
44
|
-
const e = { ...
|
|
45
|
-
return Object.assign(({ config:
|
|
46
|
-
const i = e.key({ config:
|
|
38
|
+
}, R = (r) => {
|
|
39
|
+
const { config: e } = r, { method: t, url: n, data: s } = e;
|
|
40
|
+
return [t, n, JSON.stringify(s)].join("|");
|
|
41
|
+
}, B = {
|
|
42
|
+
key: R
|
|
43
|
+
}, _ = (r) => {
|
|
44
|
+
const e = { ...B, ...r }, t = q();
|
|
45
|
+
return Object.assign(({ config: s, next: o }) => {
|
|
46
|
+
const i = e.key({ config: s }), a = t.getPromise(i);
|
|
47
47
|
if (a)
|
|
48
48
|
return a;
|
|
49
|
-
const
|
|
50
|
-
return t.setPromise(i,
|
|
49
|
+
const c = o();
|
|
50
|
+
return t.setPromise(i, c), c;
|
|
51
51
|
}, {
|
|
52
|
-
__middlewareType:
|
|
52
|
+
__middlewareType: y.IDEMPOTENT,
|
|
53
53
|
promiseCache: t
|
|
54
54
|
});
|
|
55
55
|
};
|
|
56
|
-
class
|
|
56
|
+
class K {
|
|
57
57
|
constructor(e = 4) {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
this.parallelCount = e, this.tasks = new
|
|
58
|
+
l(this, "parallelCount");
|
|
59
|
+
l(this, "tasks");
|
|
60
|
+
l(this, "runningCount");
|
|
61
|
+
this.parallelCount = e, this.tasks = new $(), this.runningCount = 0;
|
|
62
62
|
}
|
|
63
63
|
// 加入
|
|
64
64
|
add(e, t) {
|
|
65
|
-
return new Promise((
|
|
65
|
+
return new Promise((n, s) => {
|
|
66
66
|
this.tasks.enqueue(e, {
|
|
67
67
|
task: t,
|
|
68
|
-
resolve:
|
|
69
|
-
reject:
|
|
68
|
+
resolve: n,
|
|
69
|
+
reject: s
|
|
70
70
|
}), this._run();
|
|
71
71
|
});
|
|
72
72
|
}
|
|
@@ -75,8 +75,8 @@ class z {
|
|
|
75
75
|
this.tasks.remove(e);
|
|
76
76
|
}
|
|
77
77
|
execute(e) {
|
|
78
|
-
const { task: t, resolve:
|
|
79
|
-
return t().then(
|
|
78
|
+
const { task: t, resolve: n, reject: s } = e;
|
|
79
|
+
return t().then(n).catch(s).finally(() => {
|
|
80
80
|
this.runningCount--, this._run();
|
|
81
81
|
});
|
|
82
82
|
}
|
|
@@ -87,20 +87,28 @@ class z {
|
|
|
87
87
|
}
|
|
88
88
|
}
|
|
89
89
|
}
|
|
90
|
-
let
|
|
91
|
-
const
|
|
90
|
+
let A = 0;
|
|
91
|
+
const J = {
|
|
92
92
|
parallelCount: 4,
|
|
93
|
-
createId: () =>
|
|
94
|
-
},
|
|
95
|
-
const { parallelCount: e, createId: t } = {
|
|
93
|
+
createId: () => A++
|
|
94
|
+
}, z = (r) => {
|
|
95
|
+
const { parallelCount: e, createId: t } = { ...J, ...r }, n = new K(e);
|
|
96
96
|
return Object.assign(({ config: o, next: i }) => {
|
|
97
97
|
const a = t({ config: o });
|
|
98
|
-
return
|
|
99
|
-
}, { __middlewareType:
|
|
98
|
+
return n.add(a, () => i());
|
|
99
|
+
}, { __middlewareType: y.CONCURRENT, pool: n });
|
|
100
|
+
}, b = /* @__PURE__ */ new Map(), g = (r, e) => {
|
|
101
|
+
b.set(e, r);
|
|
100
102
|
};
|
|
101
|
-
|
|
103
|
+
function G(r) {
|
|
104
|
+
const e = b.get(r);
|
|
105
|
+
if (!e)
|
|
106
|
+
throw new Error(`Store实例 ${String(r)} 未注册`);
|
|
107
|
+
return e;
|
|
108
|
+
}
|
|
109
|
+
class U {
|
|
102
110
|
constructor() {
|
|
103
|
-
|
|
111
|
+
l(this, "store", /* @__PURE__ */ new Map());
|
|
104
112
|
}
|
|
105
113
|
getItem(e) {
|
|
106
114
|
return this.store.get(e);
|
|
@@ -117,353 +125,574 @@ class v {
|
|
|
117
125
|
length() {
|
|
118
126
|
return this.store.size;
|
|
119
127
|
}
|
|
128
|
+
key(e) {
|
|
129
|
+
return Array.from(this.store.keys())[e];
|
|
130
|
+
}
|
|
120
131
|
keys() {
|
|
121
132
|
return Array.from(this.store.keys());
|
|
122
133
|
}
|
|
123
134
|
iterate(e) {
|
|
124
135
|
let t = 0;
|
|
125
|
-
for (const [
|
|
126
|
-
e(
|
|
136
|
+
for (const [n, s] of this.store.entries())
|
|
137
|
+
e(s, n, t), t++;
|
|
127
138
|
}
|
|
128
139
|
}
|
|
129
|
-
const
|
|
130
|
-
|
|
140
|
+
const Y = (r) => !!r && (typeof r == "object" || typeof r == "function") && typeof r.then == "function", N = (r) => typeof window < "u" ? r() : {
|
|
141
|
+
getItem() {
|
|
142
|
+
return null;
|
|
143
|
+
},
|
|
144
|
+
setItem() {
|
|
145
|
+
},
|
|
146
|
+
removeItem() {
|
|
147
|
+
},
|
|
148
|
+
clear() {
|
|
149
|
+
},
|
|
150
|
+
key() {
|
|
151
|
+
return null;
|
|
152
|
+
},
|
|
153
|
+
get length() {
|
|
154
|
+
return 0;
|
|
155
|
+
}
|
|
156
|
+
}, d = N(() => window.localStorage);
|
|
157
|
+
class M {
|
|
131
158
|
constructor() {
|
|
132
159
|
}
|
|
133
160
|
getItem(e) {
|
|
134
|
-
const t = String(e),
|
|
135
|
-
if (
|
|
161
|
+
const t = String(e), n = d.getItem(t);
|
|
162
|
+
if (n !== null)
|
|
136
163
|
try {
|
|
137
|
-
return JSON.parse(
|
|
138
|
-
} catch (
|
|
139
|
-
console.error(`Failed to parse value for key: ${t}`,
|
|
164
|
+
return JSON.parse(n);
|
|
165
|
+
} catch (s) {
|
|
166
|
+
console.error(`Failed to parse value for key: ${t}`, s);
|
|
140
167
|
return;
|
|
141
168
|
}
|
|
142
169
|
}
|
|
143
170
|
setItem(e, t) {
|
|
144
|
-
const
|
|
171
|
+
const n = String(e);
|
|
145
172
|
try {
|
|
146
|
-
|
|
147
|
-
} catch (
|
|
148
|
-
throw console.error(`Failed to set value for key: ${
|
|
173
|
+
d.setItem(n, JSON.stringify(t));
|
|
174
|
+
} catch (s) {
|
|
175
|
+
throw console.error(`Failed to set value for key: ${n}`, s), s;
|
|
149
176
|
}
|
|
150
177
|
return t;
|
|
151
178
|
}
|
|
152
179
|
removeItem(e) {
|
|
153
180
|
const t = String(e);
|
|
154
|
-
|
|
181
|
+
d.removeItem(t);
|
|
155
182
|
}
|
|
156
183
|
clear() {
|
|
157
|
-
|
|
184
|
+
d.clear();
|
|
158
185
|
}
|
|
159
186
|
length() {
|
|
160
|
-
return
|
|
187
|
+
return d.length;
|
|
188
|
+
}
|
|
189
|
+
key(e) {
|
|
190
|
+
return d.key(e);
|
|
161
191
|
}
|
|
162
192
|
keys() {
|
|
163
193
|
const e = [];
|
|
164
|
-
for (let t = 0; t <
|
|
165
|
-
const
|
|
166
|
-
|
|
194
|
+
for (let t = 0; t < d.length; t++) {
|
|
195
|
+
const n = d.key(t);
|
|
196
|
+
n && e.push(n);
|
|
167
197
|
}
|
|
168
198
|
return e;
|
|
169
199
|
}
|
|
170
200
|
iterate(e) {
|
|
171
|
-
this.keys().forEach((
|
|
172
|
-
const
|
|
173
|
-
|
|
201
|
+
this.keys().forEach((t, n) => {
|
|
202
|
+
const s = this.getItem(t);
|
|
203
|
+
s !== void 0 && e(s, t, n);
|
|
174
204
|
});
|
|
175
205
|
}
|
|
176
206
|
}
|
|
177
|
-
|
|
207
|
+
const m = N(() => window.sessionStorage);
|
|
208
|
+
class X {
|
|
209
|
+
constructor() {
|
|
210
|
+
}
|
|
211
|
+
getItem(e) {
|
|
212
|
+
const t = String(e), n = m.getItem(t);
|
|
213
|
+
if (n !== null)
|
|
214
|
+
try {
|
|
215
|
+
return JSON.parse(n);
|
|
216
|
+
} catch (s) {
|
|
217
|
+
console.error(`Failed to parse value for key: ${t}`, s);
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
setItem(e, t) {
|
|
222
|
+
const n = String(e);
|
|
223
|
+
try {
|
|
224
|
+
m.setItem(n, JSON.stringify(t));
|
|
225
|
+
} catch (s) {
|
|
226
|
+
throw console.error(`Failed to set value for key: ${n}`, s), s;
|
|
227
|
+
}
|
|
228
|
+
return t;
|
|
229
|
+
}
|
|
230
|
+
removeItem(e) {
|
|
231
|
+
const t = String(e);
|
|
232
|
+
m.removeItem(t);
|
|
233
|
+
}
|
|
234
|
+
clear() {
|
|
235
|
+
m.clear();
|
|
236
|
+
}
|
|
237
|
+
length() {
|
|
238
|
+
return m.length;
|
|
239
|
+
}
|
|
240
|
+
key(e) {
|
|
241
|
+
return m.key(e);
|
|
242
|
+
}
|
|
243
|
+
keys() {
|
|
244
|
+
const e = [];
|
|
245
|
+
for (let t = 0; t < m.length; t++) {
|
|
246
|
+
const n = m.key(t);
|
|
247
|
+
n && e.push(n);
|
|
248
|
+
}
|
|
249
|
+
return e;
|
|
250
|
+
}
|
|
251
|
+
iterate(e) {
|
|
252
|
+
this.keys().forEach((t, n) => {
|
|
253
|
+
const s = this.getItem(t);
|
|
254
|
+
s !== void 0 && e(s, t, n);
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
class Z {
|
|
259
|
+
constructor(e, t) {
|
|
260
|
+
l(this, "dbName");
|
|
261
|
+
l(this, "storeName");
|
|
262
|
+
l(this, "dbPromise", null);
|
|
263
|
+
l(this, "DB_VERSION", 1);
|
|
264
|
+
this.dbName = e, this.storeName = t, this.initDB();
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* 初始化数据库连接
|
|
268
|
+
*/
|
|
269
|
+
initDB() {
|
|
270
|
+
if (typeof window > "u" || !window.indexedDB) {
|
|
271
|
+
console.warn("IndexedDB is not available");
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
this.dbPromise = new Promise((e, t) => {
|
|
275
|
+
const n = indexedDB.open(this.dbName, this.DB_VERSION);
|
|
276
|
+
n.onerror = () => {
|
|
277
|
+
t(new Error(`Failed to open database: ${this.dbName}`));
|
|
278
|
+
}, n.onsuccess = () => {
|
|
279
|
+
e(n.result);
|
|
280
|
+
}, n.onupgradeneeded = (s) => {
|
|
281
|
+
const o = s.target.result;
|
|
282
|
+
o.objectStoreNames.contains(this.storeName) || o.createObjectStore(this.storeName);
|
|
283
|
+
};
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* 获取数据库实例
|
|
288
|
+
*/
|
|
289
|
+
async getDB() {
|
|
290
|
+
if (!this.dbPromise)
|
|
291
|
+
throw new Error("IndexedDB is not initialized");
|
|
292
|
+
return this.dbPromise;
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* 执行事务
|
|
296
|
+
*/
|
|
297
|
+
async withStore(e, t) {
|
|
298
|
+
const n = await this.getDB();
|
|
299
|
+
return new Promise((s, o) => {
|
|
300
|
+
const i = n.transaction([this.storeName], e).objectStore(this.storeName), a = t(i);
|
|
301
|
+
a.onsuccess = () => {
|
|
302
|
+
s(a.result);
|
|
303
|
+
}, a.onerror = () => {
|
|
304
|
+
o(a.error);
|
|
305
|
+
};
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
async getItem(e) {
|
|
309
|
+
try {
|
|
310
|
+
const t = await this.withStore("readonly", (n) => n.get(String(e)));
|
|
311
|
+
return t === void 0 ? void 0 : t;
|
|
312
|
+
} catch (t) {
|
|
313
|
+
console.error(`Failed to get value for key: ${String(e)}`, t);
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
async setItem(e, t) {
|
|
318
|
+
try {
|
|
319
|
+
return await this.withStore("readwrite", (n) => n.put(t, String(e))), t;
|
|
320
|
+
} catch (n) {
|
|
321
|
+
throw console.error(`Failed to set value for key: ${String(e)}`, n), n;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
async removeItem(e) {
|
|
325
|
+
try {
|
|
326
|
+
await this.withStore("readwrite", (t) => t.delete(String(e)));
|
|
327
|
+
} catch (t) {
|
|
328
|
+
throw console.error(`Failed to remove value for key: ${String(e)}`, t), t;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
async clear() {
|
|
332
|
+
try {
|
|
333
|
+
await this.withStore("readwrite", (e) => e.clear());
|
|
334
|
+
} catch (e) {
|
|
335
|
+
throw console.error("Failed to clear storage", e), e;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
async length() {
|
|
339
|
+
try {
|
|
340
|
+
return await this.withStore("readonly", (e) => e.count());
|
|
341
|
+
} catch (e) {
|
|
342
|
+
return console.error("Failed to get storage length", e), 0;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
async key(e) {
|
|
346
|
+
try {
|
|
347
|
+
return (await this.withStore("readonly", (t) => t.getAllKeys()))[e];
|
|
348
|
+
} catch (t) {
|
|
349
|
+
console.error("Failed to get key", t);
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
async keys() {
|
|
354
|
+
try {
|
|
355
|
+
return await this.withStore("readonly", (e) => e.getAllKeys());
|
|
356
|
+
} catch (e) {
|
|
357
|
+
return console.error("Failed to get all keys", e), [];
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
async iterate(e) {
|
|
361
|
+
try {
|
|
362
|
+
const t = (await this.getDB()).transaction([this.storeName], "readonly").objectStore(this.storeName).openCursor();
|
|
363
|
+
return new Promise((n, s) => {
|
|
364
|
+
let o = 0;
|
|
365
|
+
t.onsuccess = (i) => {
|
|
366
|
+
const a = i.target.result;
|
|
367
|
+
if (a) {
|
|
368
|
+
const c = a.key, u = a.value;
|
|
369
|
+
e(u, c, o), o++, a.continue();
|
|
370
|
+
} else
|
|
371
|
+
n();
|
|
372
|
+
}, t.onerror = () => {
|
|
373
|
+
s(t.error);
|
|
374
|
+
};
|
|
375
|
+
});
|
|
376
|
+
} catch (t) {
|
|
377
|
+
console.error("Failed to iterate storage", t);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
const f = {
|
|
382
|
+
memory: "memory",
|
|
383
|
+
local: "local",
|
|
384
|
+
session: "session",
|
|
385
|
+
indexeddb: "indexeddb"
|
|
386
|
+
};
|
|
387
|
+
function x(r, e) {
|
|
388
|
+
Object.assign(r, e);
|
|
389
|
+
}
|
|
390
|
+
function H(r) {
|
|
391
|
+
return x(r, {
|
|
392
|
+
async getItemOrDefault(e, t) {
|
|
393
|
+
const n = await this.getItem(e);
|
|
394
|
+
return n !== null ? n : t;
|
|
395
|
+
},
|
|
396
|
+
async hasItem(e) {
|
|
397
|
+
return await this.getItem(e) !== null;
|
|
398
|
+
},
|
|
399
|
+
async removeItems(e) {
|
|
400
|
+
await Promise.all(e.map((t) => this.removeItem(t)));
|
|
401
|
+
},
|
|
402
|
+
async getItems(e) {
|
|
403
|
+
return await Promise.all(e.map((t) => this.getItem(t)));
|
|
404
|
+
}
|
|
405
|
+
}), r;
|
|
406
|
+
}
|
|
407
|
+
function L(r) {
|
|
408
|
+
return x(r, {
|
|
409
|
+
getItemOrDefault(e, t) {
|
|
410
|
+
const n = this.getItem(e);
|
|
411
|
+
return n !== null ? n : t;
|
|
412
|
+
},
|
|
413
|
+
hasItem(e) {
|
|
414
|
+
return this.getItem(e) !== null;
|
|
415
|
+
},
|
|
416
|
+
removeItems(e) {
|
|
417
|
+
e.forEach((t) => this.removeItem(t));
|
|
418
|
+
},
|
|
419
|
+
getItems(e) {
|
|
420
|
+
return e.map((t) => this.getItem(t));
|
|
421
|
+
}
|
|
422
|
+
}), r;
|
|
423
|
+
}
|
|
424
|
+
function Q(r) {
|
|
425
|
+
const e = r.getItem("");
|
|
426
|
+
return Y(e) ? H(r) : L(r);
|
|
427
|
+
}
|
|
428
|
+
function p(r, ...e) {
|
|
429
|
+
const t = G(r);
|
|
430
|
+
let n;
|
|
431
|
+
try {
|
|
432
|
+
n = new t(...e);
|
|
433
|
+
} catch {
|
|
434
|
+
n = t(...e);
|
|
435
|
+
}
|
|
436
|
+
return Q(n);
|
|
437
|
+
}
|
|
438
|
+
g(U, f.memory);
|
|
439
|
+
g(M, f.local);
|
|
440
|
+
g(X, f.session);
|
|
441
|
+
g(Z, f.indexeddb);
|
|
442
|
+
function I(r, e) {
|
|
178
443
|
return {
|
|
179
444
|
value: r,
|
|
180
445
|
expireAt: Date.now() + e
|
|
181
446
|
};
|
|
182
447
|
}
|
|
183
|
-
function
|
|
448
|
+
function k(r, e = Date.now()) {
|
|
184
449
|
return r.expireAt <= e;
|
|
185
450
|
}
|
|
186
|
-
function
|
|
187
|
-
return r.value;
|
|
451
|
+
function P(r, e = Date.now()) {
|
|
452
|
+
return k(r, e) ? void 0 : r.value;
|
|
188
453
|
}
|
|
189
|
-
const
|
|
190
|
-
const { config: e } = r, { method: t, url:
|
|
191
|
-
return [t,
|
|
192
|
-
},
|
|
193
|
-
key:
|
|
194
|
-
duration:
|
|
195
|
-
isValid:
|
|
196
|
-
|
|
454
|
+
const W = (r) => {
|
|
455
|
+
const { config: e } = r, { method: t, url: n, data: s } = e;
|
|
456
|
+
return [t, n, JSON.stringify(s)].join("|");
|
|
457
|
+
}, ee = () => !0, S = 24 * 60 * 60 * 1e3, te = {
|
|
458
|
+
key: W,
|
|
459
|
+
duration: S,
|
|
460
|
+
isValid: ee,
|
|
461
|
+
store: f.memory
|
|
197
462
|
// 默认不持久化,使用内存存储
|
|
198
463
|
};
|
|
199
|
-
class
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
if (t) {
|
|
209
|
-
if (m(t)) {
|
|
210
|
-
this.removeItem(e);
|
|
211
|
-
return;
|
|
464
|
+
let re = class {
|
|
465
|
+
constructor(e) {
|
|
466
|
+
l(this, "store");
|
|
467
|
+
return typeof e == "string" ? this.store = p(e) : (g(e.factory, e.key), this.store = p(e.key)), new Proxy(this, {
|
|
468
|
+
get(t, n) {
|
|
469
|
+
if (n in t)
|
|
470
|
+
return t[n];
|
|
471
|
+
const s = t.store[n];
|
|
472
|
+
return typeof s == "function" ? s.bind(t.store) : s;
|
|
212
473
|
}
|
|
213
|
-
|
|
214
|
-
}
|
|
474
|
+
});
|
|
215
475
|
}
|
|
216
476
|
/**
|
|
217
|
-
*
|
|
218
|
-
* @param key
|
|
477
|
+
* 设置缓存(自动包装成 ExpirableValue)
|
|
478
|
+
* @param key 缓存 key
|
|
219
479
|
* @param value 要缓存的值
|
|
220
|
-
* @param duration
|
|
221
|
-
*/
|
|
222
|
-
set(e, t, s = x) {
|
|
223
|
-
const n = w(t, s);
|
|
224
|
-
this.setItem(e, n);
|
|
225
|
-
}
|
|
226
|
-
/**
|
|
227
|
-
* 删除指定缓存(别名方法,等同于 removeItem)
|
|
228
|
-
* @param key 缓存键
|
|
480
|
+
* @param duration 过期时长(毫秒),默认 24 小时
|
|
229
481
|
*/
|
|
230
|
-
|
|
231
|
-
|
|
482
|
+
setCache(e, t, n = S) {
|
|
483
|
+
const s = I(t, n);
|
|
484
|
+
this.store.setItem(e, s);
|
|
232
485
|
}
|
|
233
486
|
/**
|
|
234
|
-
*
|
|
235
|
-
* @param key
|
|
487
|
+
* 获取缓存值(检查过期,如果过期返回 undefined)
|
|
488
|
+
* @param key 缓存 key
|
|
489
|
+
* @returns 未过期返回值,已过期返回 undefined
|
|
236
490
|
*/
|
|
237
|
-
|
|
238
|
-
const t = this.getItem(e);
|
|
239
|
-
|
|
491
|
+
async getCache(e) {
|
|
492
|
+
const t = await this.store.getItem(e);
|
|
493
|
+
if (t)
|
|
494
|
+
return P(t);
|
|
240
495
|
}
|
|
241
496
|
/**
|
|
242
|
-
*
|
|
497
|
+
* 检查是否有有效的缓存(未过期)
|
|
498
|
+
* @param key 缓存 key
|
|
499
|
+
* @returns true 表示有有效缓存,false 表示无缓存或已过期
|
|
243
500
|
*/
|
|
244
|
-
|
|
245
|
-
|
|
501
|
+
async hasValidCache(e) {
|
|
502
|
+
const t = await this.store.getItem(e);
|
|
503
|
+
return t ? !k(t) : !1;
|
|
246
504
|
}
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
505
|
+
};
|
|
506
|
+
const ne = re, se = (r) => {
|
|
507
|
+
const e = { ...te, ...r }, t = new ne(e.store), n = (o) => typeof e.duration == "function" ? e.duration(o) : e.duration;
|
|
508
|
+
return Object.assign(async ({ config: o, next: i }) => {
|
|
509
|
+
const a = e.key({ config: o }), c = await t.getCache(a);
|
|
510
|
+
if (c) {
|
|
511
|
+
if (await e.isValid({
|
|
512
|
+
key: a,
|
|
513
|
+
config: o,
|
|
514
|
+
cachedData: c
|
|
515
|
+
})) return c;
|
|
516
|
+
t.removeItem(a);
|
|
517
|
+
}
|
|
518
|
+
const u = await i(), h = n({ key: a, config: o, cachedData: c, response: u }), C = I(u, h);
|
|
519
|
+
return t.setItem(a, C), u;
|
|
520
|
+
}, {
|
|
521
|
+
__middlewareType: y.CACHE,
|
|
522
|
+
storage: t
|
|
523
|
+
});
|
|
524
|
+
};
|
|
525
|
+
class oe {
|
|
526
|
+
constructor(e) {
|
|
527
|
+
l(this, "store");
|
|
528
|
+
return typeof e == "string" ? this.store = p(e) : (g(e.factory, e.key), this.store = p(e.key)), new Proxy(this, {
|
|
529
|
+
get(t, n) {
|
|
530
|
+
if (n in t)
|
|
531
|
+
return t[n];
|
|
532
|
+
const s = t.store[n];
|
|
533
|
+
return typeof s == "function" ? s.bind(t.store) : s;
|
|
259
534
|
}
|
|
260
535
|
});
|
|
261
536
|
}
|
|
262
537
|
/**
|
|
263
|
-
*
|
|
264
|
-
* @
|
|
538
|
+
* 设置缓存(自动包装成 ExpirableValue)
|
|
539
|
+
* @param key 缓存 key
|
|
540
|
+
* @param value 要缓存的值
|
|
541
|
+
* @param duration 过期时长(毫秒),默认 24 小时
|
|
265
542
|
*/
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
const n = this.getItem(s);
|
|
270
|
-
n && m(n) && (this.removeItem(s), e++);
|
|
271
|
-
}), e;
|
|
543
|
+
setCache(e, t, n = S) {
|
|
544
|
+
const s = I(t, n);
|
|
545
|
+
this.store.setItem(e, s);
|
|
272
546
|
}
|
|
273
547
|
/**
|
|
274
|
-
*
|
|
275
|
-
* @param key
|
|
276
|
-
* @
|
|
277
|
-
* @returns 是否更新成功
|
|
548
|
+
* 获取缓存值(检查过期,如果过期返回 undefined)
|
|
549
|
+
* @param key 缓存 key
|
|
550
|
+
* @returns 未过期返回值,已过期返回 undefined
|
|
278
551
|
*/
|
|
279
|
-
|
|
280
|
-
const
|
|
281
|
-
if (
|
|
282
|
-
|
|
283
|
-
return this.removeItem(e), !1;
|
|
284
|
-
const n = g(s);
|
|
285
|
-
return this.set(e, n, t), !0;
|
|
552
|
+
getCache(e) {
|
|
553
|
+
const t = this.store.getItem(e);
|
|
554
|
+
if (t)
|
|
555
|
+
return P(t);
|
|
286
556
|
}
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
c(this, "delete", l.prototype.delete);
|
|
296
|
-
c(this, "has", l.prototype.has);
|
|
297
|
-
c(this, "size", l.prototype.size);
|
|
298
|
-
c(this, "forEach", l.prototype.forEach);
|
|
299
|
-
c(this, "clearExpired", l.prototype.clearExpired);
|
|
300
|
-
c(this, "touch", l.prototype.touch);
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
class U extends k {
|
|
304
|
-
constructor() {
|
|
305
|
-
super(...arguments);
|
|
306
|
-
// 继承 LocalStorage 的所有方法:getItem, setItem, removeItem, clear, length, keys, iterate
|
|
307
|
-
// 从 BaseCacheStorage 继承扩展方法
|
|
308
|
-
c(this, "get", l.prototype.get);
|
|
309
|
-
c(this, "set", l.prototype.set);
|
|
310
|
-
c(this, "delete", l.prototype.delete);
|
|
311
|
-
c(this, "has", l.prototype.has);
|
|
312
|
-
c(this, "size", l.prototype.size);
|
|
313
|
-
c(this, "forEach", l.prototype.forEach);
|
|
314
|
-
c(this, "clearExpired", l.prototype.clearExpired);
|
|
315
|
-
c(this, "touch", l.prototype.touch);
|
|
557
|
+
/**
|
|
558
|
+
* 检查是否有有效的缓存(未过期)
|
|
559
|
+
* @param key 缓存 key
|
|
560
|
+
* @returns true 表示有有效缓存,false 表示无缓存或已过期
|
|
561
|
+
*/
|
|
562
|
+
hasValidCache(e) {
|
|
563
|
+
const t = this.store.getItem(e);
|
|
564
|
+
return t ? !k(t) : !1;
|
|
316
565
|
}
|
|
317
566
|
}
|
|
318
|
-
const
|
|
319
|
-
const
|
|
320
|
-
return
|
|
321
|
-
|
|
322
|
-
if (u) {
|
|
323
|
-
if (!m(u) && await e.isValid({
|
|
324
|
-
key: a,
|
|
325
|
-
config: o,
|
|
326
|
-
cachedData: u
|
|
327
|
-
}))
|
|
328
|
-
return g(u);
|
|
329
|
-
t.removeItem(a);
|
|
330
|
-
}
|
|
331
|
-
const d = await i(), h = s({ key: a, config: o, cachedData: u, response: d }), C = w(d, h);
|
|
332
|
-
return t.setItem(a, C), d;
|
|
333
|
-
}, {
|
|
334
|
-
__middlewareType: f.CACHE,
|
|
335
|
-
storage: t
|
|
336
|
-
});
|
|
337
|
-
}, L = (r) => {
|
|
338
|
-
const { config: e } = r, { method: t, url: s } = e;
|
|
339
|
-
return [t, s].join("|");
|
|
340
|
-
}, M = {
|
|
567
|
+
const ae = oe, ie = (r) => {
|
|
568
|
+
const { config: e } = r, { method: t, url: n } = e;
|
|
569
|
+
return [t, n].join("|");
|
|
570
|
+
}, ce = {
|
|
341
571
|
suspense: !0,
|
|
342
|
-
key:
|
|
572
|
+
key: ie,
|
|
343
573
|
duration: 24 * 60 * 60 * 1e3,
|
|
344
574
|
isValid: () => !0,
|
|
345
|
-
|
|
575
|
+
store: f.memory
|
|
346
576
|
};
|
|
347
|
-
function
|
|
348
|
-
const e = { ...
|
|
577
|
+
function we(r) {
|
|
578
|
+
const e = { ...ce, ...r }, t = new ae(e.store), n = (o) => typeof e.duration == "function" ? e.duration(o) : e.duration;
|
|
349
579
|
return Object.assign(({ config: o, next: i }) => {
|
|
350
|
-
const a = e.key({ config: o }),
|
|
351
|
-
if (
|
|
352
|
-
if (
|
|
580
|
+
const a = e.key({ config: o }), c = t.getCache(a);
|
|
581
|
+
if (c) {
|
|
582
|
+
if (e.isValid({
|
|
353
583
|
key: a,
|
|
354
584
|
config: o,
|
|
355
|
-
cachedData:
|
|
356
|
-
}))
|
|
357
|
-
return g(u);
|
|
585
|
+
cachedData: c
|
|
586
|
+
})) return c;
|
|
358
587
|
t.removeItem(a);
|
|
359
588
|
}
|
|
360
589
|
if (e.suspense)
|
|
361
590
|
throw (e.wrapSuspense ? e.wrapSuspense({ key: a, config: o, p: i() }) : i()).then((h) => {
|
|
362
|
-
const C =
|
|
363
|
-
return t.
|
|
591
|
+
const C = n({ key: a, config: o, cachedData: c, response: h });
|
|
592
|
+
return t.setCache(a, h, C), h;
|
|
364
593
|
});
|
|
365
|
-
return i().then((
|
|
366
|
-
const h =
|
|
367
|
-
return t.
|
|
594
|
+
return i().then((u) => {
|
|
595
|
+
const h = n({ key: a, config: o, cachedData: c, response: u });
|
|
596
|
+
return t.setCache(a, u, h), u;
|
|
368
597
|
});
|
|
369
|
-
}, { __middlewareType:
|
|
598
|
+
}, { __middlewareType: y.SYNC });
|
|
370
599
|
}
|
|
371
|
-
const
|
|
372
|
-
|
|
373
|
-
},
|
|
374
|
-
const e =
|
|
600
|
+
const V = /* @__PURE__ */ new Map(), pe = (r, e = v) => {
|
|
601
|
+
V.set(e, r);
|
|
602
|
+
}, ue = (r = v) => {
|
|
603
|
+
const e = V.get(r);
|
|
375
604
|
if (!e) throw new Error(`Requestor实例 ${String(r)} 未注册`);
|
|
376
605
|
return e;
|
|
377
|
-
},
|
|
606
|
+
}, O = {
|
|
378
607
|
get: (r, e) => ({
|
|
379
608
|
url: r,
|
|
380
|
-
method:
|
|
609
|
+
method: w.GET,
|
|
381
610
|
...e,
|
|
382
611
|
params: e == null ? void 0 : e.params
|
|
383
612
|
}),
|
|
384
613
|
post: (r, e, t) => ({
|
|
385
614
|
url: r,
|
|
386
|
-
method:
|
|
615
|
+
method: w.POST,
|
|
387
616
|
data: e,
|
|
388
617
|
...t
|
|
389
618
|
}),
|
|
390
619
|
delete: (r, e) => ({
|
|
391
620
|
url: r,
|
|
392
|
-
method:
|
|
621
|
+
method: w.DELETE,
|
|
393
622
|
...e
|
|
394
623
|
}),
|
|
395
624
|
put: (r, e, t) => ({
|
|
396
625
|
url: r,
|
|
397
|
-
method:
|
|
626
|
+
method: w.PUT,
|
|
398
627
|
data: e,
|
|
399
628
|
...t
|
|
400
629
|
}),
|
|
401
630
|
request: (r) => r
|
|
402
|
-
},
|
|
403
|
-
function
|
|
404
|
-
const
|
|
631
|
+
}, le = Object.keys(O);
|
|
632
|
+
function he(r, e, t) {
|
|
633
|
+
const n = {}, s = (o) => {
|
|
405
634
|
if (o === e.length)
|
|
406
635
|
return t(r);
|
|
407
636
|
const i = e[o];
|
|
408
637
|
return i({
|
|
409
638
|
config: r,
|
|
410
|
-
ctx:
|
|
411
|
-
next: () =>
|
|
639
|
+
ctx: n,
|
|
640
|
+
next: () => s(o + 1)
|
|
412
641
|
});
|
|
413
642
|
};
|
|
414
|
-
return
|
|
643
|
+
return s(0);
|
|
415
644
|
}
|
|
416
|
-
function
|
|
417
|
-
const t = {},
|
|
418
|
-
return
|
|
419
|
-
(
|
|
420
|
-
t[
|
|
421
|
-
const i =
|
|
422
|
-
return
|
|
645
|
+
function de(r, e) {
|
|
646
|
+
const t = {}, n = e ?? [];
|
|
647
|
+
return le.forEach(
|
|
648
|
+
(s) => {
|
|
649
|
+
t[s] = (...o) => {
|
|
650
|
+
const i = O[s](...o);
|
|
651
|
+
return he(
|
|
423
652
|
i,
|
|
424
|
-
|
|
653
|
+
n,
|
|
425
654
|
r
|
|
426
655
|
);
|
|
427
656
|
};
|
|
428
657
|
}
|
|
429
658
|
), t;
|
|
430
659
|
}
|
|
431
|
-
function
|
|
432
|
-
const { extensions: e, instanceKey: t } = r ?? {},
|
|
433
|
-
return
|
|
434
|
-
|
|
660
|
+
function j(r) {
|
|
661
|
+
const { extensions: e, instanceKey: t } = r ?? {}, n = ue(t);
|
|
662
|
+
return de(
|
|
663
|
+
n,
|
|
435
664
|
e
|
|
436
665
|
);
|
|
437
666
|
}
|
|
438
|
-
function
|
|
439
|
-
const { instanceKey: e, key: t, duration:
|
|
440
|
-
return
|
|
667
|
+
function Ce(r) {
|
|
668
|
+
const { instanceKey: e, key: t, duration: n, isValid: s, store: o, ...i } = r;
|
|
669
|
+
return j({
|
|
441
670
|
instanceKey: e,
|
|
442
671
|
extensions: [
|
|
443
|
-
|
|
444
|
-
|
|
672
|
+
_(i),
|
|
673
|
+
se({ key: t, duration: n, isValid: s, store: o })
|
|
445
674
|
]
|
|
446
675
|
});
|
|
447
676
|
}
|
|
448
|
-
function
|
|
449
|
-
const { instanceKey: e, parallelCount: t, createId:
|
|
450
|
-
return
|
|
677
|
+
function Ie(r) {
|
|
678
|
+
const { instanceKey: e, parallelCount: t, createId: n, retries: s, delay: o, retryCondition: i } = r;
|
|
679
|
+
return j({
|
|
451
680
|
instanceKey: e,
|
|
452
681
|
extensions: [
|
|
453
|
-
|
|
454
|
-
|
|
682
|
+
z({ parallelCount: t, createId: n }),
|
|
683
|
+
T({ retries: s, delay: o, retryCondition: i })
|
|
455
684
|
]
|
|
456
685
|
});
|
|
457
686
|
}
|
|
458
687
|
export {
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
688
|
+
se as cache,
|
|
689
|
+
z as concurrent,
|
|
690
|
+
Ce as createCachedIdempotentRequestor,
|
|
691
|
+
Ie as createConcurrentRetryRequestor,
|
|
692
|
+
j as createRequestor,
|
|
693
|
+
_ as idempotent,
|
|
694
|
+
pe as inject,
|
|
695
|
+
T as retry,
|
|
696
|
+
we as sync,
|
|
697
|
+
ue as useRequestor
|
|
469
698
|
};
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(c,h){typeof exports=="object"&&typeof module<"u"?h(exports,require("id-queue")):typeof define=="function"&&define.amd?define(["exports","id-queue"],h):(c=typeof globalThis<"u"?globalThis:c||self,h(c.netVertCore={},c.idQueue))})(this,function(c,h){"use strict";var W=Object.defineProperty;var ee=(c,h,p)=>h in c?W(c,h,{enumerable:!0,configurable:!0,writable:!0,value:p}):c[h]=p;var u=(c,h,p)=>ee(c,typeof h!="symbol"?h+"":h,p);var p=(r=>(r.GET="get",r.POST="post",r.PUT="put",r.DELETE="delete",r))(p||{}),w=(r=>(r.CACHE="cache",r.RETRY="retry",r.IDEMPOTENT="idempotent",r.CONCURRENT="concurrent",r.SYNC="sync",r))(w||{});const q="default",N={retries:3,delay:0,retryCondition:()=>!0},S=r=>{const e={...N,...r};return Object.assign(async({config:s,next:n})=>{let o=0,i;for(;o<=e.retries;)try{return await n()}catch(a){if(i=a,o===e.retries)throw a;const l={config:s,lastResponse:a,attempt:o};if(!e.retryCondition(l))throw a;o++;const m=typeof e.delay=="function"?e.delay(l):e.delay;m>0&&await new Promise(f=>setTimeout(f,m))}throw i},{__middlewareType:w.RETRY})},b=()=>{const r=new Map;return{getPromise:o=>r.get(o),setPromise:(o,i)=>{r.set(o,i),i.finally(()=>r.delete(o))},delPromise:o=>r.delete(o),clearCache:()=>r.clear()}},z={key:r=>{const{config:e}=r,{method:t,url:s,data:n}=e;return[t,s,JSON.stringify(n)].join("|")}},T=r=>{const e={...z,...r},t=b();return Object.assign(({config:n,next:o})=>{const i=e.key({config:n}),a=t.getPromise(i);if(a)return a;const l=o();return t.setPromise(i,l),l},{__middlewareType:w.IDEMPOTENT,promiseCache:t})};class _{constructor(e=4){u(this,"parallelCount");u(this,"tasks");u(this,"runningCount");this.parallelCount=e,this.tasks=new h.TaskQueue,this.runningCount=0}add(e,t){return new Promise((s,n)=>{this.tasks.enqueue(e,{task:t,resolve:s,reject:n}),this._run()})}remove(e){this.tasks.remove(e)}execute(e){const{task:t,resolve:s,reject:n}=e;return t().then(s).catch(n).finally(()=>{this.runningCount--,this._run()})}_run(){for(;this.runningCount<this.parallelCount&&this.tasks.size>0;){const e=this.tasks.dequeue();this.runningCount++,this.execute(e)}}}let $=0;const J={parallelCount:4,createId:()=>$++},R=r=>{const{parallelCount:e,createId:t}={...J,...r},s=new _(e);return Object.assign(({config:o,next:i})=>{const a=t({config:o});return s.add(a,()=>i())},{__middlewareType:w.CONCURRENT,pool:s})};class E{constructor(){u(this,"store",new Map)}getItem(e){return this.store.get(e)}setItem(e,t){return this.store.set(e,t),t}removeItem(e){this.store.delete(e)}clear(){this.store.clear()}length(){return this.store.size}keys(){return Array.from(this.store.keys())}iterate(e){let t=0;for(const[s,n]of this.store.entries())e(n,s,t),t++}}const y=typeof window<"u"?window.localStorage:{};class P{constructor(){}getItem(e){const t=String(e),s=y.getItem(t);if(s!==null)try{return JSON.parse(s)}catch(n){console.error(`Failed to parse value for key: ${t}`,n);return}}setItem(e,t){const s=String(e);try{y.setItem(s,JSON.stringify(t))}catch(n){throw console.error(`Failed to set value for key: ${s}`,n),n}return t}removeItem(e){const t=String(e);y.removeItem(t)}clear(){y.clear()}length(){return y.length}keys(){const e=[];for(let t=0;t<y.length;t++){const s=y.key(t);s&&e.push(s)}return e}iterate(e){this.keys().forEach((s,n)=>{const o=this.getItem(s);o!==void 0&&e(o,s,n)})}}function I(r,e){return{value:r,expireAt:Date.now()+e}}function g(r,e=Date.now()){return r.expireAt<=e}function C(r){return r.value}const A=r=>{const{config:e}=r,{method:t,url:s,data:n}=e;return[t,s,JSON.stringify(n)].join("|")},F=()=>!0,j=24*60*60*1e3,G={key:A,duration:j,isValid:F,persist:!1};class d{get(e){const t=this.getItem(e);if(t){if(g(t)){this.removeItem(e);return}return C(t)}}set(e,t,s=j){const n=I(t,s);this.setItem(e,n)}delete(e){this.removeItem(e)}has(e){const t=this.getItem(e);return t?g(t)?(this.removeItem(e),!1):!0:!1}size(){return this.length()}forEach(e){let t=0;this.iterate((s,n,o)=>{if(g(s))this.removeItem(n);else{const i=C(s);e(i,n,t),t++}})}clearExpired(){let e=0;return this.keys().forEach(s=>{const n=this.getItem(s);n&&g(n)&&(this.removeItem(s),e++)}),e}touch(e,t){const s=this.getItem(e);if(!s)return!1;if(g(s))return this.removeItem(e),!1;const n=C(s);return this.set(e,n,t),!0}}class D extends E{constructor(){super(...arguments);u(this,"get",d.prototype.get);u(this,"set",d.prototype.set);u(this,"delete",d.prototype.delete);u(this,"has",d.prototype.has);u(this,"size",d.prototype.size);u(this,"forEach",d.prototype.forEach);u(this,"clearExpired",d.prototype.clearExpired);u(this,"touch",d.prototype.touch)}}class M extends P{constructor(){super(...arguments);u(this,"get",d.prototype.get);u(this,"set",d.prototype.set);u(this,"delete",d.prototype.delete);u(this,"has",d.prototype.has);u(this,"size",d.prototype.size);u(this,"forEach",d.prototype.forEach);u(this,"clearExpired",d.prototype.clearExpired);u(this,"touch",d.prototype.touch)}}const O=r=>{const e={...G,...r},t=e.persist?new M:new D,s=o=>typeof e.duration=="function"?e.duration(o):e.duration;return Object.assign(async({config:o,next:i})=>{const a=e.key({config:o}),l=t.getItem(a);if(l){if(!g(l)&&await e.isValid({key:a,config:o,cachedData:l}))return C(l);t.removeItem(a)}const m=await i(),f=s({key:a,config:o,cachedData:l,response:m}),k=I(m,f);return t.setItem(a,k),m},{__middlewareType:w.CACHE,storage:t})},U={suspense:!0,key:r=>{const{config:e}=r,{method:t,url:s}=e;return[t,s].join("|")},duration:24*60*60*1e3,isValid:()=>!0,persist:!1};function B(r){const e={...U,...r},t=e.persist?new P:new E,s=o=>typeof e.duration=="function"?e.duration(o):e.duration;return Object.assign(({config:o,next:i})=>{const a=e.key({config:o}),l=t.getItem(a);if(l){if(!g(l)&&e.isValid({key:a,config:o,cachedData:l}))return C(l);t.removeItem(a)}if(e.suspense)throw(e.wrapSuspense?e.wrapSuspense({key:a,config:o,p:i()}):i()).then(f=>{const k=s({key:a,config:o,cachedData:l,response:f});return t.setItem(a,I(f,k)),f});return i().then(m=>{const f=s({key:a,config:o,cachedData:l,response:m});return t.setItem(a,I(m,f)),m})},{__middlewareType:w.SYNC})}const x=new Map,L=(r,e=q)=>{x.set(e,r)},V=(r=q)=>{const e=x.get(r);if(!e)throw new Error(`Requestor实例 ${String(r)} 未注册`);return e},K={get:(r,e)=>({url:r,method:p.GET,...e,params:e==null?void 0:e.params}),post:(r,e,t)=>({url:r,method:p.POST,data:e,...t}),delete:(r,e)=>({url:r,method:p.DELETE,...e}),put:(r,e,t)=>({url:r,method:p.PUT,data:e,...t}),request:r=>r},Y=Object.keys(K);function X(r,e,t){const s={},n=o=>{if(o===e.length)return t(r);const i=e[o];return i({config:r,ctx:s,next:()=>n(o+1)})};return n(0)}function Z(r,e){const t={},s=e??[];return Y.forEach(n=>{t[n]=(...o)=>{const i=K[n](...o);return X(i,s,r)}}),t}function v(r){const{extensions:e,instanceKey:t}=r??{},s=V(t);return Z(s,e)}function H(r){const{instanceKey:e,key:t,duration:s,isValid:n,persist:o,...i}=r;return v({instanceKey:e,extensions:[T(i),O({key:t,duration:s,isValid:n,persist:o})]})}function Q(r){const{instanceKey:e,parallelCount:t,createId:s,retries:n,delay:o,retryCondition:i}=r;return v({instanceKey:e,extensions:[R({parallelCount:t,createId:s}),S({retries:n,delay:o,retryCondition:i})]})}c.cache=O,c.concurrent=R,c.createCachedIdempotentRequestor=H,c.createConcurrentRetryRequestor=Q,c.createRequestor=v,c.idempotent=T,c.inject=L,c.retry=S,c.sync=B,c.useRequestor=V,Object.defineProperty(c,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
(function(c,d){typeof exports=="object"&&typeof module<"u"?d(exports,require("id-queue")):typeof define=="function"&&define.amd?define(["exports","id-queue"],d):(c=typeof globalThis<"u"?globalThis:c||self,d(c.netVertCore={},c.idQueue))})(this,function(c,d){"use strict";var me=Object.defineProperty;var ye=(c,d,m)=>d in c?me(c,d,{enumerable:!0,configurable:!0,writable:!0,value:m}):c[d]=m;var h=(c,d,m)=>ye(c,typeof d!="symbol"?d+"":d,m);var m=(r=>(r.GET="get",r.POST="post",r.PUT="put",r.DELETE="delete",r))(m||{}),w=(r=>(r.CACHE="cache",r.RETRY="retry",r.IDEMPOTENT="idempotent",r.CONCURRENT="concurrent",r.SYNC="sync",r))(w||{});const P="default",B={retries:3,delay:0,retryCondition:()=>!0},V=r=>{const e={...B,...r};return Object.assign(async({config:n,next:s})=>{let o=0,a;for(;o<=e.retries;)try{return await s()}catch(i){if(a=i,o===e.retries)throw i;const u={config:n,lastResponse:i,attempt:o};if(!e.retryCondition(u))throw i;o++;const l=typeof e.delay=="function"?e.delay(u):e.delay;l>0&&await new Promise(y=>setTimeout(y,l))}throw a},{__middlewareType:w.RETRY})},K=()=>{const r=new Map;return{getPromise:o=>r.get(o),setPromise:(o,a)=>{r.set(o,a),a.finally(()=>r.delete(o))},delPromise:o=>r.delete(o),clearCache:()=>r.clear()}},_={key:r=>{const{config:e}=r,{method:t,url:n,data:s}=e;return[t,n,JSON.stringify(s)].join("|")}},x=r=>{const e={..._,...r},t=K();return Object.assign(({config:s,next:o})=>{const a=e.key({config:s}),i=t.getPromise(a);if(i)return i;const u=o();return t.setPromise(a,u),u},{__middlewareType:w.IDEMPOTENT,promiseCache:t})};class A{constructor(e=4){h(this,"parallelCount");h(this,"tasks");h(this,"runningCount");this.parallelCount=e,this.tasks=new d.TaskQueue,this.runningCount=0}add(e,t){return new Promise((n,s)=>{this.tasks.enqueue(e,{task:t,resolve:n,reject:s}),this._run()})}remove(e){this.tasks.remove(e)}execute(e){const{task:t,resolve:n,reject:s}=e;return t().then(n).catch(s).finally(()=>{this.runningCount--,this._run()})}_run(){for(;this.runningCount<this.parallelCount&&this.tasks.size>0;){const e=this.tasks.dequeue();this.runningCount++,this.execute(e)}}}let J=0;const z={parallelCount:4,createId:()=>J++},O=r=>{const{parallelCount:e,createId:t}={...z,...r},n=new A(e);return Object.assign(({config:o,next:a})=>{const i=t({config:o});return n.add(i,()=>a())},{__middlewareType:w.CONCURRENT,pool:n})},q=new Map,p=(r,e)=>{q.set(e,r)};function G(r){const e=q.get(r);if(!e)throw new Error(`Store实例 ${String(r)} 未注册`);return e}class U{constructor(){h(this,"store",new Map)}getItem(e){return this.store.get(e)}setItem(e,t){return this.store.set(e,t),t}removeItem(e){this.store.delete(e)}clear(){this.store.clear()}length(){return this.store.size}key(e){return Array.from(this.store.keys())[e]}keys(){return Array.from(this.store.keys())}iterate(e){let t=0;for(const[n,s]of this.store.entries())e(s,n,t),t++}}const M=r=>!!r&&(typeof r=="object"||typeof r=="function")&&typeof r.then=="function",j=r=>typeof window<"u"?r():{getItem(){return null},setItem(){},removeItem(){},clear(){},key(){return null},get length(){return 0}},f=j(()=>window.localStorage);class Y{constructor(){}getItem(e){const t=String(e),n=f.getItem(t);if(n!==null)try{return JSON.parse(n)}catch(s){console.error(`Failed to parse value for key: ${t}`,s);return}}setItem(e,t){const n=String(e);try{f.setItem(n,JSON.stringify(t))}catch(s){throw console.error(`Failed to set value for key: ${n}`,s),s}return t}removeItem(e){const t=String(e);f.removeItem(t)}clear(){f.clear()}length(){return f.length}key(e){return f.key(e)}keys(){const e=[];for(let t=0;t<f.length;t++){const n=f.key(t);n&&e.push(n)}return e}iterate(e){this.keys().forEach((t,n)=>{const s=this.getItem(t);s!==void 0&&e(s,t,n)})}}const g=j(()=>window.sessionStorage);class X{constructor(){}getItem(e){const t=String(e),n=g.getItem(t);if(n!==null)try{return JSON.parse(n)}catch(s){console.error(`Failed to parse value for key: ${t}`,s);return}}setItem(e,t){const n=String(e);try{g.setItem(n,JSON.stringify(t))}catch(s){throw console.error(`Failed to set value for key: ${n}`,s),s}return t}removeItem(e){const t=String(e);g.removeItem(t)}clear(){g.clear()}length(){return g.length}key(e){return g.key(e)}keys(){const e=[];for(let t=0;t<g.length;t++){const n=g.key(t);n&&e.push(n)}return e}iterate(e){this.keys().forEach((t,n)=>{const s=this.getItem(t);s!==void 0&&e(s,t,n)})}}class Z{constructor(e,t){h(this,"dbName");h(this,"storeName");h(this,"dbPromise",null);h(this,"DB_VERSION",1);this.dbName=e,this.storeName=t,this.initDB()}initDB(){if(typeof window>"u"||!window.indexedDB){console.warn("IndexedDB is not available");return}this.dbPromise=new Promise((e,t)=>{const n=indexedDB.open(this.dbName,this.DB_VERSION);n.onerror=()=>{t(new Error(`Failed to open database: ${this.dbName}`))},n.onsuccess=()=>{e(n.result)},n.onupgradeneeded=s=>{const o=s.target.result;o.objectStoreNames.contains(this.storeName)||o.createObjectStore(this.storeName)}})}async getDB(){if(!this.dbPromise)throw new Error("IndexedDB is not initialized");return this.dbPromise}async withStore(e,t){const n=await this.getDB();return new Promise((s,o)=>{const a=n.transaction([this.storeName],e).objectStore(this.storeName),i=t(a);i.onsuccess=()=>{s(i.result)},i.onerror=()=>{o(i.error)}})}async getItem(e){try{const t=await this.withStore("readonly",n=>n.get(String(e)));return t===void 0?void 0:t}catch(t){console.error(`Failed to get value for key: ${String(e)}`,t);return}}async setItem(e,t){try{return await this.withStore("readwrite",n=>n.put(t,String(e))),t}catch(n){throw console.error(`Failed to set value for key: ${String(e)}`,n),n}}async removeItem(e){try{await this.withStore("readwrite",t=>t.delete(String(e)))}catch(t){throw console.error(`Failed to remove value for key: ${String(e)}`,t),t}}async clear(){try{await this.withStore("readwrite",e=>e.clear())}catch(e){throw console.error("Failed to clear storage",e),e}}async length(){try{return await this.withStore("readonly",e=>e.count())}catch(e){return console.error("Failed to get storage length",e),0}}async key(e){try{return(await this.withStore("readonly",t=>t.getAllKeys()))[e]}catch(t){console.error("Failed to get key",t);return}}async keys(){try{return await this.withStore("readonly",e=>e.getAllKeys())}catch(e){return console.error("Failed to get all keys",e),[]}}async iterate(e){try{const t=(await this.getDB()).transaction([this.storeName],"readonly").objectStore(this.storeName).openCursor();return new Promise((n,s)=>{let o=0;t.onsuccess=a=>{const i=a.target.result;if(i){const u=i.key,l=i.value;e(l,u,o),o++,i.continue()}else n()},t.onerror=()=>{s(t.error)}})}catch(t){console.error("Failed to iterate storage",t)}}}const C={memory:"memory",local:"local",session:"session",indexeddb:"indexeddb"};function R(r,e){Object.assign(r,e)}function H(r){return R(r,{async getItemOrDefault(e,t){const n=await this.getItem(e);return n!==null?n:t},async hasItem(e){return await this.getItem(e)!==null},async removeItems(e){await Promise.all(e.map(t=>this.removeItem(t)))},async getItems(e){return await Promise.all(e.map(t=>this.getItem(t)))}}),r}function L(r){return R(r,{getItemOrDefault(e,t){const n=this.getItem(e);return n!==null?n:t},hasItem(e){return this.getItem(e)!==null},removeItems(e){e.forEach(t=>this.removeItem(t))},getItems(e){return e.map(t=>this.getItem(t))}}),r}function Q(r){const e=r.getItem("");return M(e)?H(r):L(r)}function I(r,...e){const t=G(r);let n;try{n=new t(...e)}catch{n=t(...e)}return Q(n)}p(U,C.memory),p(Y,C.local),p(X,C.session),p(Z,C.indexeddb);function k(r,e){return{value:r,expireAt:Date.now()+e}}function S(r,e=Date.now()){return r.expireAt<=e}function T(r,e=Date.now()){return S(r,e)?void 0:r.value}const W=r=>{const{config:e}=r,{method:t,url:n,data:s}=e;return[t,n,JSON.stringify(s)].join("|")},ee=()=>!0,b=24*60*60*1e3,te={key:W,duration:b,isValid:ee,store:C.memory},re=class{constructor(e){h(this,"store");return typeof e=="string"?this.store=I(e):(p(e.factory,e.key),this.store=I(e.key)),new Proxy(this,{get(t,n){if(n in t)return t[n];const s=t.store[n];return typeof s=="function"?s.bind(t.store):s}})}setCache(e,t,n=b){const s=k(t,n);this.store.setItem(e,s)}async getCache(e){const t=await this.store.getItem(e);if(t)return T(t)}async hasValidCache(e){const t=await this.store.getItem(e);return t?!S(t):!1}},D=r=>{const e={...te,...r},t=new re(e.store),n=o=>typeof e.duration=="function"?e.duration(o):e.duration;return Object.assign(async({config:o,next:a})=>{const i=e.key({config:o}),u=await t.getCache(i);if(u){if(await e.isValid({key:i,config:o,cachedData:u}))return u;t.removeItem(i)}const l=await a(),y=n({key:i,config:o,cachedData:u,response:l}),N=k(l,y);return t.setItem(i,N),l},{__middlewareType:w.CACHE,storage:t})};class ne{constructor(e){h(this,"store");return typeof e=="string"?this.store=I(e):(p(e.factory,e.key),this.store=I(e.key)),new Proxy(this,{get(t,n){if(n in t)return t[n];const s=t.store[n];return typeof s=="function"?s.bind(t.store):s}})}setCache(e,t,n=b){const s=k(t,n);this.store.setItem(e,s)}getCache(e){const t=this.store.getItem(e);if(t)return T(t)}hasValidCache(e){const t=this.store.getItem(e);return t?!S(t):!1}}const se=ne,oe={suspense:!0,key:r=>{const{config:e}=r,{method:t,url:n}=e;return[t,n].join("|")},duration:24*60*60*1e3,isValid:()=>!0,store:C.memory};function ie(r){const e={...oe,...r},t=new se(e.store),n=o=>typeof e.duration=="function"?e.duration(o):e.duration;return Object.assign(({config:o,next:a})=>{const i=e.key({config:o}),u=t.getCache(i);if(u){if(e.isValid({key:i,config:o,cachedData:u}))return u;t.removeItem(i)}if(e.suspense)throw(e.wrapSuspense?e.wrapSuspense({key:i,config:o,p:a()}):a()).then(y=>{const N=n({key:i,config:o,cachedData:u,response:y});return t.setCache(i,y,N),y});return a().then(l=>{const y=n({key:i,config:o,cachedData:u,response:l});return t.setCache(i,l,y),l})},{__middlewareType:w.SYNC})}const F=new Map,ae=(r,e=P)=>{F.set(e,r)},$=(r=P)=>{const e=F.get(r);if(!e)throw new Error(`Requestor实例 ${String(r)} 未注册`);return e},E={get:(r,e)=>({url:r,method:m.GET,...e,params:e==null?void 0:e.params}),post:(r,e,t)=>({url:r,method:m.POST,data:e,...t}),delete:(r,e)=>({url:r,method:m.DELETE,...e}),put:(r,e,t)=>({url:r,method:m.PUT,data:e,...t}),request:r=>r},ce=Object.keys(E);function ue(r,e,t){const n={},s=o=>{if(o===e.length)return t(r);const a=e[o];return a({config:r,ctx:n,next:()=>s(o+1)})};return s(0)}function le(r,e){const t={},n=e??[];return ce.forEach(s=>{t[s]=(...o)=>{const a=E[s](...o);return ue(a,n,r)}}),t}function v(r){const{extensions:e,instanceKey:t}=r??{},n=$(t);return le(n,e)}function de(r){const{instanceKey:e,key:t,duration:n,isValid:s,store:o,...a}=r;return v({instanceKey:e,extensions:[x(a),D({key:t,duration:n,isValid:s,store:o})]})}function he(r){const{instanceKey:e,parallelCount:t,createId:n,retries:s,delay:o,retryCondition:a}=r;return v({instanceKey:e,extensions:[O({parallelCount:t,createId:n}),V({retries:s,delay:o,retryCondition:a})]})}c.cache=D,c.concurrent=O,c.createCachedIdempotentRequestor=de,c.createConcurrentRetryRequestor=he,c.createRequestor=v,c.idempotent=x,c.inject=ae,c.retry=V,c.sync=ie,c.useRequestor=$,Object.defineProperty(c,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@net-vert/core",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Dependency Inversion Network Library with Type-Safe Injection.",
|
|
5
5
|
"main": "dist/index",
|
|
6
6
|
"type": "module",
|
|
@@ -11,9 +11,6 @@
|
|
|
11
11
|
"build-only": "vite build",
|
|
12
12
|
"type-check": "tsc --noEmit"
|
|
13
13
|
},
|
|
14
|
-
"devDependencies": {
|
|
15
|
-
"typescript": "^5.9.3"
|
|
16
|
-
},
|
|
17
14
|
"files": [
|
|
18
15
|
"dist"
|
|
19
16
|
],
|
|
@@ -34,6 +31,6 @@
|
|
|
34
31
|
},
|
|
35
32
|
"dependencies": {
|
|
36
33
|
"id-queue": "^1.1.1",
|
|
37
|
-
"
|
|
34
|
+
"store-vert": "^0.1.2"
|
|
38
35
|
}
|
|
39
36
|
}
|