create-request 0.0.1-beta.19 → 0.0.1-beta.20
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 +43 -14
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -120,7 +120,13 @@ const optionsRequest = create.options(); // OPTIONS
|
|
|
120
120
|
The library provides a comprehensive set of configuration methods that can be chained together to customize your requests:
|
|
121
121
|
|
|
122
122
|
```typescript
|
|
123
|
-
import create, {
|
|
123
|
+
import create, {
|
|
124
|
+
RequestPriority,
|
|
125
|
+
CredentialsPolicy,
|
|
126
|
+
RequestMode,
|
|
127
|
+
RedirectMode,
|
|
128
|
+
ReferrerPolicy,
|
|
129
|
+
} from "create-request";
|
|
124
130
|
|
|
125
131
|
// Configure request options
|
|
126
132
|
const request = create
|
|
@@ -176,7 +182,11 @@ Each configuration method returns the request object, allowing for a fluent inte
|
|
|
176
182
|
|
|
177
183
|
```typescript
|
|
178
184
|
// Simple example with just what's needed
|
|
179
|
-
const searchUsers = create
|
|
185
|
+
const searchUsers = create
|
|
186
|
+
.get()
|
|
187
|
+
.withBearerToken(userToken)
|
|
188
|
+
.withQueryParams({ q: searchTerm, limit: 20 })
|
|
189
|
+
.withTimeout(3000);
|
|
180
190
|
|
|
181
191
|
// Now execute the request
|
|
182
192
|
const users = await searchUsers.sendTo("https://api.example.com/users").getData();
|
|
@@ -239,7 +249,9 @@ const bodyStream = await request.sendTo("https://api.example.com/endpoint").getB
|
|
|
239
249
|
const arrayBuffer = await request.sendTo("https://api.example.com/endpoint").getArrayBuffer();
|
|
240
250
|
|
|
241
251
|
// Using the data selector API to extract specific data
|
|
242
|
-
const userData = await request
|
|
252
|
+
const userData = await request
|
|
253
|
+
.sendTo("https://api.example.com/users")
|
|
254
|
+
.getData(data => data.results.users);
|
|
243
255
|
|
|
244
256
|
// Using the data selector without a selector function just returns the full JSON response
|
|
245
257
|
const fullData = await request.sendTo("https://api.example.com/data").getData();
|
|
@@ -271,7 +283,11 @@ try {
|
|
|
271
283
|
### Caching Requests
|
|
272
284
|
|
|
273
285
|
```typescript
|
|
274
|
-
import create, {
|
|
286
|
+
import create, {
|
|
287
|
+
createMemoryStorage,
|
|
288
|
+
createLocalStorageStorage,
|
|
289
|
+
createSessionStorageStorage,
|
|
290
|
+
} from "create-request";
|
|
275
291
|
|
|
276
292
|
// Simple in-memory caching
|
|
277
293
|
const request = create
|
|
@@ -366,20 +382,28 @@ The `getData` method provides a powerful way to extract and transform specific d
|
|
|
366
382
|
|
|
367
383
|
```typescript
|
|
368
384
|
// Extract specific properties from nested structures
|
|
369
|
-
const posts = await request
|
|
385
|
+
const posts = await request
|
|
386
|
+
.sendTo("https://api.example.com/feed")
|
|
387
|
+
.getData(data => data.feed.posts);
|
|
370
388
|
|
|
371
389
|
// Transform data in the selector function
|
|
372
|
-
const usernames = await request
|
|
390
|
+
const usernames = await request
|
|
391
|
+
.sendTo("https://api.example.com/users")
|
|
392
|
+
.getData(data => data.users.map(user => user.username));
|
|
373
393
|
|
|
374
394
|
// Apply filtering in the selector
|
|
375
|
-
const activeUsers = await request
|
|
395
|
+
const activeUsers = await request
|
|
396
|
+
.sendTo("https://api.example.com/users")
|
|
397
|
+
.getData(data => data.users.filter(user => user.isActive));
|
|
376
398
|
|
|
377
399
|
// Combine data from complex nested structures
|
|
378
|
-
const combinedData = await request
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
400
|
+
const combinedData = await request
|
|
401
|
+
.sendTo("https://api.example.com/dashboard")
|
|
402
|
+
.getData(data => ({
|
|
403
|
+
userCount: data.stats.users.total,
|
|
404
|
+
recentPosts: data.content.recent.slice(0, 5),
|
|
405
|
+
notifications: data.user.notifications.unread,
|
|
406
|
+
}));
|
|
383
407
|
```
|
|
384
408
|
|
|
385
409
|
When a selector fails, the error message will contain the original response data to help diagnose the issue:
|
|
@@ -387,7 +411,9 @@ When a selector fails, the error message will contain the original response data
|
|
|
387
411
|
```typescript
|
|
388
412
|
try {
|
|
389
413
|
// This will fail if the response structure doesn't match expectations
|
|
390
|
-
const result = await request
|
|
414
|
+
const result = await request
|
|
415
|
+
.sendTo("https://api.example.com/data")
|
|
416
|
+
.getData(data => data.results.items);
|
|
391
417
|
} catch (error) {
|
|
392
418
|
console.error(error);
|
|
393
419
|
// Error message includes the original response data for debugging
|
|
@@ -469,7 +495,10 @@ interface ApiResponse<T> {
|
|
|
469
495
|
}
|
|
470
496
|
|
|
471
497
|
// Type the full response
|
|
472
|
-
const response = await create
|
|
498
|
+
const response = await create
|
|
499
|
+
.get()
|
|
500
|
+
.sendTo("https://api.example.com/users")
|
|
501
|
+
.getJson<ApiResponse<User[]>>();
|
|
473
502
|
|
|
474
503
|
// Or use getData with type parameters
|
|
475
504
|
const users = await create
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-request",
|
|
3
|
-
"version": "0.0.1-beta.
|
|
3
|
+
"version": "0.0.1-beta.20",
|
|
4
4
|
"description": "A modern, chainable wrapper for fetch with automatic retries, timeouts, comprehensive error handling, and first-class TypeScript support",
|
|
5
5
|
"main": "dist/library/index.js",
|
|
6
6
|
"module": "dist/library/index.esm.js",
|