@ureq/core 0.0.2 → 0.0.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/README.md +114 -0
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# @ureq/core
|
|
2
|
+
|
|
3
|
+
核心请求库,提供统一的请求接口和功能组合。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @ureq/core @ureq/impl-fetch
|
|
9
|
+
# 或
|
|
10
|
+
pnpm add @ureq/core @ureq/impl-fetch
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## 快速开始
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { Request } from '@ureq/core';
|
|
17
|
+
import { FetchRequestor } from '@ureq/impl-fetch';
|
|
18
|
+
|
|
19
|
+
// 创建请求实例
|
|
20
|
+
const request = new Request(new FetchRequestor({
|
|
21
|
+
baseURL: 'https://api.example.com'
|
|
22
|
+
}));
|
|
23
|
+
|
|
24
|
+
// 发起请求
|
|
25
|
+
const data = await request.get('/users');
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## 功能特性
|
|
29
|
+
|
|
30
|
+
- 🎯 **多种实现支持** - 支持 Fetch 和 Axios
|
|
31
|
+
- 🔄 **智能重试** - 自动重试失败的请求
|
|
32
|
+
- 💾 **请求缓存** - 内置缓存机制
|
|
33
|
+
- 🚦 **拦截器** - 请求和响应拦截
|
|
34
|
+
- ⚡ **并发控制** - 限制并发请求数量
|
|
35
|
+
- 🔒 **幂等性保证** - 自动去重相同请求
|
|
36
|
+
- ⏱️ **超时控制** - 灵活的超时配置
|
|
37
|
+
|
|
38
|
+
## 配置选项
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
const request = new Request(
|
|
42
|
+
new FetchRequestor({
|
|
43
|
+
baseURL: 'https://api.example.com'
|
|
44
|
+
}),
|
|
45
|
+
{
|
|
46
|
+
retry: {
|
|
47
|
+
maxRetries: 3,
|
|
48
|
+
retryDelay: 1000
|
|
49
|
+
},
|
|
50
|
+
cache: {
|
|
51
|
+
ttl: 60000
|
|
52
|
+
},
|
|
53
|
+
timeout: {
|
|
54
|
+
timeout: 5000
|
|
55
|
+
},
|
|
56
|
+
parallel: {
|
|
57
|
+
maxConcurrent: 5
|
|
58
|
+
},
|
|
59
|
+
idempotent: {
|
|
60
|
+
dedupeTime: 1000
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
);
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## 拦截器
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
// 请求拦截器
|
|
70
|
+
request.interceptors.addRequestInterceptor({
|
|
71
|
+
onRequest: (config) => {
|
|
72
|
+
config.headers = {
|
|
73
|
+
...config.headers,
|
|
74
|
+
'Authorization': `Bearer ${token}`
|
|
75
|
+
};
|
|
76
|
+
return config;
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// 响应拦截器
|
|
81
|
+
request.interceptors.addResponseInterceptor({
|
|
82
|
+
onResponse: (response) => {
|
|
83
|
+
console.log('Response:', response.status);
|
|
84
|
+
return response;
|
|
85
|
+
},
|
|
86
|
+
onResponseError: (error) => {
|
|
87
|
+
console.error('Error:', error);
|
|
88
|
+
throw error;
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## API
|
|
94
|
+
|
|
95
|
+
### Request 类
|
|
96
|
+
|
|
97
|
+
- `get<T>(url, options?)` - GET 请求
|
|
98
|
+
- `post<T>(url, data?, options?)` - POST 请求
|
|
99
|
+
- `put<T>(url, data?, options?)` - PUT 请求
|
|
100
|
+
- `delete<T>(url, options?)` - DELETE 请求
|
|
101
|
+
- `patch<T>(url, data?, options?)` - PATCH 请求
|
|
102
|
+
|
|
103
|
+
### 拦截器
|
|
104
|
+
|
|
105
|
+
- `interceptors.addRequestInterceptor(interceptor)` - 添加请求拦截器
|
|
106
|
+
- `interceptors.addResponseInterceptor(interceptor)` - 添加响应拦截器
|
|
107
|
+
|
|
108
|
+
## 文档
|
|
109
|
+
|
|
110
|
+
查看完整文档:[https://sunny-117.github.io/ureq](https://sunny-117.github.io/ureq)
|
|
111
|
+
|
|
112
|
+
## License
|
|
113
|
+
|
|
114
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ureq/core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "Universal request library core",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"access": "public"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@ureq/business": "0.0.
|
|
23
|
+
"@ureq/business": "0.0.3"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
26
|
"tsup": "^8.3.6",
|