@net-vert/core 0.5.0 → 0.5.1
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 +25 -3
- package/dist/index.js +62 -59
- package/dist/index.umd.cjs +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -31,6 +31,11 @@ declare type Duration = number | (({ key, config, response }: {
|
|
|
31
31
|
response: any;
|
|
32
32
|
}) => number);
|
|
33
33
|
|
|
34
|
+
declare type IdempotencyOptions = {
|
|
35
|
+
key?: (config: UnifiedConfig) => string;
|
|
36
|
+
duration?: number;
|
|
37
|
+
};
|
|
38
|
+
|
|
34
39
|
export declare const inject: (requestor: UnifiedRequestor, instanceKey?: string) => void;
|
|
35
40
|
|
|
36
41
|
declare type IsValidParams = {
|
|
@@ -76,10 +81,27 @@ export declare const requestExtender: {
|
|
|
76
81
|
clear: () => void;
|
|
77
82
|
};
|
|
78
83
|
};
|
|
79
|
-
idempotencyRequestor: (config?: {
|
|
80
|
-
key?: (config: UnifiedConfig) => string;
|
|
81
|
-
}) => {
|
|
84
|
+
idempotencyRequestor: (config?: IdempotencyOptions) => {
|
|
82
85
|
requestor: Requestor;
|
|
86
|
+
store: {
|
|
87
|
+
has(key: string): boolean;
|
|
88
|
+
get<T>(key: string): T | undefined;
|
|
89
|
+
set<T>(key: string, value: T): T;
|
|
90
|
+
remove(key: string): void;
|
|
91
|
+
clear(): void;
|
|
92
|
+
} | {
|
|
93
|
+
has: (key: string) => Promise<boolean>;
|
|
94
|
+
get: <T>(key: string) => Promise<T | null>;
|
|
95
|
+
set: <T>(key: string, value: T) => Promise<T>;
|
|
96
|
+
remove: (key: string) => Promise<void>;
|
|
97
|
+
clear: () => Promise<void>;
|
|
98
|
+
} | {
|
|
99
|
+
has: (key: string) => boolean;
|
|
100
|
+
get: (key: string) => any;
|
|
101
|
+
set: <T>(key: string, value: T) => Map<string, any>;
|
|
102
|
+
remove: (key: string) => boolean;
|
|
103
|
+
clear: () => void;
|
|
104
|
+
};
|
|
83
105
|
};
|
|
84
106
|
retryRequestor: (config?: RetryOptions) => {
|
|
85
107
|
requestor: Requestor;
|
package/dist/index.js
CHANGED
|
@@ -32,9 +32,9 @@ function T(t) {
|
|
|
32
32
|
const e = {};
|
|
33
33
|
return Object.keys(k).forEach(
|
|
34
34
|
(r) => {
|
|
35
|
-
e[r] = (...
|
|
36
|
-
const
|
|
37
|
-
return t(
|
|
35
|
+
e[r] = (...o) => {
|
|
36
|
+
const n = k[r](...o);
|
|
37
|
+
return t(n);
|
|
38
38
|
};
|
|
39
39
|
}
|
|
40
40
|
), e;
|
|
@@ -56,18 +56,18 @@ class O {
|
|
|
56
56
|
if (r)
|
|
57
57
|
try {
|
|
58
58
|
return JSON.parse(r);
|
|
59
|
-
} catch (
|
|
60
|
-
console.error("Error parsing cached data",
|
|
59
|
+
} catch (o) {
|
|
60
|
+
console.error("Error parsing cached data", o);
|
|
61
61
|
return;
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
64
|
// 设置缓存
|
|
65
65
|
set(e, r) {
|
|
66
66
|
try {
|
|
67
|
-
const
|
|
68
|
-
localStorage.setItem(e,
|
|
69
|
-
} catch (
|
|
70
|
-
console.error("Error saving data to localStorage",
|
|
67
|
+
const o = JSON.stringify(r);
|
|
68
|
+
localStorage.setItem(e, o);
|
|
69
|
+
} catch (o) {
|
|
70
|
+
console.error("Error saving data to localStorage", o);
|
|
71
71
|
}
|
|
72
72
|
return r;
|
|
73
73
|
}
|
|
@@ -84,27 +84,27 @@ const H = new O(), U = ({ persist: t, name: e, sync: r }) => {
|
|
|
84
84
|
if (t && r)
|
|
85
85
|
return H;
|
|
86
86
|
if (t) {
|
|
87
|
-
const
|
|
87
|
+
const o = E.createInstance({
|
|
88
88
|
name: e
|
|
89
89
|
// driver: sync
|
|
90
90
|
// ?undefined
|
|
91
91
|
// :localforage.LOCALSTORAGE,
|
|
92
92
|
});
|
|
93
93
|
return {
|
|
94
|
-
has: (
|
|
95
|
-
get: (
|
|
96
|
-
set: (
|
|
97
|
-
remove: (
|
|
98
|
-
clear: () =>
|
|
94
|
+
has: (n) => o.keys().then((s) => s.includes(n)),
|
|
95
|
+
get: (n) => o.getItem(n),
|
|
96
|
+
set: (n, s) => o.setItem(n, s),
|
|
97
|
+
remove: (n) => o.removeItem(n),
|
|
98
|
+
clear: () => o.clear()
|
|
99
99
|
};
|
|
100
100
|
} else {
|
|
101
|
-
const
|
|
101
|
+
const o = /* @__PURE__ */ new Map();
|
|
102
102
|
return {
|
|
103
|
-
has: (
|
|
104
|
-
get: (
|
|
105
|
-
set: (
|
|
106
|
-
remove: (
|
|
107
|
-
clear: () =>
|
|
103
|
+
has: (n) => o.has(n),
|
|
104
|
+
get: (n) => o.get(n),
|
|
105
|
+
set: (n, s) => o.set(n, s),
|
|
106
|
+
remove: (n) => o.delete(n),
|
|
107
|
+
clear: () => o.clear()
|
|
108
108
|
};
|
|
109
109
|
}
|
|
110
110
|
}, _ = () => {
|
|
@@ -130,48 +130,48 @@ const H = new O(), U = ({ persist: t, name: e, sync: r }) => {
|
|
|
130
130
|
duration: 1 / 0,
|
|
131
131
|
sync: !1
|
|
132
132
|
}, S = (t) => {
|
|
133
|
-
const e = { ...J, ...t }, { name: r, persist:
|
|
133
|
+
const e = { ...J, ...t }, { name: r, persist: o, sync: n } = e, s = U({ persist: o, name: r, sync: n }), { getPromise: u, setPromise: P, delPromise: g } = _();
|
|
134
134
|
function l(a, h) {
|
|
135
|
-
const { isValid: i } = e,
|
|
135
|
+
const { isValid: i } = e, f = s.get(a);
|
|
136
136
|
let C = !1;
|
|
137
|
-
if (
|
|
137
|
+
if (f && x(f)) {
|
|
138
138
|
try {
|
|
139
139
|
C = (i == null ? void 0 : i({
|
|
140
140
|
key: a,
|
|
141
141
|
config: h,
|
|
142
|
-
cachedData:
|
|
142
|
+
cachedData: f
|
|
143
143
|
})) ?? !0;
|
|
144
144
|
} catch (c) {
|
|
145
145
|
console.error(`校验异常 ${a}`, c);
|
|
146
146
|
}
|
|
147
147
|
!C && s.remove(a);
|
|
148
148
|
}
|
|
149
|
-
return { shouldUseCache: C, cachedData:
|
|
149
|
+
return { shouldUseCache: C, cachedData: f };
|
|
150
150
|
}
|
|
151
151
|
async function d(a, h) {
|
|
152
|
-
const { isValid: i } = e,
|
|
152
|
+
const { isValid: i } = e, f = await s.get(a);
|
|
153
153
|
let C = !1;
|
|
154
|
-
if (
|
|
154
|
+
if (f && x(f)) {
|
|
155
155
|
try {
|
|
156
156
|
C = await (i == null ? void 0 : i({
|
|
157
157
|
key: a,
|
|
158
158
|
config: h,
|
|
159
|
-
cachedData:
|
|
159
|
+
cachedData: f
|
|
160
160
|
})) ?? !0;
|
|
161
161
|
} catch (c) {
|
|
162
162
|
console.error(`校验异常 ${a}`, c);
|
|
163
163
|
}
|
|
164
164
|
!C && s.remove(a);
|
|
165
165
|
}
|
|
166
|
-
return { shouldUseCache: C, cachedData:
|
|
166
|
+
return { shouldUseCache: C, cachedData: f };
|
|
167
167
|
}
|
|
168
168
|
const p = {
|
|
169
169
|
get(a, h) {
|
|
170
|
-
function i(c,
|
|
170
|
+
function i(c, y, ...m) {
|
|
171
171
|
const q = Reflect.apply(a[h], a, m).then(async (w) => {
|
|
172
172
|
const R = typeof e.duration == "number" ? e.duration : e.duration({
|
|
173
173
|
key: c,
|
|
174
|
-
config:
|
|
174
|
+
config: y,
|
|
175
175
|
response: w
|
|
176
176
|
});
|
|
177
177
|
return s.set(c, A(w, R)), w;
|
|
@@ -180,20 +180,20 @@ const H = new O(), U = ({ persist: t, name: e, sync: r }) => {
|
|
|
180
180
|
});
|
|
181
181
|
return P(c, q), q;
|
|
182
182
|
}
|
|
183
|
-
return
|
|
184
|
-
const
|
|
183
|
+
return n ? (...c) => {
|
|
184
|
+
const y = k[h](...c), m = e.key(y), q = u(m);
|
|
185
185
|
if (q)
|
|
186
186
|
return q;
|
|
187
|
-
const { shouldUseCache: w, cachedData: R } = l(m,
|
|
188
|
-
return w ? R.value : i(m,
|
|
187
|
+
const { shouldUseCache: w, cachedData: R } = l(m, y);
|
|
188
|
+
return w ? R.value : i(m, y, ...c);
|
|
189
189
|
} : (...c) => {
|
|
190
|
-
const
|
|
190
|
+
const y = k[h](...c), m = e.key(y), q = u(m);
|
|
191
191
|
if (q)
|
|
192
192
|
return q;
|
|
193
193
|
const w = (async () => {
|
|
194
194
|
try {
|
|
195
|
-
const { shouldUseCache: R, cachedData: D } = await d(m,
|
|
196
|
-
return R ? D.value : await i(m,
|
|
195
|
+
const { shouldUseCache: R, cachedData: D } = await d(m, y);
|
|
196
|
+
return R ? D.value : await i(m, y, ...c);
|
|
197
197
|
} finally {
|
|
198
198
|
g(m);
|
|
199
199
|
}
|
|
@@ -207,31 +207,34 @@ const H = new O(), U = ({ persist: t, name: e, sync: r }) => {
|
|
|
207
207
|
store: s
|
|
208
208
|
};
|
|
209
209
|
}, N = (t) => {
|
|
210
|
-
const { method: e, url: r, params:
|
|
211
|
-
return [e, r, JSON.stringify(
|
|
210
|
+
const { method: e, url: r, params: o, data: n } = t;
|
|
211
|
+
return [e, r, JSON.stringify(o), JSON.stringify(n)].join("|");
|
|
212
212
|
}, b = {
|
|
213
|
-
key: N
|
|
213
|
+
key: N,
|
|
214
|
+
duration: 1e3
|
|
214
215
|
}, F = (t) => {
|
|
215
216
|
const e = { ...b, ...t }, {
|
|
216
|
-
requestor: r
|
|
217
|
+
requestor: r,
|
|
218
|
+
store: o
|
|
217
219
|
} = S({
|
|
218
|
-
|
|
220
|
+
...e,
|
|
219
221
|
persist: !1
|
|
220
222
|
});
|
|
221
223
|
return {
|
|
222
|
-
requestor: r
|
|
224
|
+
requestor: r,
|
|
225
|
+
store: o
|
|
223
226
|
};
|
|
224
227
|
}, L = {
|
|
225
228
|
retries: 3,
|
|
226
229
|
delay: 0,
|
|
227
230
|
retryCondition: () => !0
|
|
228
231
|
}, $ = (t) => {
|
|
229
|
-
const { retries: e, delay: r, retryCondition:
|
|
232
|
+
const { retries: e, delay: r, retryCondition: o } = { ...L, ...t }, n = {
|
|
230
233
|
get(s, u) {
|
|
231
234
|
return (...g) => {
|
|
232
235
|
let l = 0;
|
|
233
236
|
const d = () => Reflect.apply(s[u], s, g).catch((p) => {
|
|
234
|
-
if (l < e &&
|
|
237
|
+
if (l < e && o(p)) {
|
|
235
238
|
l++;
|
|
236
239
|
const a = typeof r == "function" ? r(l) : r;
|
|
237
240
|
return new Promise((h) => {
|
|
@@ -245,7 +248,7 @@ const H = new O(), U = ({ persist: t, name: e, sync: r }) => {
|
|
|
245
248
|
}
|
|
246
249
|
};
|
|
247
250
|
return {
|
|
248
|
-
requestor: new Proxy(v(),
|
|
251
|
+
requestor: new Proxy(v(), n)
|
|
249
252
|
};
|
|
250
253
|
};
|
|
251
254
|
class Q {
|
|
@@ -254,11 +257,11 @@ class Q {
|
|
|
254
257
|
}
|
|
255
258
|
// 加入
|
|
256
259
|
add(e, r) {
|
|
257
|
-
return new Promise((
|
|
260
|
+
return new Promise((o, n) => {
|
|
258
261
|
this.tasks.enqueue(e, {
|
|
259
262
|
task: r,
|
|
260
|
-
resolve:
|
|
261
|
-
reject:
|
|
263
|
+
resolve: o,
|
|
264
|
+
reject: n
|
|
262
265
|
}), this._run();
|
|
263
266
|
});
|
|
264
267
|
}
|
|
@@ -267,8 +270,8 @@ class Q {
|
|
|
267
270
|
this.tasks.remove(e);
|
|
268
271
|
}
|
|
269
272
|
execute(e) {
|
|
270
|
-
const { task: r, resolve:
|
|
271
|
-
return r().then(
|
|
273
|
+
const { task: r, resolve: o, reject: n } = e;
|
|
274
|
+
return r().then(o).catch(n).finally(() => {
|
|
272
275
|
this.runningCount--, this._run();
|
|
273
276
|
});
|
|
274
277
|
}
|
|
@@ -284,10 +287,10 @@ const W = {
|
|
|
284
287
|
retries: 0,
|
|
285
288
|
createId: () => z()
|
|
286
289
|
}, Y = (t) => {
|
|
287
|
-
const e = { ...W, ...t }, { parallelCount: r, createId:
|
|
290
|
+
const e = { ...W, ...t }, { parallelCount: r, createId: o, ...n } = e, s = new Q(r), { requestor: u = null } = n.retries > 0 ? $(n) : {}, P = {
|
|
288
291
|
get(g, l) {
|
|
289
292
|
return (...p) => {
|
|
290
|
-
const a = k[l](...p), h =
|
|
293
|
+
const a = k[l](...p), h = o(a), i = () => u ? Reflect.apply(u[l], u, p) : Reflect.apply(g[l], g, p);
|
|
291
294
|
return s.add(h, i);
|
|
292
295
|
};
|
|
293
296
|
}
|
|
@@ -301,8 +304,8 @@ const W = {
|
|
|
301
304
|
sync: !0
|
|
302
305
|
}, G = (t) => {
|
|
303
306
|
const e = { ...B, ...t }, { ...r } = e, {
|
|
304
|
-
requestor:
|
|
305
|
-
store:
|
|
307
|
+
requestor: o,
|
|
308
|
+
store: n
|
|
306
309
|
} = S(r), s = {
|
|
307
310
|
get(u, P) {
|
|
308
311
|
return (...l) => {
|
|
@@ -318,8 +321,8 @@ const W = {
|
|
|
318
321
|
}
|
|
319
322
|
};
|
|
320
323
|
return {
|
|
321
|
-
requestor: new Proxy(
|
|
322
|
-
store:
|
|
324
|
+
requestor: new Proxy(o, s),
|
|
325
|
+
store: n
|
|
323
326
|
};
|
|
324
327
|
}, K = {
|
|
325
328
|
cacheRequestor: S,
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(m,
|
|
1
|
+
(function(m,S){typeof exports=="object"&&typeof module<"u"?S(exports,require("localforage"),require("id-queue")):typeof define=="function"&&define.amd?define(["exports","localforage","id-queue"],S):(m=typeof globalThis<"u"?globalThis:m||self,S(m.netVertCore={},m.localforage,m.idQueue))})(this,function(m,S,D){"use strict";const k={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(k).forEach(r=>{e[r]=(...n)=>{const o=k[r](...n);return t(o)}}),e}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=S.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,u)=>{t.set(s,u)},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:u,setPromise:R,delPromise:p}=A();function l(a,h){const{isValid:i}=e,y=s.get(a);let q=!1;if(y&&T(y)){try{q=(i==null?void 0:i({key:a,config:h,cachedData:y}))??!0}catch(c){console.error(`校验异常 ${a}`,c)}!q&&s.remove(a)}return{shouldUseCache:q,cachedData:y}}async function d(a,h){const{isValid:i}=e,y=await s.get(a);let q=!1;if(y&&T(y)){try{q=await(i==null?void 0:i({key:a,config:h,cachedData:y}))??!0}catch(c){console.error(`校验异常 ${a}`,c)}!q&&s.remove(a)}return{shouldUseCache:q,cachedData:y}}const C={get(a,h){function i(c,g,...f){const P=Reflect.apply(a[h],a,f).then(async w=>{const v=typeof e.duration=="number"?e.duration:e.duration({key:c,config:g,response:w});return s.set(c,J(w,v)),w}).finally(()=>{p(c)});return R(c,P),P}return o?(...c)=>{const g=k[h](...c),f=e.key(g),P=u(f);if(P)return P;const{shouldUseCache:w,cachedData:v}=l(f,g);return w?v.value:i(f,g,...c)}:(...c)=>{const g=k[h](...c),f=e.key(g),P=u(f);if(P)return P;const w=(async()=>{try{const{shouldUseCache:v,cachedData:G}=await d(f,g);return v?G.value:await i(f,g,...c)}finally{p(f)}})();return R(f,w),w}}};return{requestor:new Proxy(M(),C),store:s}},b={key:t=>{const{method:e,url:r,params:n,data:o}=t;return[e,r,JSON.stringify(n),JSON.stringify(o)].join("|")},duration:1e3},Q=t=>{const e={...b,...t},{requestor:r,store:n}=x({...e,persist:!1});return{requestor:r,store:n}},F={retries:3,delay:0,retryCondition:()=>!0},$=t=>{const{retries:e,delay:r,retryCondition:n}={...F,...t},o={get(s,u){return(...p)=>{let l=0;const d=()=>Reflect.apply(s[u],s,p).catch(C=>{if(l<e&&n(C)){l++;const a=typeof r=="function"?r(l):r;return new Promise(h=>{setTimeout(()=>h(d()),a)})}return Promise.reject(C)});return d()}}};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 V={parallelCount:4,retries:0,createId:()=>O()},W=t=>{const e={...V,...t},{parallelCount:r,createId:n,...o}=e,s=new L(r),{requestor:u=null}=o.retries>0?$(o):{},R={get(p,l){return(...C)=>{const a=k[l](...C),h=n(a),i=()=>u?Reflect.apply(u[l],u,C):Reflect.apply(p[l],p,C);return s.add(h,i)}}};return{requestor:new Proxy(M(),R),concurrentPool:s}},Y={persist:!1,sync:!0},B={cacheRequestor:x,idempotencyRequestor:Q,retryRequestor:$,concurrentPoolRequestor:W,syncRequestor:t=>{const e={...Y,...t},{...r}=e,{requestor:n,store:o}=x(r),s={get(u,R){return(...l)=>{try{const d=Reflect.apply(u[R],u,l);if(d instanceof Promise)throw d;return d}catch(d){throw d}}}};return{requestor:new Proxy(n,s),store:o}}};m.inject=z,m.requestExtender=B,m.useRequestor=M,Object.defineProperty(m,Symbol.toStringTag,{value:"Module"})});
|