hook-fetch 2.0.0 → 2.0.1
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.en.md +16 -16
- package/README.md +25 -25
- package/package.json +1 -1
package/README.en.md
CHANGED
|
@@ -27,14 +27,14 @@ pnpm add hook-fetch
|
|
|
27
27
|
import hookFetch from 'hook-fetch';
|
|
28
28
|
|
|
29
29
|
// Make a GET request
|
|
30
|
-
const response = await hookFetch('https://example.com/api/data');
|
|
31
|
-
console.log(response); //
|
|
30
|
+
const response = await hookFetch('https://example.com/api/data').json();
|
|
31
|
+
console.log(response); // Call json() method to parse response data as JSON
|
|
32
32
|
|
|
33
33
|
// Using other HTTP methods
|
|
34
34
|
const postResponse = await hookFetch('https://example.com/api/data', {
|
|
35
35
|
method: 'POST',
|
|
36
36
|
data: { name: 'hook-fetch' }
|
|
37
|
-
});
|
|
37
|
+
}).json();
|
|
38
38
|
```
|
|
39
39
|
|
|
40
40
|
### Creating an Instance
|
|
@@ -50,26 +50,26 @@ const api = hookFetch.create({
|
|
|
50
50
|
});
|
|
51
51
|
|
|
52
52
|
// Use the instance to make requests
|
|
53
|
-
const userData = await api.get('/users/1');
|
|
53
|
+
const userData = await api.get('/users/1').json();
|
|
54
54
|
```
|
|
55
55
|
|
|
56
56
|
### HTTP Request Methods
|
|
57
57
|
|
|
58
58
|
```typescript
|
|
59
59
|
// GET request
|
|
60
|
-
const data = await api.get('/users', { page: 1, limit: 10 });
|
|
60
|
+
const data = await api.get('/users', { page: 1, limit: 10 }).json();
|
|
61
61
|
|
|
62
62
|
// POST request
|
|
63
|
-
const newUser = await api.post('/users', { name: 'John', age: 30 });
|
|
63
|
+
const newUser = await api.post('/users', { name: 'John', age: 30 }).json();
|
|
64
64
|
|
|
65
65
|
// PUT request
|
|
66
|
-
const updatedUser = await api.put('/users/1', { name: 'John Doe' });
|
|
66
|
+
const updatedUser = await api.put('/users/1', { name: 'John Doe' }).json();
|
|
67
67
|
|
|
68
68
|
// PATCH request
|
|
69
|
-
const patchedUser = await api.patch('/users/1', { age: 31 });
|
|
69
|
+
const patchedUser = await api.patch('/users/1', { age: 31 }).json();
|
|
70
70
|
|
|
71
71
|
// DELETE request
|
|
72
|
-
const deleted = await api.delete('/users/1');
|
|
72
|
+
const deleted = await api.delete('/users/1').json();
|
|
73
73
|
|
|
74
74
|
// HEAD request
|
|
75
75
|
const headers = await api.head('/users/1');
|
|
@@ -87,8 +87,8 @@ Hook-Fetch supports various response data handling methods:
|
|
|
87
87
|
```typescript
|
|
88
88
|
const req = hookFetch('https://example.com/api/data');
|
|
89
89
|
|
|
90
|
-
// JSON parsing
|
|
91
|
-
const jsonData = await req;
|
|
90
|
+
// JSON parsing
|
|
91
|
+
const jsonData = await req.json();
|
|
92
92
|
|
|
93
93
|
// Text parsing
|
|
94
94
|
const textData = await req.text();
|
|
@@ -128,7 +128,7 @@ req.abort();
|
|
|
128
128
|
|
|
129
129
|
// Retry the request
|
|
130
130
|
const newReq = req.retry();
|
|
131
|
-
const result = await newReq;
|
|
131
|
+
const result = await newReq.json();
|
|
132
132
|
```
|
|
133
133
|
|
|
134
134
|
### Streaming Data Processing
|
|
@@ -266,7 +266,7 @@ interface User {
|
|
|
266
266
|
}
|
|
267
267
|
|
|
268
268
|
// Use the type in a request
|
|
269
|
-
const res = await request.get<User>('/users/1');
|
|
269
|
+
const res = await request.get<User>('/users/1').json();
|
|
270
270
|
console.log(res.data); // TypeScript provides complete type hints
|
|
271
271
|
```
|
|
272
272
|
|
|
@@ -371,7 +371,7 @@ const YourComponent = defineComponent({
|
|
|
371
371
|
|
|
372
372
|
// Make request
|
|
373
373
|
const fetchData = async () => {
|
|
374
|
-
const response = await request('/users');
|
|
374
|
+
const response = await request('/users').json();
|
|
375
375
|
console.log(response);
|
|
376
376
|
};
|
|
377
377
|
|
|
@@ -429,7 +429,7 @@ const YourComponent = () => {
|
|
|
429
429
|
|
|
430
430
|
// Make request
|
|
431
431
|
const fetchData = async () => {
|
|
432
|
-
const response = await request('/users');
|
|
432
|
+
const response = await request('/users').json();
|
|
433
433
|
console.log(response);
|
|
434
434
|
};
|
|
435
435
|
|
|
@@ -473,7 +473,7 @@ const YourComponent = () => {
|
|
|
473
473
|
|
|
474
474
|
## Notes
|
|
475
475
|
|
|
476
|
-
1. Hook-Fetch
|
|
476
|
+
1. Hook-Fetch requires explicitly calling the `.json()` method to parse JSON responses.
|
|
477
477
|
2. All request methods return Promise objects.
|
|
478
478
|
3. You can retry aborted requests using the `.retry()` method.
|
|
479
479
|
4. Plugins execute in order of priority.
|
package/README.md
CHANGED
|
@@ -27,14 +27,14 @@ pnpm add hook-fetch
|
|
|
27
27
|
import hookFetch from 'hook-fetch';
|
|
28
28
|
|
|
29
29
|
// 发起 GET 请求
|
|
30
|
-
const response = await hookFetch('https://example.com/api/data');
|
|
31
|
-
console.log(response); //
|
|
30
|
+
const response = await hookFetch('https://example.com/api/data').json();
|
|
31
|
+
console.log(response); // 调用 json() 方法解析响应数据为JSON
|
|
32
32
|
|
|
33
33
|
// 使用其他HTTP方法
|
|
34
34
|
const postResponse = await hookFetch('https://example.com/api/data', {
|
|
35
35
|
method: 'POST',
|
|
36
36
|
data: { name: 'hook-fetch' }
|
|
37
|
-
});
|
|
37
|
+
}).json();
|
|
38
38
|
```
|
|
39
39
|
|
|
40
40
|
### 创建实例
|
|
@@ -50,26 +50,26 @@ const api = hookFetch.create({
|
|
|
50
50
|
});
|
|
51
51
|
|
|
52
52
|
// 使用实例发起请求
|
|
53
|
-
const userData = await api.get('/users/1');
|
|
53
|
+
const userData = await api.get('/users/1').json();
|
|
54
54
|
```
|
|
55
55
|
|
|
56
56
|
### HTTP请求方法
|
|
57
57
|
|
|
58
58
|
```typescript
|
|
59
59
|
// GET 请求
|
|
60
|
-
const data = await api.get('/users', { page: 1, limit: 10 });
|
|
60
|
+
const data = await api.get('/users', { page: 1, limit: 10 }).json();
|
|
61
61
|
|
|
62
62
|
// POST 请求
|
|
63
|
-
const newUser = await api.post('/users', { name: 'John', age: 30 });
|
|
63
|
+
const newUser = await api.post('/users', { name: 'John', age: 30 }).json();
|
|
64
64
|
|
|
65
65
|
// PUT 请求
|
|
66
|
-
const updatedUser = await api.put('/users/1', { name: 'John Doe' });
|
|
66
|
+
const updatedUser = await api.put('/users/1', { name: 'John Doe' }).json();
|
|
67
67
|
|
|
68
68
|
// PATCH 请求
|
|
69
|
-
const patchedUser = await api.patch('/users/1', { age: 31 });
|
|
69
|
+
const patchedUser = await api.patch('/users/1', { age: 31 }).json();
|
|
70
70
|
|
|
71
71
|
// DELETE 请求
|
|
72
|
-
const deleted = await api.delete('/users/1');
|
|
72
|
+
const deleted = await api.delete('/users/1').json();
|
|
73
73
|
|
|
74
74
|
// HEAD 请求
|
|
75
75
|
const headers = await api.head('/users/1');
|
|
@@ -87,8 +87,8 @@ Hook-Fetch 支持多种响应数据处理方式:
|
|
|
87
87
|
```typescript
|
|
88
88
|
const req = hookFetch('https://example.com/api/data');
|
|
89
89
|
|
|
90
|
-
// JSON 解析
|
|
91
|
-
const jsonData = await req;
|
|
90
|
+
// JSON 解析
|
|
91
|
+
const jsonData = await req.json();
|
|
92
92
|
|
|
93
93
|
// 文本解析
|
|
94
94
|
const textData = await req.text();
|
|
@@ -128,7 +128,7 @@ req.abort();
|
|
|
128
128
|
|
|
129
129
|
// 重试请求
|
|
130
130
|
const newReq = req.retry();
|
|
131
|
-
const result = await newReq;
|
|
131
|
+
const result = await newReq.json();
|
|
132
132
|
```
|
|
133
133
|
|
|
134
134
|
### 流式数据处理
|
|
@@ -308,21 +308,21 @@ const createRequest = () => {
|
|
|
308
308
|
return {
|
|
309
309
|
// 用户相关接口
|
|
310
310
|
user: {
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
311
|
+
// 获取用户信息
|
|
312
|
+
getInfo: () => request.get('/user/info').json(),
|
|
313
|
+
// 更新用户信息
|
|
314
|
+
updateInfo: (data) => request.put('/user/info', data).json(),
|
|
315
|
+
// 修改密码
|
|
316
|
+
changePassword: (data) => request.post('/user/password', data).json()
|
|
317
317
|
},
|
|
318
318
|
// 订单相关接口
|
|
319
319
|
order: {
|
|
320
320
|
// 获取订单列表
|
|
321
|
-
getList: (params) => request.get('/orders', params),
|
|
321
|
+
getList: (params) => request.get('/orders', params).json(),
|
|
322
322
|
// 创建订单
|
|
323
|
-
create: (data) => request.post('/orders', data),
|
|
323
|
+
create: (data) => request.post('/orders', data).json(),
|
|
324
324
|
// 取消订单
|
|
325
|
-
cancel: (id) => request.post(`/orders/${id}/cancel`)
|
|
325
|
+
cancel: (id) => request.post(`/orders/${id}/cancel`).json()
|
|
326
326
|
}
|
|
327
327
|
};
|
|
328
328
|
};
|
|
@@ -377,7 +377,7 @@ interface User {
|
|
|
377
377
|
}
|
|
378
378
|
|
|
379
379
|
// 在请求中使用类型
|
|
380
|
-
const res = await request.get<User>('/users/1');
|
|
380
|
+
const res = await request.get<User>('/users/1').json();
|
|
381
381
|
console.log(res.data); // TypeScript提供完整类型提示
|
|
382
382
|
```
|
|
383
383
|
|
|
@@ -479,7 +479,7 @@ const YourComponent = defineComponent({
|
|
|
479
479
|
|
|
480
480
|
// 发起请求
|
|
481
481
|
const fetchData = async () => {
|
|
482
|
-
const response = await request('/users');
|
|
482
|
+
const response = await request('/users').json();
|
|
483
483
|
console.log(response);
|
|
484
484
|
};
|
|
485
485
|
|
|
@@ -537,7 +537,7 @@ const YourComponent = () => {
|
|
|
537
537
|
|
|
538
538
|
// 发起请求
|
|
539
539
|
const fetchData = async () => {
|
|
540
|
-
const response = await request('/users');
|
|
540
|
+
const response = await request('/users').json();
|
|
541
541
|
console.log(response);
|
|
542
542
|
};
|
|
543
543
|
|
|
@@ -581,7 +581,7 @@ const YourComponent = () => {
|
|
|
581
581
|
|
|
582
582
|
## 注意事项
|
|
583
583
|
|
|
584
|
-
1. Hook-Fetch
|
|
584
|
+
1. Hook-Fetch 需要显式调用 `.json()` 方法来解析JSON响应
|
|
585
585
|
2. 所有的请求方法都返回Promise对象
|
|
586
586
|
3. 可以通过`.retry()`方法重试已中断的请求
|
|
587
587
|
4. 插件按照优先级顺序执行
|