@ahoo-wang/fetcher 0.1.6 → 0.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.zh-CN.md +258 -0
- package/package.json +3 -3
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
# @ahoo-wang/fetcher
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@ahoo-wang/fetcher)
|
|
4
|
+
[](https://github.com/Ahoo-Wang/fetcher/actions)
|
|
5
|
+
[](https://codecov.io/gh/Ahoo-Wang/Fetcher)
|
|
6
|
+
[](https://github.com/Ahoo-Wang/fetcher/blob/main/LICENSE)
|
|
7
|
+
[](https://www.npmjs.com/package/@ahoo-wang/fetcher)
|
|
8
|
+
|
|
9
|
+
一个基于 Fetch API 的现代 HTTP 客户端库,旨在简化和优化与后端 RESTful API 的交互。它提供了类似 Axios 的 API,支持路径参数、查询参数、超时设置和请求/响应拦截器。
|
|
10
|
+
|
|
11
|
+
## 特性
|
|
12
|
+
|
|
13
|
+
- **Fetch API 兼容**:Fetcher 的 API 与原生 Fetch API 完全兼容,易于上手。
|
|
14
|
+
- **路径和查询参数**:支持请求中的路径参数和查询参数,路径参数用 `{}` 包装。
|
|
15
|
+
- **超时设置**:可以配置请求超时。
|
|
16
|
+
- **请求拦截器**:支持在发送请求前修改请求。
|
|
17
|
+
- **响应拦截器**:支持在返回响应后处理响应。
|
|
18
|
+
- **错误拦截器**:支持在请求生命周期中处理错误。
|
|
19
|
+
- **模块化设计**:代码结构清晰,易于维护和扩展。
|
|
20
|
+
- **自动请求体转换**:自动将普通对象转换为 JSON 并设置适当的 Content-Type 头部。
|
|
21
|
+
- **TypeScript 支持**:完整的 TypeScript 类型定义。
|
|
22
|
+
|
|
23
|
+
## 安装
|
|
24
|
+
|
|
25
|
+
使用 pnpm:
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pnpm add @ahoo-wang/fetcher
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
使用 npm:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
npm install @ahoo-wang/fetcher
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
使用 yarn:
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
yarn add @ahoo-wang/fetcher
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## 使用
|
|
44
|
+
|
|
45
|
+
### 基本用法
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
import { Fetcher } from '@ahoo-wang/fetcher';
|
|
49
|
+
|
|
50
|
+
const fetcher = new Fetcher({
|
|
51
|
+
baseURL: 'https://api.example.com',
|
|
52
|
+
timeout: 5000,
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// 带路径参数和查询参数的 GET 请求
|
|
56
|
+
fetcher
|
|
57
|
+
.get('/users/{id}', {
|
|
58
|
+
pathParams: { id: 123 },
|
|
59
|
+
queryParams: { include: 'profile' },
|
|
60
|
+
})
|
|
61
|
+
.then(response => {
|
|
62
|
+
console.log(response.data);
|
|
63
|
+
})
|
|
64
|
+
.catch(error => {
|
|
65
|
+
console.error(error);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// 带 JSON 体的 POST 请求(自动转换为 JSON 字符串)
|
|
69
|
+
fetcher
|
|
70
|
+
.post('/users', {
|
|
71
|
+
body: { name: 'John Doe', email: 'john@example.com' },
|
|
72
|
+
})
|
|
73
|
+
.then(response => {
|
|
74
|
+
console.log(response.data);
|
|
75
|
+
})
|
|
76
|
+
.catch(error => {
|
|
77
|
+
console.error(error);
|
|
78
|
+
});
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### 拦截器用法
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
import { Fetcher } from '@ahoo-wang/fetcher';
|
|
85
|
+
|
|
86
|
+
const fetcher = new Fetcher({ baseURL: 'https://api.example.com' });
|
|
87
|
+
|
|
88
|
+
// 添加请求拦截器
|
|
89
|
+
const requestInterceptorId = fetcher.interceptors.request.use({
|
|
90
|
+
intercept(request) {
|
|
91
|
+
// 修改请求配置,例如添加认证头部
|
|
92
|
+
return {
|
|
93
|
+
...request,
|
|
94
|
+
headers: {
|
|
95
|
+
...request.headers,
|
|
96
|
+
Authorization: 'Bearer token',
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
// 添加响应拦截器
|
|
103
|
+
const responseInterceptorId = fetcher.interceptors.response.use({
|
|
104
|
+
intercept(response) {
|
|
105
|
+
// 处理响应数据,例如解析 JSON
|
|
106
|
+
return response;
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// 添加错误拦截器
|
|
111
|
+
const errorInterceptorId = fetcher.interceptors.error.use({
|
|
112
|
+
intercept(error) {
|
|
113
|
+
// 处理错误,例如记录日志
|
|
114
|
+
console.error('请求失败:', error);
|
|
115
|
+
return error;
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## API 参考
|
|
121
|
+
|
|
122
|
+
### Fetcher 类
|
|
123
|
+
|
|
124
|
+
提供各种 HTTP 方法的核心 HTTP 客户端类。
|
|
125
|
+
|
|
126
|
+
#### 构造函数
|
|
127
|
+
|
|
128
|
+
```typescript
|
|
129
|
+
new Fetcher(options?: FetcherOptions)
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
**参数:**
|
|
133
|
+
|
|
134
|
+
- `options.baseURL`:基础 URL
|
|
135
|
+
- `options.timeout`:以毫秒为单位的请求超时
|
|
136
|
+
- `options.headers`:默认请求头部
|
|
137
|
+
|
|
138
|
+
#### 方法
|
|
139
|
+
|
|
140
|
+
- `fetch(url: string, request?: FetcherRequest): Promise<Response>` - 通用 HTTP 请求方法
|
|
141
|
+
- `get(url: string, request?: Omit<FetcherRequest, 'method' | 'body'>): Promise<Response>` - GET 请求
|
|
142
|
+
- `post(url: string, request?: Omit<FetcherRequest, 'method'>): Promise<Response>` - POST 请求
|
|
143
|
+
- `put(url: string, request?: Omit<FetcherRequest, 'method'>): Promise<Response>` - PUT 请求
|
|
144
|
+
- `delete(url: string, request?: Omit<FetcherRequest, 'method'>): Promise<Response>` - DELETE 请求
|
|
145
|
+
- `patch(url: string, request?: Omit<FetcherRequest, 'method'>): Promise<Response>` - PATCH 请求
|
|
146
|
+
- `head(url: string, request?: Omit<FetcherRequest, 'method' | 'body'>): Promise<Response>` - HEAD 请求
|
|
147
|
+
- `options(url: string, request?: Omit<FetcherRequest, 'method' | 'body'>): Promise<Response>` - OPTIONS 请求
|
|
148
|
+
|
|
149
|
+
### UrlBuilder 类
|
|
150
|
+
|
|
151
|
+
用于构建带参数的完整 URL 的 URL 构建器。
|
|
152
|
+
|
|
153
|
+
#### 方法
|
|
154
|
+
|
|
155
|
+
- `build(path: string, pathParams?: Record<string, any>, queryParams?: Record<string, any>): string` - 构建完整 URL
|
|
156
|
+
|
|
157
|
+
### InterceptorManager 类
|
|
158
|
+
|
|
159
|
+
用于管理同一类型多个拦截器的拦截器管理器。
|
|
160
|
+
|
|
161
|
+
#### 方法
|
|
162
|
+
|
|
163
|
+
- `use(interceptor: T): number` - 添加拦截器,返回拦截器 ID
|
|
164
|
+
- `eject(index: number): void` - 按 ID 移除拦截器
|
|
165
|
+
- `clear(): void` - 清除所有拦截器
|
|
166
|
+
- `intercept(data: R): Promise<R>` - 顺序执行所有拦截器
|
|
167
|
+
|
|
168
|
+
### FetcherInterceptors 类
|
|
169
|
+
|
|
170
|
+
Fetcher 拦截器集合,包括请求、响应和错误拦截器管理器。
|
|
171
|
+
|
|
172
|
+
#### 属性
|
|
173
|
+
|
|
174
|
+
- `request: RequestInterceptorManager` - 请求拦截器管理器
|
|
175
|
+
- `response: ResponseInterceptorManager` - 响应拦截器管理器
|
|
176
|
+
- `error: ErrorInterceptorManager` - 错误拦截器管理器
|
|
177
|
+
|
|
178
|
+
## 完整示例
|
|
179
|
+
|
|
180
|
+
```typescript
|
|
181
|
+
import { Fetcher } from '@ahoo-wang/fetcher';
|
|
182
|
+
|
|
183
|
+
// 创建 fetcher 实例
|
|
184
|
+
const fetcher = new Fetcher({
|
|
185
|
+
baseURL: 'https://api.example.com',
|
|
186
|
+
timeout: 10000,
|
|
187
|
+
headers: {
|
|
188
|
+
'Content-Type': 'application/json',
|
|
189
|
+
},
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
// 添加请求拦截器 - 添加认证头部
|
|
193
|
+
fetcher.interceptors.request.use({
|
|
194
|
+
intercept(request) {
|
|
195
|
+
return {
|
|
196
|
+
...request,
|
|
197
|
+
headers: {
|
|
198
|
+
...request.headers,
|
|
199
|
+
Authorization: 'Bearer ' + getAuthToken(),
|
|
200
|
+
},
|
|
201
|
+
};
|
|
202
|
+
},
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
// 添加响应拦截器 - 自动解析 JSON
|
|
206
|
+
fetcher.interceptors.response.use({
|
|
207
|
+
intercept(response) {
|
|
208
|
+
if (response.headers.get('content-type')?.includes('application/json')) {
|
|
209
|
+
return response.json().then(data => {
|
|
210
|
+
return new Response(JSON.stringify(data), response);
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
return response;
|
|
214
|
+
},
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
// 添加错误拦截器 - 统一错误处理
|
|
218
|
+
fetcher.interceptors.error.use({
|
|
219
|
+
intercept(error) {
|
|
220
|
+
if (error.name === 'FetchTimeoutError') {
|
|
221
|
+
console.error('请求超时:', error.message);
|
|
222
|
+
} else {
|
|
223
|
+
console.error('网络错误:', error.message);
|
|
224
|
+
}
|
|
225
|
+
return error;
|
|
226
|
+
},
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
// 使用 fetcher 发起请求
|
|
230
|
+
fetcher
|
|
231
|
+
.get('/users/{id}', {
|
|
232
|
+
pathParams: { id: 123 },
|
|
233
|
+
queryParams: { include: 'profile,posts' },
|
|
234
|
+
})
|
|
235
|
+
.then(response => response.json())
|
|
236
|
+
.then(data => {
|
|
237
|
+
console.log('用户数据:', data);
|
|
238
|
+
})
|
|
239
|
+
.catch(error => {
|
|
240
|
+
console.error('获取用户失败:', error);
|
|
241
|
+
});
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
## 测试
|
|
245
|
+
|
|
246
|
+
运行测试:
|
|
247
|
+
|
|
248
|
+
```bash
|
|
249
|
+
pnpm test
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
## 贡献
|
|
253
|
+
|
|
254
|
+
欢迎任何形式的贡献!请查看 [贡献指南](https://github.com/Ahoo-Wang/fetcher/blob/main/CONTRIBUTING.md) 了解更多详情。
|
|
255
|
+
|
|
256
|
+
## 许可证
|
|
257
|
+
|
|
258
|
+
本项目采用 [Apache-2.0 许可证](https://opensource.org/licenses/Apache-2.0)。
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ahoo-wang/fetcher",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Core library providing basic HTTP client functionality for Fetcher",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"fetch",
|
|
@@ -24,9 +24,9 @@
|
|
|
24
24
|
"types": "./dist/index.d.ts",
|
|
25
25
|
"exports": {
|
|
26
26
|
".": {
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
27
28
|
"import": "./dist/index.es.js",
|
|
28
|
-
"require": "./dist/index.umd.js"
|
|
29
|
-
"types": "./dist/index.d.ts"
|
|
29
|
+
"require": "./dist/index.umd.js"
|
|
30
30
|
}
|
|
31
31
|
},
|
|
32
32
|
"files": [
|