@net-vert/core 0.1.1 → 0.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 +57 -18
- package/dist/index.js +205 -105
- package/dist/index.umd.cjs +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
import { TaskQueue } from 'id-queue';
|
|
2
2
|
|
|
3
|
+
declare type CachedData = {
|
|
4
|
+
value: any;
|
|
5
|
+
expiresAt: number;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
declare type CacheRequestor<P extends boolean, S extends boolean> = {
|
|
9
|
+
key?: (config: UnifiedConfig) => string;
|
|
10
|
+
duration?: Duration;
|
|
11
|
+
sync?: S;
|
|
12
|
+
persist?: P;
|
|
13
|
+
name?: [P, S] extends [true, false] ? string : undefined;
|
|
14
|
+
isValid?: S extends true ? (params: IsValidParams) => boolean : (params: IsValidParams) => boolean | Promise<boolean>;
|
|
15
|
+
};
|
|
16
|
+
|
|
3
17
|
declare class ConcurrentPool {
|
|
4
18
|
parallelCount: number;
|
|
5
19
|
tasks: TaskItemList;
|
|
@@ -11,8 +25,20 @@ declare class ConcurrentPool {
|
|
|
11
25
|
_run(): void;
|
|
12
26
|
}
|
|
13
27
|
|
|
28
|
+
declare type Duration = number | (({ key, config, response }: {
|
|
29
|
+
key: string;
|
|
30
|
+
config: UnifiedConfig;
|
|
31
|
+
response: any;
|
|
32
|
+
}) => number);
|
|
33
|
+
|
|
14
34
|
export declare const inject: (requestor: UnifiedRequestor, instanceKey?: string) => void;
|
|
15
35
|
|
|
36
|
+
declare type IsValidParams = {
|
|
37
|
+
key: string;
|
|
38
|
+
config: UnifiedConfig;
|
|
39
|
+
cachedData: CachedData;
|
|
40
|
+
};
|
|
41
|
+
|
|
16
42
|
declare interface RequestConfig<D = any> {
|
|
17
43
|
url?: string;
|
|
18
44
|
method?: keyof Requestor;
|
|
@@ -28,25 +54,32 @@ declare interface RequestConfig<D = any> {
|
|
|
28
54
|
}
|
|
29
55
|
|
|
30
56
|
export declare const requestExtender: {
|
|
31
|
-
cacheRequestor: (config?: {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
key: string;
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
key: string
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
57
|
+
cacheRequestor: <P extends boolean = false, S extends boolean = false>(config?: CacheRequestor<P, S>) => {
|
|
58
|
+
requestor: Requestor;
|
|
59
|
+
store: {
|
|
60
|
+
has(key: string): boolean;
|
|
61
|
+
get<T>(key: string): T | undefined;
|
|
62
|
+
set<T>(key: string, value: T): T;
|
|
63
|
+
remove(key: string): void;
|
|
64
|
+
clear(): void;
|
|
65
|
+
} | {
|
|
66
|
+
has: (key: string) => Promise<boolean>;
|
|
67
|
+
get: <T>(key: string) => Promise<T | null>;
|
|
68
|
+
set: <T>(key: string, value: T) => Promise<T>;
|
|
69
|
+
remove: (key: string) => Promise<void>;
|
|
70
|
+
clear: () => Promise<void>;
|
|
71
|
+
} | {
|
|
72
|
+
has: (key: string) => boolean;
|
|
73
|
+
get: (key: string) => any;
|
|
74
|
+
set: <T>(key: string, value: T) => Map<string, any>;
|
|
75
|
+
remove: (key: string) => boolean;
|
|
76
|
+
clear: () => void;
|
|
77
|
+
};
|
|
78
|
+
};
|
|
48
79
|
idempotencyRequestor: (genKey?: (config: UnifiedConfig) => string) => Requestor;
|
|
49
|
-
retryRequestor: (config?: RetryOptions) =>
|
|
80
|
+
retryRequestor: (config?: RetryOptions) => {
|
|
81
|
+
requestor: Requestor;
|
|
82
|
+
};
|
|
50
83
|
concurrentPoolRequestor: (config?: {
|
|
51
84
|
parallelCount?: number;
|
|
52
85
|
createId?: (config: UnifiedConfig) => string;
|
|
@@ -54,6 +87,12 @@ export declare const requestExtender: {
|
|
|
54
87
|
requestor: Requestor;
|
|
55
88
|
concurrentPool: ConcurrentPool;
|
|
56
89
|
};
|
|
90
|
+
syncRequestor: (config?: {
|
|
91
|
+
persist?: false;
|
|
92
|
+
sync?: true;
|
|
93
|
+
} & CacheRequestor<false, true>) => {
|
|
94
|
+
requestor: Requestor;
|
|
95
|
+
};
|
|
57
96
|
};
|
|
58
97
|
|
|
59
98
|
export declare interface Requestor {
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { TaskQueue as
|
|
3
|
-
const
|
|
1
|
+
import D from "localforage";
|
|
2
|
+
import { TaskQueue as E } from "id-queue";
|
|
3
|
+
const k = {
|
|
4
4
|
get: (t, e) => ({
|
|
5
5
|
url: t,
|
|
6
6
|
method: "get",
|
|
@@ -30,11 +30,11 @@ const f = {
|
|
|
30
30
|
};
|
|
31
31
|
function j(t) {
|
|
32
32
|
const e = {};
|
|
33
|
-
return Object.keys(
|
|
33
|
+
return Object.keys(k).forEach(
|
|
34
34
|
(r) => {
|
|
35
|
-
e[r] = (...
|
|
36
|
-
const
|
|
37
|
-
return t(
|
|
35
|
+
e[r] = (...n) => {
|
|
36
|
+
const o = k[r](...n);
|
|
37
|
+
return t(o);
|
|
38
38
|
};
|
|
39
39
|
}
|
|
40
40
|
), {
|
|
@@ -42,39 +42,75 @@ function j(t) {
|
|
|
42
42
|
request: t
|
|
43
43
|
};
|
|
44
44
|
}
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
},
|
|
48
|
-
const e =
|
|
45
|
+
const M = "default", z = () => `${Date.now()}_${Math.random().toString().slice(2, 8)}`, I = /* @__PURE__ */ new Map(), V = (t, e = M) => {
|
|
46
|
+
I.set(e, j(t));
|
|
47
|
+
}, v = (t = M) => {
|
|
48
|
+
const e = I.get(t);
|
|
49
49
|
if (!e) throw new Error(`Requestor实例 ${t} 未注册`);
|
|
50
50
|
return e;
|
|
51
51
|
};
|
|
52
|
-
class
|
|
53
|
-
|
|
54
|
-
|
|
52
|
+
class O {
|
|
53
|
+
has(e) {
|
|
54
|
+
return !!this.get(e);
|
|
55
55
|
}
|
|
56
|
-
|
|
57
|
-
|
|
56
|
+
// 获取缓存
|
|
57
|
+
get(e) {
|
|
58
|
+
const r = localStorage.getItem(e);
|
|
59
|
+
if (r)
|
|
60
|
+
try {
|
|
61
|
+
return JSON.parse(r);
|
|
62
|
+
} catch (n) {
|
|
63
|
+
console.error("Error parsing cached data", n);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
58
66
|
}
|
|
59
|
-
|
|
67
|
+
// 设置缓存
|
|
68
|
+
set(e, r) {
|
|
60
69
|
try {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
70
|
+
const n = JSON.stringify(r);
|
|
71
|
+
localStorage.setItem(e, n);
|
|
72
|
+
} catch (n) {
|
|
73
|
+
console.error("Error saving data to localStorage", n);
|
|
65
74
|
}
|
|
75
|
+
return r;
|
|
66
76
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
async delete(e) {
|
|
71
|
-
await h.removeItem(e);
|
|
77
|
+
// 删除缓存
|
|
78
|
+
remove(e) {
|
|
79
|
+
localStorage.removeItem(e);
|
|
72
80
|
}
|
|
73
|
-
|
|
74
|
-
|
|
81
|
+
// 清空所有缓存
|
|
82
|
+
clear() {
|
|
83
|
+
localStorage.clear();
|
|
75
84
|
}
|
|
76
85
|
}
|
|
77
|
-
const
|
|
86
|
+
const T = new O(), H = ({ persist: t, name: e, sync: r }) => {
|
|
87
|
+
if (t && r)
|
|
88
|
+
return T;
|
|
89
|
+
if (t) {
|
|
90
|
+
const n = D.createInstance({
|
|
91
|
+
name: e
|
|
92
|
+
// driver: sync
|
|
93
|
+
// ?undefined
|
|
94
|
+
// :localforage.LOCALSTORAGE,
|
|
95
|
+
});
|
|
96
|
+
return {
|
|
97
|
+
has: (o) => n.keys().then((s) => s.includes(o)),
|
|
98
|
+
get: (o) => n.getItem(o),
|
|
99
|
+
set: (o, s) => n.setItem(o, s),
|
|
100
|
+
remove: (o) => n.removeItem(o),
|
|
101
|
+
clear: () => n.clear()
|
|
102
|
+
};
|
|
103
|
+
} else {
|
|
104
|
+
const n = /* @__PURE__ */ new Map();
|
|
105
|
+
return {
|
|
106
|
+
has: (o) => n.has(o),
|
|
107
|
+
get: (o) => n.get(o),
|
|
108
|
+
set: (o, s) => n.set(o, s),
|
|
109
|
+
remove: (o) => n.delete(o),
|
|
110
|
+
clear: () => n.clear()
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
}, U = () => {
|
|
78
114
|
const t = /* @__PURE__ */ new Map();
|
|
79
115
|
return {
|
|
80
116
|
getPromise: (s) => t.get(s),
|
|
@@ -91,90 +127,130 @@ const S = new E(), T = /* @__PURE__ */ new Map(), $ = (t) => t ? S : T, D = () =
|
|
|
91
127
|
}, _ = (t, e) => ({
|
|
92
128
|
value: t,
|
|
93
129
|
expiresAt: Date.now() + e
|
|
94
|
-
}),
|
|
130
|
+
}), x = (t) => t.expiresAt > Date.now(), A = {
|
|
95
131
|
key: (t) => t.url,
|
|
96
132
|
persist: !1,
|
|
97
|
-
duration: 1 / 0
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
133
|
+
duration: 1 / 0,
|
|
134
|
+
sync: !1
|
|
135
|
+
}, S = (t) => {
|
|
136
|
+
const e = { ...A, ...t }, { name: r, persist: n, sync: o } = e, s = H({ persist: n, name: r, sync: o }), { getPromise: i, setPromise: P, delPromise: f } = U();
|
|
137
|
+
function c(a, d) {
|
|
138
|
+
const { isValid: l } = e, h = s.get(a);
|
|
139
|
+
let g = !1;
|
|
140
|
+
if (h && x(h)) {
|
|
141
|
+
try {
|
|
142
|
+
g = (l == null ? void 0 : l({
|
|
143
|
+
key: a,
|
|
144
|
+
config: d,
|
|
145
|
+
cachedData: h
|
|
146
|
+
})) ?? !0;
|
|
147
|
+
} catch (u) {
|
|
148
|
+
console.error(`校验异常 ${a}`, u);
|
|
149
|
+
}
|
|
150
|
+
!g && s.remove(a);
|
|
151
|
+
}
|
|
152
|
+
return { shouldUseCache: g, cachedData: h };
|
|
153
|
+
}
|
|
154
|
+
async function w(a, d) {
|
|
155
|
+
const { isValid: l } = e, h = await s.get(a);
|
|
156
|
+
let g = !1;
|
|
157
|
+
if (h && x(h)) {
|
|
158
|
+
try {
|
|
159
|
+
g = await (l == null ? void 0 : l({
|
|
160
|
+
key: a,
|
|
161
|
+
config: d,
|
|
162
|
+
cachedData: h
|
|
163
|
+
})) ?? !0;
|
|
164
|
+
} catch (u) {
|
|
165
|
+
console.error(`校验异常 ${a}`, u);
|
|
166
|
+
}
|
|
167
|
+
!g && s.remove(a);
|
|
168
|
+
}
|
|
169
|
+
return { shouldUseCache: g, cachedData: h };
|
|
170
|
+
}
|
|
171
|
+
const p = {
|
|
172
|
+
get(a, d) {
|
|
173
|
+
function l(u, m, ...y) {
|
|
174
|
+
const C = Reflect.apply(a[d], a, y).then(async (q) => {
|
|
175
|
+
const R = typeof e.duration == "number" ? e.duration : e.duration({
|
|
176
|
+
key: u,
|
|
177
|
+
config: m,
|
|
178
|
+
response: q
|
|
126
179
|
});
|
|
127
|
-
return
|
|
180
|
+
return s.set(u, _(q, R)), q;
|
|
128
181
|
}).finally(() => {
|
|
129
|
-
|
|
182
|
+
f(u);
|
|
130
183
|
});
|
|
131
|
-
return
|
|
184
|
+
return P(u, C), C;
|
|
185
|
+
}
|
|
186
|
+
return o ? (...u) => {
|
|
187
|
+
const m = k[d](...u), y = e.key(m), C = i(y);
|
|
188
|
+
if (C)
|
|
189
|
+
return C;
|
|
190
|
+
const { shouldUseCache: q, cachedData: R } = c(y, m);
|
|
191
|
+
return q ? R.value : l(y, m, ...u);
|
|
192
|
+
} : async (...u) => {
|
|
193
|
+
const m = k[d](...u), y = e.key(m), C = i(y);
|
|
194
|
+
if (C)
|
|
195
|
+
return C;
|
|
196
|
+
const { shouldUseCache: q, cachedData: R } = await w(y, m);
|
|
197
|
+
return q ? R.value : l(y, m, ...u);
|
|
132
198
|
};
|
|
133
199
|
}
|
|
134
200
|
};
|
|
135
|
-
return
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
},
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
}
|
|
201
|
+
return {
|
|
202
|
+
requestor: new Proxy(v(), p),
|
|
203
|
+
store: s
|
|
204
|
+
};
|
|
205
|
+
}, J = (t) => {
|
|
206
|
+
const { method: e, url: r, params: n, data: o } = t;
|
|
207
|
+
return [e, r, JSON.stringify(n), JSON.stringify(o)].join("|");
|
|
208
|
+
}, N = (t) => {
|
|
209
|
+
const {
|
|
210
|
+
requestor: e
|
|
211
|
+
} = S({
|
|
212
|
+
key: (r) => t ? t(r) : J(r),
|
|
213
|
+
persist: !1
|
|
214
|
+
});
|
|
215
|
+
return e;
|
|
216
|
+
}, b = {
|
|
143
217
|
retries: 3,
|
|
144
218
|
delay: 0,
|
|
145
219
|
retryCondition: () => !0
|
|
146
|
-
},
|
|
147
|
-
const { retries: e, delay: r, retryCondition:
|
|
220
|
+
}, $ = (t) => {
|
|
221
|
+
const { retries: e, delay: r, retryCondition: n } = { ...b, ...t }, o = {
|
|
148
222
|
get(s, i) {
|
|
149
|
-
return (...
|
|
223
|
+
return (...f) => {
|
|
150
224
|
let c = 0;
|
|
151
|
-
const
|
|
152
|
-
if (c < e &&
|
|
225
|
+
const w = () => Reflect.apply(s[i], s, f).catch((p) => {
|
|
226
|
+
if (c < e && n(p)) {
|
|
153
227
|
c++;
|
|
154
|
-
const
|
|
155
|
-
return new Promise((
|
|
156
|
-
setTimeout(() =>
|
|
228
|
+
const a = typeof r == "function" ? r(c) : r;
|
|
229
|
+
return new Promise((d) => {
|
|
230
|
+
setTimeout(() => d(w()), a);
|
|
157
231
|
});
|
|
158
232
|
}
|
|
159
|
-
return Promise.reject(
|
|
233
|
+
return Promise.reject(p);
|
|
160
234
|
});
|
|
161
|
-
return
|
|
235
|
+
return w();
|
|
162
236
|
};
|
|
163
237
|
}
|
|
164
238
|
};
|
|
165
|
-
return
|
|
239
|
+
return {
|
|
240
|
+
requestor: new Proxy(v(), o)
|
|
241
|
+
};
|
|
166
242
|
};
|
|
167
|
-
class
|
|
243
|
+
class F {
|
|
168
244
|
constructor(e = 4) {
|
|
169
|
-
this.parallelCount = e, this.tasks = new
|
|
245
|
+
this.parallelCount = e, this.tasks = new E(), this.runningCount = 0;
|
|
170
246
|
}
|
|
171
247
|
// 加入
|
|
172
248
|
add(e, r) {
|
|
173
|
-
return
|
|
249
|
+
return new Promise((n, o) => {
|
|
174
250
|
this.tasks.enqueue(e, {
|
|
175
251
|
task: r,
|
|
176
|
-
resolve:
|
|
177
|
-
reject:
|
|
252
|
+
resolve: n,
|
|
253
|
+
reject: o
|
|
178
254
|
}), this._run();
|
|
179
255
|
});
|
|
180
256
|
}
|
|
@@ -183,8 +259,8 @@ class b {
|
|
|
183
259
|
this.tasks.remove(e);
|
|
184
260
|
}
|
|
185
261
|
execute(e) {
|
|
186
|
-
const { task: r, resolve:
|
|
187
|
-
return r().then(
|
|
262
|
+
const { task: r, resolve: n, reject: o } = e;
|
|
263
|
+
return r().then(n).catch(o).finally(() => {
|
|
188
264
|
this.runningCount--, this._run();
|
|
189
265
|
});
|
|
190
266
|
}
|
|
@@ -195,31 +271,55 @@ class b {
|
|
|
195
271
|
}
|
|
196
272
|
}
|
|
197
273
|
}
|
|
198
|
-
const
|
|
274
|
+
const L = {
|
|
199
275
|
parallelCount: 4,
|
|
200
276
|
retries: 0,
|
|
201
|
-
createId: () =>
|
|
202
|
-
},
|
|
203
|
-
const e = { ...
|
|
204
|
-
get(
|
|
205
|
-
return (...
|
|
206
|
-
const
|
|
207
|
-
return s.add(
|
|
277
|
+
createId: () => z()
|
|
278
|
+
}, Q = (t) => {
|
|
279
|
+
const e = { ...L, ...t }, { parallelCount: r, createId: n, ...o } = e, s = new F(r), { requestor: i = null } = o.retries > 0 ? $(o) : {}, P = {
|
|
280
|
+
get(f, c) {
|
|
281
|
+
return (...p) => {
|
|
282
|
+
const a = k[c](...p), d = n(a), l = () => i ? Reflect.apply(i[c], i, p) : Reflect.apply(f[c], f, p);
|
|
283
|
+
return s.add(d, l);
|
|
208
284
|
};
|
|
209
285
|
}
|
|
210
286
|
};
|
|
211
287
|
return {
|
|
212
|
-
requestor: new Proxy(
|
|
288
|
+
requestor: new Proxy(v(), P),
|
|
213
289
|
concurrentPool: s
|
|
214
290
|
};
|
|
215
|
-
},
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
291
|
+
}, W = {
|
|
292
|
+
persist: !1,
|
|
293
|
+
sync: !0
|
|
294
|
+
}, Y = (t) => {
|
|
295
|
+
const e = { ...W, ...t }, { ...r } = e, {
|
|
296
|
+
requestor: n
|
|
297
|
+
} = S(r), o = {
|
|
298
|
+
get(s, i) {
|
|
299
|
+
return (...f) => {
|
|
300
|
+
try {
|
|
301
|
+
const c = Reflect.apply(s[i], s, f);
|
|
302
|
+
if (c instanceof Promise)
|
|
303
|
+
throw c;
|
|
304
|
+
return c;
|
|
305
|
+
} catch (c) {
|
|
306
|
+
throw c;
|
|
307
|
+
}
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
return {
|
|
312
|
+
requestor: new Proxy(n, o)
|
|
313
|
+
};
|
|
314
|
+
}, X = {
|
|
315
|
+
cacheRequestor: S,
|
|
316
|
+
idempotencyRequestor: N,
|
|
317
|
+
retryRequestor: $,
|
|
318
|
+
concurrentPoolRequestor: Q,
|
|
319
|
+
syncRequestor: Y
|
|
220
320
|
};
|
|
221
321
|
export {
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
322
|
+
V as inject,
|
|
323
|
+
X as requestExtender,
|
|
324
|
+
v as useRequestor
|
|
225
325
|
};
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(h,w){typeof exports=="object"&&typeof module<"u"?w(exports,require("localforage"),require("id-queue")):typeof define=="function"&&define.amd?define(["exports","localforage","id-queue"],w):(h=typeof globalThis<"u"?globalThis:h||self,w(h["net-vert/core"]={},h.localforage,h.idQueue))})(this,function(h,w,D){"use strict";const R={get:(t,e)=>({url:t,method:"get",...e,params:e==null?void 0:e.params}),post:(t,e,r)=>({url:t,method:"post",data:e,headers:{"Content-Type":"application/json",...r==null?void 0:r.headers},...r}),delete:(t,e)=>({url:t,method:"delete",...e}),put:(t,e,r)=>({url:t,method:"put",data:e,headers:{"Content-Type":"application/json",...r==null?void 0:r.headers},...r}),request:t=>t};function E(t){const e={};return Object.keys(R).forEach(r=>{e[r]=(...n)=>{const o=R[r](...n);return t(o)}}),{...e,request:t}}const I="default",O=()=>`${Date.now()}_${Math.random().toString().slice(2,8)}`,j=new Map,z=(t,e=I)=>{j.set(e,E(t))},M=(t=I)=>{const e=j.get(t);if(!e)throw new Error(`Requestor实例 ${t} 未注册`);return e};class H{has(e){return!!this.get(e)}get(e){const r=localStorage.getItem(e);if(r)try{return JSON.parse(r)}catch(n){console.error("Error parsing cached data",n);return}}set(e,r){try{const n=JSON.stringify(r);localStorage.setItem(e,n)}catch(n){console.error("Error saving data to localStorage",n)}return r}remove(e){localStorage.removeItem(e)}clear(){localStorage.clear()}}const U=new H,_=({persist:t,name:e,sync:r})=>{if(t&&r)return U;if(t){const n=w.createInstance({name:e});return{has:o=>n.keys().then(s=>s.includes(o)),get:o=>n.getItem(o),set:(o,s)=>n.setItem(o,s),remove:o=>n.removeItem(o),clear:()=>n.clear()}}else{const n=new Map;return{has:o=>n.has(o),get:o=>n.get(o),set:(o,s)=>n.set(o,s),remove:o=>n.delete(o),clear:()=>n.clear()}}},A=()=>{const t=new Map;return{getPromise:s=>t.get(s),setPromise:(s,i)=>{t.set(s,i)},delPromise:s=>{t.delete(s)},clearCache:()=>{t.clear()}}},J=(t,e)=>({value:t,expiresAt:Date.now()+e}),T=t=>t.expiresAt>Date.now(),N={key:t=>t.url,persist:!1,duration:1/0,sync:!1},x=t=>{const e={...N,...t},{name:r,persist:n,sync:o}=e,s=_({persist:n,name:r,sync:o}),{getPromise:i,setPromise:S,delPromise:p}=A();function a(c,d){const{isValid:l}=e,m=s.get(c);let q=!1;if(m&&T(m)){try{q=(l==null?void 0:l({key:c,config:d,cachedData:m}))??!0}catch(u){console.error(`校验异常 ${c}`,u)}!q&&s.remove(c)}return{shouldUseCache:q,cachedData:m}}async function k(c,d){const{isValid:l}=e,m=await s.get(c);let q=!1;if(m&&T(m)){try{q=await(l==null?void 0:l({key:c,config:d,cachedData:m}))??!0}catch(u){console.error(`校验异常 ${c}`,u)}!q&&s.remove(c)}return{shouldUseCache:q,cachedData:m}}const g={get(c,d){function l(u,f,...y){const C=Reflect.apply(c[d],c,y).then(async P=>{const v=typeof e.duration=="number"?e.duration:e.duration({key:u,config:f,response:P});return s.set(u,J(P,v)),P}).finally(()=>{p(u)});return S(u,C),C}return o?(...u)=>{const f=R[d](...u),y=e.key(f),C=i(y);if(C)return C;const{shouldUseCache:P,cachedData:v}=a(y,f);return P?v.value:l(y,f,...u)}:async(...u)=>{const f=R[d](...u),y=e.key(f),C=i(y);if(C)return C;const{shouldUseCache:P,cachedData:v}=await k(y,f);return P?v.value:l(y,f,...u)}}};return{requestor:new Proxy(M(),g),store:s}},b=t=>{const{method:e,url:r,params:n,data:o}=t;return[e,r,JSON.stringify(n),JSON.stringify(o)].join("|")},Q=t=>{const{requestor:e}=x({key:r=>t?t(r):b(r),persist:!1});return e},F={retries:3,delay:0,retryCondition:()=>!0},$=t=>{const{retries:e,delay:r,retryCondition:n}={...F,...t},o={get(s,i){return(...p)=>{let a=0;const k=()=>Reflect.apply(s[i],s,p).catch(g=>{if(a<e&&n(g)){a++;const c=typeof r=="function"?r(a):r;return new Promise(d=>{setTimeout(()=>d(k()),c)})}return Promise.reject(g)});return k()}}};return{requestor:new Proxy(M(),o)}};class L{constructor(e=4){this.parallelCount=e,this.tasks=new D.TaskQueue,this.runningCount=0}add(e,r){return new Promise((n,o)=>{this.tasks.enqueue(e,{task:r,resolve:n,reject:o}),this._run()})}remove(e){this.tasks.remove(e)}execute(e){const{task:r,resolve:n,reject:o}=e;return r().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)}}}const W={parallelCount:4,retries:0,createId:()=>O()},Y=t=>{const e={...W,...t},{parallelCount:r,createId:n,...o}=e,s=new L(r),{requestor:i=null}=o.retries>0?$(o):{},S={get(p,a){return(...g)=>{const c=R[a](...g),d=n(c),l=()=>i?Reflect.apply(i[a],i,g):Reflect.apply(p[a],p,g);return s.add(d,l)}}};return{requestor:new Proxy(M(),S),concurrentPool:s}},B={persist:!1,sync:!0},G={cacheRequestor:x,idempotencyRequestor:Q,retryRequestor:$,concurrentPoolRequestor:Y,syncRequestor:t=>{const e={...B,...t},{...r}=e,{requestor:n}=x(r),o={get(s,i){return(...p)=>{try{const a=Reflect.apply(s[i],s,p);if(a instanceof Promise)throw a;return a}catch(a){throw a}}}};return{requestor:new Proxy(n,o)}}};h.inject=z,h.requestExtender=G,h.useRequestor=M,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"})});
|