create-request 0.0.1-beta.8 → 0.0.1-beta.9
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 +12 -36
- package/dist/library/BaseRequest.d.ts +1 -0
- package/dist/library/index.d.ts +1 -1
- package/dist/library/index.esm.js +4 -1
- package/dist/library/index.js +5 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -96,7 +96,7 @@ const optionsRequest = create.options();
|
|
|
96
96
|
The library provides a comprehensive set of configuration methods that can be chained together to customize your requests:
|
|
97
97
|
|
|
98
98
|
```typescript
|
|
99
|
-
import create, { RequestPriority, CredentialsPolicy, RequestMode,
|
|
99
|
+
import create, { RequestPriority, CredentialsPolicy, RequestMode, CacheMode, RedirectMode, ReferrerPolicy } from 'create-request';
|
|
100
100
|
|
|
101
101
|
// Configure request options
|
|
102
102
|
const request = create.get()
|
|
@@ -109,10 +109,8 @@ const request = create.get()
|
|
|
109
109
|
|
|
110
110
|
// Automatic retry configuration
|
|
111
111
|
.withRetries(3) // Retry up to 3 times on failure
|
|
112
|
-
.
|
|
113
|
-
|
|
114
|
-
.onRetry(({ attempt, error, delay }) => {
|
|
115
|
-
console.log(`Attempt ${attempt} failed: ${error.message}. Retrying in ${delay}ms`);
|
|
112
|
+
.onRetry(({ attempt, error }) => {
|
|
113
|
+
console.log(`Attempt ${attempt} failed: ${error.message}. Retrying...`);
|
|
116
114
|
})
|
|
117
115
|
|
|
118
116
|
// Authentication methods
|
|
@@ -129,7 +127,6 @@ const request = create.get()
|
|
|
129
127
|
// Fetch API options
|
|
130
128
|
.withCredentials(CredentialsPolicy.INCLUDE) // Includes cookies with cross-origin requests
|
|
131
129
|
.withMode(RequestMode.CORS) // Controls CORS behavior
|
|
132
|
-
.withCache(CachePolicy.NO_STORE) // Controls how request interacts with browser cache
|
|
133
130
|
.withRedirect(RedirectMode.FOLLOW) // Controls redirect behavior (follow, error, manual)
|
|
134
131
|
.withReferrer('https://example.com') // Sets request referrer
|
|
135
132
|
.withReferrerPolicy(ReferrerPolicy.NO_REFERRER_WHEN_DOWNGRADE) // Controls referrer policy
|
|
@@ -247,9 +244,7 @@ try {
|
|
|
247
244
|
.getData(data => data.results.items); // Will fail if structure is different
|
|
248
245
|
} catch (error) {
|
|
249
246
|
console.error(error);
|
|
250
|
-
// Error message includes the original response data
|
|
251
|
-
// "Data selector failed: Cannot read properties of undefined (reading 'items').
|
|
252
|
-
// Original data: { "status": "error", "message": "Invalid request" }"
|
|
247
|
+
// Error message includes the original response data
|
|
253
248
|
}
|
|
254
249
|
```
|
|
255
250
|
|
|
@@ -412,31 +407,12 @@ const request = create.get()
|
|
|
412
407
|
storage: createMemoryStorage(),
|
|
413
408
|
keyGenerator: (url, method, headers) => {
|
|
414
409
|
// Include user ID from authorization header in the cache key
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
},
|
|
422
|
-
has(key: string): boolean {
|
|
423
|
-
// Check if exists in your custom storage
|
|
424
|
-
return localStorage.getItem(`my-app:${key}`) !== null;
|
|
425
|
-
},
|
|
426
|
-
delete(key: string): void {
|
|
427
|
-
// Remove from your custom storage
|
|
428
|
-
localStorage.removeItem(`my-app:${key}`);
|
|
429
|
-
},
|
|
430
|
-
clear(): void {
|
|
431
|
-
// Clear all items from your namespace
|
|
432
|
-
Object.keys(localStorage)
|
|
433
|
-
.filter(key => key.startsWith('my-app:'))
|
|
434
|
-
.forEach(key => localStorage.removeItem(key));
|
|
435
|
-
}
|
|
436
|
-
};
|
|
437
|
-
|
|
438
|
-
const request = create.get()
|
|
439
|
-
.withCache({ storage: customStorage })
|
|
410
|
+
const authHeader = headers?.['Authorization'] || '';
|
|
411
|
+
const userMatch = authHeader.match(/User-(\d+)/);
|
|
412
|
+
const userId = userMatch ? userMatch[1] : 'anonymous';
|
|
413
|
+
return `${method}:${userId}:${url}`;
|
|
414
|
+
}
|
|
415
|
+
})
|
|
440
416
|
.sendTo('https://api.example.com/data');
|
|
441
417
|
```
|
|
442
418
|
|
|
@@ -526,8 +502,8 @@ You can configure CSRF settings globally for all requests:
|
|
|
526
502
|
create.config.setCsrfToken('your-csrf-token');
|
|
527
503
|
create.config.setCsrfHeaderName('X-CSRF-Token'); // Default header name
|
|
528
504
|
create.config.setXsrfCookieName('XSRF-TOKEN'); // Default cookie name
|
|
529
|
-
create.config.
|
|
530
|
-
create.config.
|
|
505
|
+
create.config.setEnableAntiCsrf(true); // Enable/disable X-Requested-With header
|
|
506
|
+
create.config.setEnableAutoXsrf(true); // Enable/disable automatic cookie-to-header token
|
|
531
507
|
```
|
|
532
508
|
|
|
533
509
|
### Per-Request CSRF Settings
|
|
@@ -13,6 +13,7 @@ export declare abstract class BaseRequest {
|
|
|
13
13
|
protected cacheEnabled: boolean;
|
|
14
14
|
constructor();
|
|
15
15
|
withHeaders(headers: Record<string, string>): this;
|
|
16
|
+
withHeader(key: string, value: string): this;
|
|
16
17
|
withTimeout(timeout: number): this;
|
|
17
18
|
withRetries(retries: number): this;
|
|
18
19
|
onRetry(callback: RetryCallback): this;
|
package/dist/library/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { get, put, head, post, patch, del, options } from "./requestFactories";
|
|
2
2
|
import { Config } from "./utils/Config";
|
|
3
|
-
export { HttpMethod, RequestPriority, CredentialsPolicy, RequestMode, RedirectMode, CacheMode, SameSitePolicy } from "./enums";
|
|
3
|
+
export { HttpMethod, RequestPriority, CredentialsPolicy, RequestMode, RedirectMode, CacheMode, SameSitePolicy, ReferrerPolicy } from "./enums";
|
|
4
4
|
export type { RetryCallback, RequestOptions, CookieOptions, CookiesRecord } from "./types";
|
|
5
5
|
export { RequestError } from "./RequestError";
|
|
6
6
|
export { ResponseWrapper } from "./ResponseWrapper";
|
|
@@ -794,6 +794,9 @@ class BaseRequest {
|
|
|
794
794
|
};
|
|
795
795
|
return this;
|
|
796
796
|
}
|
|
797
|
+
withHeader(key, value) {
|
|
798
|
+
return this.withHeaders({ [key]: value });
|
|
799
|
+
}
|
|
797
800
|
withTimeout(timeout) {
|
|
798
801
|
if (!Number.isFinite(timeout) || timeout <= 0)
|
|
799
802
|
throw new Error("Timeout must be a positive number");
|
|
@@ -1495,4 +1498,4 @@ const create = {
|
|
|
1495
1498
|
config: Config.getInstance(),
|
|
1496
1499
|
};
|
|
1497
1500
|
|
|
1498
|
-
export { CacheManager, CacheMode, CookieUtils, CredentialsPolicy, DeleteRequest, GetRequest, HeadRequest, HttpMethod, OptionsRequest, PatchRequest, PostRequest, PutRequest, RedirectMode, RequestError, RequestMode, RequestPriority, ResponseWrapper, SameSitePolicy, createLocalStorageStorage, createMemoryStorage, createSessionStorageStorage, create as default };
|
|
1501
|
+
export { CacheManager, CacheMode, CookieUtils, CredentialsPolicy, DeleteRequest, GetRequest, HeadRequest, HttpMethod, OptionsRequest, PatchRequest, PostRequest, PutRequest, RedirectMode, ReferrerPolicy, RequestError, RequestMode, RequestPriority, ResponseWrapper, SameSitePolicy, createLocalStorageStorage, createMemoryStorage, createSessionStorageStorage, create as default };
|
package/dist/library/index.js
CHANGED
|
@@ -87,7 +87,7 @@
|
|
|
87
87
|
/**
|
|
88
88
|
* Referrer policies for fetch requests
|
|
89
89
|
*/
|
|
90
|
-
|
|
90
|
+
exports.ReferrerPolicy = void 0;
|
|
91
91
|
(function (ReferrerPolicy) {
|
|
92
92
|
ReferrerPolicy["ORIGIN"] = "origin";
|
|
93
93
|
ReferrerPolicy["UNSAFE_URL"] = "unsafe-url";
|
|
@@ -97,7 +97,7 @@
|
|
|
97
97
|
ReferrerPolicy["ORIGIN_WHEN_CROSS_ORIGIN"] = "origin-when-cross-origin";
|
|
98
98
|
ReferrerPolicy["NO_REFERRER_WHEN_DOWNGRADE"] = "no-referrer-when-downgrade";
|
|
99
99
|
ReferrerPolicy["STRICT_ORIGIN_WHEN_CROSS_ORIGIN"] = "strict-origin-when-cross-origin";
|
|
100
|
-
})(ReferrerPolicy || (ReferrerPolicy = {}));
|
|
100
|
+
})(exports.ReferrerPolicy || (exports.ReferrerPolicy = {}));
|
|
101
101
|
|
|
102
102
|
class RequestError extends Error {
|
|
103
103
|
constructor(message, url, method, options = {}) {
|
|
@@ -800,6 +800,9 @@
|
|
|
800
800
|
};
|
|
801
801
|
return this;
|
|
802
802
|
}
|
|
803
|
+
withHeader(key, value) {
|
|
804
|
+
return this.withHeaders({ [key]: value });
|
|
805
|
+
}
|
|
803
806
|
withTimeout(timeout) {
|
|
804
807
|
if (!Number.isFinite(timeout) || timeout <= 0)
|
|
805
808
|
throw new Error("Timeout must be a positive number");
|
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.9",
|
|
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",
|