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