@net-vert/core 0.5.0 → 1.0.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 +437 -115
- package/dist/index.js +431 -297
- package/dist/index.umd.cjs +1 -1
- package/package.json +10 -10
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(c,h){typeof exports=="object"&&typeof module<"u"?h(exports,require("id-queue")):typeof define=="function"&&define.amd?define(["exports","id-queue"],h):(c=typeof globalThis<"u"?globalThis:c||self,h(c.netVertCore={},c.idQueue))})(this,function(c,h){"use strict";var W=Object.defineProperty;var ee=(c,h,p)=>h in c?W(c,h,{enumerable:!0,configurable:!0,writable:!0,value:p}):c[h]=p;var u=(c,h,p)=>ee(c,typeof h!="symbol"?h+"":h,p);var p=(r=>(r.GET="get",r.POST="post",r.PUT="put",r.DELETE="delete",r))(p||{}),w=(r=>(r.CACHE="cache",r.RETRY="retry",r.IDEMPOTENT="idempotent",r.CONCURRENT="concurrent",r.SYNC="sync",r))(w||{});const q="default",N={retries:3,delay:0,retryCondition:()=>!0},S=r=>{const e={...N,...r};return Object.assign(async({config:s,next:n})=>{let o=0,i;for(;o<=e.retries;)try{return await n()}catch(a){if(i=a,o===e.retries)throw a;const l={config:s,lastResponse:a,attempt:o};if(!e.retryCondition(l))throw a;o++;const m=typeof e.delay=="function"?e.delay(l):e.delay;m>0&&await new Promise(f=>setTimeout(f,m))}throw i},{__middlewareType:w.RETRY})},b=()=>{const r=new Map;return{getPromise:o=>r.get(o),setPromise:(o,i)=>{r.set(o,i),i.finally(()=>r.delete(o))},delPromise:o=>r.delete(o),clearCache:()=>r.clear()}},z={key:r=>{const{config:e}=r,{method:t,url:s,data:n}=e;return[t,s,JSON.stringify(n)].join("|")}},T=r=>{const e={...z,...r},t=b();return Object.assign(({config:n,next:o})=>{const i=e.key({config:n}),a=t.getPromise(i);if(a)return a;const l=o();return t.setPromise(i,l),l},{__middlewareType:w.IDEMPOTENT,promiseCache:t})};class _{constructor(e=4){u(this,"parallelCount");u(this,"tasks");u(this,"runningCount");this.parallelCount=e,this.tasks=new h.TaskQueue,this.runningCount=0}add(e,t){return new Promise((s,n)=>{this.tasks.enqueue(e,{task:t,resolve:s,reject:n}),this._run()})}remove(e){this.tasks.remove(e)}execute(e){const{task:t,resolve:s,reject:n}=e;return t().then(s).catch(n).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 $=0;const J={parallelCount:4,createId:()=>$++},R=r=>{const{parallelCount:e,createId:t}={...J,...r},s=new _(e);return Object.assign(({config:o,next:i})=>{const a=t({config:o});return s.add(a,()=>i())},{__middlewareType:w.CONCURRENT,pool:s})};class E{constructor(){u(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}keys(){return Array.from(this.store.keys())}iterate(e){let t=0;for(const[s,n]of this.store.entries())e(n,s,t),t++}}const y=typeof window<"u"?window.localStorage:{};class P{constructor(){}getItem(e){const t=String(e),s=y.getItem(t);if(s!==null)try{return JSON.parse(s)}catch(n){console.error(`Failed to parse value for key: ${t}`,n);return}}setItem(e,t){const s=String(e);try{y.setItem(s,JSON.stringify(t))}catch(n){throw console.error(`Failed to set value for key: ${s}`,n),n}return t}removeItem(e){const t=String(e);y.removeItem(t)}clear(){y.clear()}length(){return y.length}keys(){const e=[];for(let t=0;t<y.length;t++){const s=y.key(t);s&&e.push(s)}return e}iterate(e){this.keys().forEach((s,n)=>{const o=this.getItem(s);o!==void 0&&e(o,s,n)})}}function I(r,e){return{value:r,expireAt:Date.now()+e}}function g(r,e=Date.now()){return r.expireAt<=e}function C(r){return r.value}const A=r=>{const{config:e}=r,{method:t,url:s,data:n}=e;return[t,s,JSON.stringify(n)].join("|")},F=()=>!0,j=24*60*60*1e3,G={key:A,duration:j,isValid:F,persist:!1};class d{get(e){const t=this.getItem(e);if(t){if(g(t)){this.removeItem(e);return}return C(t)}}set(e,t,s=j){const n=I(t,s);this.setItem(e,n)}delete(e){this.removeItem(e)}has(e){const t=this.getItem(e);return t?g(t)?(this.removeItem(e),!1):!0:!1}size(){return this.length()}forEach(e){let t=0;this.iterate((s,n,o)=>{if(g(s))this.removeItem(n);else{const i=C(s);e(i,n,t),t++}})}clearExpired(){let e=0;return this.keys().forEach(s=>{const n=this.getItem(s);n&&g(n)&&(this.removeItem(s),e++)}),e}touch(e,t){const s=this.getItem(e);if(!s)return!1;if(g(s))return this.removeItem(e),!1;const n=C(s);return this.set(e,n,t),!0}}class D extends E{constructor(){super(...arguments);u(this,"get",d.prototype.get);u(this,"set",d.prototype.set);u(this,"delete",d.prototype.delete);u(this,"has",d.prototype.has);u(this,"size",d.prototype.size);u(this,"forEach",d.prototype.forEach);u(this,"clearExpired",d.prototype.clearExpired);u(this,"touch",d.prototype.touch)}}class M extends P{constructor(){super(...arguments);u(this,"get",d.prototype.get);u(this,"set",d.prototype.set);u(this,"delete",d.prototype.delete);u(this,"has",d.prototype.has);u(this,"size",d.prototype.size);u(this,"forEach",d.prototype.forEach);u(this,"clearExpired",d.prototype.clearExpired);u(this,"touch",d.prototype.touch)}}const O=r=>{const e={...G,...r},t=e.persist?new M:new D,s=o=>typeof e.duration=="function"?e.duration(o):e.duration;return Object.assign(async({config:o,next:i})=>{const a=e.key({config:o}),l=t.getItem(a);if(l){if(!g(l)&&await e.isValid({key:a,config:o,cachedData:l}))return C(l);t.removeItem(a)}const m=await i(),f=s({key:a,config:o,cachedData:l,response:m}),k=I(m,f);return t.setItem(a,k),m},{__middlewareType:w.CACHE,storage:t})},U={suspense:!0,key:r=>{const{config:e}=r,{method:t,url:s}=e;return[t,s].join("|")},duration:24*60*60*1e3,isValid:()=>!0,persist:!1};function B(r){const e={...U,...r},t=e.persist?new P:new E,s=o=>typeof e.duration=="function"?e.duration(o):e.duration;return Object.assign(({config:o,next:i})=>{const a=e.key({config:o}),l=t.getItem(a);if(l){if(!g(l)&&e.isValid({key:a,config:o,cachedData:l}))return C(l);t.removeItem(a)}if(e.suspense)throw(e.wrapSuspense?e.wrapSuspense({key:a,config:o,p:i()}):i()).then(f=>{const k=s({key:a,config:o,cachedData:l,response:f});return t.setItem(a,I(f,k)),f});return i().then(m=>{const f=s({key:a,config:o,cachedData:l,response:m});return t.setItem(a,I(m,f)),m})},{__middlewareType:w.SYNC})}const x=new Map,L=(r,e=q)=>{x.set(e,r)},V=(r=q)=>{const e=x.get(r);if(!e)throw new Error(`Requestor实例 ${String(r)} 未注册`);return e},K={get:(r,e)=>({url:r,method:p.GET,...e,params:e==null?void 0:e.params}),post:(r,e,t)=>({url:r,method:p.POST,data:e,...t}),delete:(r,e)=>({url:r,method:p.DELETE,...e}),put:(r,e,t)=>({url:r,method:p.PUT,data:e,...t}),request:r=>r},Y=Object.keys(K);function X(r,e,t){const s={},n=o=>{if(o===e.length)return t(r);const i=e[o];return i({config:r,ctx:s,next:()=>n(o+1)})};return n(0)}function Z(r,e){const t={},s=e??[];return Y.forEach(n=>{t[n]=(...o)=>{const i=K[n](...o);return X(i,s,r)}}),t}function v(r){const{extensions:e,instanceKey:t}=r??{},s=V(t);return Z(s,e)}function H(r){const{instanceKey:e,key:t,duration:s,isValid:n,persist:o,...i}=r;return v({instanceKey:e,extensions:[T(i),O({key:t,duration:s,isValid:n,persist:o})]})}function Q(r){const{instanceKey:e,parallelCount:t,createId:s,retries:n,delay:o,retryCondition:i}=r;return v({instanceKey:e,extensions:[R({parallelCount:t,createId:s}),S({retries:n,delay:o,retryCondition:i})]})}c.cache=O,c.concurrent=R,c.createCachedIdempotentRequestor=H,c.createConcurrentRetryRequestor=Q,c.createRequestor=v,c.idempotent=T,c.inject=L,c.retry=S,c.sync=B,c.useRequestor=V,Object.defineProperty(c,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@net-vert/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.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
|
+
"scripts": {
|
|
9
|
+
"test": "vitest run",
|
|
10
|
+
"build": "run-p type-check build-only",
|
|
11
|
+
"build-only": "vite build",
|
|
12
|
+
"type-check": "tsc --noEmit"
|
|
13
|
+
},
|
|
8
14
|
"devDependencies": {
|
|
9
|
-
"typescript": "^5.
|
|
15
|
+
"typescript": "^5.9.3"
|
|
10
16
|
},
|
|
11
17
|
"files": [
|
|
12
18
|
"dist"
|
|
@@ -27,13 +33,7 @@
|
|
|
27
33
|
"access": "public"
|
|
28
34
|
},
|
|
29
35
|
"dependencies": {
|
|
30
|
-
"id-queue": "^1.
|
|
36
|
+
"id-queue": "^1.1.1",
|
|
31
37
|
"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
38
|
}
|
|
39
|
-
}
|
|
39
|
+
}
|