@net-vert/core 0.3.6 → 0.3.8
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 +141 -141
- package/dist/index.d.ts +23 -4
- package/dist/index.js +96 -94
- package/dist/index.umd.cjs +1 -1
- package/package.json +39 -39
package/README.md
CHANGED
|
@@ -1,142 +1,142 @@
|
|
|
1
|
-
|
|
2
|
-
---
|
|
3
|
-
|
|
4
|
-
# @net-vert/core
|
|
5
|
-
|
|
6
|
-
**轻量级依赖倒置网络请求库,专为扩展和易用而设计。**
|
|
7
|
-
|
|
8
|
-
GitHub 开源仓库 👉 [https://github.com/yvygyyth/net-vert](https://github.com/yvygyyth/net-vert)
|
|
9
|
-
|
|
10
|
-
---
|
|
11
|
-
|
|
12
|
-
## ✨ 核心特性
|
|
13
|
-
|
|
14
|
-
✅ 解耦网络层,按需注入 axios、fetch 或自定义请求器
|
|
15
|
-
✅ 支持缓存、幂等、重试等扩展
|
|
16
|
-
✅ TypeScript 全类型提示,开发更丝滑
|
|
17
|
-
✅ 内置幂等、缓存、重试等扩展
|
|
18
|
-
✅ 零配置上手,API 极简
|
|
19
|
-
|
|
20
|
-
---
|
|
21
|
-
|
|
22
|
-
## 📦 安装
|
|
23
|
-
|
|
24
|
-
```bash
|
|
25
|
-
npm install @net-vert/core
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
---
|
|
29
|
-
|
|
30
|
-
## 🚀 快速上手
|
|
31
|
-
|
|
32
|
-
### 1️⃣ 注入请求器(以 axios 为例)
|
|
33
|
-
|
|
34
|
-
```typescript
|
|
35
|
-
import axios from 'axios';
|
|
36
|
-
import { inject, useRequestor } from '@net-vert/core';
|
|
37
|
-
|
|
38
|
-
const instance = axios.create({ baseURL: '/api', timeout: 60000 });
|
|
39
|
-
const axiosAdapter = (config) => instance.request(config);
|
|
40
|
-
|
|
41
|
-
inject(axiosAdapter); // 注入 axios 实例
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
---
|
|
45
|
-
|
|
46
|
-
### 2️⃣ 发起请求
|
|
47
|
-
|
|
48
|
-
```typescript
|
|
49
|
-
const requestor = useRequestor();
|
|
50
|
-
|
|
51
|
-
requestor.get('/user/info', { params: { id: 1 } }).then(console.log);
|
|
52
|
-
requestor.post('/user/create', { name: 'Alice' }).then(console.log);
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
---
|
|
56
|
-
|
|
57
|
-
## 🛠 扩展能力(requestExtender)
|
|
58
|
-
|
|
59
|
-
### 缓存请求器(cacheRequestor)
|
|
60
|
-
```typescript
|
|
61
|
-
interface CacheConfig {
|
|
62
|
-
key?: (config: UnifiedConfig) => string
|
|
63
|
-
duration?: number | ({ key, config, response }) => number
|
|
64
|
-
persist?: boolean
|
|
65
|
-
sync?: boolean
|
|
66
|
-
isValid?: ({ key, config, cachedData }) => boolean | Promise<boolean>
|
|
67
|
-
}
|
|
68
|
-
```
|
|
69
|
-
- **key**:缓存键生成函数(默认使用URL+参数哈希)
|
|
70
|
-
- **duration**:缓存时长(ms),支持动态计算过期时间
|
|
71
|
-
- **persist**:是否持久化到indexdeeb, localStorage(同步模式只能存储到localStorage)
|
|
72
|
-
- **sync**:是否启用同步模式
|
|
73
|
-
- **isValid**:缓存有效性校验函数(同步模式只能传入同步校验函数)
|
|
74
|
-
|
|
75
|
-
### 幂等请求器(idempotencyRequestor)
|
|
76
|
-
```typescript
|
|
77
|
-
(genKey?: (config) => string)
|
|
78
|
-
```
|
|
79
|
-
- **genKey**:自定义请求唯一标识生成函数(默认使用method+url+参数哈希)
|
|
80
|
-
|
|
81
|
-
### 同步请求器(syncRequestor)
|
|
82
|
-
```typescript
|
|
83
|
-
{
|
|
84
|
-
persist?: false
|
|
85
|
-
sync?: true
|
|
86
|
-
}
|
|
87
|
-
```
|
|
88
|
-
入参与 cacheRequestor 一致,不能采用异步方案,第一次请求自动报错,之后再次请求均可同步获取
|
|
89
|
-
|
|
90
|
-
### 并发池请求器(concurrentPoolRequestor)
|
|
91
|
-
```typescript
|
|
92
|
-
{
|
|
93
|
-
parallelCount?: number;
|
|
94
|
-
createId?: (config: UnifiedConfig) => string
|
|
95
|
-
}
|
|
96
|
-
```
|
|
97
|
-
- **maxConcurrent**:最大并行请求数量,默认为4
|
|
98
|
-
- **createId**:任务唯一标识生成函数(默认使用时间加随机数)
|
|
99
|
-
其他参数继承于重试请求器
|
|
100
|
-
|
|
101
|
-
### 重试请求器(retryRequestor)
|
|
102
|
-
```typescript
|
|
103
|
-
{
|
|
104
|
-
retries?: number
|
|
105
|
-
delay?: number | (attempt: number) => number
|
|
106
|
-
retryCondition?: (error: any) => boolean
|
|
107
|
-
}
|
|
108
|
-
```
|
|
109
|
-
- **retries**:最大重试次数(默认3次)
|
|
110
|
-
- **delay**:重试延迟时间,支持固定数值或动态计算函数
|
|
111
|
-
- **retryCondition**:触发重试的条件判断函数
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
```typescript
|
|
115
|
-
import { requestExtender } from '@net-vert/core';
|
|
116
|
-
```
|
|
117
|
-
|
|
118
|
-
✅ **缓存请求例子**
|
|
119
|
-
```typescript
|
|
120
|
-
const {requestor} = requestExtender.cacheRequestor();
|
|
121
|
-
requestor.get('/user/info', { params: { id: 1 } });
|
|
122
|
-
```
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
## 📤 开源信息
|
|
126
|
-
|
|
127
|
-
- 仓库地址:[https://github.com/yvygyyth/net-vert](https://github.com/yvygyyth/net-vert)
|
|
128
|
-
- 许可证:MIT
|
|
129
|
-
- 支持 Tree-Shaking
|
|
130
|
-
- 无副作用 (`sideEffects: false`)
|
|
131
|
-
|
|
132
|
-
---
|
|
133
|
-
|
|
134
|
-
## 🔥 设计理念
|
|
135
|
-
|
|
136
|
-
- 网络层完全解耦,未来自由扩展
|
|
137
|
-
- 内置强大的请求能力,零上手成本
|
|
138
|
-
- 存储与缓存拆分,保持核心轻量纯粹
|
|
139
|
-
|
|
140
|
-
---
|
|
141
|
-
|
|
1
|
+
|
|
2
|
+
---
|
|
3
|
+
|
|
4
|
+
# @net-vert/core
|
|
5
|
+
|
|
6
|
+
**轻量级依赖倒置网络请求库,专为扩展和易用而设计。**
|
|
7
|
+
|
|
8
|
+
GitHub 开源仓库 👉 [https://github.com/yvygyyth/net-vert](https://github.com/yvygyyth/net-vert)
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## ✨ 核心特性
|
|
13
|
+
|
|
14
|
+
✅ 解耦网络层,按需注入 axios、fetch 或自定义请求器
|
|
15
|
+
✅ 支持缓存、幂等、重试等扩展
|
|
16
|
+
✅ TypeScript 全类型提示,开发更丝滑
|
|
17
|
+
✅ 内置幂等、缓存、重试等扩展
|
|
18
|
+
✅ 零配置上手,API 极简
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## 📦 安装
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install @net-vert/core
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
## 🚀 快速上手
|
|
31
|
+
|
|
32
|
+
### 1️⃣ 注入请求器(以 axios 为例)
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
import axios from 'axios';
|
|
36
|
+
import { inject, useRequestor } from '@net-vert/core';
|
|
37
|
+
|
|
38
|
+
const instance = axios.create({ baseURL: '/api', timeout: 60000 });
|
|
39
|
+
const axiosAdapter = (config) => instance.request(config);
|
|
40
|
+
|
|
41
|
+
inject(axiosAdapter); // 注入 axios 实例
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
---
|
|
45
|
+
|
|
46
|
+
### 2️⃣ 发起请求
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
const requestor = useRequestor();
|
|
50
|
+
|
|
51
|
+
requestor.get('/user/info', { params: { id: 1 } }).then(console.log);
|
|
52
|
+
requestor.post('/user/create', { name: 'Alice' }).then(console.log);
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
---
|
|
56
|
+
|
|
57
|
+
## 🛠 扩展能力(requestExtender)
|
|
58
|
+
|
|
59
|
+
### 缓存请求器(cacheRequestor)
|
|
60
|
+
```typescript
|
|
61
|
+
interface CacheConfig {
|
|
62
|
+
key?: (config: UnifiedConfig) => string
|
|
63
|
+
duration?: number | ({ key, config, response }) => number
|
|
64
|
+
persist?: boolean
|
|
65
|
+
sync?: boolean
|
|
66
|
+
isValid?: ({ key, config, cachedData }) => boolean | Promise<boolean>
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
- **key**:缓存键生成函数(默认使用URL+参数哈希)
|
|
70
|
+
- **duration**:缓存时长(ms),支持动态计算过期时间
|
|
71
|
+
- **persist**:是否持久化到indexdeeb, localStorage(同步模式只能存储到localStorage)
|
|
72
|
+
- **sync**:是否启用同步模式
|
|
73
|
+
- **isValid**:缓存有效性校验函数(同步模式只能传入同步校验函数)
|
|
74
|
+
|
|
75
|
+
### 幂等请求器(idempotencyRequestor)
|
|
76
|
+
```typescript
|
|
77
|
+
(genKey?: (config) => string)
|
|
78
|
+
```
|
|
79
|
+
- **genKey**:自定义请求唯一标识生成函数(默认使用method+url+参数哈希)
|
|
80
|
+
|
|
81
|
+
### 同步请求器(syncRequestor)
|
|
82
|
+
```typescript
|
|
83
|
+
{
|
|
84
|
+
persist?: false
|
|
85
|
+
sync?: true
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
入参与 cacheRequestor 一致,不能采用异步方案,第一次请求自动报错,之后再次请求均可同步获取
|
|
89
|
+
|
|
90
|
+
### 并发池请求器(concurrentPoolRequestor)
|
|
91
|
+
```typescript
|
|
92
|
+
{
|
|
93
|
+
parallelCount?: number;
|
|
94
|
+
createId?: (config: UnifiedConfig) => string
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
- **maxConcurrent**:最大并行请求数量,默认为4
|
|
98
|
+
- **createId**:任务唯一标识生成函数(默认使用时间加随机数)
|
|
99
|
+
其他参数继承于重试请求器
|
|
100
|
+
|
|
101
|
+
### 重试请求器(retryRequestor)
|
|
102
|
+
```typescript
|
|
103
|
+
{
|
|
104
|
+
retries?: number
|
|
105
|
+
delay?: number | (attempt: number) => number
|
|
106
|
+
retryCondition?: (error: any) => boolean
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
- **retries**:最大重试次数(默认3次)
|
|
110
|
+
- **delay**:重试延迟时间,支持固定数值或动态计算函数
|
|
111
|
+
- **retryCondition**:触发重试的条件判断函数
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
import { requestExtender } from '@net-vert/core';
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
✅ **缓存请求例子**
|
|
119
|
+
```typescript
|
|
120
|
+
const {requestor} = requestExtender.cacheRequestor();
|
|
121
|
+
requestor.get('/user/info', { params: { id: 1 } });
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
## 📤 开源信息
|
|
126
|
+
|
|
127
|
+
- 仓库地址:[https://github.com/yvygyyth/net-vert](https://github.com/yvygyyth/net-vert)
|
|
128
|
+
- 许可证:MIT
|
|
129
|
+
- 支持 Tree-Shaking
|
|
130
|
+
- 无副作用 (`sideEffects: false`)
|
|
131
|
+
|
|
132
|
+
---
|
|
133
|
+
|
|
134
|
+
## 🔥 设计理念
|
|
135
|
+
|
|
136
|
+
- 网络层完全解耦,未来自由扩展
|
|
137
|
+
- 内置强大的请求能力,零上手成本
|
|
138
|
+
- 存储与缓存拆分,保持核心轻量纯粹
|
|
139
|
+
|
|
140
|
+
---
|
|
141
|
+
|
|
142
142
|
如果你确定了包名是 `@net-vert/cache`,我可以直接帮你生成一段未来文档里的“缓存插件使用示例”,随时告诉我!
|
package/dist/index.d.ts
CHANGED
|
@@ -94,15 +94,34 @@ export declare const requestExtender: {
|
|
|
94
94
|
sync?: true;
|
|
95
95
|
} & CacheRequestor<false, true>) => {
|
|
96
96
|
requestor: Requestor;
|
|
97
|
+
store: {
|
|
98
|
+
has(key: string): boolean;
|
|
99
|
+
get<T>(key: string): T | undefined;
|
|
100
|
+
set<T>(key: string, value: T): T;
|
|
101
|
+
remove(key: string): void;
|
|
102
|
+
clear(): void;
|
|
103
|
+
} | {
|
|
104
|
+
has: (key: string) => Promise<boolean>;
|
|
105
|
+
get: <T>(key: string) => Promise<T | null>;
|
|
106
|
+
set: <T>(key: string, value: T) => Promise<T>;
|
|
107
|
+
remove: (key: string) => Promise<void>;
|
|
108
|
+
clear: () => Promise<void>;
|
|
109
|
+
} | {
|
|
110
|
+
has: (key: string) => boolean;
|
|
111
|
+
get: (key: string) => any;
|
|
112
|
+
set: <T>(key: string, value: T) => Map<string, any>;
|
|
113
|
+
remove: (key: string) => boolean;
|
|
114
|
+
clear: () => void;
|
|
115
|
+
};
|
|
97
116
|
};
|
|
98
117
|
};
|
|
99
118
|
|
|
100
119
|
export declare interface Requestor {
|
|
101
|
-
get<R = any, D = any>(url: string, config?: WithDynamicProps<RequestConfig<D>>): Promise<R
|
|
102
|
-
post<R = any, D = any>(url: string, data?: D, config?: RequestConfig<D>): Promise<R
|
|
120
|
+
get<R = any, D = any>(url: string, config?: WithDynamicProps<RequestConfig<D>>): Promise<R> | R;
|
|
121
|
+
post<R = any, D = any>(url: string, data?: D, config?: RequestConfig<D>): Promise<R> | R;
|
|
103
122
|
delete<R = any, D = any>(url: string, config?: WithDynamicProps<RequestConfig<D>>): Promise<R>;
|
|
104
|
-
put<R = any, D = any>(url: string, data?: D, config?: WithDynamicProps<RequestConfig<D>>): Promise<R
|
|
105
|
-
request<R = any, D = any>(config: WithDynamicProps<UnifiedConfig<D>>): Promise<R
|
|
123
|
+
put<R = any, D = any>(url: string, data?: D, config?: WithDynamicProps<RequestConfig<D>>): Promise<R> | R;
|
|
124
|
+
request<R = any, D = any>(config: WithDynamicProps<UnifiedConfig<D>>): Promise<R> | R;
|
|
106
125
|
}
|
|
107
126
|
|
|
108
127
|
declare type RetryOptions = {
|
package/dist/index.js
CHANGED
|
@@ -32,9 +32,9 @@ function j(t) {
|
|
|
32
32
|
const e = {};
|
|
33
33
|
return Object.keys(k).forEach(
|
|
34
34
|
(r) => {
|
|
35
|
-
e[r] = (...
|
|
36
|
-
const
|
|
37
|
-
return t(
|
|
35
|
+
e[r] = (...o) => {
|
|
36
|
+
const n = k[r](...o);
|
|
37
|
+
return t(n);
|
|
38
38
|
};
|
|
39
39
|
}
|
|
40
40
|
), {
|
|
@@ -59,18 +59,18 @@ class O {
|
|
|
59
59
|
if (r)
|
|
60
60
|
try {
|
|
61
61
|
return JSON.parse(r);
|
|
62
|
-
} catch (
|
|
63
|
-
console.error("Error parsing cached data",
|
|
62
|
+
} catch (o) {
|
|
63
|
+
console.error("Error parsing cached data", o);
|
|
64
64
|
return;
|
|
65
65
|
}
|
|
66
66
|
}
|
|
67
67
|
// 设置缓存
|
|
68
68
|
set(e, r) {
|
|
69
69
|
try {
|
|
70
|
-
const
|
|
71
|
-
localStorage.setItem(e,
|
|
72
|
-
} catch (
|
|
73
|
-
console.error("Error saving data to localStorage",
|
|
70
|
+
const o = JSON.stringify(r);
|
|
71
|
+
localStorage.setItem(e, o);
|
|
72
|
+
} catch (o) {
|
|
73
|
+
console.error("Error saving data to localStorage", o);
|
|
74
74
|
}
|
|
75
75
|
return r;
|
|
76
76
|
}
|
|
@@ -87,35 +87,35 @@ const T = new O(), H = ({ persist: t, name: e, sync: r }) => {
|
|
|
87
87
|
if (t && r)
|
|
88
88
|
return T;
|
|
89
89
|
if (t) {
|
|
90
|
-
const
|
|
90
|
+
const o = D.createInstance({
|
|
91
91
|
name: e
|
|
92
92
|
// driver: sync
|
|
93
93
|
// ?undefined
|
|
94
94
|
// :localforage.LOCALSTORAGE,
|
|
95
95
|
});
|
|
96
96
|
return {
|
|
97
|
-
has: (
|
|
98
|
-
get: (
|
|
99
|
-
set: (
|
|
100
|
-
remove: (
|
|
101
|
-
clear: () =>
|
|
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()
|
|
102
102
|
};
|
|
103
103
|
} else {
|
|
104
|
-
const
|
|
104
|
+
const o = /* @__PURE__ */ new Map();
|
|
105
105
|
return {
|
|
106
|
-
has: (
|
|
107
|
-
get: (
|
|
108
|
-
set: (
|
|
109
|
-
remove: (
|
|
110
|
-
clear: () =>
|
|
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()
|
|
111
111
|
};
|
|
112
112
|
}
|
|
113
113
|
}, U = () => {
|
|
114
114
|
const t = /* @__PURE__ */ new Map();
|
|
115
115
|
return {
|
|
116
116
|
getPromise: (s) => t.get(s),
|
|
117
|
-
setPromise: (s,
|
|
118
|
-
t.set(s,
|
|
117
|
+
setPromise: (s, u) => {
|
|
118
|
+
t.set(s, u);
|
|
119
119
|
},
|
|
120
120
|
delPromise: (s) => {
|
|
121
121
|
t.delete(s);
|
|
@@ -133,68 +133,68 @@ const T = new O(), H = ({ persist: t, name: e, sync: r }) => {
|
|
|
133
133
|
duration: 1 / 0,
|
|
134
134
|
sync: !1
|
|
135
135
|
}, S = (t) => {
|
|
136
|
-
const e = { ...A, ...t }, { name: r, persist:
|
|
137
|
-
function
|
|
138
|
-
const { isValid:
|
|
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();
|
|
137
|
+
function l(a, h) {
|
|
138
|
+
const { isValid: i } = e, m = s.get(a);
|
|
139
139
|
let g = !1;
|
|
140
|
-
if (
|
|
140
|
+
if (m && x(m)) {
|
|
141
141
|
try {
|
|
142
|
-
g = (
|
|
142
|
+
g = (i == null ? void 0 : i({
|
|
143
143
|
key: a,
|
|
144
|
-
config:
|
|
145
|
-
cachedData:
|
|
144
|
+
config: h,
|
|
145
|
+
cachedData: m
|
|
146
146
|
})) ?? !0;
|
|
147
|
-
} catch (
|
|
148
|
-
console.error(`校验异常 ${a}`,
|
|
147
|
+
} catch (c) {
|
|
148
|
+
console.error(`校验异常 ${a}`, c);
|
|
149
149
|
}
|
|
150
150
|
!g && s.remove(a);
|
|
151
151
|
}
|
|
152
|
-
return { shouldUseCache: g, cachedData:
|
|
152
|
+
return { shouldUseCache: g, cachedData: m };
|
|
153
153
|
}
|
|
154
|
-
async function
|
|
155
|
-
const { isValid:
|
|
154
|
+
async function d(a, h) {
|
|
155
|
+
const { isValid: i } = e, m = await s.get(a);
|
|
156
156
|
let g = !1;
|
|
157
|
-
if (
|
|
157
|
+
if (m && x(m)) {
|
|
158
158
|
try {
|
|
159
|
-
g = await (
|
|
159
|
+
g = await (i == null ? void 0 : i({
|
|
160
160
|
key: a,
|
|
161
|
-
config:
|
|
162
|
-
cachedData:
|
|
161
|
+
config: h,
|
|
162
|
+
cachedData: m
|
|
163
163
|
})) ?? !0;
|
|
164
|
-
} catch (
|
|
165
|
-
console.error(`校验异常 ${a}`,
|
|
164
|
+
} catch (c) {
|
|
165
|
+
console.error(`校验异常 ${a}`, c);
|
|
166
166
|
}
|
|
167
167
|
!g && s.remove(a);
|
|
168
168
|
}
|
|
169
|
-
return { shouldUseCache: g, cachedData:
|
|
169
|
+
return { shouldUseCache: g, cachedData: m };
|
|
170
170
|
}
|
|
171
171
|
const p = {
|
|
172
|
-
get(a,
|
|
173
|
-
function
|
|
174
|
-
const C = Reflect.apply(a[
|
|
172
|
+
get(a, h) {
|
|
173
|
+
function i(c, y, ...f) {
|
|
174
|
+
const C = Reflect.apply(a[h], a, f).then(async (P) => {
|
|
175
175
|
const R = typeof e.duration == "number" ? e.duration : e.duration({
|
|
176
|
-
key:
|
|
177
|
-
config:
|
|
178
|
-
response:
|
|
176
|
+
key: c,
|
|
177
|
+
config: y,
|
|
178
|
+
response: P
|
|
179
179
|
});
|
|
180
|
-
return s.set(
|
|
180
|
+
return s.set(c, _(P, R)), P;
|
|
181
181
|
}).finally(() => {
|
|
182
|
-
|
|
182
|
+
q(c);
|
|
183
183
|
});
|
|
184
|
-
return
|
|
184
|
+
return w(c, C), C;
|
|
185
185
|
}
|
|
186
|
-
return
|
|
187
|
-
const
|
|
186
|
+
return n ? (...c) => {
|
|
187
|
+
const y = k[h](...c), f = e.key(y), C = u(f);
|
|
188
188
|
if (C)
|
|
189
189
|
return C;
|
|
190
|
-
const { shouldUseCache:
|
|
191
|
-
return
|
|
192
|
-
} : async (...
|
|
193
|
-
const
|
|
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
194
|
if (C)
|
|
195
195
|
return C;
|
|
196
|
-
const { shouldUseCache:
|
|
197
|
-
return
|
|
196
|
+
const { shouldUseCache: P, cachedData: R } = await d(f, y);
|
|
197
|
+
return P ? R.value : i(f, y, ...c);
|
|
198
198
|
};
|
|
199
199
|
}
|
|
200
200
|
};
|
|
@@ -203,8 +203,8 @@ const T = new O(), H = ({ persist: t, name: e, sync: r }) => {
|
|
|
203
203
|
store: s
|
|
204
204
|
};
|
|
205
205
|
}, J = (t) => {
|
|
206
|
-
const { method: e, url: r, params:
|
|
207
|
-
return [e, r, JSON.stringify(
|
|
206
|
+
const { method: e, url: r, params: o, data: n } = t;
|
|
207
|
+
return [e, r, JSON.stringify(o), JSON.stringify(n)].join("|");
|
|
208
208
|
}, N = (t) => {
|
|
209
209
|
const {
|
|
210
210
|
requestor: e
|
|
@@ -220,26 +220,26 @@ const T = new O(), H = ({ persist: t, name: e, sync: r }) => {
|
|
|
220
220
|
delay: 0,
|
|
221
221
|
retryCondition: () => !0
|
|
222
222
|
}, $ = (t) => {
|
|
223
|
-
const { retries: e, delay: r, retryCondition:
|
|
224
|
-
get(s,
|
|
225
|
-
return (...
|
|
226
|
-
let
|
|
227
|
-
const
|
|
228
|
-
if (
|
|
229
|
-
|
|
230
|
-
const a = typeof r == "function" ? r(
|
|
231
|
-
return new Promise((
|
|
232
|
-
setTimeout(() => d(
|
|
223
|
+
const { retries: e, delay: r, retryCondition: o } = { ...b, ...t }, n = {
|
|
224
|
+
get(s, u) {
|
|
225
|
+
return (...q) => {
|
|
226
|
+
let l = 0;
|
|
227
|
+
const d = () => Reflect.apply(s[u], s, q).catch((p) => {
|
|
228
|
+
if (l < e && o(p)) {
|
|
229
|
+
l++;
|
|
230
|
+
const a = typeof r == "function" ? r(l) : r;
|
|
231
|
+
return new Promise((h) => {
|
|
232
|
+
setTimeout(() => h(d()), a);
|
|
233
233
|
});
|
|
234
234
|
}
|
|
235
235
|
return Promise.reject(p);
|
|
236
236
|
});
|
|
237
|
-
return
|
|
237
|
+
return d();
|
|
238
238
|
};
|
|
239
239
|
}
|
|
240
240
|
};
|
|
241
241
|
return {
|
|
242
|
-
requestor: new Proxy(v(),
|
|
242
|
+
requestor: new Proxy(v(), n)
|
|
243
243
|
};
|
|
244
244
|
};
|
|
245
245
|
class F {
|
|
@@ -248,11 +248,11 @@ class F {
|
|
|
248
248
|
}
|
|
249
249
|
// 加入
|
|
250
250
|
add(e, r) {
|
|
251
|
-
return new Promise((
|
|
251
|
+
return new Promise((o, n) => {
|
|
252
252
|
this.tasks.enqueue(e, {
|
|
253
253
|
task: r,
|
|
254
|
-
resolve:
|
|
255
|
-
reject:
|
|
254
|
+
resolve: o,
|
|
255
|
+
reject: n
|
|
256
256
|
}), this._run();
|
|
257
257
|
});
|
|
258
258
|
}
|
|
@@ -261,8 +261,8 @@ class F {
|
|
|
261
261
|
this.tasks.remove(e);
|
|
262
262
|
}
|
|
263
263
|
execute(e) {
|
|
264
|
-
const { task: r, resolve:
|
|
265
|
-
return r().then(
|
|
264
|
+
const { task: r, resolve: o, reject: n } = e;
|
|
265
|
+
return r().then(o).catch(n).finally(() => {
|
|
266
266
|
this.runningCount--, this._run();
|
|
267
267
|
});
|
|
268
268
|
}
|
|
@@ -278,16 +278,16 @@ const L = {
|
|
|
278
278
|
retries: 0,
|
|
279
279
|
createId: () => z()
|
|
280
280
|
}, Q = (t) => {
|
|
281
|
-
const e = { ...L, ...t }, { parallelCount: r, createId:
|
|
282
|
-
get(
|
|
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) {
|
|
283
283
|
return (...p) => {
|
|
284
|
-
const a = k[
|
|
285
|
-
return s.add(
|
|
284
|
+
const a = k[l](...p), h = o(a), i = () => u ? Reflect.apply(u[l], u, p) : Reflect.apply(q[l], q, p);
|
|
285
|
+
return s.add(h, i);
|
|
286
286
|
};
|
|
287
287
|
}
|
|
288
288
|
};
|
|
289
289
|
return {
|
|
290
|
-
requestor: new Proxy(v(),
|
|
290
|
+
requestor: new Proxy(v(), w),
|
|
291
291
|
concurrentPool: s
|
|
292
292
|
};
|
|
293
293
|
}, W = {
|
|
@@ -295,23 +295,25 @@ const L = {
|
|
|
295
295
|
sync: !0
|
|
296
296
|
}, Y = (t) => {
|
|
297
297
|
const e = { ...W, ...t }, { ...r } = e, {
|
|
298
|
-
requestor:
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
298
|
+
requestor: o,
|
|
299
|
+
store: n
|
|
300
|
+
} = S(r), s = {
|
|
301
|
+
get(u, w) {
|
|
302
|
+
return (...l) => {
|
|
302
303
|
try {
|
|
303
|
-
const
|
|
304
|
-
if (
|
|
305
|
-
throw
|
|
306
|
-
return
|
|
307
|
-
} catch (
|
|
308
|
-
throw
|
|
304
|
+
const d = Reflect.apply(u[w], u, l);
|
|
305
|
+
if (d instanceof Promise)
|
|
306
|
+
throw d;
|
|
307
|
+
return d;
|
|
308
|
+
} catch (d) {
|
|
309
|
+
throw d;
|
|
309
310
|
}
|
|
310
311
|
};
|
|
311
312
|
}
|
|
312
313
|
};
|
|
313
314
|
return {
|
|
314
|
-
requestor: new Proxy(
|
|
315
|
+
requestor: new Proxy(o, s),
|
|
316
|
+
store: n
|
|
315
317
|
};
|
|
316
318
|
}, X = {
|
|
317
319
|
cacheRequestor: S,
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
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"})});
|
package/package.json
CHANGED
|
@@ -1,39 +1,39 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@net-vert/core",
|
|
3
|
-
"version": "0.3.
|
|
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.3.8",
|
|
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
|
+
}
|