@ahoo-wang/fetcher 0.0.2 → 0.0.4
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 +84 -77
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,50 +1,57 @@
|
|
|
1
|
-
# @fetcher
|
|
1
|
+
# @ahoo-wang/fetcher
|
|
2
2
|
|
|
3
|
-
|
|
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
|
+
[](https://bundlephobia.com/package/@ahoo-wang/fetcher)
|
|
4
9
|
|
|
5
|
-
|
|
10
|
+
A modern HTTP client library based on the Fetch API, designed to simplify and optimize interactions with backend RESTful APIs. It provides an Axios-like API with support for path parameters, query parameters, timeout settings, and request/response interceptors.
|
|
6
11
|
|
|
7
|
-
|
|
8
|
-
- **路径和查询参数**: 支持在请求中使用路径参数和查询参数,路径参数使用 `{}` 包裹。
|
|
9
|
-
- **超时设置**: 可以为请求设置超时时间。
|
|
10
|
-
- **请求拦截器**: 支持在请求发送前对请求进行修改。
|
|
11
|
-
- **响应拦截器**: 支持在响应返回后对响应进行处理。
|
|
12
|
-
- **模块化设计**: 代码结构清晰,易于维护和扩展。
|
|
13
|
-
- **TypeScript 支持**: 完整的 TypeScript 类型定义。
|
|
12
|
+
## Features
|
|
14
13
|
|
|
15
|
-
|
|
14
|
+
- **Fetch API Compatible**: Fetcher's API is fully compatible with the native Fetch API, making it easy to get started.
|
|
15
|
+
- **Path and Query Parameters**: Supports path parameters and query parameters in requests, with path parameters wrapped in `{}`.
|
|
16
|
+
- **Timeout Settings**: Request timeout can be configured.
|
|
17
|
+
- **Request Interceptors**: Supports modifying requests before they are sent.
|
|
18
|
+
- **Response Interceptors**: Supports processing responses after they are returned.
|
|
19
|
+
- **Modular Design**: Clear code structure for easy maintenance and extension.
|
|
20
|
+
- **TypeScript Support**: Complete TypeScript type definitions.
|
|
16
21
|
|
|
17
|
-
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
Using pnpm:
|
|
18
25
|
|
|
19
26
|
```bash
|
|
20
|
-
pnpm add @fetcher
|
|
27
|
+
pnpm add @ahoo-wang/fetcher
|
|
21
28
|
```
|
|
22
29
|
|
|
23
|
-
|
|
30
|
+
Using npm:
|
|
24
31
|
|
|
25
32
|
```bash
|
|
26
|
-
npm install @fetcher
|
|
33
|
+
npm install @ahoo-wang/fetcher
|
|
27
34
|
```
|
|
28
35
|
|
|
29
|
-
|
|
36
|
+
Using yarn:
|
|
30
37
|
|
|
31
38
|
```bash
|
|
32
|
-
yarn add @fetcher
|
|
39
|
+
yarn add @ahoo-wang/fetcher
|
|
33
40
|
```
|
|
34
41
|
|
|
35
|
-
##
|
|
42
|
+
## Usage
|
|
36
43
|
|
|
37
|
-
###
|
|
44
|
+
### Basic Usage
|
|
38
45
|
|
|
39
46
|
```typescript
|
|
40
|
-
import { Fetcher } from '@fetcher
|
|
47
|
+
import { Fetcher } from '@ahoo-wang/fetcher';
|
|
41
48
|
|
|
42
49
|
const fetcher = new Fetcher({
|
|
43
50
|
baseURL: 'https://api.example.com',
|
|
44
51
|
timeout: 5000,
|
|
45
52
|
});
|
|
46
53
|
|
|
47
|
-
// GET
|
|
54
|
+
// GET request with path parameters and query parameters
|
|
48
55
|
fetcher
|
|
49
56
|
.get('/users/{id}', {
|
|
50
57
|
pathParams: { id: 123 },
|
|
@@ -57,7 +64,7 @@ fetcher
|
|
|
57
64
|
console.error(error);
|
|
58
65
|
});
|
|
59
66
|
|
|
60
|
-
// POST
|
|
67
|
+
// POST request with JSON body
|
|
61
68
|
fetcher
|
|
62
69
|
.post('/users', {
|
|
63
70
|
body: JSON.stringify({ name: 'John Doe', email: 'john@example.com' }),
|
|
@@ -71,17 +78,17 @@ fetcher
|
|
|
71
78
|
});
|
|
72
79
|
```
|
|
73
80
|
|
|
74
|
-
###
|
|
81
|
+
### Interceptor Usage
|
|
75
82
|
|
|
76
83
|
```typescript
|
|
77
|
-
import { Fetcher } from '@fetcher
|
|
84
|
+
import { Fetcher } from '@ahoo-wang/fetcher';
|
|
78
85
|
|
|
79
86
|
const fetcher = new Fetcher({ baseURL: 'https://api.example.com' });
|
|
80
87
|
|
|
81
|
-
//
|
|
88
|
+
// Add request interceptor
|
|
82
89
|
const requestInterceptorId = fetcher.interceptors.request.use({
|
|
83
90
|
intercept(request) {
|
|
84
|
-
//
|
|
91
|
+
// Modify request configuration, e.g., add auth header
|
|
85
92
|
return {
|
|
86
93
|
...request,
|
|
87
94
|
headers: {
|
|
@@ -92,88 +99,88 @@ const requestInterceptorId = fetcher.interceptors.request.use({
|
|
|
92
99
|
},
|
|
93
100
|
});
|
|
94
101
|
|
|
95
|
-
//
|
|
102
|
+
// Add response interceptor
|
|
96
103
|
const responseInterceptorId = fetcher.interceptors.response.use({
|
|
97
104
|
intercept(response) {
|
|
98
|
-
//
|
|
105
|
+
// Process response data, e.g., parse JSON
|
|
99
106
|
return response;
|
|
100
107
|
},
|
|
101
108
|
});
|
|
102
109
|
|
|
103
|
-
//
|
|
110
|
+
// Add error interceptor
|
|
104
111
|
const errorInterceptorId = fetcher.interceptors.error.use({
|
|
105
112
|
intercept(error) {
|
|
106
|
-
//
|
|
113
|
+
// Handle errors, e.g., log them
|
|
107
114
|
console.error('Request failed:', error);
|
|
108
115
|
return error;
|
|
109
116
|
},
|
|
110
117
|
});
|
|
111
118
|
```
|
|
112
119
|
|
|
113
|
-
## API
|
|
120
|
+
## API Reference
|
|
114
121
|
|
|
115
|
-
### Fetcher
|
|
122
|
+
### Fetcher Class
|
|
116
123
|
|
|
117
|
-
|
|
124
|
+
Core HTTP client class that provides various HTTP methods.
|
|
118
125
|
|
|
119
|
-
####
|
|
126
|
+
#### Constructor
|
|
120
127
|
|
|
121
128
|
```typescript
|
|
122
129
|
new Fetcher(options?: FetcherOptions)
|
|
123
130
|
```
|
|
124
131
|
|
|
125
|
-
|
|
132
|
+
**Parameters:**
|
|
126
133
|
|
|
127
|
-
- `options.baseURL`:
|
|
128
|
-
- `options.timeout`:
|
|
129
|
-
- `options.headers`:
|
|
134
|
+
- `options.baseURL`: Base URL
|
|
135
|
+
- `options.timeout`: Request timeout in milliseconds
|
|
136
|
+
- `options.headers`: Default request headers
|
|
130
137
|
|
|
131
|
-
####
|
|
138
|
+
#### Methods
|
|
132
139
|
|
|
133
|
-
- `fetch(url: string, request?: FetcherRequest): Promise<Response>` -
|
|
134
|
-
- `get(url: string, request?: Omit<FetcherRequest, 'method' | 'body'>): Promise<Response>` - GET
|
|
135
|
-
- `post(url: string, request?: Omit<FetcherRequest, 'method'>): Promise<Response>` - POST
|
|
136
|
-
- `put(url: string, request?: Omit<FetcherRequest, 'method'>): Promise<Response>` - PUT
|
|
137
|
-
- `delete(url: string, request?: Omit<FetcherRequest, 'method'>): Promise<Response>` - DELETE
|
|
138
|
-
- `patch(url: string, request?: Omit<FetcherRequest, 'method'>): Promise<Response>` - PATCH
|
|
139
|
-
- `head(url: string, request?: Omit<FetcherRequest, 'method' | 'body'>): Promise<Response>` - HEAD
|
|
140
|
-
- `options(url: string, request?: Omit<FetcherRequest, 'method' | 'body'>): Promise<Response>` - OPTIONS
|
|
140
|
+
- `fetch(url: string, request?: FetcherRequest): Promise<Response>` - Generic HTTP request method
|
|
141
|
+
- `get(url: string, request?: Omit<FetcherRequest, 'method' | 'body'>): Promise<Response>` - GET request
|
|
142
|
+
- `post(url: string, request?: Omit<FetcherRequest, 'method'>): Promise<Response>` - POST request
|
|
143
|
+
- `put(url: string, request?: Omit<FetcherRequest, 'method'>): Promise<Response>` - PUT request
|
|
144
|
+
- `delete(url: string, request?: Omit<FetcherRequest, 'method'>): Promise<Response>` - DELETE request
|
|
145
|
+
- `patch(url: string, request?: Omit<FetcherRequest, 'method'>): Promise<Response>` - PATCH request
|
|
146
|
+
- `head(url: string, request?: Omit<FetcherRequest, 'method' | 'body'>): Promise<Response>` - HEAD request
|
|
147
|
+
- `options(url: string, request?: Omit<FetcherRequest, 'method' | 'body'>): Promise<Response>` - OPTIONS request
|
|
141
148
|
|
|
142
|
-
### UrlBuilder
|
|
149
|
+
### UrlBuilder Class
|
|
143
150
|
|
|
144
|
-
URL
|
|
151
|
+
URL builder for constructing complete URLs with parameters.
|
|
145
152
|
|
|
146
|
-
####
|
|
153
|
+
#### Methods
|
|
147
154
|
|
|
148
|
-
- `build(path: string, pathParams?: Record<string, any>, queryParams?: Record<string, any>): string` -
|
|
155
|
+
- `build(path: string, pathParams?: Record<string, any>, queryParams?: Record<string, any>): string` - Build complete URL
|
|
149
156
|
|
|
150
|
-
### InterceptorManager
|
|
157
|
+
### InterceptorManager Class
|
|
151
158
|
|
|
152
|
-
|
|
159
|
+
Interceptor manager for managing multiple interceptors of the same type.
|
|
153
160
|
|
|
154
|
-
####
|
|
161
|
+
#### Methods
|
|
155
162
|
|
|
156
|
-
- `use(interceptor: T): number` -
|
|
157
|
-
- `eject(index: number): void` -
|
|
158
|
-
- `clear(): void` -
|
|
159
|
-
- `intercept(data: R): Promise<R>` -
|
|
163
|
+
- `use(interceptor: T): number` - Add interceptor, returns interceptor ID
|
|
164
|
+
- `eject(index: number): void` - Remove interceptor by ID
|
|
165
|
+
- `clear(): void` - Clear all interceptors
|
|
166
|
+
- `intercept(data: R): Promise<R>` - Execute all interceptors sequentially
|
|
160
167
|
|
|
161
|
-
### FetcherInterceptors
|
|
168
|
+
### FetcherInterceptors Class
|
|
162
169
|
|
|
163
|
-
Fetcher
|
|
170
|
+
Fetcher interceptor collection, including request, response, and error interceptor managers.
|
|
164
171
|
|
|
165
|
-
####
|
|
172
|
+
#### Properties
|
|
166
173
|
|
|
167
|
-
- `request: RequestInterceptorManager` -
|
|
168
|
-
- `response: ResponseInterceptorManager` -
|
|
169
|
-
- `error: ErrorInterceptorManager` -
|
|
174
|
+
- `request: RequestInterceptorManager` - Request interceptor manager
|
|
175
|
+
- `response: ResponseInterceptorManager` - Response interceptor manager
|
|
176
|
+
- `error: ErrorInterceptorManager` - Error interceptor manager
|
|
170
177
|
|
|
171
|
-
##
|
|
178
|
+
## Complete Example
|
|
172
179
|
|
|
173
180
|
```typescript
|
|
174
|
-
import { Fetcher } from '@fetcher
|
|
181
|
+
import { Fetcher } from '@ahoo-wang/fetcher';
|
|
175
182
|
|
|
176
|
-
//
|
|
183
|
+
// Create fetcher instance
|
|
177
184
|
const fetcher = new Fetcher({
|
|
178
185
|
baseURL: 'https://api.example.com',
|
|
179
186
|
timeout: 10000,
|
|
@@ -182,7 +189,7 @@ const fetcher = new Fetcher({
|
|
|
182
189
|
},
|
|
183
190
|
});
|
|
184
191
|
|
|
185
|
-
//
|
|
192
|
+
// Add request interceptor - Add auth header
|
|
186
193
|
fetcher.interceptors.request.use({
|
|
187
194
|
intercept(request) {
|
|
188
195
|
return {
|
|
@@ -195,7 +202,7 @@ fetcher.interceptors.request.use({
|
|
|
195
202
|
},
|
|
196
203
|
});
|
|
197
204
|
|
|
198
|
-
//
|
|
205
|
+
// Add response interceptor - Auto-parse JSON
|
|
199
206
|
fetcher.interceptors.response.use({
|
|
200
207
|
intercept(response) {
|
|
201
208
|
if (response.headers.get('content-type')?.includes('application/json')) {
|
|
@@ -207,7 +214,7 @@ fetcher.interceptors.response.use({
|
|
|
207
214
|
},
|
|
208
215
|
});
|
|
209
216
|
|
|
210
|
-
//
|
|
217
|
+
// Add error interceptor - Unified error handling
|
|
211
218
|
fetcher.interceptors.error.use({
|
|
212
219
|
intercept(error) {
|
|
213
220
|
if (error.name === 'FetchTimeoutError') {
|
|
@@ -219,7 +226,7 @@ fetcher.interceptors.error.use({
|
|
|
219
226
|
},
|
|
220
227
|
});
|
|
221
228
|
|
|
222
|
-
//
|
|
229
|
+
// Use fetcher to make requests
|
|
223
230
|
fetcher
|
|
224
231
|
.get('/users/{id}', {
|
|
225
232
|
pathParams: { id: 123 },
|
|
@@ -234,18 +241,18 @@ fetcher
|
|
|
234
241
|
});
|
|
235
242
|
```
|
|
236
243
|
|
|
237
|
-
##
|
|
244
|
+
## Testing
|
|
238
245
|
|
|
239
|
-
|
|
246
|
+
Run tests:
|
|
240
247
|
|
|
241
248
|
```bash
|
|
242
249
|
pnpm test
|
|
243
250
|
```
|
|
244
251
|
|
|
245
|
-
##
|
|
252
|
+
## Contributing
|
|
246
253
|
|
|
247
|
-
|
|
254
|
+
Contributions of any kind are welcome! Please see the [contributing guide](https://github.com/Ahoo-Wang/fetcher/blob/main/CONTRIBUTING.md) for more details.
|
|
248
255
|
|
|
249
|
-
##
|
|
256
|
+
## License
|
|
250
257
|
|
|
251
|
-
|
|
258
|
+
This project is licensed under the [Apache-2.0 License](https://opensource.org/licenses/Apache-2.0).
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,cAAc;IAC7B;;;OAGG;IACH,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,cAAc;IAC7B;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAClC;AAED;;GAEG;AACH,oBAAY,UAAU;IACpB,GAAG,QAAQ;IACX,IAAI,SAAS;IACb,GAAG,QAAQ;IACX,MAAM,WAAW;IACjB,KAAK,UAAU;IACf,IAAI,SAAS;IACb,OAAO,YAAY;CACpB;AAED,oBAAY,YAAY;IACtB,MAAM,WAAW;IACjB,IAAI,SAAS;CACd"}
|