@net-vert/core 0.5.0 → 1.0.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/README.md +1044 -67
- package/dist/index.d.ts +437 -115
- package/dist/index.js +431 -297
- package/dist/index.umd.cjs +1 -1
- package/package.json +10 -10
package/dist/index.js
CHANGED
|
@@ -1,335 +1,469 @@
|
|
|
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
|
-
|
|
1
|
+
var T = Object.defineProperty;
|
|
2
|
+
var q = (r, e, t) => e in r ? T(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t;
|
|
3
|
+
var c = (r, e, t) => q(r, typeof e != "symbol" ? e + "" : e, t);
|
|
4
|
+
import { TaskQueue as O } from "id-queue";
|
|
5
|
+
var y = /* @__PURE__ */ ((r) => (r.GET = "get", r.POST = "post", r.PUT = "put", r.DELETE = "delete", r))(y || {}), f = /* @__PURE__ */ ((r) => (r.CACHE = "cache", r.RETRY = "retry", r.IDEMPOTENT = "idempotent", r.CONCURRENT = "concurrent", r.SYNC = "sync", r))(f || {});
|
|
6
|
+
const I = "default", N = {
|
|
7
|
+
retries: 3,
|
|
8
|
+
delay: 0,
|
|
9
|
+
retryCondition: () => !0
|
|
10
|
+
}, V = (r) => {
|
|
11
|
+
const e = { ...N, ...r };
|
|
12
|
+
return Object.assign(async ({ config: s, next: n }) => {
|
|
13
|
+
let o = 0, i;
|
|
14
|
+
for (; o <= e.retries; )
|
|
15
|
+
try {
|
|
16
|
+
return await n();
|
|
17
|
+
} catch (a) {
|
|
18
|
+
if (i = a, o === e.retries)
|
|
19
|
+
throw a;
|
|
20
|
+
const u = {
|
|
21
|
+
config: s,
|
|
22
|
+
lastResponse: a,
|
|
23
|
+
attempt: o
|
|
24
|
+
};
|
|
25
|
+
if (!e.retryCondition(u))
|
|
26
|
+
throw a;
|
|
27
|
+
o++;
|
|
28
|
+
const d = typeof e.delay == "function" ? e.delay(u) : e.delay;
|
|
29
|
+
d > 0 && await new Promise((h) => setTimeout(h, d));
|
|
30
|
+
}
|
|
31
|
+
throw i;
|
|
32
|
+
}, { __middlewareType: f.RETRY });
|
|
33
|
+
}, j = () => {
|
|
34
|
+
const r = /* @__PURE__ */ new Map();
|
|
35
|
+
return { getPromise: (o) => r.get(o), setPromise: (o, i) => {
|
|
36
|
+
r.set(o, i), i.finally(() => r.delete(o));
|
|
37
|
+
}, delPromise: (o) => r.delete(o), clearCache: () => r.clear() };
|
|
38
|
+
}, K = (r) => {
|
|
39
|
+
const { config: e } = r, { method: t, url: s, data: n } = e;
|
|
40
|
+
return [t, s, JSON.stringify(n)].join("|");
|
|
41
|
+
}, R = {
|
|
42
|
+
key: K
|
|
43
|
+
}, b = (r) => {
|
|
44
|
+
const e = { ...R, ...r }, t = j();
|
|
45
|
+
return Object.assign(({ config: n, next: o }) => {
|
|
46
|
+
const i = e.key({ config: n }), a = t.getPromise(i);
|
|
47
|
+
if (a)
|
|
48
|
+
return a;
|
|
49
|
+
const u = o();
|
|
50
|
+
return t.setPromise(i, u), u;
|
|
51
|
+
}, {
|
|
52
|
+
__middlewareType: f.IDEMPOTENT,
|
|
53
|
+
promiseCache: t
|
|
54
|
+
});
|
|
30
55
|
};
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
(
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
56
|
+
class z {
|
|
57
|
+
constructor(e = 4) {
|
|
58
|
+
c(this, "parallelCount");
|
|
59
|
+
c(this, "tasks");
|
|
60
|
+
c(this, "runningCount");
|
|
61
|
+
this.parallelCount = e, this.tasks = new O(), this.runningCount = 0;
|
|
62
|
+
}
|
|
63
|
+
// 加入
|
|
64
|
+
add(e, t) {
|
|
65
|
+
return new Promise((s, n) => {
|
|
66
|
+
this.tasks.enqueue(e, {
|
|
67
|
+
task: t,
|
|
68
|
+
resolve: s,
|
|
69
|
+
reject: n
|
|
70
|
+
}), this._run();
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
// 删除
|
|
74
|
+
remove(e) {
|
|
75
|
+
this.tasks.remove(e);
|
|
76
|
+
}
|
|
77
|
+
execute(e) {
|
|
78
|
+
const { task: t, resolve: s, reject: n } = e;
|
|
79
|
+
return t().then(s).catch(n).finally(() => {
|
|
80
|
+
this.runningCount--, this._run();
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
_run() {
|
|
84
|
+
for (; this.runningCount < this.parallelCount && this.tasks.size > 0; ) {
|
|
85
|
+
const e = this.tasks.dequeue();
|
|
86
|
+
this.runningCount++, this.execute(e);
|
|
39
87
|
}
|
|
40
|
-
|
|
88
|
+
}
|
|
41
89
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
90
|
+
let _ = 0;
|
|
91
|
+
const $ = {
|
|
92
|
+
parallelCount: 4,
|
|
93
|
+
createId: () => _++
|
|
94
|
+
}, J = (r) => {
|
|
95
|
+
const { parallelCount: e, createId: t } = { ...$, ...r }, s = new z(e);
|
|
96
|
+
return Object.assign(({ config: o, next: i }) => {
|
|
97
|
+
const a = t({ config: o });
|
|
98
|
+
return s.add(a, () => i());
|
|
99
|
+
}, { __middlewareType: f.CONCURRENT, pool: s });
|
|
48
100
|
};
|
|
49
|
-
class
|
|
50
|
-
|
|
51
|
-
|
|
101
|
+
class v {
|
|
102
|
+
constructor() {
|
|
103
|
+
c(this, "store", /* @__PURE__ */ new Map());
|
|
52
104
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
105
|
+
getItem(e) {
|
|
106
|
+
return this.store.get(e);
|
|
107
|
+
}
|
|
108
|
+
setItem(e, t) {
|
|
109
|
+
return this.store.set(e, t), t;
|
|
110
|
+
}
|
|
111
|
+
removeItem(e) {
|
|
112
|
+
this.store.delete(e);
|
|
113
|
+
}
|
|
114
|
+
clear() {
|
|
115
|
+
this.store.clear();
|
|
116
|
+
}
|
|
117
|
+
length() {
|
|
118
|
+
return this.store.size;
|
|
119
|
+
}
|
|
120
|
+
keys() {
|
|
121
|
+
return Array.from(this.store.keys());
|
|
122
|
+
}
|
|
123
|
+
iterate(e) {
|
|
124
|
+
let t = 0;
|
|
125
|
+
for (const [s, n] of this.store.entries())
|
|
126
|
+
e(n, s, t), t++;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
const p = typeof window < "u" ? window.localStorage : {};
|
|
130
|
+
class k {
|
|
131
|
+
constructor() {
|
|
132
|
+
}
|
|
133
|
+
getItem(e) {
|
|
134
|
+
const t = String(e), s = p.getItem(t);
|
|
135
|
+
if (s !== null)
|
|
57
136
|
try {
|
|
58
|
-
return JSON.parse(
|
|
137
|
+
return JSON.parse(s);
|
|
59
138
|
} catch (n) {
|
|
60
|
-
console.error(
|
|
139
|
+
console.error(`Failed to parse value for key: ${t}`, n);
|
|
61
140
|
return;
|
|
62
141
|
}
|
|
63
142
|
}
|
|
64
|
-
|
|
65
|
-
|
|
143
|
+
setItem(e, t) {
|
|
144
|
+
const s = String(e);
|
|
66
145
|
try {
|
|
67
|
-
|
|
68
|
-
localStorage.setItem(e, n);
|
|
146
|
+
p.setItem(s, JSON.stringify(t));
|
|
69
147
|
} catch (n) {
|
|
70
|
-
console.error(
|
|
148
|
+
throw console.error(`Failed to set value for key: ${s}`, n), n;
|
|
71
149
|
}
|
|
72
|
-
return
|
|
150
|
+
return t;
|
|
73
151
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
152
|
+
removeItem(e) {
|
|
153
|
+
const t = String(e);
|
|
154
|
+
p.removeItem(t);
|
|
77
155
|
}
|
|
78
|
-
// 清空所有缓存
|
|
79
156
|
clear() {
|
|
80
|
-
|
|
157
|
+
p.clear();
|
|
81
158
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
159
|
+
length() {
|
|
160
|
+
return p.length;
|
|
161
|
+
}
|
|
162
|
+
keys() {
|
|
163
|
+
const e = [];
|
|
164
|
+
for (let t = 0; t < p.length; t++) {
|
|
165
|
+
const s = p.key(t);
|
|
166
|
+
s && e.push(s);
|
|
167
|
+
}
|
|
168
|
+
return e;
|
|
169
|
+
}
|
|
170
|
+
iterate(e) {
|
|
171
|
+
this.keys().forEach((s, n) => {
|
|
172
|
+
const o = this.getItem(s);
|
|
173
|
+
o !== void 0 && e(o, s, n);
|
|
92
174
|
});
|
|
93
|
-
return {
|
|
94
|
-
has: (o) => n.keys().then((s) => s.includes(o)),
|
|
95
|
-
get: (o) => n.getItem(o),
|
|
96
|
-
set: (o, s) => n.setItem(o, s),
|
|
97
|
-
remove: (o) => n.removeItem(o),
|
|
98
|
-
clear: () => n.clear()
|
|
99
|
-
};
|
|
100
|
-
} else {
|
|
101
|
-
const n = /* @__PURE__ */ new Map();
|
|
102
|
-
return {
|
|
103
|
-
has: (o) => n.has(o),
|
|
104
|
-
get: (o) => n.get(o),
|
|
105
|
-
set: (o, s) => n.set(o, s),
|
|
106
|
-
remove: (o) => n.delete(o),
|
|
107
|
-
clear: () => n.clear()
|
|
108
|
-
};
|
|
109
175
|
}
|
|
110
|
-
}
|
|
111
|
-
|
|
176
|
+
}
|
|
177
|
+
function w(r, e) {
|
|
112
178
|
return {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
t.set(s, u);
|
|
116
|
-
},
|
|
117
|
-
delPromise: (s) => {
|
|
118
|
-
t.delete(s);
|
|
119
|
-
},
|
|
120
|
-
clearCache: () => {
|
|
121
|
-
t.clear();
|
|
122
|
-
}
|
|
179
|
+
value: r,
|
|
180
|
+
expireAt: Date.now() + e
|
|
123
181
|
};
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
},
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
182
|
+
}
|
|
183
|
+
function m(r, e = Date.now()) {
|
|
184
|
+
return r.expireAt <= e;
|
|
185
|
+
}
|
|
186
|
+
function g(r) {
|
|
187
|
+
return r.value;
|
|
188
|
+
}
|
|
189
|
+
const A = (r) => {
|
|
190
|
+
const { config: e } = r, { method: t, url: s, data: n } = e;
|
|
191
|
+
return [t, s, JSON.stringify(n)].join("|");
|
|
192
|
+
}, F = () => !0, x = 24 * 60 * 60 * 1e3, G = {
|
|
193
|
+
key: A,
|
|
194
|
+
duration: x,
|
|
195
|
+
isValid: F,
|
|
196
|
+
persist: !1
|
|
197
|
+
// 默认不持久化,使用内存存储
|
|
198
|
+
};
|
|
199
|
+
class l {
|
|
200
|
+
// ============ 扩展方法(自动处理过期和封装)============
|
|
201
|
+
/**
|
|
202
|
+
* 获取缓存值(自动处理过期检查和解包)
|
|
203
|
+
* @param key 缓存键
|
|
204
|
+
* @returns 缓存的原始值,如果不存在或已过期返回 undefined
|
|
205
|
+
*/
|
|
206
|
+
get(e) {
|
|
207
|
+
const t = this.getItem(e);
|
|
208
|
+
if (t) {
|
|
209
|
+
if (m(t)) {
|
|
210
|
+
this.removeItem(e);
|
|
211
|
+
return;
|
|
146
212
|
}
|
|
147
|
-
|
|
213
|
+
return g(t);
|
|
148
214
|
}
|
|
149
|
-
return { shouldUseCache: C, cachedData: y };
|
|
150
215
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
})) ?? !0;
|
|
161
|
-
} catch (c) {
|
|
162
|
-
console.error(`校验异常 ${a}`, c);
|
|
163
|
-
}
|
|
164
|
-
!C && s.remove(a);
|
|
165
|
-
}
|
|
166
|
-
return { shouldUseCache: C, cachedData: y };
|
|
216
|
+
/**
|
|
217
|
+
* 设置缓存值(自动包装为 ExpirableValue)
|
|
218
|
+
* @param key 缓存键
|
|
219
|
+
* @param value 要缓存的值
|
|
220
|
+
* @param duration 有效期(毫秒),默认 24 小时
|
|
221
|
+
*/
|
|
222
|
+
set(e, t, s = x) {
|
|
223
|
+
const n = w(t, s);
|
|
224
|
+
this.setItem(e, n);
|
|
167
225
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
config: f,
|
|
175
|
-
response: w
|
|
176
|
-
});
|
|
177
|
-
return s.set(c, A(w, R)), w;
|
|
178
|
-
}).finally(() => {
|
|
179
|
-
g(c);
|
|
180
|
-
});
|
|
181
|
-
return P(c, q), q;
|
|
182
|
-
}
|
|
183
|
-
return o ? (...c) => {
|
|
184
|
-
const f = k[h](...c), m = e.key(f), q = u(m);
|
|
185
|
-
if (q)
|
|
186
|
-
return q;
|
|
187
|
-
const { shouldUseCache: w, cachedData: R } = l(m, f);
|
|
188
|
-
return w ? R.value : i(m, f, ...c);
|
|
189
|
-
} : (...c) => {
|
|
190
|
-
const f = k[h](...c), m = e.key(f), q = u(m);
|
|
191
|
-
if (q)
|
|
192
|
-
return q;
|
|
193
|
-
const w = (async () => {
|
|
194
|
-
try {
|
|
195
|
-
const { shouldUseCache: R, cachedData: D } = await d(m, f);
|
|
196
|
-
return R ? D.value : await i(m, f, ...c);
|
|
197
|
-
} finally {
|
|
198
|
-
g(m);
|
|
199
|
-
}
|
|
200
|
-
})();
|
|
201
|
-
return P(m, w), w;
|
|
202
|
-
};
|
|
203
|
-
}
|
|
204
|
-
};
|
|
205
|
-
return {
|
|
206
|
-
requestor: new Proxy(v(), p),
|
|
207
|
-
store: s
|
|
208
|
-
};
|
|
209
|
-
}, N = (t) => {
|
|
210
|
-
const { method: e, url: r, params: n, data: o } = t;
|
|
211
|
-
return [e, r, JSON.stringify(n), JSON.stringify(o)].join("|");
|
|
212
|
-
}, b = {
|
|
213
|
-
key: N
|
|
214
|
-
}, F = (t) => {
|
|
215
|
-
const e = { ...b, ...t }, {
|
|
216
|
-
requestor: r
|
|
217
|
-
} = S({
|
|
218
|
-
key: e.key,
|
|
219
|
-
persist: !1
|
|
220
|
-
});
|
|
221
|
-
return {
|
|
222
|
-
requestor: r
|
|
223
|
-
};
|
|
224
|
-
}, L = {
|
|
225
|
-
retries: 3,
|
|
226
|
-
delay: 0,
|
|
227
|
-
retryCondition: () => !0
|
|
228
|
-
}, $ = (t) => {
|
|
229
|
-
const { retries: e, delay: r, retryCondition: n } = { ...L, ...t }, o = {
|
|
230
|
-
get(s, u) {
|
|
231
|
-
return (...g) => {
|
|
232
|
-
let l = 0;
|
|
233
|
-
const d = () => Reflect.apply(s[u], s, g).catch((p) => {
|
|
234
|
-
if (l < e && n(p)) {
|
|
235
|
-
l++;
|
|
236
|
-
const a = typeof r == "function" ? r(l) : r;
|
|
237
|
-
return new Promise((h) => {
|
|
238
|
-
setTimeout(() => h(d()), a);
|
|
239
|
-
});
|
|
240
|
-
}
|
|
241
|
-
return Promise.reject(p);
|
|
242
|
-
});
|
|
243
|
-
return d();
|
|
244
|
-
};
|
|
245
|
-
}
|
|
246
|
-
};
|
|
247
|
-
return {
|
|
248
|
-
requestor: new Proxy(v(), o)
|
|
249
|
-
};
|
|
250
|
-
};
|
|
251
|
-
class Q {
|
|
252
|
-
constructor(e = 4) {
|
|
253
|
-
this.parallelCount = e, this.tasks = new j(), this.runningCount = 0;
|
|
226
|
+
/**
|
|
227
|
+
* 删除指定缓存(别名方法,等同于 removeItem)
|
|
228
|
+
* @param key 缓存键
|
|
229
|
+
*/
|
|
230
|
+
delete(e) {
|
|
231
|
+
this.removeItem(e);
|
|
254
232
|
}
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
}), this._run();
|
|
263
|
-
});
|
|
233
|
+
/**
|
|
234
|
+
* 检查缓存是否存在且未过期
|
|
235
|
+
* @param key 缓存键
|
|
236
|
+
*/
|
|
237
|
+
has(e) {
|
|
238
|
+
const t = this.getItem(e);
|
|
239
|
+
return t ? m(t) ? (this.removeItem(e), !1) : !0 : !1;
|
|
264
240
|
}
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
241
|
+
/**
|
|
242
|
+
* 获取缓存数量(别名方法,等同于 length)
|
|
243
|
+
*/
|
|
244
|
+
size() {
|
|
245
|
+
return this.length();
|
|
268
246
|
}
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
247
|
+
/**
|
|
248
|
+
* 遍历所有有效的缓存(自动跳过已过期的,并解包值)
|
|
249
|
+
* @param callback 回调函数,接收解包后的值、键和索引
|
|
250
|
+
*/
|
|
251
|
+
forEach(e) {
|
|
252
|
+
let t = 0;
|
|
253
|
+
this.iterate((s, n, o) => {
|
|
254
|
+
if (m(s))
|
|
255
|
+
this.removeItem(n);
|
|
256
|
+
else {
|
|
257
|
+
const i = g(s);
|
|
258
|
+
e(i, n, t), t++;
|
|
259
|
+
}
|
|
273
260
|
});
|
|
274
261
|
}
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
262
|
+
/**
|
|
263
|
+
* 清理所有过期的缓存
|
|
264
|
+
* @returns 清理的缓存数量
|
|
265
|
+
*/
|
|
266
|
+
clearExpired() {
|
|
267
|
+
let e = 0;
|
|
268
|
+
return this.keys().forEach((s) => {
|
|
269
|
+
const n = this.getItem(s);
|
|
270
|
+
n && m(n) && (this.removeItem(s), e++);
|
|
271
|
+
}), e;
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* 更新缓存的过期时间(不改变值)
|
|
275
|
+
* @param key 缓存键
|
|
276
|
+
* @param duration 新的有效期(毫秒)
|
|
277
|
+
* @returns 是否更新成功
|
|
278
|
+
*/
|
|
279
|
+
touch(e, t) {
|
|
280
|
+
const s = this.getItem(e);
|
|
281
|
+
if (!s) return !1;
|
|
282
|
+
if (m(s))
|
|
283
|
+
return this.removeItem(e), !1;
|
|
284
|
+
const n = g(s);
|
|
285
|
+
return this.set(e, n, t), !0;
|
|
280
286
|
}
|
|
281
287
|
}
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
288
|
+
class D extends v {
|
|
289
|
+
constructor() {
|
|
290
|
+
super(...arguments);
|
|
291
|
+
// 继承 MemoryStorage 的所有方法:getItem, setItem, removeItem, clear, length, keys, iterate
|
|
292
|
+
// 从 BaseCacheStorage 继承扩展方法
|
|
293
|
+
c(this, "get", l.prototype.get);
|
|
294
|
+
c(this, "set", l.prototype.set);
|
|
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);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
const B = (r) => {
|
|
319
|
+
const e = { ...G, ...r }, t = e.persist ? new U() : new D(), s = (o) => typeof e.duration == "function" ? e.duration(o) : e.duration;
|
|
320
|
+
return Object.assign(async ({ config: o, next: i }) => {
|
|
321
|
+
const a = e.key({ config: o }), u = t.getItem(a);
|
|
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);
|
|
293
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 = {
|
|
341
|
+
suspense: !0,
|
|
342
|
+
key: L,
|
|
343
|
+
duration: 24 * 60 * 60 * 1e3,
|
|
344
|
+
isValid: () => !0,
|
|
345
|
+
persist: !1
|
|
346
|
+
};
|
|
347
|
+
function te(r) {
|
|
348
|
+
const e = { ...M, ...r }, t = e.persist ? new k() : new v(), s = (o) => typeof e.duration == "function" ? e.duration(o) : e.duration;
|
|
349
|
+
return Object.assign(({ config: o, next: i }) => {
|
|
350
|
+
const a = e.key({ config: o }), u = t.getItem(a);
|
|
351
|
+
if (u) {
|
|
352
|
+
if (!m(u) && e.isValid({
|
|
353
|
+
key: a,
|
|
354
|
+
config: o,
|
|
355
|
+
cachedData: u
|
|
356
|
+
}))
|
|
357
|
+
return g(u);
|
|
358
|
+
t.removeItem(a);
|
|
359
|
+
}
|
|
360
|
+
if (e.suspense)
|
|
361
|
+
throw (e.wrapSuspense ? e.wrapSuspense({ key: a, config: o, p: i() }) : i()).then((h) => {
|
|
362
|
+
const C = s({ key: a, config: o, cachedData: u, response: h });
|
|
363
|
+
return t.setItem(a, w(h, C)), h;
|
|
364
|
+
});
|
|
365
|
+
return i().then((d) => {
|
|
366
|
+
const h = s({ key: a, config: o, cachedData: u, response: d });
|
|
367
|
+
return t.setItem(a, w(d, h)), d;
|
|
368
|
+
});
|
|
369
|
+
}, { __middlewareType: f.SYNC });
|
|
370
|
+
}
|
|
371
|
+
const S = /* @__PURE__ */ new Map(), re = (r, e = I) => {
|
|
372
|
+
S.set(e, r);
|
|
373
|
+
}, Y = (r = I) => {
|
|
374
|
+
const e = S.get(r);
|
|
375
|
+
if (!e) throw new Error(`Requestor实例 ${String(r)} 未注册`);
|
|
376
|
+
return e;
|
|
377
|
+
}, E = {
|
|
378
|
+
get: (r, e) => ({
|
|
379
|
+
url: r,
|
|
380
|
+
method: y.GET,
|
|
381
|
+
...e,
|
|
382
|
+
params: e == null ? void 0 : e.params
|
|
383
|
+
}),
|
|
384
|
+
post: (r, e, t) => ({
|
|
385
|
+
url: r,
|
|
386
|
+
method: y.POST,
|
|
387
|
+
data: e,
|
|
388
|
+
...t
|
|
389
|
+
}),
|
|
390
|
+
delete: (r, e) => ({
|
|
391
|
+
url: r,
|
|
392
|
+
method: y.DELETE,
|
|
393
|
+
...e
|
|
394
|
+
}),
|
|
395
|
+
put: (r, e, t) => ({
|
|
396
|
+
url: r,
|
|
397
|
+
method: y.PUT,
|
|
398
|
+
data: e,
|
|
399
|
+
...t
|
|
400
|
+
}),
|
|
401
|
+
request: (r) => r
|
|
402
|
+
}, X = Object.keys(E);
|
|
403
|
+
function Z(r, e, t) {
|
|
404
|
+
const s = {}, n = (o) => {
|
|
405
|
+
if (o === e.length)
|
|
406
|
+
return t(r);
|
|
407
|
+
const i = e[o];
|
|
408
|
+
return i({
|
|
409
|
+
config: r,
|
|
410
|
+
ctx: s,
|
|
411
|
+
next: () => n(o + 1)
|
|
412
|
+
});
|
|
294
413
|
};
|
|
295
|
-
return
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
};
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
return (...l) => {
|
|
309
|
-
try {
|
|
310
|
-
const d = Reflect.apply(u[P], u, l);
|
|
311
|
-
if (d instanceof Promise)
|
|
312
|
-
throw d;
|
|
313
|
-
return d;
|
|
314
|
-
} catch (d) {
|
|
315
|
-
throw d;
|
|
316
|
-
}
|
|
414
|
+
return n(0);
|
|
415
|
+
}
|
|
416
|
+
function H(r, e) {
|
|
417
|
+
const t = {}, s = e ?? [];
|
|
418
|
+
return X.forEach(
|
|
419
|
+
(n) => {
|
|
420
|
+
t[n] = (...o) => {
|
|
421
|
+
const i = E[n](...o);
|
|
422
|
+
return Z(
|
|
423
|
+
i,
|
|
424
|
+
s,
|
|
425
|
+
r
|
|
426
|
+
);
|
|
317
427
|
};
|
|
318
428
|
}
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
429
|
+
), t;
|
|
430
|
+
}
|
|
431
|
+
function P(r) {
|
|
432
|
+
const { extensions: e, instanceKey: t } = r ?? {}, s = Y(t);
|
|
433
|
+
return H(
|
|
434
|
+
s,
|
|
435
|
+
e
|
|
436
|
+
);
|
|
437
|
+
}
|
|
438
|
+
function se(r) {
|
|
439
|
+
const { instanceKey: e, key: t, duration: s, isValid: n, persist: o, ...i } = r;
|
|
440
|
+
return P({
|
|
441
|
+
instanceKey: e,
|
|
442
|
+
extensions: [
|
|
443
|
+
b(i),
|
|
444
|
+
B({ key: t, duration: s, isValid: n, persist: o })
|
|
445
|
+
]
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
function ne(r) {
|
|
449
|
+
const { instanceKey: e, parallelCount: t, createId: s, retries: n, delay: o, retryCondition: i } = r;
|
|
450
|
+
return P({
|
|
451
|
+
instanceKey: e,
|
|
452
|
+
extensions: [
|
|
453
|
+
J({ parallelCount: t, createId: s }),
|
|
454
|
+
V({ retries: n, delay: o, retryCondition: i })
|
|
455
|
+
]
|
|
456
|
+
});
|
|
457
|
+
}
|
|
331
458
|
export {
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
459
|
+
B as cache,
|
|
460
|
+
J as concurrent,
|
|
461
|
+
se as createCachedIdempotentRequestor,
|
|
462
|
+
ne as createConcurrentRetryRequestor,
|
|
463
|
+
P as createRequestor,
|
|
464
|
+
b as idempotent,
|
|
465
|
+
re as inject,
|
|
466
|
+
V as retry,
|
|
467
|
+
te as sync,
|
|
468
|
+
Y as useRequestor
|
|
335
469
|
};
|