@net-vert/core 0.5.1 → 1.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/README.md +1044 -67
- package/dist/index.d.ts +322 -134
- package/dist/index.js +659 -299
- package/dist/index.umd.cjs +1 -1
- package/package.json +9 -12
package/dist/index.umd.cjs
CHANGED
|
@@ -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}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"})});
|
|
1
|
+
(function(c,d){typeof exports=="object"&&typeof module<"u"?d(exports,require("id-queue")):typeof define=="function"&&define.amd?define(["exports","id-queue"],d):(c=typeof globalThis<"u"?globalThis:c||self,d(c.netVertCore={},c.idQueue))})(this,function(c,d){"use strict";var me=Object.defineProperty;var ye=(c,d,m)=>d in c?me(c,d,{enumerable:!0,configurable:!0,writable:!0,value:m}):c[d]=m;var h=(c,d,m)=>ye(c,typeof d!="symbol"?d+"":d,m);var m=(r=>(r.GET="get",r.POST="post",r.PUT="put",r.DELETE="delete",r))(m||{}),w=(r=>(r.CACHE="cache",r.RETRY="retry",r.IDEMPOTENT="idempotent",r.CONCURRENT="concurrent",r.SYNC="sync",r))(w||{});const P="default",B={retries:3,delay:0,retryCondition:()=>!0},V=r=>{const e={...B,...r};return Object.assign(async({config:n,next:s})=>{let o=0,a;for(;o<=e.retries;)try{return await s()}catch(i){if(a=i,o===e.retries)throw i;const u={config:n,lastResponse:i,attempt:o};if(!e.retryCondition(u))throw i;o++;const l=typeof e.delay=="function"?e.delay(u):e.delay;l>0&&await new Promise(y=>setTimeout(y,l))}throw a},{__middlewareType:w.RETRY})},K=()=>{const r=new Map;return{getPromise:o=>r.get(o),setPromise:(o,a)=>{r.set(o,a),a.finally(()=>r.delete(o))},delPromise:o=>r.delete(o),clearCache:()=>r.clear()}},_={key:r=>{const{config:e}=r,{method:t,url:n,data:s}=e;return[t,n,JSON.stringify(s)].join("|")}},x=r=>{const e={..._,...r},t=K();return Object.assign(({config:s,next:o})=>{const a=e.key({config:s}),i=t.getPromise(a);if(i)return i;const u=o();return t.setPromise(a,u),u},{__middlewareType:w.IDEMPOTENT,promiseCache:t})};class A{constructor(e=4){h(this,"parallelCount");h(this,"tasks");h(this,"runningCount");this.parallelCount=e,this.tasks=new d.TaskQueue,this.runningCount=0}add(e,t){return new Promise((n,s)=>{this.tasks.enqueue(e,{task:t,resolve:n,reject:s}),this._run()})}remove(e){this.tasks.remove(e)}execute(e){const{task:t,resolve:n,reject:s}=e;return t().then(n).catch(s).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)}}}let J=0;const z={parallelCount:4,createId:()=>J++},O=r=>{const{parallelCount:e,createId:t}={...z,...r},n=new A(e);return Object.assign(({config:o,next:a})=>{const i=t({config:o});return n.add(i,()=>a())},{__middlewareType:w.CONCURRENT,pool:n})},q=new Map,p=(r,e)=>{q.set(e,r)};function G(r){const e=q.get(r);if(!e)throw new Error(`Store实例 ${String(r)} 未注册`);return e}class U{constructor(){h(this,"store",new Map)}getItem(e){return this.store.get(e)}setItem(e,t){return this.store.set(e,t),t}removeItem(e){this.store.delete(e)}clear(){this.store.clear()}length(){return this.store.size}key(e){return Array.from(this.store.keys())[e]}keys(){return Array.from(this.store.keys())}iterate(e){let t=0;for(const[n,s]of this.store.entries())e(s,n,t),t++}}const M=r=>!!r&&(typeof r=="object"||typeof r=="function")&&typeof r.then=="function",j=r=>typeof window<"u"?r():{getItem(){return null},setItem(){},removeItem(){},clear(){},key(){return null},get length(){return 0}},f=j(()=>window.localStorage);class Y{constructor(){}getItem(e){const t=String(e),n=f.getItem(t);if(n!==null)try{return JSON.parse(n)}catch(s){console.error(`Failed to parse value for key: ${t}`,s);return}}setItem(e,t){const n=String(e);try{f.setItem(n,JSON.stringify(t))}catch(s){throw console.error(`Failed to set value for key: ${n}`,s),s}return t}removeItem(e){const t=String(e);f.removeItem(t)}clear(){f.clear()}length(){return f.length}key(e){return f.key(e)}keys(){const e=[];for(let t=0;t<f.length;t++){const n=f.key(t);n&&e.push(n)}return e}iterate(e){this.keys().forEach((t,n)=>{const s=this.getItem(t);s!==void 0&&e(s,t,n)})}}const g=j(()=>window.sessionStorage);class X{constructor(){}getItem(e){const t=String(e),n=g.getItem(t);if(n!==null)try{return JSON.parse(n)}catch(s){console.error(`Failed to parse value for key: ${t}`,s);return}}setItem(e,t){const n=String(e);try{g.setItem(n,JSON.stringify(t))}catch(s){throw console.error(`Failed to set value for key: ${n}`,s),s}return t}removeItem(e){const t=String(e);g.removeItem(t)}clear(){g.clear()}length(){return g.length}key(e){return g.key(e)}keys(){const e=[];for(let t=0;t<g.length;t++){const n=g.key(t);n&&e.push(n)}return e}iterate(e){this.keys().forEach((t,n)=>{const s=this.getItem(t);s!==void 0&&e(s,t,n)})}}class Z{constructor(e,t){h(this,"dbName");h(this,"storeName");h(this,"dbPromise",null);h(this,"DB_VERSION",1);this.dbName=e,this.storeName=t,this.initDB()}initDB(){if(typeof window>"u"||!window.indexedDB){console.warn("IndexedDB is not available");return}this.dbPromise=new Promise((e,t)=>{const n=indexedDB.open(this.dbName,this.DB_VERSION);n.onerror=()=>{t(new Error(`Failed to open database: ${this.dbName}`))},n.onsuccess=()=>{e(n.result)},n.onupgradeneeded=s=>{const o=s.target.result;o.objectStoreNames.contains(this.storeName)||o.createObjectStore(this.storeName)}})}async getDB(){if(!this.dbPromise)throw new Error("IndexedDB is not initialized");return this.dbPromise}async withStore(e,t){const n=await this.getDB();return new Promise((s,o)=>{const a=n.transaction([this.storeName],e).objectStore(this.storeName),i=t(a);i.onsuccess=()=>{s(i.result)},i.onerror=()=>{o(i.error)}})}async getItem(e){try{const t=await this.withStore("readonly",n=>n.get(String(e)));return t===void 0?void 0:t}catch(t){console.error(`Failed to get value for key: ${String(e)}`,t);return}}async setItem(e,t){try{return await this.withStore("readwrite",n=>n.put(t,String(e))),t}catch(n){throw console.error(`Failed to set value for key: ${String(e)}`,n),n}}async removeItem(e){try{await this.withStore("readwrite",t=>t.delete(String(e)))}catch(t){throw console.error(`Failed to remove value for key: ${String(e)}`,t),t}}async clear(){try{await this.withStore("readwrite",e=>e.clear())}catch(e){throw console.error("Failed to clear storage",e),e}}async length(){try{return await this.withStore("readonly",e=>e.count())}catch(e){return console.error("Failed to get storage length",e),0}}async key(e){try{return(await this.withStore("readonly",t=>t.getAllKeys()))[e]}catch(t){console.error("Failed to get key",t);return}}async keys(){try{return await this.withStore("readonly",e=>e.getAllKeys())}catch(e){return console.error("Failed to get all keys",e),[]}}async iterate(e){try{const t=(await this.getDB()).transaction([this.storeName],"readonly").objectStore(this.storeName).openCursor();return new Promise((n,s)=>{let o=0;t.onsuccess=a=>{const i=a.target.result;if(i){const u=i.key,l=i.value;e(l,u,o),o++,i.continue()}else n()},t.onerror=()=>{s(t.error)}})}catch(t){console.error("Failed to iterate storage",t)}}}const C={memory:"memory",local:"local",session:"session",indexeddb:"indexeddb"};function R(r,e){Object.assign(r,e)}function H(r){return R(r,{async getItemOrDefault(e,t){const n=await this.getItem(e);return n!==null?n:t},async hasItem(e){return await this.getItem(e)!==null},async removeItems(e){await Promise.all(e.map(t=>this.removeItem(t)))},async getItems(e){return await Promise.all(e.map(t=>this.getItem(t)))}}),r}function L(r){return R(r,{getItemOrDefault(e,t){const n=this.getItem(e);return n!==null?n:t},hasItem(e){return this.getItem(e)!==null},removeItems(e){e.forEach(t=>this.removeItem(t))},getItems(e){return e.map(t=>this.getItem(t))}}),r}function Q(r){const e=r.getItem("");return M(e)?H(r):L(r)}function I(r,...e){const t=G(r);let n;try{n=new t(...e)}catch{n=t(...e)}return Q(n)}p(U,C.memory),p(Y,C.local),p(X,C.session),p(Z,C.indexeddb);function k(r,e){return{value:r,expireAt:Date.now()+e}}function S(r,e=Date.now()){return r.expireAt<=e}function T(r,e=Date.now()){return S(r,e)?void 0:r.value}const W=r=>{const{config:e}=r,{method:t,url:n,data:s}=e;return[t,n,JSON.stringify(s)].join("|")},ee=()=>!0,b=24*60*60*1e3,te={key:W,duration:b,isValid:ee,store:C.memory},re=class{constructor(e){h(this,"store");return typeof e=="string"?this.store=I(e):(p(e.factory,e.key),this.store=I(e.key)),new Proxy(this,{get(t,n){if(n in t)return t[n];const s=t.store[n];return typeof s=="function"?s.bind(t.store):s}})}setCache(e,t,n=b){const s=k(t,n);this.store.setItem(e,s)}async getCache(e){const t=await this.store.getItem(e);if(t)return T(t)}async hasValidCache(e){const t=await this.store.getItem(e);return t?!S(t):!1}},D=r=>{const e={...te,...r},t=new re(e.store),n=o=>typeof e.duration=="function"?e.duration(o):e.duration;return Object.assign(async({config:o,next:a})=>{const i=e.key({config:o}),u=await t.getCache(i);if(u){if(await e.isValid({key:i,config:o,cachedData:u}))return u;t.removeItem(i)}const l=await a(),y=n({key:i,config:o,cachedData:u,response:l}),N=k(l,y);return t.setItem(i,N),l},{__middlewareType:w.CACHE,storage:t})};class ne{constructor(e){h(this,"store");return typeof e=="string"?this.store=I(e):(p(e.factory,e.key),this.store=I(e.key)),new Proxy(this,{get(t,n){if(n in t)return t[n];const s=t.store[n];return typeof s=="function"?s.bind(t.store):s}})}setCache(e,t,n=b){const s=k(t,n);this.store.setItem(e,s)}getCache(e){const t=this.store.getItem(e);if(t)return T(t)}hasValidCache(e){const t=this.store.getItem(e);return t?!S(t):!1}}const se=ne,oe={suspense:!0,key:r=>{const{config:e}=r,{method:t,url:n}=e;return[t,n].join("|")},duration:24*60*60*1e3,isValid:()=>!0,store:C.memory};function ie(r){const e={...oe,...r},t=new se(e.store),n=o=>typeof e.duration=="function"?e.duration(o):e.duration;return Object.assign(({config:o,next:a})=>{const i=e.key({config:o}),u=t.getCache(i);if(u){if(e.isValid({key:i,config:o,cachedData:u}))return u;t.removeItem(i)}if(e.suspense)throw(e.wrapSuspense?e.wrapSuspense({key:i,config:o,p:a()}):a()).then(y=>{const N=n({key:i,config:o,cachedData:u,response:y});return t.setCache(i,y,N),y});return a().then(l=>{const y=n({key:i,config:o,cachedData:u,response:l});return t.setCache(i,l,y),l})},{__middlewareType:w.SYNC})}const F=new Map,ae=(r,e=P)=>{F.set(e,r)},$=(r=P)=>{const e=F.get(r);if(!e)throw new Error(`Requestor实例 ${String(r)} 未注册`);return e},E={get:(r,e)=>({url:r,method:m.GET,...e,params:e==null?void 0:e.params}),post:(r,e,t)=>({url:r,method:m.POST,data:e,...t}),delete:(r,e)=>({url:r,method:m.DELETE,...e}),put:(r,e,t)=>({url:r,method:m.PUT,data:e,...t}),request:r=>r},ce=Object.keys(E);function ue(r,e,t){const n={},s=o=>{if(o===e.length)return t(r);const a=e[o];return a({config:r,ctx:n,next:()=>s(o+1)})};return s(0)}function le(r,e){const t={},n=e??[];return ce.forEach(s=>{t[s]=(...o)=>{const a=E[s](...o);return ue(a,n,r)}}),t}function v(r){const{extensions:e,instanceKey:t}=r??{},n=$(t);return le(n,e)}function de(r){const{instanceKey:e,key:t,duration:n,isValid:s,store:o,...a}=r;return v({instanceKey:e,extensions:[x(a),D({key:t,duration:n,isValid:s,store:o})]})}function he(r){const{instanceKey:e,parallelCount:t,createId:n,retries:s,delay:o,retryCondition:a}=r;return v({instanceKey:e,extensions:[O({parallelCount:t,createId:n}),V({retries:s,delay:o,retryCondition:a})]})}c.cache=D,c.concurrent=O,c.createCachedIdempotentRequestor=de,c.createConcurrentRetryRequestor=he,c.createRequestor=v,c.idempotent=x,c.inject=ae,c.retry=V,c.sync=ie,c.useRequestor=$,Object.defineProperty(c,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@net-vert/core",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Dependency Inversion Network Library with Type-Safe Injection.",
|
|
5
5
|
"main": "dist/index",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
8
|
-
"
|
|
9
|
-
"
|
|
8
|
+
"scripts": {
|
|
9
|
+
"test": "vitest run",
|
|
10
|
+
"build": "run-p type-check build-only",
|
|
11
|
+
"build-only": "vite build",
|
|
12
|
+
"type-check": "tsc --noEmit"
|
|
10
13
|
},
|
|
11
14
|
"files": [
|
|
12
15
|
"dist"
|
|
@@ -27,13 +30,7 @@
|
|
|
27
30
|
"access": "public"
|
|
28
31
|
},
|
|
29
32
|
"dependencies": {
|
|
30
|
-
"id-queue": "^1.
|
|
31
|
-
"
|
|
32
|
-
},
|
|
33
|
-
"scripts": {
|
|
34
|
-
"test": "vitest",
|
|
35
|
-
"build": "run-p type-check build-only",
|
|
36
|
-
"build-only": "vite build",
|
|
37
|
-
"type-check": "tsc --noEmit"
|
|
33
|
+
"id-queue": "^1.1.1",
|
|
34
|
+
"store-vert": "^0.1.2"
|
|
38
35
|
}
|
|
39
|
-
}
|
|
36
|
+
}
|