@net-vert/core 1.3.1 → 1.3.4
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 +89 -1
- package/dist/index.js +186 -121
- package/dist/index.umd.cjs +1 -1
- package/package.json +35 -35
package/dist/index.d.ts
CHANGED
|
@@ -244,7 +244,7 @@ declare enum MIDDLEWARE_TYPE {
|
|
|
244
244
|
RETRY = "retry",
|
|
245
245
|
IDEMPOTENT = "idempotent",
|
|
246
246
|
CONCURRENT = "concurrent",
|
|
247
|
-
|
|
247
|
+
THROTTLE = "throttle"
|
|
248
248
|
}
|
|
249
249
|
|
|
250
250
|
export declare interface MiddlewareContext extends Record<string, any> {
|
|
@@ -308,6 +308,94 @@ declare type TaskItem<T> = {
|
|
|
308
308
|
|
|
309
309
|
declare type TaskItemList<T> = TaskQueue<TaskItem<T>>;
|
|
310
310
|
|
|
311
|
+
/**
|
|
312
|
+
* 节流中间件
|
|
313
|
+
*
|
|
314
|
+
* 确保所有请求按照最小间隔时间排队执行
|
|
315
|
+
*
|
|
316
|
+
* @example
|
|
317
|
+
* ```typescript
|
|
318
|
+
* // 创建节流中间件,每次请求最少间隔 1 秒
|
|
319
|
+
* const throttleMiddleware = throttle({ interval: 1000 })
|
|
320
|
+
*
|
|
321
|
+
* // 带超时控制,超过 5 秒丢弃请求
|
|
322
|
+
* const throttleMiddleware = throttle({
|
|
323
|
+
* interval: 1000,
|
|
324
|
+
* timeout: 5000
|
|
325
|
+
* })
|
|
326
|
+
*
|
|
327
|
+
* // 所有使用该中间件的请求共享同一个节流器
|
|
328
|
+
* const api1 = createRequest(config1, [throttleMiddleware])
|
|
329
|
+
* const api2 = createRequest(config2, [throttleMiddleware])
|
|
330
|
+
*
|
|
331
|
+
* // 组合使用
|
|
332
|
+
* const api3 = createRequest(config3, [
|
|
333
|
+
* cache({ duration: 5000 }), // 优先走缓存
|
|
334
|
+
* idempotent(), // 去重相同请求
|
|
335
|
+
* throttle({ interval: 1000 }), // 限制请求速率
|
|
336
|
+
* retry({ retries: 3 }) // 失败重试
|
|
337
|
+
* ])
|
|
338
|
+
*
|
|
339
|
+
* // 查看节流器状态
|
|
340
|
+
* throttleMiddleware.throttler.getStatus()
|
|
341
|
+
* ```
|
|
342
|
+
*/
|
|
343
|
+
export declare const throttle: <D = any, R = any>(options?: Partial<ThrottleOptions>) => ThrottleMiddleware<D, R>;
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* 节流中间件类型
|
|
347
|
+
*/
|
|
348
|
+
declare type ThrottleMiddleware<D = any, R = any> = Middleware<D, R> & {
|
|
349
|
+
__middlewareType: MIDDLEWARE_TYPE.THROTTLE;
|
|
350
|
+
throttler: Throttler;
|
|
351
|
+
};
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* 节流中间件配置
|
|
355
|
+
*/
|
|
356
|
+
declare interface ThrottleOptions {
|
|
357
|
+
/**
|
|
358
|
+
* 最小请求间隔(毫秒)
|
|
359
|
+
* @default 1000
|
|
360
|
+
*/
|
|
361
|
+
interval: number;
|
|
362
|
+
/**
|
|
363
|
+
* 队列超时时间(毫秒)
|
|
364
|
+
* 如果请求在队列中等待超过此时间,将被拒绝并抛出 ThrottleTimeoutError
|
|
365
|
+
* - 不设置(undefined):永不超时,所有请求都会执行
|
|
366
|
+
* - 设为 0:立即超时,直接拒绝排队的请求
|
|
367
|
+
* - 设为正数:等待指定时间后超时
|
|
368
|
+
* @default undefined
|
|
369
|
+
*/
|
|
370
|
+
timeout?: number;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* 节流器接口
|
|
375
|
+
*/
|
|
376
|
+
declare interface Throttler {
|
|
377
|
+
/** 添加任务到队列 */
|
|
378
|
+
add: <T>(task: () => Promise<T>) => Promise<T>;
|
|
379
|
+
/** 获取节流器状态 */
|
|
380
|
+
getStatus: () => ThrottlerStatus;
|
|
381
|
+
/** 清空队列(慎用) */
|
|
382
|
+
clear: () => void;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* 节流器状态
|
|
387
|
+
*/
|
|
388
|
+
declare interface ThrottlerStatus {
|
|
389
|
+
/** 当前队列长度 */
|
|
390
|
+
queueLength: number;
|
|
391
|
+
/** 上次执行时间戳 */
|
|
392
|
+
lastExecutionTime: number;
|
|
393
|
+
/** 是否正在处理队列 */
|
|
394
|
+
isProcessing: boolean;
|
|
395
|
+
/** 预计下次执行时间 */
|
|
396
|
+
nextExecutionTime: number | null;
|
|
397
|
+
}
|
|
398
|
+
|
|
311
399
|
export declare type TypedMiddleware<Type extends MiddlewareType, D = any, R = any> = Middleware<D, R> & {
|
|
312
400
|
__middlewareType: Type;
|
|
313
401
|
};
|
package/dist/index.js
CHANGED
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
import { TaskQueue as
|
|
2
|
-
var
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
},
|
|
6
|
-
const e =
|
|
1
|
+
import { TaskQueue as j } from "id-queue";
|
|
2
|
+
var f = /* @__PURE__ */ ((r) => (r.GET = "get", r.POST = "post", r.PUT = "put", r.DELETE = "delete", r))(f || {}), g = /* @__PURE__ */ ((r) => (r.CACHE = "cache", r.RETRY = "retry", r.IDEMPOTENT = "idempotent", r.CONCURRENT = "concurrent", r.THROTTLE = "throttle", r))(g || {});
|
|
3
|
+
const k = "default", S = /* @__PURE__ */ new Map(), me = (r, e = k) => {
|
|
4
|
+
S.set(e, r);
|
|
5
|
+
}, q = (r = k) => {
|
|
6
|
+
const e = S.get(r);
|
|
7
7
|
if (!e) throw new Error(`Requestor实例 ${String(r)} 未注册`);
|
|
8
8
|
return e;
|
|
9
|
-
},
|
|
9
|
+
}, v = {
|
|
10
10
|
get: (r, e) => ({
|
|
11
11
|
url: r,
|
|
12
|
-
method:
|
|
12
|
+
method: f.GET,
|
|
13
13
|
...e,
|
|
14
14
|
params: e?.params
|
|
15
15
|
}),
|
|
16
16
|
post: (r, e, t) => ({
|
|
17
17
|
url: r,
|
|
18
|
-
method:
|
|
18
|
+
method: f.POST,
|
|
19
19
|
data: e,
|
|
20
20
|
...t
|
|
21
21
|
}),
|
|
22
22
|
delete: (r, e) => ({
|
|
23
23
|
url: r,
|
|
24
|
-
method:
|
|
24
|
+
method: f.DELETE,
|
|
25
25
|
...e
|
|
26
26
|
}),
|
|
27
27
|
put: (r, e, t) => ({
|
|
28
28
|
url: r,
|
|
29
|
-
method:
|
|
29
|
+
method: f.PUT,
|
|
30
30
|
data: e,
|
|
31
31
|
...t
|
|
32
32
|
}),
|
|
33
33
|
request: (r) => r
|
|
34
|
-
},
|
|
35
|
-
function
|
|
34
|
+
}, F = Object.keys(v);
|
|
35
|
+
function $(r, e, t) {
|
|
36
36
|
const n = {}, s = (o) => {
|
|
37
37
|
if (o === e.length)
|
|
38
38
|
return t(r);
|
|
@@ -45,13 +45,13 @@ function T(r, e, t) {
|
|
|
45
45
|
};
|
|
46
46
|
return s(0);
|
|
47
47
|
}
|
|
48
|
-
function
|
|
48
|
+
function R(r, e) {
|
|
49
49
|
const t = {}, n = e ?? [];
|
|
50
|
-
return
|
|
50
|
+
return F.forEach(
|
|
51
51
|
(s) => {
|
|
52
52
|
t[s] = (...o) => {
|
|
53
|
-
const i =
|
|
54
|
-
return
|
|
53
|
+
const i = v[s](...o);
|
|
54
|
+
return $(
|
|
55
55
|
i,
|
|
56
56
|
n,
|
|
57
57
|
r
|
|
@@ -60,19 +60,19 @@ function V(r, e) {
|
|
|
60
60
|
}
|
|
61
61
|
), t;
|
|
62
62
|
}
|
|
63
|
-
function
|
|
64
|
-
const { extensions: e, instanceKey: t } = r ?? {}, n =
|
|
65
|
-
return
|
|
63
|
+
function b(r) {
|
|
64
|
+
const { extensions: e, instanceKey: t } = r ?? {}, n = q(t);
|
|
65
|
+
return R(
|
|
66
66
|
n,
|
|
67
67
|
e
|
|
68
68
|
);
|
|
69
69
|
}
|
|
70
|
-
const
|
|
70
|
+
const V = {
|
|
71
71
|
retries: 3,
|
|
72
72
|
delay: 0,
|
|
73
73
|
retryCondition: () => !0
|
|
74
|
-
},
|
|
75
|
-
const e = { ...
|
|
74
|
+
}, B = (r) => {
|
|
75
|
+
const e = { ...V, ...r };
|
|
76
76
|
return Object.assign(async ({ config: n, next: s }) => {
|
|
77
77
|
let o = 0, i;
|
|
78
78
|
for (; o <= e.retries; )
|
|
@@ -89,23 +89,23 @@ const q = {
|
|
|
89
89
|
if (!e.retryCondition(c))
|
|
90
90
|
throw a;
|
|
91
91
|
o++;
|
|
92
|
-
const
|
|
93
|
-
|
|
92
|
+
const h = typeof e.delay == "function" ? e.delay(c) : e.delay;
|
|
93
|
+
h > 0 && await new Promise((I) => setTimeout(I, h));
|
|
94
94
|
}
|
|
95
95
|
throw i;
|
|
96
|
-
}, { __middlewareType:
|
|
97
|
-
},
|
|
96
|
+
}, { __middlewareType: g.RETRY });
|
|
97
|
+
}, _ = () => {
|
|
98
98
|
const r = /* @__PURE__ */ new Map();
|
|
99
99
|
return { getPromise: (o) => r.get(o), setPromise: (o, i) => {
|
|
100
100
|
r.set(o, i), i.finally(() => r.delete(o));
|
|
101
101
|
}, delPromise: (o) => r.delete(o), clearCache: () => r.clear() };
|
|
102
|
-
},
|
|
102
|
+
}, K = (r) => {
|
|
103
103
|
const { config: e } = r, { method: t, url: n, data: s } = e;
|
|
104
104
|
return [t, n, JSON.stringify(s)].join("|");
|
|
105
|
-
},
|
|
106
|
-
key:
|
|
107
|
-
},
|
|
108
|
-
const e = { ...
|
|
105
|
+
}, A = {
|
|
106
|
+
key: K
|
|
107
|
+
}, J = (r) => {
|
|
108
|
+
const e = { ...A, ...r }, t = _();
|
|
109
109
|
return Object.assign(({ config: s, next: o }) => {
|
|
110
110
|
const i = e.key({ config: s }), a = t.getPromise(i);
|
|
111
111
|
if (a)
|
|
@@ -113,16 +113,16 @@ const q = {
|
|
|
113
113
|
const c = o();
|
|
114
114
|
return t.setPromise(i, c), c;
|
|
115
115
|
}, {
|
|
116
|
-
__middlewareType:
|
|
116
|
+
__middlewareType: g.IDEMPOTENT,
|
|
117
117
|
promiseCache: t
|
|
118
118
|
});
|
|
119
119
|
};
|
|
120
|
-
class
|
|
120
|
+
class z {
|
|
121
121
|
parallelCount;
|
|
122
122
|
tasks;
|
|
123
123
|
runningCount;
|
|
124
124
|
constructor(e = 4) {
|
|
125
|
-
this.parallelCount = e, this.tasks = new
|
|
125
|
+
this.parallelCount = e, this.tasks = new j(), this.runningCount = 0;
|
|
126
126
|
}
|
|
127
127
|
// 加入
|
|
128
128
|
add(e, t) {
|
|
@@ -151,26 +151,26 @@ class _ {
|
|
|
151
151
|
}
|
|
152
152
|
}
|
|
153
153
|
}
|
|
154
|
-
let
|
|
155
|
-
const
|
|
154
|
+
let G = 0;
|
|
155
|
+
const H = {
|
|
156
156
|
parallelCount: 4,
|
|
157
|
-
createId: () =>
|
|
158
|
-
},
|
|
159
|
-
const { parallelCount: e, createId: t } = { ...
|
|
157
|
+
createId: () => G++
|
|
158
|
+
}, L = (r) => {
|
|
159
|
+
const { parallelCount: e, createId: t } = { ...H, ...r }, n = new z(e);
|
|
160
160
|
return Object.assign(({ config: o, next: i }) => {
|
|
161
161
|
const a = t({ config: o });
|
|
162
162
|
return n.add(a, () => i());
|
|
163
|
-
}, { __middlewareType:
|
|
164
|
-
},
|
|
165
|
-
|
|
163
|
+
}, { __middlewareType: g.CONCURRENT, pool: n });
|
|
164
|
+
}, T = /* @__PURE__ */ new Map(), w = (r, e) => {
|
|
165
|
+
T.set(e, r);
|
|
166
166
|
};
|
|
167
|
-
function
|
|
168
|
-
const e =
|
|
167
|
+
function U(r) {
|
|
168
|
+
const e = T.get(r);
|
|
169
169
|
if (!e)
|
|
170
170
|
throw new Error(`Store实例 ${String(r)} 未注册`);
|
|
171
171
|
return e;
|
|
172
172
|
}
|
|
173
|
-
class
|
|
173
|
+
class M {
|
|
174
174
|
store = /* @__PURE__ */ new Map();
|
|
175
175
|
getItem(e) {
|
|
176
176
|
return this.store.get(e);
|
|
@@ -199,7 +199,7 @@ class U {
|
|
|
199
199
|
e(s, n, t), t++;
|
|
200
200
|
}
|
|
201
201
|
}
|
|
202
|
-
const
|
|
202
|
+
const X = (r) => !!r && (typeof r == "object" || typeof r == "function") && typeof r.then == "function", x = (r) => typeof window < "u" ? r() : {
|
|
203
203
|
getItem() {
|
|
204
204
|
return null;
|
|
205
205
|
},
|
|
@@ -215,12 +215,12 @@ const M = (r) => !!r && (typeof r == "object" || typeof r == "function") && type
|
|
|
215
215
|
get length() {
|
|
216
216
|
return 0;
|
|
217
217
|
}
|
|
218
|
-
},
|
|
219
|
-
class
|
|
218
|
+
}, m = x(() => window.localStorage);
|
|
219
|
+
class Z {
|
|
220
220
|
constructor() {
|
|
221
221
|
}
|
|
222
222
|
getItem(e) {
|
|
223
|
-
const t = String(e), n =
|
|
223
|
+
const t = String(e), n = m.getItem(t);
|
|
224
224
|
if (n !== null)
|
|
225
225
|
try {
|
|
226
226
|
return JSON.parse(n);
|
|
@@ -232,7 +232,7 @@ class X {
|
|
|
232
232
|
setItem(e, t) {
|
|
233
233
|
const n = String(e);
|
|
234
234
|
try {
|
|
235
|
-
|
|
235
|
+
m.setItem(n, JSON.stringify(t));
|
|
236
236
|
} catch (s) {
|
|
237
237
|
throw console.error(`Failed to set value for key: ${n}`, s), s;
|
|
238
238
|
}
|
|
@@ -240,21 +240,21 @@ class X {
|
|
|
240
240
|
}
|
|
241
241
|
removeItem(e) {
|
|
242
242
|
const t = String(e);
|
|
243
|
-
|
|
243
|
+
m.removeItem(t);
|
|
244
244
|
}
|
|
245
245
|
clear() {
|
|
246
|
-
|
|
246
|
+
m.clear();
|
|
247
247
|
}
|
|
248
248
|
length() {
|
|
249
|
-
return
|
|
249
|
+
return m.length;
|
|
250
250
|
}
|
|
251
251
|
key(e) {
|
|
252
|
-
return
|
|
252
|
+
return m.key(e);
|
|
253
253
|
}
|
|
254
254
|
keys() {
|
|
255
255
|
const e = [];
|
|
256
|
-
for (let t = 0; t <
|
|
257
|
-
const n =
|
|
256
|
+
for (let t = 0; t < m.length; t++) {
|
|
257
|
+
const n = m.key(t);
|
|
258
258
|
n && e.push(n);
|
|
259
259
|
}
|
|
260
260
|
return e;
|
|
@@ -266,12 +266,12 @@ class X {
|
|
|
266
266
|
});
|
|
267
267
|
}
|
|
268
268
|
}
|
|
269
|
-
const
|
|
270
|
-
class
|
|
269
|
+
const y = x(() => window.sessionStorage);
|
|
270
|
+
class Q {
|
|
271
271
|
constructor() {
|
|
272
272
|
}
|
|
273
273
|
getItem(e) {
|
|
274
|
-
const t = String(e), n =
|
|
274
|
+
const t = String(e), n = y.getItem(t);
|
|
275
275
|
if (n !== null)
|
|
276
276
|
try {
|
|
277
277
|
return JSON.parse(n);
|
|
@@ -283,7 +283,7 @@ class Y {
|
|
|
283
283
|
setItem(e, t) {
|
|
284
284
|
const n = String(e);
|
|
285
285
|
try {
|
|
286
|
-
|
|
286
|
+
y.setItem(n, JSON.stringify(t));
|
|
287
287
|
} catch (s) {
|
|
288
288
|
throw console.error(`Failed to set value for key: ${n}`, s), s;
|
|
289
289
|
}
|
|
@@ -291,21 +291,21 @@ class Y {
|
|
|
291
291
|
}
|
|
292
292
|
removeItem(e) {
|
|
293
293
|
const t = String(e);
|
|
294
|
-
|
|
294
|
+
y.removeItem(t);
|
|
295
295
|
}
|
|
296
296
|
clear() {
|
|
297
|
-
|
|
297
|
+
y.clear();
|
|
298
298
|
}
|
|
299
299
|
length() {
|
|
300
|
-
return
|
|
300
|
+
return y.length;
|
|
301
301
|
}
|
|
302
302
|
key(e) {
|
|
303
|
-
return
|
|
303
|
+
return y.key(e);
|
|
304
304
|
}
|
|
305
305
|
keys() {
|
|
306
306
|
const e = [];
|
|
307
|
-
for (let t = 0; t <
|
|
308
|
-
const n =
|
|
307
|
+
for (let t = 0; t < y.length; t++) {
|
|
308
|
+
const n = y.key(t);
|
|
309
309
|
n && e.push(n);
|
|
310
310
|
}
|
|
311
311
|
return e;
|
|
@@ -317,7 +317,7 @@ class Y {
|
|
|
317
317
|
});
|
|
318
318
|
}
|
|
319
319
|
}
|
|
320
|
-
class
|
|
320
|
+
class Y {
|
|
321
321
|
dbName;
|
|
322
322
|
storeName;
|
|
323
323
|
dbPromise = null;
|
|
@@ -427,8 +427,8 @@ class Z {
|
|
|
427
427
|
t.onsuccess = (i) => {
|
|
428
428
|
const a = i.target.result;
|
|
429
429
|
if (a) {
|
|
430
|
-
const c = a.key,
|
|
431
|
-
e(
|
|
430
|
+
const c = a.key, h = a.value;
|
|
431
|
+
e(h, c, o), o++, a.continue();
|
|
432
432
|
} else
|
|
433
433
|
n();
|
|
434
434
|
}, t.onerror = () => {
|
|
@@ -440,17 +440,17 @@ class Z {
|
|
|
440
440
|
}
|
|
441
441
|
}
|
|
442
442
|
}
|
|
443
|
-
const
|
|
443
|
+
const p = {
|
|
444
444
|
memory: "memory",
|
|
445
445
|
local: "local",
|
|
446
446
|
session: "session",
|
|
447
447
|
indexeddb: "indexeddb"
|
|
448
448
|
};
|
|
449
|
-
function
|
|
449
|
+
function P(r, e) {
|
|
450
450
|
Object.assign(r, e);
|
|
451
451
|
}
|
|
452
|
-
function
|
|
453
|
-
return
|
|
452
|
+
function W(r) {
|
|
453
|
+
return P(r, {
|
|
454
454
|
async getItemOrDefault(e, t) {
|
|
455
455
|
const n = await this.getItem(e);
|
|
456
456
|
return n !== null ? n : t;
|
|
@@ -466,8 +466,8 @@ function H(r) {
|
|
|
466
466
|
}
|
|
467
467
|
}), r;
|
|
468
468
|
}
|
|
469
|
-
function
|
|
470
|
-
return
|
|
469
|
+
function ee(r) {
|
|
470
|
+
return P(r, {
|
|
471
471
|
getItemOrDefault(e, t) {
|
|
472
472
|
const n = this.getItem(e);
|
|
473
473
|
return n !== null ? n : t;
|
|
@@ -483,50 +483,50 @@ function L(r) {
|
|
|
483
483
|
}
|
|
484
484
|
}), r;
|
|
485
485
|
}
|
|
486
|
-
function
|
|
486
|
+
function te(r) {
|
|
487
487
|
const e = r.getItem("");
|
|
488
|
-
return
|
|
488
|
+
return X(e) ? W(r) : ee(r);
|
|
489
489
|
}
|
|
490
|
-
function
|
|
491
|
-
const t =
|
|
490
|
+
function C(r, ...e) {
|
|
491
|
+
const t = U(r);
|
|
492
492
|
let n;
|
|
493
493
|
try {
|
|
494
494
|
n = new t(...e);
|
|
495
495
|
} catch {
|
|
496
496
|
n = t(...e);
|
|
497
497
|
}
|
|
498
|
-
return
|
|
498
|
+
return te(n);
|
|
499
499
|
}
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
500
|
+
w(M, p.memory);
|
|
501
|
+
w(Z, p.local);
|
|
502
|
+
w(Q, p.session);
|
|
503
|
+
w(Y, p.indexeddb);
|
|
504
504
|
function N(r, e) {
|
|
505
505
|
return {
|
|
506
506
|
value: r,
|
|
507
507
|
expireAt: Date.now() + e
|
|
508
508
|
};
|
|
509
509
|
}
|
|
510
|
-
function
|
|
510
|
+
function O(r, e = Date.now()) {
|
|
511
511
|
return r.expireAt <= e;
|
|
512
512
|
}
|
|
513
|
-
function
|
|
514
|
-
return
|
|
513
|
+
function re(r, e = Date.now()) {
|
|
514
|
+
return O(r, e) ? void 0 : r.value;
|
|
515
515
|
}
|
|
516
|
-
const
|
|
516
|
+
const ne = (r) => {
|
|
517
517
|
const { config: e } = r, { method: t, url: n, data: s } = e;
|
|
518
518
|
return [t, n, JSON.stringify(s)].join("|");
|
|
519
|
-
},
|
|
520
|
-
key:
|
|
521
|
-
duration:
|
|
522
|
-
isValid:
|
|
523
|
-
store:
|
|
519
|
+
}, se = () => !0, E = 24 * 60 * 60 * 1e3, oe = {
|
|
520
|
+
key: ne,
|
|
521
|
+
duration: E,
|
|
522
|
+
isValid: se,
|
|
523
|
+
store: p.memory
|
|
524
524
|
// 默认不持久化,使用内存存储
|
|
525
525
|
};
|
|
526
|
-
class
|
|
526
|
+
class ae {
|
|
527
527
|
store;
|
|
528
528
|
constructor(e) {
|
|
529
|
-
return typeof e == "string" ? this.store =
|
|
529
|
+
return typeof e == "string" ? this.store = C(e) : (w(e.factory, e.key), this.store = C(e.key)), new Proxy(this, {
|
|
530
530
|
get(t, n) {
|
|
531
531
|
if (n in t)
|
|
532
532
|
return t[n];
|
|
@@ -541,7 +541,7 @@ class ne {
|
|
|
541
541
|
* @param value 要缓存的值
|
|
542
542
|
* @param duration 过期时长(毫秒),默认 24 小时
|
|
543
543
|
*/
|
|
544
|
-
setCache(e, t, n =
|
|
544
|
+
setCache(e, t, n = E) {
|
|
545
545
|
const s = N(t, n);
|
|
546
546
|
this.store.setItem(e, s);
|
|
547
547
|
}
|
|
@@ -553,7 +553,7 @@ class ne {
|
|
|
553
553
|
async getCache(e) {
|
|
554
554
|
const t = await this.store.getItem(e);
|
|
555
555
|
if (t)
|
|
556
|
-
return
|
|
556
|
+
return re(t);
|
|
557
557
|
}
|
|
558
558
|
/**
|
|
559
559
|
* 检查是否有有效的缓存(未过期)
|
|
@@ -562,11 +562,11 @@ class ne {
|
|
|
562
562
|
*/
|
|
563
563
|
async hasValidCache(e) {
|
|
564
564
|
const t = await this.store.getItem(e);
|
|
565
|
-
return t ? !
|
|
565
|
+
return t ? !O(t) : !1;
|
|
566
566
|
}
|
|
567
567
|
}
|
|
568
|
-
const
|
|
569
|
-
const e = { ...
|
|
568
|
+
const ie = ae, ce = (r) => {
|
|
569
|
+
const e = { ...oe, ...r }, t = new ie(e.store), n = (o) => typeof e.duration == "function" ? e.duration(o) : e.duration;
|
|
570
570
|
return Object.assign(async ({ config: o, next: i }) => {
|
|
571
571
|
const a = e.key({ config: o }), c = await t.getCache(a);
|
|
572
572
|
if (c) {
|
|
@@ -577,41 +577,106 @@ const se = ne, oe = (r) => {
|
|
|
577
577
|
})) return c;
|
|
578
578
|
t.removeItem(a);
|
|
579
579
|
}
|
|
580
|
-
const
|
|
581
|
-
return t.setItem(a,
|
|
580
|
+
const h = await i(), I = n({ key: a, config: o, cachedData: c, response: h }), u = N(h, I);
|
|
581
|
+
return t.setItem(a, u), h;
|
|
582
582
|
}, {
|
|
583
|
-
__middlewareType:
|
|
583
|
+
__middlewareType: g.CACHE,
|
|
584
584
|
storage: t
|
|
585
585
|
});
|
|
586
586
|
};
|
|
587
|
-
|
|
587
|
+
class ue extends Error {
|
|
588
|
+
constructor(e, t) {
|
|
589
|
+
super(`请求在节流队列中等待超时:配置超时 ${e}ms,实际等待 ${t}ms`), this.name = "ThrottleTimeoutError";
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
function le(r, e) {
|
|
593
|
+
const t = [];
|
|
594
|
+
let n = 0, s = !1;
|
|
595
|
+
const o = (u) => new Promise((l) => setTimeout(l, u)), i = () => {
|
|
596
|
+
if (e === void 0 || t.length === 0)
|
|
597
|
+
return !1;
|
|
598
|
+
const u = Date.now(), l = t[0], d = u - l.timestamp;
|
|
599
|
+
return d >= e ? (t.shift(), l.reject(new ue(e, d)), !0) : !1;
|
|
600
|
+
}, a = async () => {
|
|
601
|
+
if (!(s || t.length === 0)) {
|
|
602
|
+
for (s = !0; t.length > 0; ) {
|
|
603
|
+
if (e !== void 0 && i())
|
|
604
|
+
continue;
|
|
605
|
+
if (t.length === 0)
|
|
606
|
+
break;
|
|
607
|
+
const l = Date.now() - n;
|
|
608
|
+
if (n > 0 && l < r) {
|
|
609
|
+
const D = r - l;
|
|
610
|
+
await o(D);
|
|
611
|
+
}
|
|
612
|
+
const d = t.shift();
|
|
613
|
+
d && (n = Date.now(), d.task().then(d.resolve).catch(d.reject));
|
|
614
|
+
}
|
|
615
|
+
s = !1;
|
|
616
|
+
}
|
|
617
|
+
};
|
|
618
|
+
return {
|
|
619
|
+
add: (u) => new Promise((l, d) => {
|
|
620
|
+
t.push({
|
|
621
|
+
task: u,
|
|
622
|
+
resolve: l,
|
|
623
|
+
reject: d,
|
|
624
|
+
timestamp: Date.now()
|
|
625
|
+
}), a();
|
|
626
|
+
}),
|
|
627
|
+
getStatus: () => {
|
|
628
|
+
const u = t.length > 0 ? n + r : null;
|
|
629
|
+
return {
|
|
630
|
+
queueLength: t.length,
|
|
631
|
+
lastExecutionTime: n,
|
|
632
|
+
isProcessing: s,
|
|
633
|
+
nextExecutionTime: u
|
|
634
|
+
};
|
|
635
|
+
},
|
|
636
|
+
clear: () => {
|
|
637
|
+
t.length = 0;
|
|
638
|
+
}
|
|
639
|
+
};
|
|
640
|
+
}
|
|
641
|
+
const he = {
|
|
642
|
+
interval: 1e3
|
|
643
|
+
// timeout 默认 undefined,永不超时
|
|
644
|
+
}, ye = (r) => {
|
|
645
|
+
const e = { ...he, ...r }, t = le(e.interval, e.timeout);
|
|
646
|
+
return Object.assign(async ({ config: s, next: o }) => t.add(o), {
|
|
647
|
+
__middlewareType: g.THROTTLE,
|
|
648
|
+
throttler: t
|
|
649
|
+
});
|
|
650
|
+
};
|
|
651
|
+
function ge(r) {
|
|
588
652
|
const { instanceKey: e, key: t, duration: n, isValid: s, store: o, ...i } = r;
|
|
589
|
-
return
|
|
653
|
+
return b({
|
|
590
654
|
instanceKey: e,
|
|
591
655
|
extensions: [
|
|
592
|
-
|
|
593
|
-
|
|
656
|
+
J(i),
|
|
657
|
+
ce({ key: t, duration: n, isValid: s, store: o })
|
|
594
658
|
]
|
|
595
659
|
});
|
|
596
660
|
}
|
|
597
|
-
function
|
|
661
|
+
function fe(r) {
|
|
598
662
|
const { instanceKey: e, parallelCount: t, createId: n, retries: s, delay: o, retryCondition: i } = r;
|
|
599
|
-
return
|
|
663
|
+
return b({
|
|
600
664
|
instanceKey: e,
|
|
601
665
|
extensions: [
|
|
602
|
-
|
|
603
|
-
|
|
666
|
+
L({ parallelCount: t, createId: n }),
|
|
667
|
+
B({ retries: s, delay: o, retryCondition: i })
|
|
604
668
|
]
|
|
605
669
|
});
|
|
606
670
|
}
|
|
607
671
|
export {
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
672
|
+
ce as cache,
|
|
673
|
+
L as concurrent,
|
|
674
|
+
ge as createCachedIdempotentRequestor,
|
|
675
|
+
fe as createConcurrentRetryRequestor,
|
|
676
|
+
b as createRequestor,
|
|
677
|
+
J as idempotent,
|
|
678
|
+
me as inject,
|
|
679
|
+
B as retry,
|
|
680
|
+
ye as throttle,
|
|
681
|
+
q as useRequestor
|
|
617
682
|
};
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(c,m){typeof exports=="object"&&typeof module<"u"?m(exports,require("id-queue")):typeof define=="function"&&define.amd?define(["exports","id-queue"],m):(c=typeof globalThis<"u"?globalThis:c||self,m(c.netVertCore={},c.idQueue))})(this,function(c,m){"use strict";var y=(r=>(r.GET="get",r.POST="post",r.PUT="put",r.DELETE="delete",r))(y||{}),g=(r=>(r.CACHE="cache",r.RETRY="retry",r.IDEMPOTENT="idempotent",r.CONCURRENT="concurrent",r.SYNC="sync",r))(g||{});const C="default",k=new Map,V=(r,e=C)=>{k.set(e,r)},S=(r=C)=>{const e=k.get(r);if(!e)throw new Error(`Requestor实例 ${String(r)} 未注册`);return e},v={get:(r,e)=>({url:r,method:y.GET,...e,params:e?.params}),post:(r,e,t)=>({url:r,method:y.POST,data:e,...t}),delete:(r,e)=>({url:r,method:y.DELETE,...e}),put:(r,e,t)=>({url:r,method:y.PUT,data:e,...t}),request:r=>r},E=Object.keys(v);function $(r,e,t){const n={},o=s=>{if(s===e.length)return t(r);const a=e[s];return a({config:r,ctx:n,next:()=>o(s+1)})};return o(0)}function B(r,e){const t={},n=e??[];return E.forEach(o=>{t[o]=(...s)=>{const a=v[o](...s);return $(a,n,r)}}),t}function p(r){const{extensions:e,instanceKey:t}=r??{},n=S(t);return B(n,e)}const K={retries:3,delay:0,retryCondition:()=>!0},b=r=>{const e={...K,...r};return Object.assign(async({config:n,next:o})=>{let s=0,a;for(;s<=e.retries;)try{return await o()}catch(i){if(a=i,s===e.retries)throw i;const u={config:n,lastResponse:i,attempt:s};if(!e.retryCondition(u))throw i;s++;const h=typeof e.delay=="function"?e.delay(u):e.delay;h>0&&await new Promise(I=>setTimeout(I,h))}throw a},{__middlewareType:g.RETRY})},_=()=>{const r=new Map;return{getPromise:s=>r.get(s),setPromise:(s,a)=>{r.set(s,a),a.finally(()=>r.delete(s))},delPromise:s=>r.delete(s),clearCache:()=>r.clear()}},A={key:r=>{const{config:e}=r,{method:t,url:n,data:o}=e;return[t,n,JSON.stringify(o)].join("|")}},N=r=>{const e={...A,...r},t=_();return Object.assign(({config:o,next:s})=>{const a=e.key({config:o}),i=t.getPromise(a);if(i)return i;const u=s();return t.setPromise(a,u),u},{__middlewareType:g.IDEMPOTENT,promiseCache:t})};class J{parallelCount;tasks;runningCount;constructor(e=4){this.parallelCount=e,this.tasks=new m.TaskQueue,this.runningCount=0}add(e,t){return new Promise((n,o)=>{this.tasks.enqueue(e,{task:t,resolve:n,reject:o}),this._run()})}remove(e){this.tasks.remove(e)}execute(e){const{task:t,resolve:n,reject:o}=e;return t().then(n).catch(o).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 z=0;const G={parallelCount:4,createId:()=>z++},P=r=>{const{parallelCount:e,createId:t}={...G,...r},n=new J(e);return Object.assign(({config:s,next:a})=>{const i=t({config:s});return n.add(i,()=>a())},{__middlewareType:g.CONCURRENT,pool:n})},q=new Map,f=(r,e)=>{q.set(e,r)};function U(r){const e=q.get(r);if(!e)throw new Error(`Store实例 ${String(r)} 未注册`);return e}class M{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,o]of this.store.entries())e(o,n,t),t++}}const X=r=>!!r&&(typeof r=="object"||typeof r=="function")&&typeof r.then=="function",O=r=>typeof window<"u"?r():{getItem(){return null},setItem(){},removeItem(){},clear(){},key(){return null},get length(){return 0}},l=O(()=>window.localStorage);class Y{constructor(){}getItem(e){const t=String(e),n=l.getItem(t);if(n!==null)try{return JSON.parse(n)}catch(o){console.error(`Failed to parse value for key: ${t}`,o);return}}setItem(e,t){const n=String(e);try{l.setItem(n,JSON.stringify(t))}catch(o){throw console.error(`Failed to set value for key: ${n}`,o),o}return t}removeItem(e){const t=String(e);l.removeItem(t)}clear(){l.clear()}length(){return l.length}key(e){return l.key(e)}keys(){const e=[];for(let t=0;t<l.length;t++){const n=l.key(t);n&&e.push(n)}return e}iterate(e){this.keys().forEach((t,n)=>{const o=this.getItem(t);o!==void 0&&e(o,t,n)})}}const d=O(()=>window.sessionStorage);class Z{constructor(){}getItem(e){const t=String(e),n=d.getItem(t);if(n!==null)try{return JSON.parse(n)}catch(o){console.error(`Failed to parse value for key: ${t}`,o);return}}setItem(e,t){const n=String(e);try{d.setItem(n,JSON.stringify(t))}catch(o){throw console.error(`Failed to set value for key: ${n}`,o),o}return t}removeItem(e){const t=String(e);d.removeItem(t)}clear(){d.clear()}length(){return d.length}key(e){return d.key(e)}keys(){const e=[];for(let t=0;t<d.length;t++){const n=d.key(t);n&&e.push(n)}return e}iterate(e){this.keys().forEach((t,n)=>{const o=this.getItem(t);o!==void 0&&e(o,t,n)})}}class H{dbName;storeName;dbPromise=null;DB_VERSION=1;constructor(e,t){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=o=>{const s=o.target.result;s.objectStoreNames.contains(this.storeName)||s.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((o,s)=>{const a=n.transaction([this.storeName],e).objectStore(this.storeName),i=t(a);i.onsuccess=()=>{o(i.result)},i.onerror=()=>{s(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,o)=>{let s=0;t.onsuccess=a=>{const i=a.target.result;if(i){const u=i.key,h=i.value;e(h,u,s),s++,i.continue()}else n()},t.onerror=()=>{o(t.error)}})}catch(t){console.error("Failed to iterate storage",t)}}}const w={memory:"memory",local:"local",session:"session",indexeddb:"indexeddb"};function R(r,e){Object.assign(r,e)}function L(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 Q(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 W(r){const e=r.getItem("");return X(e)?L(r):Q(r)}function j(r,...e){const t=U(r);let n;try{n=new t(...e)}catch{n=t(...e)}return W(n)}f(M,w.memory),f(Y,w.local),f(Z,w.session),f(H,w.indexeddb);function x(r,e){return{value:r,expireAt:Date.now()+e}}function T(r,e=Date.now()){return r.expireAt<=e}function ee(r,e=Date.now()){return T(r,e)?void 0:r.value}const te=r=>{const{config:e}=r,{method:t,url:n,data:o}=e;return[t,n,JSON.stringify(o)].join("|")},re=()=>!0,F=24*60*60*1e3,ne={key:te,duration:F,isValid:re,store:w.memory};class oe{store;constructor(e){return typeof e=="string"?this.store=j(e):(f(e.factory,e.key),this.store=j(e.key)),new Proxy(this,{get(t,n){if(n in t)return t[n];const o=t.store[n];return typeof o=="function"?o.bind(t.store):o}})}setCache(e,t,n=F){const o=x(t,n);this.store.setItem(e,o)}async getCache(e){const t=await this.store.getItem(e);if(t)return ee(t)}async hasValidCache(e){const t=await this.store.getItem(e);return t?!T(t):!1}}const se=oe,D=r=>{const e={...ne,...r},t=new se(e.store),n=s=>typeof e.duration=="function"?e.duration(s):e.duration;return Object.assign(async({config:s,next:a})=>{const i=e.key({config:s}),u=await t.getCache(i);if(u){if(await e.isValid({key:i,config:s,cachedData:u}))return u;t.removeItem(i)}const h=await a(),I=n({key:i,config:s,cachedData:u,response:h}),ce=x(h,I);return t.setItem(i,ce),h},{__middlewareType:g.CACHE,storage:t})};function ie(r){const{instanceKey:e,key:t,duration:n,isValid:o,store:s,...a}=r;return p({instanceKey:e,extensions:[N(a),D({key:t,duration:n,isValid:o,store:s})]})}function ae(r){const{instanceKey:e,parallelCount:t,createId:n,retries:o,delay:s,retryCondition:a}=r;return p({instanceKey:e,extensions:[P({parallelCount:t,createId:n}),b({retries:o,delay:s,retryCondition:a})]})}c.cache=D,c.concurrent=P,c.createCachedIdempotentRequestor=ie,c.createConcurrentRetryRequestor=ae,c.createRequestor=p,c.idempotent=N,c.inject=V,c.retry=b,c.useRequestor=S,Object.defineProperty(c,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
(function(c,w){typeof exports=="object"&&typeof module<"u"?w(exports,require("id-queue")):typeof define=="function"&&define.amd?define(["exports","id-queue"],w):(c=typeof globalThis<"u"?globalThis:c||self,w(c.netVertCore={},c.idQueue))})(this,function(c,w){"use strict";var p=(r=>(r.GET="get",r.POST="post",r.PUT="put",r.DELETE="delete",r))(p||{}),f=(r=>(r.CACHE="cache",r.RETRY="retry",r.IDEMPOTENT="idempotent",r.CONCURRENT="concurrent",r.THROTTLE="throttle",r))(f||{});const v="default",b=new Map,B=(r,e=v)=>{b.set(e,r)},T=(r=v)=>{const e=b.get(r);if(!e)throw new Error(`Requestor实例 ${String(r)} 未注册`);return e},P={get:(r,e)=>({url:r,method:p.GET,...e,params: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},_=Object.keys(P);function K(r,e,t){const n={},o=s=>{if(s===e.length)return t(r);const a=e[s];return a({config:r,ctx:n,next:()=>o(s+1)})};return o(0)}function A(r,e){const t={},n=e??[];return _.forEach(o=>{t[o]=(...s)=>{const a=P[o](...s);return K(a,n,r)}}),t}function S(r){const{extensions:e,instanceKey:t}=r??{},n=T(t);return A(n,e)}const J={retries:3,delay:0,retryCondition:()=>!0},N=r=>{const e={...J,...r};return Object.assign(async({config:n,next:o})=>{let s=0,a;for(;s<=e.retries;)try{return await o()}catch(i){if(a=i,s===e.retries)throw i;const u={config:n,lastResponse:i,attempt:s};if(!e.retryCondition(u))throw i;s++;const h=typeof e.delay=="function"?e.delay(u):e.delay;h>0&&await new Promise(k=>setTimeout(k,h))}throw a},{__middlewareType:f.RETRY})},z=()=>{const r=new Map;return{getPromise:s=>r.get(s),setPromise:(s,a)=>{r.set(s,a),a.finally(()=>r.delete(s))},delPromise:s=>r.delete(s),clearCache:()=>r.clear()}},G={key:r=>{const{config:e}=r,{method:t,url:n,data:o}=e;return[t,n,JSON.stringify(o)].join("|")}},q=r=>{const e={...G,...r},t=z();return Object.assign(({config:o,next:s})=>{const a=e.key({config:o}),i=t.getPromise(a);if(i)return i;const u=s();return t.setPromise(a,u),u},{__middlewareType:f.IDEMPOTENT,promiseCache:t})};class H{parallelCount;tasks;runningCount;constructor(e=4){this.parallelCount=e,this.tasks=new w.TaskQueue,this.runningCount=0}add(e,t){return new Promise((n,o)=>{this.tasks.enqueue(e,{task:t,resolve:n,reject:o}),this._run()})}remove(e){this.tasks.remove(e)}execute(e){const{task:t,resolve:n,reject:o}=e;return t().then(n).catch(o).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 L=0;const U={parallelCount:4,createId:()=>L++},O=r=>{const{parallelCount:e,createId:t}={...U,...r},n=new H(e);return Object.assign(({config:s,next:a})=>{const i=t({config:s});return n.add(i,()=>a())},{__middlewareType:f.CONCURRENT,pool:n})},j=new Map,C=(r,e)=>{j.set(e,r)};function M(r){const e=j.get(r);if(!e)throw new Error(`Store实例 ${String(r)} 未注册`);return e}class Q{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,o]of this.store.entries())e(o,n,t),t++}}const X=r=>!!r&&(typeof r=="object"||typeof r=="function")&&typeof r.then=="function",x=r=>typeof window<"u"?r():{getItem(){return null},setItem(){},removeItem(){},clear(){},key(){return null},get length(){return 0}},y=x(()=>window.localStorage);class Z{constructor(){}getItem(e){const t=String(e),n=y.getItem(t);if(n!==null)try{return JSON.parse(n)}catch(o){console.error(`Failed to parse value for key: ${t}`,o);return}}setItem(e,t){const n=String(e);try{y.setItem(n,JSON.stringify(t))}catch(o){throw console.error(`Failed to set value for key: ${n}`,o),o}return t}removeItem(e){const t=String(e);y.removeItem(t)}clear(){y.clear()}length(){return y.length}key(e){return y.key(e)}keys(){const e=[];for(let t=0;t<y.length;t++){const n=y.key(t);n&&e.push(n)}return e}iterate(e){this.keys().forEach((t,n)=>{const o=this.getItem(t);o!==void 0&&e(o,t,n)})}}const g=x(()=>window.sessionStorage);class Y{constructor(){}getItem(e){const t=String(e),n=g.getItem(t);if(n!==null)try{return JSON.parse(n)}catch(o){console.error(`Failed to parse value for key: ${t}`,o);return}}setItem(e,t){const n=String(e);try{g.setItem(n,JSON.stringify(t))}catch(o){throw console.error(`Failed to set value for key: ${n}`,o),o}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 o=this.getItem(t);o!==void 0&&e(o,t,n)})}}class W{dbName;storeName;dbPromise=null;DB_VERSION=1;constructor(e,t){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=o=>{const s=o.target.result;s.objectStoreNames.contains(this.storeName)||s.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((o,s)=>{const a=n.transaction([this.storeName],e).objectStore(this.storeName),i=t(a);i.onsuccess=()=>{o(i.result)},i.onerror=()=>{s(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,o)=>{let s=0;t.onsuccess=a=>{const i=a.target.result;if(i){const u=i.key,h=i.value;e(h,u,s),s++,i.continue()}else n()},t.onerror=()=>{o(t.error)}})}catch(t){console.error("Failed to iterate storage",t)}}}const I={memory:"memory",local:"local",session:"session",indexeddb:"indexeddb"};function R(r,e){Object.assign(r,e)}function ee(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 te(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 re(r){const e=r.getItem("");return X(e)?ee(r):te(r)}function E(r,...e){const t=M(r);let n;try{n=new t(...e)}catch{n=t(...e)}return re(n)}C(Q,I.memory),C(Z,I.local),C(Y,I.session),C(W,I.indexeddb);function D(r,e){return{value:r,expireAt:Date.now()+e}}function F(r,e=Date.now()){return r.expireAt<=e}function ne(r,e=Date.now()){return F(r,e)?void 0:r.value}const oe=r=>{const{config:e}=r,{method:t,url:n,data:o}=e;return[t,n,JSON.stringify(o)].join("|")},se=()=>!0,$=24*60*60*1e3,ie={key:oe,duration:$,isValid:se,store:I.memory};class ae{store;constructor(e){return typeof e=="string"?this.store=E(e):(C(e.factory,e.key),this.store=E(e.key)),new Proxy(this,{get(t,n){if(n in t)return t[n];const o=t.store[n];return typeof o=="function"?o.bind(t.store):o}})}setCache(e,t,n=$){const o=D(t,n);this.store.setItem(e,o)}async getCache(e){const t=await this.store.getItem(e);if(t)return ne(t)}async hasValidCache(e){const t=await this.store.getItem(e);return t?!F(t):!1}}const ce=ae,V=r=>{const e={...ie,...r},t=new ce(e.store),n=s=>typeof e.duration=="function"?e.duration(s):e.duration;return Object.assign(async({config:s,next:a})=>{const i=e.key({config:s}),u=await t.getCache(i);if(u){if(await e.isValid({key:i,config:s,cachedData:u}))return u;t.removeItem(i)}const h=await a(),k=n({key:i,config:s,cachedData:u,response:h}),l=D(h,k);return t.setItem(i,l),h},{__middlewareType:f.CACHE,storage:t})};class ue extends Error{constructor(e,t){super(`请求在节流队列中等待超时:配置超时 ${e}ms,实际等待 ${t}ms`),this.name="ThrottleTimeoutError"}}function le(r,e){const t=[];let n=0,o=!1;const s=l=>new Promise(d=>setTimeout(d,l)),a=()=>{if(e===void 0||t.length===0)return!1;const l=Date.now(),d=t[0],m=l-d.timestamp;return m>=e?(t.shift(),d.reject(new ue(e,m)),!0):!1},i=async()=>{if(!(o||t.length===0)){for(o=!0;t.length>0;){if(e!==void 0&&a())continue;if(t.length===0)break;const d=Date.now()-n;if(n>0&&d<r){const ge=r-d;await s(ge)}const m=t.shift();m&&(n=Date.now(),m.task().then(m.resolve).catch(m.reject))}o=!1}};return{add:l=>new Promise((d,m)=>{t.push({task:l,resolve:d,reject:m,timestamp:Date.now()}),i()}),getStatus:()=>{const l=t.length>0?n+r:null;return{queueLength:t.length,lastExecutionTime:n,isProcessing:o,nextExecutionTime:l}},clear:()=>{t.length=0}}}const de={interval:1e3},he=r=>{const e={...de,...r},t=le(e.interval,e.timeout);return Object.assign(async({config:o,next:s})=>t.add(s),{__middlewareType:f.THROTTLE,throttler:t})};function me(r){const{instanceKey:e,key:t,duration:n,isValid:o,store:s,...a}=r;return S({instanceKey:e,extensions:[q(a),V({key:t,duration:n,isValid:o,store:s})]})}function ye(r){const{instanceKey:e,parallelCount:t,createId:n,retries:o,delay:s,retryCondition:a}=r;return S({instanceKey:e,extensions:[O({parallelCount:t,createId:n}),N({retries:o,delay:s,retryCondition:a})]})}c.cache=V,c.concurrent=O,c.createCachedIdempotentRequestor=me,c.createConcurrentRetryRequestor=ye,c.createRequestor=S,c.idempotent=q,c.inject=B,c.retry=N,c.throttle=he,c.useRequestor=T,Object.defineProperty(c,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -1,36 +1,36 @@
|
|
|
1
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
|
-
}
|
|
2
|
+
"name": "@net-vert/core",
|
|
3
|
+
"version": "1.3.4",
|
|
4
|
+
"description": "Dependency Inversion Network Library with Type-Safe Injection.",
|
|
5
|
+
"main": "dist/index",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"test": "vitest run",
|
|
10
|
+
"build": "run-p type-check build-only",
|
|
11
|
+
"build-only": "vite build",
|
|
12
|
+
"type-check": "tsc --noEmit"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"sideEffects": false,
|
|
18
|
+
"keywords": [
|
|
19
|
+
"dependency-injection",
|
|
20
|
+
"di",
|
|
21
|
+
"network-library",
|
|
22
|
+
"adapter-pattern",
|
|
23
|
+
"extensible",
|
|
24
|
+
"http-client",
|
|
25
|
+
"lightweight"
|
|
26
|
+
],
|
|
27
|
+
"author": "yuzinan <1589937631@qq.com>",
|
|
28
|
+
"license": "MIT",
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"id-queue": "^1.1.1",
|
|
34
|
+
"store-vert": "^0.2.0"
|
|
35
|
+
}
|
|
36
|
+
}
|