@ahoo-wang/fetcher 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 +77 -77
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,50 +1,50 @@
|
|
|
1
|
-
# @fetcher
|
|
1
|
+
# @ahoo-wang/fetcher
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
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.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Features
|
|
6
6
|
|
|
7
|
-
-
|
|
8
|
-
-
|
|
9
|
-
-
|
|
10
|
-
-
|
|
11
|
-
-
|
|
12
|
-
-
|
|
13
|
-
- **TypeScript
|
|
7
|
+
- **Fetch API Compatible**: Fetcher's API is fully compatible with the native Fetch API, making it easy to get started.
|
|
8
|
+
- **Path and Query Parameters**: Supports path parameters and query parameters in requests, with path parameters wrapped in `{}`.
|
|
9
|
+
- **Timeout Settings**: Request timeout can be configured.
|
|
10
|
+
- **Request Interceptors**: Supports modifying requests before they are sent.
|
|
11
|
+
- **Response Interceptors**: Supports processing responses after they are returned.
|
|
12
|
+
- **Modular Design**: Clear code structure for easy maintenance and extension.
|
|
13
|
+
- **TypeScript Support**: Complete TypeScript type definitions.
|
|
14
14
|
|
|
15
|
-
##
|
|
15
|
+
## Installation
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
Using pnpm:
|
|
18
18
|
|
|
19
19
|
```bash
|
|
20
|
-
pnpm add @fetcher
|
|
20
|
+
pnpm add @ahoo-wang/fetcher
|
|
21
21
|
```
|
|
22
22
|
|
|
23
|
-
|
|
23
|
+
Using npm:
|
|
24
24
|
|
|
25
25
|
```bash
|
|
26
|
-
npm install @fetcher
|
|
26
|
+
npm install @ahoo-wang/fetcher
|
|
27
27
|
```
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
Using yarn:
|
|
30
30
|
|
|
31
31
|
```bash
|
|
32
|
-
yarn add @fetcher
|
|
32
|
+
yarn add @ahoo-wang/fetcher
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
-
##
|
|
35
|
+
## Usage
|
|
36
36
|
|
|
37
|
-
###
|
|
37
|
+
### Basic Usage
|
|
38
38
|
|
|
39
39
|
```typescript
|
|
40
|
-
import { Fetcher } from '@fetcher
|
|
40
|
+
import { Fetcher } from '@ahoo-wang/fetcher';
|
|
41
41
|
|
|
42
42
|
const fetcher = new Fetcher({
|
|
43
43
|
baseURL: 'https://api.example.com',
|
|
44
44
|
timeout: 5000,
|
|
45
45
|
});
|
|
46
46
|
|
|
47
|
-
// GET
|
|
47
|
+
// GET request with path parameters and query parameters
|
|
48
48
|
fetcher
|
|
49
49
|
.get('/users/{id}', {
|
|
50
50
|
pathParams: { id: 123 },
|
|
@@ -57,7 +57,7 @@ fetcher
|
|
|
57
57
|
console.error(error);
|
|
58
58
|
});
|
|
59
59
|
|
|
60
|
-
// POST
|
|
60
|
+
// POST request with JSON body
|
|
61
61
|
fetcher
|
|
62
62
|
.post('/users', {
|
|
63
63
|
body: JSON.stringify({ name: 'John Doe', email: 'john@example.com' }),
|
|
@@ -71,17 +71,17 @@ fetcher
|
|
|
71
71
|
});
|
|
72
72
|
```
|
|
73
73
|
|
|
74
|
-
###
|
|
74
|
+
### Interceptor Usage
|
|
75
75
|
|
|
76
76
|
```typescript
|
|
77
|
-
import { Fetcher } from '@fetcher
|
|
77
|
+
import { Fetcher } from '@ahoo-wang/fetcher';
|
|
78
78
|
|
|
79
79
|
const fetcher = new Fetcher({ baseURL: 'https://api.example.com' });
|
|
80
80
|
|
|
81
|
-
//
|
|
81
|
+
// Add request interceptor
|
|
82
82
|
const requestInterceptorId = fetcher.interceptors.request.use({
|
|
83
83
|
intercept(request) {
|
|
84
|
-
//
|
|
84
|
+
// Modify request configuration, e.g., add auth header
|
|
85
85
|
return {
|
|
86
86
|
...request,
|
|
87
87
|
headers: {
|
|
@@ -92,88 +92,88 @@ const requestInterceptorId = fetcher.interceptors.request.use({
|
|
|
92
92
|
},
|
|
93
93
|
});
|
|
94
94
|
|
|
95
|
-
//
|
|
95
|
+
// Add response interceptor
|
|
96
96
|
const responseInterceptorId = fetcher.interceptors.response.use({
|
|
97
97
|
intercept(response) {
|
|
98
|
-
//
|
|
98
|
+
// Process response data, e.g., parse JSON
|
|
99
99
|
return response;
|
|
100
100
|
},
|
|
101
101
|
});
|
|
102
102
|
|
|
103
|
-
//
|
|
103
|
+
// Add error interceptor
|
|
104
104
|
const errorInterceptorId = fetcher.interceptors.error.use({
|
|
105
105
|
intercept(error) {
|
|
106
|
-
//
|
|
106
|
+
// Handle errors, e.g., log them
|
|
107
107
|
console.error('Request failed:', error);
|
|
108
108
|
return error;
|
|
109
109
|
},
|
|
110
110
|
});
|
|
111
111
|
```
|
|
112
112
|
|
|
113
|
-
## API
|
|
113
|
+
## API Reference
|
|
114
114
|
|
|
115
|
-
### Fetcher
|
|
115
|
+
### Fetcher Class
|
|
116
116
|
|
|
117
|
-
|
|
117
|
+
Core HTTP client class that provides various HTTP methods.
|
|
118
118
|
|
|
119
|
-
####
|
|
119
|
+
#### Constructor
|
|
120
120
|
|
|
121
121
|
```typescript
|
|
122
122
|
new Fetcher(options?: FetcherOptions)
|
|
123
123
|
```
|
|
124
124
|
|
|
125
|
-
|
|
125
|
+
**Parameters:**
|
|
126
126
|
|
|
127
|
-
- `options.baseURL`:
|
|
128
|
-
- `options.timeout`:
|
|
129
|
-
- `options.headers`:
|
|
127
|
+
- `options.baseURL`: Base URL
|
|
128
|
+
- `options.timeout`: Request timeout in milliseconds
|
|
129
|
+
- `options.headers`: Default request headers
|
|
130
130
|
|
|
131
|
-
####
|
|
131
|
+
#### Methods
|
|
132
132
|
|
|
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
|
|
133
|
+
- `fetch(url: string, request?: FetcherRequest): Promise<Response>` - Generic HTTP request method
|
|
134
|
+
- `get(url: string, request?: Omit<FetcherRequest, 'method' | 'body'>): Promise<Response>` - GET request
|
|
135
|
+
- `post(url: string, request?: Omit<FetcherRequest, 'method'>): Promise<Response>` - POST request
|
|
136
|
+
- `put(url: string, request?: Omit<FetcherRequest, 'method'>): Promise<Response>` - PUT request
|
|
137
|
+
- `delete(url: string, request?: Omit<FetcherRequest, 'method'>): Promise<Response>` - DELETE request
|
|
138
|
+
- `patch(url: string, request?: Omit<FetcherRequest, 'method'>): Promise<Response>` - PATCH request
|
|
139
|
+
- `head(url: string, request?: Omit<FetcherRequest, 'method' | 'body'>): Promise<Response>` - HEAD request
|
|
140
|
+
- `options(url: string, request?: Omit<FetcherRequest, 'method' | 'body'>): Promise<Response>` - OPTIONS request
|
|
141
141
|
|
|
142
|
-
### UrlBuilder
|
|
142
|
+
### UrlBuilder Class
|
|
143
143
|
|
|
144
|
-
URL
|
|
144
|
+
URL builder for constructing complete URLs with parameters.
|
|
145
145
|
|
|
146
|
-
####
|
|
146
|
+
#### Methods
|
|
147
147
|
|
|
148
|
-
- `build(path: string, pathParams?: Record<string, any>, queryParams?: Record<string, any>): string` -
|
|
148
|
+
- `build(path: string, pathParams?: Record<string, any>, queryParams?: Record<string, any>): string` - Build complete URL
|
|
149
149
|
|
|
150
|
-
### InterceptorManager
|
|
150
|
+
### InterceptorManager Class
|
|
151
151
|
|
|
152
|
-
|
|
152
|
+
Interceptor manager for managing multiple interceptors of the same type.
|
|
153
153
|
|
|
154
|
-
####
|
|
154
|
+
#### Methods
|
|
155
155
|
|
|
156
|
-
- `use(interceptor: T): number` -
|
|
157
|
-
- `eject(index: number): void` -
|
|
158
|
-
- `clear(): void` -
|
|
159
|
-
- `intercept(data: R): Promise<R>` -
|
|
156
|
+
- `use(interceptor: T): number` - Add interceptor, returns interceptor ID
|
|
157
|
+
- `eject(index: number): void` - Remove interceptor by ID
|
|
158
|
+
- `clear(): void` - Clear all interceptors
|
|
159
|
+
- `intercept(data: R): Promise<R>` - Execute all interceptors sequentially
|
|
160
160
|
|
|
161
|
-
### FetcherInterceptors
|
|
161
|
+
### FetcherInterceptors Class
|
|
162
162
|
|
|
163
|
-
Fetcher
|
|
163
|
+
Fetcher interceptor collection, including request, response, and error interceptor managers.
|
|
164
164
|
|
|
165
|
-
####
|
|
165
|
+
#### Properties
|
|
166
166
|
|
|
167
|
-
- `request: RequestInterceptorManager` -
|
|
168
|
-
- `response: ResponseInterceptorManager` -
|
|
169
|
-
- `error: ErrorInterceptorManager` -
|
|
167
|
+
- `request: RequestInterceptorManager` - Request interceptor manager
|
|
168
|
+
- `response: ResponseInterceptorManager` - Response interceptor manager
|
|
169
|
+
- `error: ErrorInterceptorManager` - Error interceptor manager
|
|
170
170
|
|
|
171
|
-
##
|
|
171
|
+
## Complete Example
|
|
172
172
|
|
|
173
173
|
```typescript
|
|
174
|
-
import { Fetcher } from '@fetcher
|
|
174
|
+
import { Fetcher } from '@ahoo-wang/fetcher';
|
|
175
175
|
|
|
176
|
-
//
|
|
176
|
+
// Create fetcher instance
|
|
177
177
|
const fetcher = new Fetcher({
|
|
178
178
|
baseURL: 'https://api.example.com',
|
|
179
179
|
timeout: 10000,
|
|
@@ -182,7 +182,7 @@ const fetcher = new Fetcher({
|
|
|
182
182
|
},
|
|
183
183
|
});
|
|
184
184
|
|
|
185
|
-
//
|
|
185
|
+
// Add request interceptor - Add auth header
|
|
186
186
|
fetcher.interceptors.request.use({
|
|
187
187
|
intercept(request) {
|
|
188
188
|
return {
|
|
@@ -195,7 +195,7 @@ fetcher.interceptors.request.use({
|
|
|
195
195
|
},
|
|
196
196
|
});
|
|
197
197
|
|
|
198
|
-
//
|
|
198
|
+
// Add response interceptor - Auto-parse JSON
|
|
199
199
|
fetcher.interceptors.response.use({
|
|
200
200
|
intercept(response) {
|
|
201
201
|
if (response.headers.get('content-type')?.includes('application/json')) {
|
|
@@ -207,7 +207,7 @@ fetcher.interceptors.response.use({
|
|
|
207
207
|
},
|
|
208
208
|
});
|
|
209
209
|
|
|
210
|
-
//
|
|
210
|
+
// Add error interceptor - Unified error handling
|
|
211
211
|
fetcher.interceptors.error.use({
|
|
212
212
|
intercept(error) {
|
|
213
213
|
if (error.name === 'FetchTimeoutError') {
|
|
@@ -219,7 +219,7 @@ fetcher.interceptors.error.use({
|
|
|
219
219
|
},
|
|
220
220
|
});
|
|
221
221
|
|
|
222
|
-
//
|
|
222
|
+
// Use fetcher to make requests
|
|
223
223
|
fetcher
|
|
224
224
|
.get('/users/{id}', {
|
|
225
225
|
pathParams: { id: 123 },
|
|
@@ -234,18 +234,18 @@ fetcher
|
|
|
234
234
|
});
|
|
235
235
|
```
|
|
236
236
|
|
|
237
|
-
##
|
|
237
|
+
## Testing
|
|
238
238
|
|
|
239
|
-
|
|
239
|
+
Run tests:
|
|
240
240
|
|
|
241
241
|
```bash
|
|
242
242
|
pnpm test
|
|
243
243
|
```
|
|
244
244
|
|
|
245
|
-
##
|
|
245
|
+
## Contributing
|
|
246
246
|
|
|
247
|
-
|
|
247
|
+
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
248
|
|
|
249
|
-
##
|
|
249
|
+
## License
|
|
250
250
|
|
|
251
|
-
|
|
251
|
+
This project is licensed under the [Apache-2.0 License](https://opensource.org/licenses/Apache-2.0).
|