@net-vert/core 0.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/dist/index.d.ts +66 -0
- package/dist/index.js +171 -0
- package/dist/index.umd.cjs +1 -0
- package/package.json +32 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
declare interface BasicCredentials {
|
|
2
|
+
username: string;
|
|
3
|
+
password: string;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
declare interface EnhancedProgressEvent {
|
|
7
|
+
readonly lengthComputable: boolean;
|
|
8
|
+
readonly loaded: number;
|
|
9
|
+
readonly total: number;
|
|
10
|
+
readonly target?: EventTarget;
|
|
11
|
+
[key: string]: unknown;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export declare const extendRequestor: {
|
|
15
|
+
createCacheRequestor: (config?: {
|
|
16
|
+
key?: (config: UnifiedConfig) => string;
|
|
17
|
+
persist?: boolean;
|
|
18
|
+
cacheTime?: number | (({ config, response }: {
|
|
19
|
+
config: UnifiedConfig;
|
|
20
|
+
response: any;
|
|
21
|
+
}) => number);
|
|
22
|
+
}) => Requestor;
|
|
23
|
+
createIdempotencyRequestor: (genKey?: (config: UnifiedConfig) => string) => Requestor;
|
|
24
|
+
createRetryRequestor: (config?: {
|
|
25
|
+
retries?: number;
|
|
26
|
+
delay?: number | ((attempt: number) => number);
|
|
27
|
+
retryCondition?: (error: any) => boolean;
|
|
28
|
+
}) => Requestor;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export declare const inject: (requestor: UnifiedRequestor, instanceKey?: string) => void;
|
|
32
|
+
|
|
33
|
+
declare interface RequestConfig<D = any> {
|
|
34
|
+
url?: string;
|
|
35
|
+
method?: keyof Requestor;
|
|
36
|
+
baseURL?: string;
|
|
37
|
+
headers?: Record<string, any>;
|
|
38
|
+
params?: any;
|
|
39
|
+
data?: D;
|
|
40
|
+
timeout?: number;
|
|
41
|
+
auth?: BasicCredentials;
|
|
42
|
+
responseType?: ResponseType;
|
|
43
|
+
onUploadProgress?: (progressEvent: EnhancedProgressEvent) => void;
|
|
44
|
+
onDownloadProgress?: (progressEvent: EnhancedProgressEvent) => void;
|
|
45
|
+
validateStatus?: ((status: number) => boolean) | null;
|
|
46
|
+
signal?: AbortSignal;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
declare interface Requestor {
|
|
50
|
+
get<R = any, D = any>(url: string, config?: RequestConfig<D>): Promise<R>;
|
|
51
|
+
post<R = any, D = any>(url: string, data?: D, config?: RequestConfig<D>): Promise<R>;
|
|
52
|
+
delete<R = any, D = any>(url: string, config?: RequestConfig<D>): Promise<R>;
|
|
53
|
+
put<R = any, D = any>(url: string, data?: D, config?: RequestConfig<D>): Promise<R>;
|
|
54
|
+
request<R = any, D = any>(config: UnifiedConfig<D>): Promise<R>;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export declare type UnifiedConfig<D = any> = RequestConfig<D> & {
|
|
58
|
+
url: string;
|
|
59
|
+
method: keyof Requestor;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export declare type UnifiedRequestor = <R = any, D = any>(config: UnifiedConfig<D>) => Promise<R>;
|
|
63
|
+
|
|
64
|
+
export declare const useRequestor: (instanceKey?: string) => Requestor;
|
|
65
|
+
|
|
66
|
+
export { }
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
const y = {
|
|
2
|
+
get: (t, e) => ({
|
|
3
|
+
url: t,
|
|
4
|
+
method: "get",
|
|
5
|
+
...e,
|
|
6
|
+
params: e == null ? void 0 : e.params
|
|
7
|
+
}),
|
|
8
|
+
post: (t, e, r) => ({
|
|
9
|
+
url: t,
|
|
10
|
+
method: "post",
|
|
11
|
+
data: e,
|
|
12
|
+
headers: { "Content-Type": "application/json", ...r == null ? void 0 : r.headers },
|
|
13
|
+
...r
|
|
14
|
+
}),
|
|
15
|
+
delete: (t, e) => ({
|
|
16
|
+
url: t,
|
|
17
|
+
method: "delete",
|
|
18
|
+
...e
|
|
19
|
+
}),
|
|
20
|
+
put: (t, e, r) => ({
|
|
21
|
+
url: t,
|
|
22
|
+
method: "put",
|
|
23
|
+
data: e,
|
|
24
|
+
headers: { "Content-Type": "application/json", ...r == null ? void 0 : r.headers },
|
|
25
|
+
...r
|
|
26
|
+
}),
|
|
27
|
+
request: (t) => t
|
|
28
|
+
};
|
|
29
|
+
function R(t) {
|
|
30
|
+
const e = {};
|
|
31
|
+
return Object.keys(y).forEach(
|
|
32
|
+
(r) => {
|
|
33
|
+
e[r] = (...s) => {
|
|
34
|
+
const a = y[r](...s);
|
|
35
|
+
return t(a);
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
), {
|
|
39
|
+
...e,
|
|
40
|
+
request: t
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
const q = /* @__PURE__ */ new Map(), z = (t, e = "default") => {
|
|
44
|
+
q.set(e, R(t));
|
|
45
|
+
}, f = (t = "default") => {
|
|
46
|
+
const e = q.get(t);
|
|
47
|
+
if (!e) throw new Error(`Requestor实例 ${t} 未注册`);
|
|
48
|
+
return e;
|
|
49
|
+
};
|
|
50
|
+
class x {
|
|
51
|
+
// 是否存在
|
|
52
|
+
has(e) {
|
|
53
|
+
return !!localStorage.getItem(e);
|
|
54
|
+
}
|
|
55
|
+
// 获取缓存
|
|
56
|
+
get(e) {
|
|
57
|
+
const r = localStorage.getItem(e);
|
|
58
|
+
if (r)
|
|
59
|
+
try {
|
|
60
|
+
return JSON.parse(r);
|
|
61
|
+
} catch (s) {
|
|
62
|
+
console.error("Error parsing cached data", s);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
// 设置缓存
|
|
67
|
+
set(e, r) {
|
|
68
|
+
try {
|
|
69
|
+
const s = JSON.stringify(r);
|
|
70
|
+
localStorage.setItem(e, s);
|
|
71
|
+
} catch (s) {
|
|
72
|
+
console.error("Error saving data to localStorage", s);
|
|
73
|
+
}
|
|
74
|
+
return r;
|
|
75
|
+
}
|
|
76
|
+
// 删除缓存
|
|
77
|
+
delete(e) {
|
|
78
|
+
localStorage.removeItem(e);
|
|
79
|
+
}
|
|
80
|
+
// 清空所有缓存
|
|
81
|
+
clear() {
|
|
82
|
+
localStorage.clear();
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const M = new x(), T = /* @__PURE__ */ new Map(), v = (t) => t ? M : T, j = () => {
|
|
86
|
+
const t = /* @__PURE__ */ new Map();
|
|
87
|
+
return {
|
|
88
|
+
getPromise: (o) => t.get(o),
|
|
89
|
+
setPromise: (o, i) => {
|
|
90
|
+
t.set(o, i);
|
|
91
|
+
},
|
|
92
|
+
delPromise: (o) => {
|
|
93
|
+
t.delete(o);
|
|
94
|
+
},
|
|
95
|
+
clearCache: () => {
|
|
96
|
+
t.clear();
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
}, E = {
|
|
100
|
+
key: (t) => t.url,
|
|
101
|
+
persist: !1,
|
|
102
|
+
cacheTime: 1 / 0
|
|
103
|
+
}, P = (t, e) => ({
|
|
104
|
+
value: t,
|
|
105
|
+
expiresAt: Date.now() + e
|
|
106
|
+
}), I = (t) => Date.now() > t.expiresAt, w = (t = {}) => {
|
|
107
|
+
const e = { ...E, ...t }, r = v(e.persist), { getPromise: s, setPromise: a, delPromise: o } = j(), i = {
|
|
108
|
+
get(d, c) {
|
|
109
|
+
return (...l) => {
|
|
110
|
+
const h = y[c](...l), n = e.key(h), g = s(n);
|
|
111
|
+
if (g)
|
|
112
|
+
return console.log(`===> 已存在该请求: ${n}`), g;
|
|
113
|
+
const p = r.get(n);
|
|
114
|
+
if (p && !I(p))
|
|
115
|
+
return p.value;
|
|
116
|
+
{
|
|
117
|
+
const C = Reflect.apply(d[c], d, l).then((u) => (typeof e.cacheTime == "number" ? r.set(n, P(u, e.cacheTime)) : r.set(
|
|
118
|
+
n,
|
|
119
|
+
P(
|
|
120
|
+
u,
|
|
121
|
+
e.cacheTime({ config: h, response: u })
|
|
122
|
+
)
|
|
123
|
+
), console.log("===>cache", "成功", n, u), u)).finally(() => {
|
|
124
|
+
o(n);
|
|
125
|
+
});
|
|
126
|
+
return a(n, C), C;
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
return new Proxy(f(), i);
|
|
132
|
+
}, O = (t) => {
|
|
133
|
+
const { method: e, url: r, params: s, data: a } = t;
|
|
134
|
+
return [e, r, JSON.stringify(s), JSON.stringify(a)].join("|");
|
|
135
|
+
}, k = (t) => w({
|
|
136
|
+
key: (e) => t ? t(e) : O(e),
|
|
137
|
+
persist: !1
|
|
138
|
+
}), J = {
|
|
139
|
+
retries: 3,
|
|
140
|
+
delay: 1e3,
|
|
141
|
+
retryCondition: () => !0
|
|
142
|
+
}, N = (t = {}) => {
|
|
143
|
+
const { retries: e, delay: r, retryCondition: s } = { ...J, ...t }, a = {
|
|
144
|
+
get(o, i) {
|
|
145
|
+
return (...d) => {
|
|
146
|
+
let c = 0;
|
|
147
|
+
const m = () => Reflect.apply(o[i], o, d).catch((l) => {
|
|
148
|
+
if (c < e && s(l)) {
|
|
149
|
+
c++;
|
|
150
|
+
const h = typeof r == "function" ? r(c) : r;
|
|
151
|
+
return new Promise((n) => {
|
|
152
|
+
setTimeout(() => n(m()), h);
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
return Promise.reject(l);
|
|
156
|
+
});
|
|
157
|
+
return m();
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
return new Proxy(f(), a);
|
|
162
|
+
}, A = {
|
|
163
|
+
createCacheRequestor: w,
|
|
164
|
+
createIdempotencyRequestor: k,
|
|
165
|
+
createRetryRequestor: N
|
|
166
|
+
};
|
|
167
|
+
export {
|
|
168
|
+
A as extendRequestor,
|
|
169
|
+
z as inject,
|
|
170
|
+
f as useRequestor
|
|
171
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(a,i){typeof exports=="object"&&typeof module<"u"?i(exports):typeof define=="function"&&define.amd?define(["exports"],i):(a=typeof globalThis<"u"?globalThis:a||self,i(a["net-vert/core"]={}))})(this,function(a){"use strict";const i={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 w(t){const e={};return Object.keys(i).forEach(r=>{e[r]=(...o)=>{const c=i[r](...o);return t(c)}}),{...e,request:t}}const q=new Map,T=(t,e="default")=>{q.set(e,w(t))},y=(t="default")=>{const e=q.get(t);if(!e)throw new Error(`Requestor实例 ${t} 未注册`);return e};class j{has(e){return!!localStorage.getItem(e)}get(e){const r=localStorage.getItem(e);if(r)try{return JSON.parse(r)}catch(o){console.error("Error parsing cached data",o);return}}set(e,r){try{const o=JSON.stringify(r);localStorage.setItem(e,o)}catch(o){console.error("Error saving data to localStorage",o)}return r}delete(e){localStorage.removeItem(e)}clear(){localStorage.clear()}}const v=new j,x=new Map,M=t=>t?v:x,O=()=>{const t=new Map;return{getPromise:s=>t.get(s),setPromise:(s,l)=>{t.set(s,l)},delPromise:s=>{t.delete(s)},clearCache:()=>{t.clear()}}},E={key:t=>t.url,persist:!1,cacheTime:1/0},P=(t,e)=>({value:t,expiresAt:Date.now()+e}),I=t=>Date.now()>t.expiresAt,C=(t={})=>{const e={...E,...t},r=M(e.persist),{getPromise:o,setPromise:c,delPromise:s}=O(),l={get(m,u){return(...d)=>{const p=i[u](...d),n=e.key(p),S=o(n);if(S)return console.log(`===> 已存在该请求: ${n}`),S;const g=r.get(n);if(g&&!I(g))return g.value;{const R=Reflect.apply(m[u],m,d).then(h=>(typeof e.cacheTime=="number"?r.set(n,P(h,e.cacheTime)):r.set(n,P(h,e.cacheTime({config:p,response:h}))),console.log("===>cache","成功",n,h),h)).finally(()=>{s(n)});return c(n,R),R}}}};return new Proxy(y(),l)},b=t=>{const{method:e,url:r,params:o,data:c}=t;return[e,r,JSON.stringify(o),JSON.stringify(c)].join("|")},k=t=>C({key:e=>t?t(e):b(e),persist:!1}),J={retries:3,delay:1e3,retryCondition:()=>!0},N={createCacheRequestor:C,createIdempotencyRequestor:k,createRetryRequestor:(t={})=>{const{retries:e,delay:r,retryCondition:o}={...J,...t},c={get(s,l){return(...m)=>{let u=0;const f=()=>Reflect.apply(s[l],s,m).catch(d=>{if(u<e&&o(d)){u++;const p=typeof r=="function"?r(u):r;return new Promise(n=>{setTimeout(()=>n(f()),p)})}return Promise.reject(d)});return f()}}};return new Proxy(y(),c)}};a.extendRequestor=N,a.inject=T,a.useRequestor=y,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@net-vert/core",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Dependency Inversion Network Library with Type-Safe Injection.",
|
|
5
|
+
"main": "dist/index",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "run-p type-check \"build-only\" --",
|
|
9
|
+
"build-only": "vite build",
|
|
10
|
+
"type-check": "tsconfig.json --composite false"
|
|
11
|
+
},
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"typescript": "~5.7.2"
|
|
14
|
+
},
|
|
15
|
+
"files": ["dist"],
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"keywords": [
|
|
18
|
+
"dependency-injection",
|
|
19
|
+
"di",
|
|
20
|
+
"network-library",
|
|
21
|
+
"adapter-pattern",
|
|
22
|
+
"extensible",
|
|
23
|
+
"http-client",
|
|
24
|
+
"lightweight"
|
|
25
|
+
],
|
|
26
|
+
"author": "yuzinan <1589937631@qq.com>",
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public",
|
|
30
|
+
"registry": "https://registry.npmjs.org/"
|
|
31
|
+
}
|
|
32
|
+
}
|