@net-vert/core 0.3.2 → 0.3.3
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 +3 -1
- package/dist/index.js +3 -1
- package/dist/index.umd.cjs +1 -1
- package/package.json +8 -8
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: (genKey?: (config: UnifiedConfig) => string) => {
|
|
80
|
+
requestor: Requestor;
|
|
81
|
+
};
|
|
80
82
|
retryRequestor: (config?: RetryOptions) => {
|
|
81
83
|
requestor: Requestor;
|
|
82
84
|
};
|
package/dist/index.js
CHANGED
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
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
|
|
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{requestor: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"})});
|
package/package.json
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@net-vert/core",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.3",
|
|
4
4
|
"description": "Dependency Inversion Network Library with Type-Safe Injection.",
|
|
5
5
|
"main": "dist/index",
|
|
6
6
|
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "vitest",
|
|
9
|
+
"build": "run-p type-check build-only",
|
|
10
|
+
"build-only": "vite build",
|
|
11
|
+
"type-check": "tsc --noEmit"
|
|
12
|
+
},
|
|
7
13
|
"devDependencies": {
|
|
8
14
|
"typescript": "~5.7.2"
|
|
9
15
|
},
|
|
@@ -28,11 +34,5 @@
|
|
|
28
34
|
"dependencies": {
|
|
29
35
|
"id-queue": "^1.0.9",
|
|
30
36
|
"localforage": "^1.10.0"
|
|
31
|
-
},
|
|
32
|
-
"scripts": {
|
|
33
|
-
"test": "vitest",
|
|
34
|
-
"build": "run-p type-check build-only",
|
|
35
|
-
"build-only": "vite build",
|
|
36
|
-
"type-check": "tsc --noEmit"
|
|
37
37
|
}
|
|
38
|
-
}
|
|
38
|
+
}
|