@talosjs/fetcher 1.1.0 → 1.1.2
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 +4 -463
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -1,29 +1,6 @@
|
|
|
1
1
|
# @talosjs/fetcher
|
|
2
2
|
|
|
3
|
-
Lightweight HTTP client with typed headers, response parsing, and configurable request/response handling for external API integration
|
|
4
|
-
|
|
5
|
-

|
|
6
|
-

|
|
7
|
-

|
|
8
|
-

|
|
9
|
-
|
|
10
|
-
## Features
|
|
11
|
-
|
|
12
|
-
✅ **Simple API** - Intuitive methods for GET, POST, PUT, PATCH, DELETE, HEAD, and OPTIONS
|
|
13
|
-
|
|
14
|
-
✅ **Typed Responses** - Generic response types with `ResponseDataType<T>` for full TypeScript support
|
|
15
|
-
|
|
16
|
-
✅ **Authentication** - Built-in support for Bearer and Basic authentication tokens
|
|
17
|
-
|
|
18
|
-
✅ **File Uploads** - Easy file upload with automatic FormData handling
|
|
19
|
-
|
|
20
|
-
✅ **Request Cancellation** - Abort in-flight requests with AbortController integration
|
|
21
|
-
|
|
22
|
-
✅ **Header Management** - Typed header manipulation via `@talosjs/http-header`
|
|
23
|
-
|
|
24
|
-
✅ **Base URL Support** - Configure base URL for cleaner API calls
|
|
25
|
-
|
|
26
|
-
✅ **Cloneable** - Create independent copies of configured fetcher instances
|
|
3
|
+
Lightweight HTTP client with typed headers, response parsing, and configurable request/response handling for external API integration
|
|
27
4
|
|
|
28
5
|
## Installation
|
|
29
6
|
|
|
@@ -31,446 +8,10 @@ Lightweight HTTP client with typed headers, response parsing, and configurable r
|
|
|
31
8
|
bun add @talosjs/fetcher
|
|
32
9
|
```
|
|
33
10
|
|
|
34
|
-
##
|
|
35
|
-
|
|
36
|
-
### Basic GET Request
|
|
37
|
-
|
|
38
|
-
```typescript
|
|
39
|
-
import { Fetcher } from '@talosjs/fetcher';
|
|
40
|
-
|
|
41
|
-
const fetcher = new Fetcher('https://api.example.com');
|
|
42
|
-
|
|
43
|
-
const response = await fetcher.get<{ users: User[] }>('/users');
|
|
44
|
-
|
|
45
|
-
if (response.success) {
|
|
46
|
-
console.log(response.data.users);
|
|
47
|
-
} else {
|
|
48
|
-
console.error(response.message);
|
|
49
|
-
}
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
### POST Request with Data
|
|
53
|
-
|
|
54
|
-
```typescript
|
|
55
|
-
import { Fetcher } from '@talosjs/fetcher';
|
|
56
|
-
|
|
57
|
-
const fetcher = new Fetcher('https://api.example.com');
|
|
58
|
-
|
|
59
|
-
const response = await fetcher.post<{ user: User }>('/users', {
|
|
60
|
-
name: 'John Doe',
|
|
61
|
-
email: 'john@example.com'
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
if (response.success) {
|
|
65
|
-
console.log('Created user:', response.data.user);
|
|
66
|
-
}
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
### With Authentication
|
|
70
|
-
|
|
71
|
-
```typescript
|
|
72
|
-
import { Fetcher } from '@talosjs/fetcher';
|
|
73
|
-
|
|
74
|
-
const fetcher = new Fetcher('https://api.example.com');
|
|
75
|
-
|
|
76
|
-
// Set Bearer token
|
|
77
|
-
fetcher.setBearerToken('your-jwt-token');
|
|
78
|
-
|
|
79
|
-
const response = await fetcher.get<{ profile: Profile }>('/me');
|
|
80
|
-
|
|
81
|
-
// Clear token when done
|
|
82
|
-
fetcher.clearBearerToken();
|
|
83
|
-
```
|
|
84
|
-
|
|
85
|
-
### File Upload
|
|
86
|
-
|
|
87
|
-
```typescript
|
|
88
|
-
import { Fetcher } from '@talosjs/fetcher';
|
|
89
|
-
|
|
90
|
-
const fetcher = new Fetcher('https://api.example.com');
|
|
91
|
-
|
|
92
|
-
const file = document.querySelector('input[type="file"]').files[0];
|
|
93
|
-
|
|
94
|
-
const response = await fetcher.upload<{ url: string }>(
|
|
95
|
-
'/upload',
|
|
96
|
-
file,
|
|
97
|
-
'avatar' // form field name
|
|
98
|
-
);
|
|
99
|
-
|
|
100
|
-
if (response.success) {
|
|
101
|
-
console.log('Uploaded to:', response.data.url);
|
|
102
|
-
}
|
|
103
|
-
```
|
|
104
|
-
|
|
105
|
-
### Request Cancellation
|
|
106
|
-
|
|
107
|
-
```typescript
|
|
108
|
-
import { Fetcher } from '@talosjs/fetcher';
|
|
109
|
-
|
|
110
|
-
const fetcher = new Fetcher('https://api.example.com');
|
|
111
|
-
|
|
112
|
-
// Start a request
|
|
113
|
-
const promise = fetcher.get('/slow-endpoint');
|
|
114
|
-
|
|
115
|
-
// Cancel it
|
|
116
|
-
fetcher.abort();
|
|
117
|
-
|
|
118
|
-
// Handle the cancellation
|
|
119
|
-
try {
|
|
120
|
-
await promise;
|
|
121
|
-
} catch (error) {
|
|
122
|
-
if (error.name === 'AbortError') {
|
|
123
|
-
console.log('Request was cancelled');
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
```
|
|
127
|
-
|
|
128
|
-
## API Reference
|
|
129
|
-
|
|
130
|
-
### Classes
|
|
131
|
-
|
|
132
|
-
#### `Fetcher`
|
|
133
|
-
|
|
134
|
-
HTTP client class for making fetch requests.
|
|
135
|
-
|
|
136
|
-
**Constructor:**
|
|
137
|
-
```typescript
|
|
138
|
-
new Fetcher(baseURL?: string)
|
|
139
|
-
```
|
|
140
|
-
|
|
141
|
-
**Parameters:**
|
|
142
|
-
- `baseURL` - Optional base URL to prepend to all request paths
|
|
143
|
-
|
|
144
|
-
**Properties:**
|
|
145
|
-
|
|
146
|
-
##### `header: Header`
|
|
147
|
-
|
|
148
|
-
Access to the underlying Header instance for advanced header manipulation.
|
|
149
|
-
|
|
150
|
-
**Methods:**
|
|
151
|
-
|
|
152
|
-
##### `get<T>(path: string): Promise<ResponseDataType<T>>`
|
|
153
|
-
|
|
154
|
-
Performs a GET request.
|
|
155
|
-
|
|
156
|
-
**Parameters:**
|
|
157
|
-
- `path` - Request path (appended to base URL if set)
|
|
158
|
-
|
|
159
|
-
**Returns:** Promise resolving to typed response data
|
|
160
|
-
|
|
161
|
-
**Example:**
|
|
162
|
-
```typescript
|
|
163
|
-
const response = await fetcher.get<{ items: Item[] }>('/items');
|
|
164
|
-
```
|
|
165
|
-
|
|
166
|
-
##### `post<T>(path: string, data?: unknown): Promise<ResponseDataType<T>>`
|
|
167
|
-
|
|
168
|
-
Performs a POST request with optional body data.
|
|
169
|
-
|
|
170
|
-
**Parameters:**
|
|
171
|
-
- `path` - Request path
|
|
172
|
-
- `data` - Optional request body (automatically JSON-stringified for objects)
|
|
173
|
-
|
|
174
|
-
**Returns:** Promise resolving to typed response data
|
|
175
|
-
|
|
176
|
-
**Example:**
|
|
177
|
-
```typescript
|
|
178
|
-
const response = await fetcher.post<{ id: string }>('/items', { name: 'New Item' });
|
|
179
|
-
```
|
|
180
|
-
|
|
181
|
-
##### `put<T>(path: string, data?: unknown): Promise<ResponseDataType<T>>`
|
|
182
|
-
|
|
183
|
-
Performs a PUT request with optional body data.
|
|
184
|
-
|
|
185
|
-
##### `patch<T>(path: string, data?: unknown): Promise<ResponseDataType<T>>`
|
|
186
|
-
|
|
187
|
-
Performs a PATCH request with optional body data.
|
|
188
|
-
|
|
189
|
-
##### `delete<T>(path: string): Promise<ResponseDataType<T>>`
|
|
190
|
-
|
|
191
|
-
Performs a DELETE request.
|
|
192
|
-
|
|
193
|
-
##### `head<T>(path: string): Promise<ResponseDataType<T>>`
|
|
194
|
-
|
|
195
|
-
Performs a HEAD request.
|
|
196
|
-
|
|
197
|
-
##### `options<T>(path: string): Promise<ResponseDataType<T>>`
|
|
198
|
-
|
|
199
|
-
Performs an OPTIONS request.
|
|
200
|
-
|
|
201
|
-
##### `request<T>(method: HttpMethodType, path: string, data?: unknown): Promise<ResponseDataType<T>>`
|
|
202
|
-
|
|
203
|
-
Performs a custom HTTP request.
|
|
204
|
-
|
|
205
|
-
**Parameters:**
|
|
206
|
-
- `method` - HTTP method (GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS)
|
|
207
|
-
- `path` - Request path
|
|
208
|
-
- `data` - Optional request body
|
|
209
|
-
|
|
210
|
-
**Example:**
|
|
211
|
-
```typescript
|
|
212
|
-
const response = await fetcher.request<{ result: string }>('POST', '/custom', { foo: 'bar' });
|
|
213
|
-
```
|
|
214
|
-
|
|
215
|
-
##### `upload<T>(path: string, file: File | Blob, name?: string): Promise<ResponseDataType<T>>`
|
|
216
|
-
|
|
217
|
-
Uploads a file using multipart/form-data.
|
|
11
|
+
## Documentation
|
|
218
12
|
|
|
219
|
-
|
|
220
|
-
- `path` - Upload endpoint path
|
|
221
|
-
- `file` - File or Blob to upload
|
|
222
|
-
- `name` - Form field name (default: 'file')
|
|
223
|
-
|
|
224
|
-
**Returns:** Promise resolving to typed response data
|
|
225
|
-
|
|
226
|
-
**Example:**
|
|
227
|
-
```typescript
|
|
228
|
-
const response = await fetcher.upload<{ url: string }>('/upload', file, 'document');
|
|
229
|
-
```
|
|
230
|
-
|
|
231
|
-
##### `setBearerToken(token: string): this`
|
|
232
|
-
|
|
233
|
-
Sets the Authorization header with a Bearer token.
|
|
234
|
-
|
|
235
|
-
**Parameters:**
|
|
236
|
-
- `token` - JWT or other bearer token
|
|
237
|
-
|
|
238
|
-
**Returns:** The fetcher instance for chaining
|
|
239
|
-
|
|
240
|
-
##### `setBasicToken(token: string): this`
|
|
241
|
-
|
|
242
|
-
Sets the Authorization header with Basic authentication.
|
|
243
|
-
|
|
244
|
-
**Parameters:**
|
|
245
|
-
- `token` - Base64-encoded credentials
|
|
246
|
-
|
|
247
|
-
**Returns:** The fetcher instance for chaining
|
|
248
|
-
|
|
249
|
-
##### `clearBearerToken(): this`
|
|
250
|
-
|
|
251
|
-
Removes the Authorization header.
|
|
252
|
-
|
|
253
|
-
**Returns:** The fetcher instance for chaining
|
|
254
|
-
|
|
255
|
-
##### `clearBasicToken(): this`
|
|
256
|
-
|
|
257
|
-
Removes the Authorization header.
|
|
258
|
-
|
|
259
|
-
**Returns:** The fetcher instance for chaining
|
|
260
|
-
|
|
261
|
-
##### `setContentType(contentType: MimeType): this`
|
|
262
|
-
|
|
263
|
-
Sets the Content-Type header.
|
|
264
|
-
|
|
265
|
-
**Parameters:**
|
|
266
|
-
- `contentType` - MIME type string
|
|
267
|
-
|
|
268
|
-
**Returns:** The fetcher instance for chaining
|
|
269
|
-
|
|
270
|
-
##### `setLang(lang: string): this`
|
|
271
|
-
|
|
272
|
-
Sets the Accept-Language or custom language header.
|
|
273
|
-
|
|
274
|
-
**Parameters:**
|
|
275
|
-
- `lang` - Language code (e.g., 'en', 'fr')
|
|
276
|
-
|
|
277
|
-
**Returns:** The fetcher instance for chaining
|
|
278
|
-
|
|
279
|
-
##### `abort(): this`
|
|
280
|
-
|
|
281
|
-
Cancels any in-flight requests and resets the AbortController.
|
|
282
|
-
|
|
283
|
-
**Returns:** The fetcher instance for chaining
|
|
284
|
-
|
|
285
|
-
##### `clone(): Fetcher`
|
|
286
|
-
|
|
287
|
-
Creates a new Fetcher instance with the same base URL.
|
|
288
|
-
|
|
289
|
-
**Returns:** New Fetcher instance
|
|
290
|
-
|
|
291
|
-
### Interfaces
|
|
292
|
-
|
|
293
|
-
#### `IFetcher`
|
|
294
|
-
|
|
295
|
-
```typescript
|
|
296
|
-
interface IFetcher {
|
|
297
|
-
readonly header: Header;
|
|
298
|
-
|
|
299
|
-
setBearerToken(token: string): IFetcher;
|
|
300
|
-
setBasicToken(token: string): IFetcher;
|
|
301
|
-
clearBearerToken(): IFetcher;
|
|
302
|
-
clearBasicToken(): IFetcher;
|
|
303
|
-
setContentType(contentType: MimeType): IFetcher;
|
|
304
|
-
setLang(lang: string): IFetcher;
|
|
305
|
-
abort(): IFetcher;
|
|
306
|
-
clone(): IFetcher;
|
|
307
|
-
|
|
308
|
-
get<T>(path: string): Promise<ResponseDataType<T>>;
|
|
309
|
-
post<T>(path: string, data?: unknown): Promise<ResponseDataType<T>>;
|
|
310
|
-
put<T>(path: string, data?: unknown): Promise<ResponseDataType<T>>;
|
|
311
|
-
patch<T>(path: string, data?: unknown): Promise<ResponseDataType<T>>;
|
|
312
|
-
delete<T>(path: string): Promise<ResponseDataType<T>>;
|
|
313
|
-
head<T>(path: string): Promise<ResponseDataType<T>>;
|
|
314
|
-
options<T>(path: string): Promise<ResponseDataType<T>>;
|
|
315
|
-
request<T>(method: HttpMethodType, path: string, data?: unknown): Promise<ResponseDataType<T>>;
|
|
316
|
-
upload<T>(path: string, file: File | Blob, name?: string): Promise<ResponseDataType<T>>;
|
|
317
|
-
}
|
|
318
|
-
```
|
|
319
|
-
|
|
320
|
-
## Advanced Usage
|
|
321
|
-
|
|
322
|
-
### Creating API Client Classes
|
|
323
|
-
|
|
324
|
-
```typescript
|
|
325
|
-
import { Fetcher } from '@talosjs/fetcher';
|
|
326
|
-
|
|
327
|
-
class UserApi {
|
|
328
|
-
private fetcher: Fetcher;
|
|
329
|
-
|
|
330
|
-
constructor(baseUrl: string, token?: string) {
|
|
331
|
-
this.fetcher = new Fetcher(baseUrl);
|
|
332
|
-
if (token) {
|
|
333
|
-
this.fetcher.setBearerToken(token);
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
public async getUsers() {
|
|
338
|
-
return this.fetcher.get<{ users: User[] }>('/users');
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
public async createUser(data: CreateUserDto) {
|
|
342
|
-
return this.fetcher.post<{ user: User }>('/users', data);
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
public async updateUser(id: string, data: UpdateUserDto) {
|
|
346
|
-
return this.fetcher.patch<{ user: User }>(`/users/${id}`, data);
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
public async deleteUser(id: string) {
|
|
350
|
-
return this.fetcher.delete<{ success: boolean }>(`/users/${id}`);
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
```
|
|
354
|
-
|
|
355
|
-
### Request Interceptors with Header Manipulation
|
|
356
|
-
|
|
357
|
-
```typescript
|
|
358
|
-
import { Fetcher } from '@talosjs/fetcher';
|
|
359
|
-
|
|
360
|
-
const fetcher = new Fetcher('https://api.example.com');
|
|
361
|
-
|
|
362
|
-
// Add custom headers
|
|
363
|
-
fetcher.header.set('X-Request-ID', generateRequestId());
|
|
364
|
-
fetcher.header.set('X-Client-Version', '1.0.0');
|
|
365
|
-
|
|
366
|
-
const response = await fetcher.get('/endpoint');
|
|
367
|
-
```
|
|
368
|
-
|
|
369
|
-
### Error Handling
|
|
370
|
-
|
|
371
|
-
```typescript
|
|
372
|
-
import { Fetcher } from '@talosjs/fetcher';
|
|
373
|
-
|
|
374
|
-
const fetcher = new Fetcher('https://api.example.com');
|
|
375
|
-
|
|
376
|
-
const response = await fetcher.get<{ data: string }>('/resource');
|
|
377
|
-
|
|
378
|
-
if (response.success) {
|
|
379
|
-
// Handle successful response
|
|
380
|
-
console.log(response.data);
|
|
381
|
-
} else if (response.isClientError) {
|
|
382
|
-
// Handle 4xx errors
|
|
383
|
-
console.error('Client error:', response.message);
|
|
384
|
-
} else if (response.isServerError) {
|
|
385
|
-
// Handle 5xx errors
|
|
386
|
-
console.error('Server error:', response.message);
|
|
387
|
-
} else if (response.isNotFound) {
|
|
388
|
-
// Handle 404
|
|
389
|
-
console.error('Resource not found');
|
|
390
|
-
} else if (response.isUnauthorized) {
|
|
391
|
-
// Handle 401
|
|
392
|
-
console.error('Unauthorized - please login');
|
|
393
|
-
}
|
|
394
|
-
```
|
|
395
|
-
|
|
396
|
-
### Chaining Configuration
|
|
397
|
-
|
|
398
|
-
```typescript
|
|
399
|
-
import { Fetcher } from '@talosjs/fetcher';
|
|
400
|
-
|
|
401
|
-
const fetcher = new Fetcher('https://api.example.com')
|
|
402
|
-
.setBearerToken('your-token')
|
|
403
|
-
.setLang('en')
|
|
404
|
-
.setContentType('application/json');
|
|
405
|
-
|
|
406
|
-
const response = await fetcher.post('/data', { key: 'value' });
|
|
407
|
-
```
|
|
408
|
-
|
|
409
|
-
### Cloning for Different Contexts
|
|
410
|
-
|
|
411
|
-
```typescript
|
|
412
|
-
import { Fetcher } from '@talosjs/fetcher';
|
|
413
|
-
|
|
414
|
-
const baseFetcher = new Fetcher('https://api.example.com');
|
|
415
|
-
|
|
416
|
-
// Create authenticated clone
|
|
417
|
-
const authFetcher = baseFetcher.clone();
|
|
418
|
-
authFetcher.setBearerToken('user-token');
|
|
419
|
-
|
|
420
|
-
// Create admin clone
|
|
421
|
-
const adminFetcher = baseFetcher.clone();
|
|
422
|
-
adminFetcher.setBearerToken('admin-token');
|
|
423
|
-
|
|
424
|
-
// Use independently
|
|
425
|
-
await authFetcher.get('/user/profile');
|
|
426
|
-
await adminFetcher.get('/admin/dashboard');
|
|
427
|
-
```
|
|
428
|
-
|
|
429
|
-
### Uploading Multiple Files
|
|
430
|
-
|
|
431
|
-
```typescript
|
|
432
|
-
import { Fetcher } from '@talosjs/fetcher';
|
|
433
|
-
|
|
434
|
-
const fetcher = new Fetcher('https://api.example.com');
|
|
435
|
-
|
|
436
|
-
async function uploadFiles(files: File[]) {
|
|
437
|
-
const results = [];
|
|
438
|
-
|
|
439
|
-
for (const file of files) {
|
|
440
|
-
const response = await fetcher.upload<{ url: string }>(
|
|
441
|
-
'/upload',
|
|
442
|
-
file,
|
|
443
|
-
'file'
|
|
444
|
-
);
|
|
445
|
-
results.push(response);
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
return results;
|
|
449
|
-
}
|
|
450
|
-
```
|
|
13
|
+
Read the full documentation at [docs.talosjs.com/utilities/fetcher](https://docs.talosjs.com/utilities/fetcher).
|
|
451
14
|
|
|
452
15
|
## License
|
|
453
16
|
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
## Contributing
|
|
457
|
-
|
|
458
|
-
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
|
|
459
|
-
|
|
460
|
-
### Development Setup
|
|
461
|
-
|
|
462
|
-
1. Clone the repository
|
|
463
|
-
2. Install dependencies: `bun install`
|
|
464
|
-
3. Run tests: `bun run test`
|
|
465
|
-
4. Build the project: `bun run build`
|
|
466
|
-
|
|
467
|
-
### Guidelines
|
|
468
|
-
|
|
469
|
-
- Write tests for new features
|
|
470
|
-
- Follow the existing code style
|
|
471
|
-
- Update documentation for API changes
|
|
472
|
-
- Ensure all tests pass before submitting PR
|
|
473
|
-
|
|
474
|
-
---
|
|
475
|
-
|
|
476
|
-
Made with ❤️ by the Talos team
|
|
17
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@talosjs/fetcher",
|
|
3
3
|
"description": "Lightweight HTTP client with typed headers, response parsing, and configurable request/response handling for external API integration",
|
|
4
|
-
"version": "1.1.
|
|
4
|
+
"version": "1.1.2",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist",
|
|
@@ -24,16 +24,16 @@
|
|
|
24
24
|
"scripts": {
|
|
25
25
|
"test": "bun test tests",
|
|
26
26
|
"build": "bunup",
|
|
27
|
-
"
|
|
28
|
-
"
|
|
27
|
+
"fmt": "bunx biome check --write",
|
|
28
|
+
"lint": "tsc --noEmit && bunx biome lint"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@talosjs/http-header": "^1.1.
|
|
31
|
+
"@talosjs/http-header": "^1.1.2"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@talosjs/http-response": "^1.1.
|
|
35
|
-
"@talosjs/http-mimes": "^1.1.
|
|
36
|
-
"@talosjs/types": "^1.1.
|
|
34
|
+
"@talosjs/http-response": "^1.1.2",
|
|
35
|
+
"@talosjs/http-mimes": "^1.1.2",
|
|
36
|
+
"@talosjs/types": "^1.1.2"
|
|
37
37
|
},
|
|
38
38
|
"keywords": [
|
|
39
39
|
"api-client",
|