@foundatiofx/fetchclient 1.1.0 → 1.2.0
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/esm/mod.js +83 -0
- package/esm/src/DefaultHelpers.js +12 -5
- package/esm/src/FetchClient.js +66 -41
- package/esm/src/FetchClientError.js +73 -0
- package/esm/src/FetchClientProvider.js +24 -0
- package/esm/src/RateLimitMiddleware.js +35 -1
- package/esm/src/ResponsePromise.js +163 -0
- package/esm/src/RetryMiddleware.js +179 -0
- package/esm/src/mocks/MockHistory.js +8 -0
- package/esm/src/mocks/MockRegistry.js +7 -0
- package/package.json +1 -1
- package/readme.md +8 -15
- package/script/mod.js +94 -1
- package/script/src/DefaultHelpers.js +13 -5
- package/script/src/FetchClient.js +66 -41
- package/script/src/FetchClientError.js +77 -0
- package/script/src/FetchClientProvider.js +24 -0
- package/script/src/RateLimitMiddleware.js +36 -0
- package/script/src/ResponsePromise.js +167 -0
- package/script/src/RetryMiddleware.js +184 -0
- package/script/src/mocks/MockHistory.js +8 -0
- package/script/src/mocks/MockRegistry.js +7 -0
- package/types/mod.d.ts +96 -0
- package/types/mod.d.ts.map +1 -1
- package/types/src/DefaultHelpers.d.ts +11 -5
- package/types/src/DefaultHelpers.d.ts.map +1 -1
- package/types/src/FetchClient.d.ts +31 -15
- package/types/src/FetchClient.d.ts.map +1 -1
- package/types/src/FetchClientError.d.ts +29 -0
- package/types/src/FetchClientError.d.ts.map +1 -0
- package/types/src/FetchClientProvider.d.ts +11 -0
- package/types/src/FetchClientProvider.d.ts.map +1 -1
- package/types/src/RateLimitMiddleware.d.ts +27 -0
- package/types/src/RateLimitMiddleware.d.ts.map +1 -1
- package/types/src/ResponsePromise.d.ts +93 -0
- package/types/src/ResponsePromise.d.ts.map +1 -0
- package/types/src/RetryMiddleware.d.ts +88 -0
- package/types/src/RetryMiddleware.d.ts.map +1 -0
- package/types/src/mocks/MockHistory.d.ts +1 -0
- package/types/src/mocks/MockHistory.d.ts.map +1 -1
- package/types/src/mocks/MockRegistry.d.ts +5 -0
- package/types/src/mocks/MockRegistry.d.ts.map +1 -1
- package/types/src/mocks/types.d.ts +2 -0
- package/types/src/mocks/types.d.ts.map +1 -1
- package/types/src/tests/RetryMiddleware.test.d.ts.map +1 -0
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RetryMiddleware = void 0;
|
|
4
|
+
exports.createRetryMiddleware = createRetryMiddleware;
|
|
5
|
+
/**
|
|
6
|
+
* Default HTTP methods that are eligible for retry.
|
|
7
|
+
* These are idempotent methods that can be safely retried without side effects.
|
|
8
|
+
*/
|
|
9
|
+
const DEFAULT_RETRY_METHODS = [
|
|
10
|
+
"GET",
|
|
11
|
+
"HEAD",
|
|
12
|
+
"PUT",
|
|
13
|
+
"DELETE",
|
|
14
|
+
"OPTIONS",
|
|
15
|
+
"TRACE",
|
|
16
|
+
];
|
|
17
|
+
/**
|
|
18
|
+
* Default HTTP status codes that trigger a retry.
|
|
19
|
+
*/
|
|
20
|
+
const DEFAULT_RETRY_STATUS_CODES = [
|
|
21
|
+
408, // Request Timeout
|
|
22
|
+
413, // Payload Too Large (rate limiting)
|
|
23
|
+
429, // Too Many Requests
|
|
24
|
+
500, // Internal Server Error
|
|
25
|
+
502, // Bad Gateway
|
|
26
|
+
503, // Service Unavailable
|
|
27
|
+
504, // Gateway Timeout
|
|
28
|
+
];
|
|
29
|
+
/**
|
|
30
|
+
* Retry middleware that automatically retries failed requests with exponential backoff.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* const provider = new FetchClientProvider();
|
|
35
|
+
* provider.useRetry({
|
|
36
|
+
* limit: 3,
|
|
37
|
+
* statusCodes: [500, 502, 503, 504],
|
|
38
|
+
* jitter: 0.1,
|
|
39
|
+
* });
|
|
40
|
+
*
|
|
41
|
+
* const client = provider.getFetchClient();
|
|
42
|
+
* const response = await client.getJSON('/api/data');
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
class RetryMiddleware {
|
|
46
|
+
#options;
|
|
47
|
+
constructor(options) {
|
|
48
|
+
this.#options = {
|
|
49
|
+
limit: options?.limit ?? 2,
|
|
50
|
+
methods: (options?.methods ?? DEFAULT_RETRY_METHODS).map((m) => m.toUpperCase()),
|
|
51
|
+
statusCodes: options?.statusCodes ?? DEFAULT_RETRY_STATUS_CODES,
|
|
52
|
+
maxRetryAfter: options?.maxRetryAfter ?? Infinity,
|
|
53
|
+
backoffLimit: options?.backoffLimit ?? 30000,
|
|
54
|
+
jitter: options?.jitter ?? 0.1,
|
|
55
|
+
delay: options?.delay,
|
|
56
|
+
shouldRetry: options?.shouldRetry,
|
|
57
|
+
onRetry: options?.onRetry,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Creates the middleware function.
|
|
62
|
+
* @returns The middleware function
|
|
63
|
+
*/
|
|
64
|
+
middleware() {
|
|
65
|
+
return async (context, next) => {
|
|
66
|
+
const method = context.request.method.toUpperCase();
|
|
67
|
+
// Check if method is eligible for retry
|
|
68
|
+
if (!this.#options.methods.includes(method)) {
|
|
69
|
+
await next();
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
let attemptNumber = 0;
|
|
73
|
+
while (true) {
|
|
74
|
+
// Store retry metadata in context for observability
|
|
75
|
+
if (attemptNumber > 0) {
|
|
76
|
+
context.retryAttempt = attemptNumber;
|
|
77
|
+
}
|
|
78
|
+
await next();
|
|
79
|
+
// If no response or we've exhausted retries, stop
|
|
80
|
+
if (!context.response || attemptNumber >= this.#options.limit) {
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
const response = context.response;
|
|
84
|
+
// Check if status code is retryable
|
|
85
|
+
if (!this.#options.statusCodes.includes(response.status)) {
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
// Check custom shouldRetry predicate
|
|
89
|
+
if (this.#options.shouldRetry) {
|
|
90
|
+
const shouldRetry = await this.#options.shouldRetry(response, attemptNumber);
|
|
91
|
+
if (!shouldRetry) {
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
// Calculate base delay
|
|
96
|
+
let delay = this.#calculateDelay(attemptNumber, response);
|
|
97
|
+
// Check Retry-After header
|
|
98
|
+
const retryAfterDelay = this.#parseRetryAfter(response);
|
|
99
|
+
if (retryAfterDelay !== null) {
|
|
100
|
+
// If Retry-After exceeds maxRetryAfter, don't retry
|
|
101
|
+
if (retryAfterDelay > this.#options.maxRetryAfter) {
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
// Use the larger of computed delay or Retry-After
|
|
105
|
+
delay = Math.max(delay, retryAfterDelay);
|
|
106
|
+
}
|
|
107
|
+
// Invoke onRetry callback
|
|
108
|
+
this.#options.onRetry?.(attemptNumber, response, delay);
|
|
109
|
+
// Wait before retry
|
|
110
|
+
await this.#sleep(delay);
|
|
111
|
+
// Reset response for next attempt
|
|
112
|
+
context.response = null;
|
|
113
|
+
attemptNumber++;
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Calculates the delay for a given attempt with exponential backoff and jitter.
|
|
119
|
+
*/
|
|
120
|
+
#calculateDelay(attemptNumber, response) {
|
|
121
|
+
let baseDelay;
|
|
122
|
+
if (this.#options.delay) {
|
|
123
|
+
baseDelay = this.#options.delay(attemptNumber, response);
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
// Default exponential backoff: 1s, 2s, 4s, 8s, ...
|
|
127
|
+
baseDelay = Math.min(1000 * Math.pow(2, attemptNumber), this.#options.backoffLimit);
|
|
128
|
+
}
|
|
129
|
+
// Apply jitter
|
|
130
|
+
return this.#applyJitter(baseDelay);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Applies jitter to a delay value.
|
|
134
|
+
*/
|
|
135
|
+
#applyJitter(delay) {
|
|
136
|
+
if (this.#options.jitter <= 0) {
|
|
137
|
+
return delay;
|
|
138
|
+
}
|
|
139
|
+
const jitterRange = delay * this.#options.jitter;
|
|
140
|
+
// Random value between -jitterRange and +jitterRange
|
|
141
|
+
const jitter = (Math.random() * 2 - 1) * jitterRange;
|
|
142
|
+
return Math.max(0, Math.round(delay + jitter));
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Parses the Retry-After header and returns the delay in milliseconds.
|
|
146
|
+
* Supports both delta-seconds and HTTP-date formats.
|
|
147
|
+
*/
|
|
148
|
+
#parseRetryAfter(response) {
|
|
149
|
+
const retryAfter = response.headers.get("Retry-After");
|
|
150
|
+
if (!retryAfter) {
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
// Try parsing as seconds (integer)
|
|
154
|
+
const seconds = parseInt(retryAfter, 10);
|
|
155
|
+
if (!isNaN(seconds)) {
|
|
156
|
+
return seconds * 1000;
|
|
157
|
+
}
|
|
158
|
+
// Try parsing as HTTP-date
|
|
159
|
+
const date = Date.parse(retryAfter);
|
|
160
|
+
if (!isNaN(date)) {
|
|
161
|
+
return Math.max(0, date - Date.now());
|
|
162
|
+
}
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Sleep for the specified number of milliseconds.
|
|
167
|
+
*/
|
|
168
|
+
#sleep(ms) {
|
|
169
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
exports.RetryMiddleware = RetryMiddleware;
|
|
173
|
+
/**
|
|
174
|
+
* Creates a retry middleware with the given options.
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* ```typescript
|
|
178
|
+
* const client = new FetchClient();
|
|
179
|
+
* client.use(createRetryMiddleware({ limit: 3 }));
|
|
180
|
+
* ```
|
|
181
|
+
*/
|
|
182
|
+
function createRetryMiddleware(options) {
|
|
183
|
+
return new RetryMiddleware(options).middleware();
|
|
184
|
+
}
|
|
@@ -6,6 +6,7 @@ exports.MockHistoryImpl = void 0;
|
|
|
6
6
|
*/
|
|
7
7
|
class MockHistoryImpl {
|
|
8
8
|
#get = [];
|
|
9
|
+
#head = [];
|
|
9
10
|
#post = [];
|
|
10
11
|
#put = [];
|
|
11
12
|
#patch = [];
|
|
@@ -14,6 +15,9 @@ class MockHistoryImpl {
|
|
|
14
15
|
get get() {
|
|
15
16
|
return [...this.#get];
|
|
16
17
|
}
|
|
18
|
+
get head() {
|
|
19
|
+
return [...this.#head];
|
|
20
|
+
}
|
|
17
21
|
get post() {
|
|
18
22
|
return [...this.#post];
|
|
19
23
|
}
|
|
@@ -38,6 +42,9 @@ class MockHistoryImpl {
|
|
|
38
42
|
case "GET":
|
|
39
43
|
this.#get.push(request);
|
|
40
44
|
break;
|
|
45
|
+
case "HEAD":
|
|
46
|
+
this.#head.push(request);
|
|
47
|
+
break;
|
|
41
48
|
case "POST":
|
|
42
49
|
this.#post.push(request);
|
|
43
50
|
break;
|
|
@@ -57,6 +64,7 @@ class MockHistoryImpl {
|
|
|
57
64
|
*/
|
|
58
65
|
clear() {
|
|
59
66
|
this.#get = [];
|
|
67
|
+
this.#head = [];
|
|
60
68
|
this.#post = [];
|
|
61
69
|
this.#put = [];
|
|
62
70
|
this.#patch = [];
|
|
@@ -43,6 +43,13 @@ class MockRegistry {
|
|
|
43
43
|
onGet(url) {
|
|
44
44
|
return this.#addMock("GET", url);
|
|
45
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* Creates a mock for HEAD requests matching the given URL.
|
|
48
|
+
* @param url - URL string or RegExp to match
|
|
49
|
+
*/
|
|
50
|
+
onHead(url) {
|
|
51
|
+
return this.#addMock("HEAD", url);
|
|
52
|
+
}
|
|
46
53
|
/**
|
|
47
54
|
* Creates a mock for POST requests matching the given URL.
|
|
48
55
|
* @param url - URL string or RegExp to match
|
package/types/mod.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export { FetchClient } from "./src/FetchClient.js";
|
|
2
2
|
export type { FetchClientOptions } from "./src/FetchClientOptions.js";
|
|
3
3
|
export type { FetchClientResponse } from "./src/FetchClientResponse.js";
|
|
4
|
+
export { FetchClientError } from "./src/FetchClientError.js";
|
|
5
|
+
export { ResponsePromise } from "./src/ResponsePromise.js";
|
|
4
6
|
export { ProblemDetails } from "./src/ProblemDetails.js";
|
|
5
7
|
export { type CacheKey, type CacheTag, FetchClientCache, } from "./src/FetchClientCache.js";
|
|
6
8
|
export type { RequestOptions } from "./src/RequestOptions.js";
|
|
@@ -10,4 +12,98 @@ export { defaultInstance as defaultProviderInstance, FetchClientProvider, } from
|
|
|
10
12
|
export * from "./src/DefaultHelpers.js";
|
|
11
13
|
export { CircuitBreaker, type CircuitBreakerOptions, type CircuitState, groupByDomain as circuitBreakerGroupByDomain, type GroupCircuitBreakerOptions, } from "./src/CircuitBreaker.js";
|
|
12
14
|
export { CircuitBreakerMiddleware, type CircuitBreakerMiddlewareOptions, CircuitOpenError, createCircuitBreakerMiddleware, createPerDomainCircuitBreakerMiddleware, } from "./src/CircuitBreakerMiddleware.js";
|
|
15
|
+
export { createRetryMiddleware, RetryMiddleware, type RetryMiddlewareOptions, } from "./src/RetryMiddleware.js";
|
|
16
|
+
export { createPerDomainRateLimitMiddleware, createRateLimitMiddleware, RateLimitError, RateLimitMiddleware, type RateLimitMiddlewareOptions, } from "./src/RateLimitMiddleware.js";
|
|
17
|
+
export { groupByDomain as rateLimiterGroupByDomain, RateLimiter, type RateLimiterOptions, } from "./src/RateLimiter.js";
|
|
18
|
+
import { createRetryMiddleware } from "./src/RetryMiddleware.js";
|
|
19
|
+
import { createPerDomainRateLimitMiddleware, createRateLimitMiddleware } from "./src/RateLimitMiddleware.js";
|
|
20
|
+
import { createCircuitBreakerMiddleware, createPerDomainCircuitBreakerMiddleware } from "./src/CircuitBreakerMiddleware.js";
|
|
21
|
+
import { deleteJSON, getJSON, patchJSON, postJSON, putJSON, useMiddleware } from "./src/DefaultHelpers.js";
|
|
22
|
+
import type { GetRequestOptions, RequestOptions } from "./src/RequestOptions.js";
|
|
23
|
+
import type { ResponsePromise } from "./src/ResponsePromise.js";
|
|
24
|
+
/**
|
|
25
|
+
* Convenience middleware factory functions for use with FetchClient.use()
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* import { FetchClient, middleware } from "@foundatiofx/fetchclient";
|
|
30
|
+
*
|
|
31
|
+
* const client = new FetchClient();
|
|
32
|
+
* client.use(
|
|
33
|
+
* middleware.retry({ limit: 3 }),
|
|
34
|
+
* middleware.rateLimit({ maxRequests: 100, windowSeconds: 60 }),
|
|
35
|
+
* middleware.circuitBreaker({ failureThreshold: 5 })
|
|
36
|
+
* );
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export declare const middleware: {
|
|
40
|
+
/** Retry failed requests with exponential backoff and jitter */
|
|
41
|
+
retry: typeof createRetryMiddleware;
|
|
42
|
+
/** Rate limit requests to prevent overwhelming servers */
|
|
43
|
+
rateLimit: typeof createRateLimitMiddleware;
|
|
44
|
+
/** Per-domain rate limit (each domain tracked separately) */
|
|
45
|
+
perDomainRateLimit: typeof createPerDomainRateLimitMiddleware;
|
|
46
|
+
/** Circuit breaker for fault tolerance */
|
|
47
|
+
circuitBreaker: typeof createCircuitBreakerMiddleware;
|
|
48
|
+
/** Per-domain circuit breaker (each domain tracked separately) */
|
|
49
|
+
perDomainCircuitBreaker: typeof createPerDomainCircuitBreakerMiddleware;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Default export for convenient access to all HTTP methods.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```typescript
|
|
56
|
+
* import fc from "@foundatiofx/fetchclient";
|
|
57
|
+
*
|
|
58
|
+
* // Configure middleware
|
|
59
|
+
* fc.use(fc.middleware.retry({ limit: 3 }));
|
|
60
|
+
*
|
|
61
|
+
* // Use JSON methods (recommended)
|
|
62
|
+
* const { data: user } = await fc.getJSON<User>("/api/user/1");
|
|
63
|
+
* const { data: created } = await fc.postJSON<User>("/api/users", { name: "Alice" });
|
|
64
|
+
*
|
|
65
|
+
* // Or use fluent API for other response types
|
|
66
|
+
* const html = await fc.get("/page").text();
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
declare const fetchClient: {
|
|
70
|
+
/** Sends a GET request. Use `.json<T>()` for typed JSON response. */
|
|
71
|
+
get: (url: string, options?: GetRequestOptions) => ResponsePromise<unknown>;
|
|
72
|
+
/** Sends a POST request. Use `.json<T>()` for typed JSON response. */
|
|
73
|
+
post: (url: string, body?: object | string | FormData, options?: RequestOptions) => ResponsePromise<unknown>;
|
|
74
|
+
/** Sends a PUT request. Use `.json<T>()` for typed JSON response. */
|
|
75
|
+
put: (url: string, body?: object | string | FormData, options?: RequestOptions) => ResponsePromise<unknown>;
|
|
76
|
+
/** Sends a PATCH request. Use `.json<T>()` for typed JSON response. */
|
|
77
|
+
patch: (url: string, body?: object | string | FormData, options?: RequestOptions) => ResponsePromise<unknown>;
|
|
78
|
+
/** Sends a DELETE request. Use `.json<T>()` for typed JSON response. */
|
|
79
|
+
delete: (url: string, options?: RequestOptions) => ResponsePromise<unknown>;
|
|
80
|
+
/** Sends a HEAD request. */
|
|
81
|
+
head: (url: string, options?: GetRequestOptions) => ResponsePromise<void>;
|
|
82
|
+
/** Sends a GET request and returns parsed JSON in response.data */
|
|
83
|
+
getJSON: typeof getJSON;
|
|
84
|
+
/** Sends a POST request and returns parsed JSON in response.data */
|
|
85
|
+
postJSON: typeof postJSON;
|
|
86
|
+
/** Sends a PUT request and returns parsed JSON in response.data */
|
|
87
|
+
putJSON: typeof putJSON;
|
|
88
|
+
/** Sends a PATCH request and returns parsed JSON in response.data */
|
|
89
|
+
patchJSON: typeof patchJSON;
|
|
90
|
+
/** Sends a DELETE request and returns parsed JSON in response.data */
|
|
91
|
+
deleteJSON: typeof deleteJSON;
|
|
92
|
+
/** Adds middleware to the default provider */
|
|
93
|
+
use: typeof useMiddleware;
|
|
94
|
+
/** Middleware factory functions */
|
|
95
|
+
middleware: {
|
|
96
|
+
/** Retry failed requests with exponential backoff and jitter */
|
|
97
|
+
retry: typeof createRetryMiddleware;
|
|
98
|
+
/** Rate limit requests to prevent overwhelming servers */
|
|
99
|
+
rateLimit: typeof createRateLimitMiddleware;
|
|
100
|
+
/** Per-domain rate limit (each domain tracked separately) */
|
|
101
|
+
perDomainRateLimit: typeof createPerDomainRateLimitMiddleware;
|
|
102
|
+
/** Circuit breaker for fault tolerance */
|
|
103
|
+
circuitBreaker: typeof createCircuitBreakerMiddleware;
|
|
104
|
+
/** Per-domain circuit breaker (each domain tracked separately) */
|
|
105
|
+
perDomainCircuitBreaker: typeof createPerDomainCircuitBreakerMiddleware;
|
|
106
|
+
};
|
|
107
|
+
};
|
|
108
|
+
export default fetchClient;
|
|
13
109
|
//# sourceMappingURL=mod.d.ts.map
|
package/types/mod.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACnD,YAAY,EAAE,kBAAkB,EAAE,MAAM,6BAA6B,CAAC;AACtE,YAAY,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AACxE,OAAO,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AACzD,OAAO,EACL,KAAK,QAAQ,EACb,KAAK,QAAQ,EACb,gBAAgB,GACjB,MAAM,2BAA2B,CAAC;AACnC,YAAY,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAC9D,YAAY,EAAE,qBAAqB,EAAE,MAAM,gCAAgC,CAAC;AAC5E,YAAY,EAAE,kBAAkB,EAAE,MAAM,6BAA6B,CAAC;AACtE,OAAO,EACL,eAAe,IAAI,uBAAuB,EAC1C,mBAAmB,GACp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,cAAc,KACvB,eAAe,CAAC,OAAO,CAAC;IAE3B,uEAAuE;iBAEhE,MAAM,SACJ,MAAM,GAAG,MAAM,GAAG,QAAQ,YACvB,cAAc,KACvB,eAAe,CAAC,OAAO,CAAC;IAE3B,wEAAwE;kBAC1D,MAAM,YAAY,cAAc,KAAG,eAAe,CAAC,OAAO,CAAC;IAGzE,4BAA4B;gBAChB,MAAM,YAAY,iBAAiB,KAAG,eAAe,CAAC,IAAI,CAAC;IAGvE,mEAAmE;;IAGnE,oEAAoE;;IAGpE,mEAAmE;;IAGnE,qEAAqE;;IAGrE,sEAAsE;;IAGtE,8CAA8C;;IAG9C,mCAAmC;;QAlFnC,gEAAgE;;QAEhE,0DAA0D;;QAE1D,6DAA6D;;QAE7D,0CAA0C;;QAE1C,kEAAkE;;;CA4EnE,CAAC;AAEF,eAAe,WAAW,CAAC"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { FetchClient } from "./FetchClient.js";
|
|
2
|
+
import type { FetchClientCache } from "./FetchClientCache.js";
|
|
2
3
|
import type { FetchClientMiddleware } from "./FetchClientMiddleware.js";
|
|
3
4
|
import type { FetchClientOptions } from "./FetchClientOptions.js";
|
|
4
5
|
import { type FetchClientProvider } from "./FetchClientProvider.js";
|
|
@@ -16,7 +17,7 @@ export declare function useFetchClient(options?: FetchClientOptions): FetchClien
|
|
|
16
17
|
* Sends a GET request to the specified URL using the default client and provider and returns the response as JSON.
|
|
17
18
|
* @param url - The URL to send the GET request to.
|
|
18
19
|
* @param options - Optional request options.
|
|
19
|
-
* @returns A promise that resolves to the response
|
|
20
|
+
* @returns A promise that resolves to the response with parsed JSON in `data`.
|
|
20
21
|
*/
|
|
21
22
|
export declare function getJSON<T>(url: string, options?: GetRequestOptions): Promise<FetchClientResponse<T>>;
|
|
22
23
|
/**
|
|
@@ -26,7 +27,7 @@ export declare function getJSON<T>(url: string, options?: GetRequestOptions): Pr
|
|
|
26
27
|
* @param {string} url - The URL to send the request to.
|
|
27
28
|
* @param {object | string | FormData} [body] - The JSON payload or form data to send with the request.
|
|
28
29
|
* @param {RequestOptions} [options] - Additional options for the request.
|
|
29
|
-
* @returns
|
|
30
|
+
* @returns A promise that resolves to the response with parsed JSON in `data`.
|
|
30
31
|
*/
|
|
31
32
|
export declare function postJSON<T>(url: string, body?: object | string | FormData, options?: RequestOptions): Promise<FetchClientResponse<T>>;
|
|
32
33
|
/**
|
|
@@ -36,7 +37,7 @@ export declare function postJSON<T>(url: string, body?: object | string | FormDa
|
|
|
36
37
|
* @param {string} url - The URL to send the request to.
|
|
37
38
|
* @param {object | string} [body] - The JSON payload to send with the request.
|
|
38
39
|
* @param {RequestOptions} [options] - Additional options for the request.
|
|
39
|
-
* @returns
|
|
40
|
+
* @returns A promise that resolves to the response with parsed JSON in `data`.
|
|
40
41
|
*/
|
|
41
42
|
export declare function putJSON<T>(url: string, body?: object | string, options?: RequestOptions): Promise<FetchClientResponse<T>>;
|
|
42
43
|
/**
|
|
@@ -46,7 +47,7 @@ export declare function putJSON<T>(url: string, body?: object | string, options?
|
|
|
46
47
|
* @param {string} url - The URL to send the request to.
|
|
47
48
|
* @param {object | string} [body] - The JSON payload to send with the request.
|
|
48
49
|
* @param {RequestOptions} [options] - Additional options for the request.
|
|
49
|
-
* @returns
|
|
50
|
+
* @returns A promise that resolves to the response with parsed JSON in `data`.
|
|
50
51
|
*/
|
|
51
52
|
export declare function patchJSON<T>(url: string, body?: object | string, options?: RequestOptions): Promise<FetchClientResponse<T>>;
|
|
52
53
|
/**
|
|
@@ -55,7 +56,7 @@ export declare function patchJSON<T>(url: string, body?: object | string, option
|
|
|
55
56
|
* @template T - The type of the response data.
|
|
56
57
|
* @param {string} url - The URL to send the request to.
|
|
57
58
|
* @param {RequestOptions} [options] - Additional options for the request.
|
|
58
|
-
* @returns
|
|
59
|
+
* @returns A promise that resolves to the response with parsed JSON in `data`.
|
|
59
60
|
*/
|
|
60
61
|
export declare function deleteJSON<T>(url: string, options?: RequestOptions): Promise<FetchClientResponse<T>>;
|
|
61
62
|
/**
|
|
@@ -63,6 +64,11 @@ export declare function deleteJSON<T>(url: string, options?: RequestOptions): Pr
|
|
|
63
64
|
* @returns The current FetchClientProvider.
|
|
64
65
|
*/
|
|
65
66
|
export declare function getCurrentProvider(): FetchClientProvider;
|
|
67
|
+
/**
|
|
68
|
+
* Gets the cache from the current provider.
|
|
69
|
+
* @returns The FetchClientCache instance.
|
|
70
|
+
*/
|
|
71
|
+
export declare function getCache(): FetchClientCache;
|
|
66
72
|
/**
|
|
67
73
|
* Sets the function that retrieves the current FetchClientProvider using whatever scoping mechanism is available.
|
|
68
74
|
* @param getProviderFunc - The function that retrieves the current FetchClientProvider.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DefaultHelpers.d.ts","sourceRoot":"","sources":["../../src/src/DefaultHelpers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AACpD,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AACxE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAClE,OAAO,EAEL,KAAK,mBAAmB,EACzB,MAAM,0BAA0B,CAAC;AAClC,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AACpE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAC1D,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,0BAA0B,CAAC;AAC3E,OAAO,KAAK,EAAE,+BAA+B,EAAE,MAAM,+BAA+B,CAAC;AACrF,OAAO,KAAK,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAI7E;;;GAGG;AACH,wBAAgB,cAAc,CAAC,OAAO,CAAC,EAAE,kBAAkB,GAAG,WAAW,CAExE;AAED;;;;;GAKG;AACH,wBAAgB,OAAO,CAAC,CAAC,EACvB,GAAG,EAA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mBAAmB,GAAG,IAAI,QAGlD;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,OAAO,EAAE,MAAM,QAEzC;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAChC,eAAe,EAAE,MAAM,MAAM,GAAG,IAAI,QAGrC;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAC/B,QAAQ,EAAE,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,KAAK,OAAO,CAAC,cAAc,GAAG,IAAI,CAAC,QAGnE;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,UAAU,EAAE,qBAAqB,QAE9D;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,cAAc,QAExD;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAC1B,OAAO,EAAE,0BAA0B,QAGpC;AAED;;;GAGG;AACH,wBAAgB,qBAAqB,CACnC,OAAO,EAAE,IAAI,CAAC,0BAA0B,EAAE,cAAc,CAAC,QAG1D;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,CAAC,EAAE,+BAA+B,QAE1E;AAED;;;;GAIG;AACH,wBAAgB,0BAA0B,CACxC,OAAO,CAAC,EAAE,IAAI,CAAC,+BAA+B,EAAE,cAAc,CAAC,QAGhE"}
|
|
@@ -5,6 +5,7 @@ import type { FetchClientCache } from "./FetchClientCache.js";
|
|
|
5
5
|
import { FetchClientProvider } from "./FetchClientProvider.js";
|
|
6
6
|
import type { FetchClientOptions } from "./FetchClientOptions.js";
|
|
7
7
|
import { type IObjectEvent } from "./ObjectEvent.js";
|
|
8
|
+
import { ResponsePromise } from "./ResponsePromise.js";
|
|
8
9
|
type Fetch = typeof globalThis.fetch;
|
|
9
10
|
/**
|
|
10
11
|
* Represents a client for making HTTP requests using the Fetch API.
|
|
@@ -57,14 +58,16 @@ export declare class FetchClient {
|
|
|
57
58
|
*
|
|
58
59
|
* @param url - The URL to send the GET request to.
|
|
59
60
|
* @param options - The optional request options.
|
|
60
|
-
* @returns A
|
|
61
|
+
* @returns A ResponsePromise that resolves to the response. Can use `.json<T>()` for typed JSON.
|
|
61
62
|
*/
|
|
62
|
-
get(url: string, options?: GetRequestOptions):
|
|
63
|
+
get(url: string, options?: GetRequestOptions): ResponsePromise<unknown>;
|
|
63
64
|
/**
|
|
64
65
|
* Sends a GET request to the specified URL and returns the response as JSON.
|
|
66
|
+
* The response will have the parsed JSON in `response.data`.
|
|
67
|
+
*
|
|
65
68
|
* @param url - The URL to send the GET request to.
|
|
66
69
|
* @param options - Optional request options.
|
|
67
|
-
* @returns A promise that resolves to the response
|
|
70
|
+
* @returns A promise that resolves to the response with parsed JSON in `data`.
|
|
68
71
|
*/
|
|
69
72
|
getJSON<T>(url: string, options?: GetRequestOptions): Promise<FetchClientResponse<T>>;
|
|
70
73
|
/**
|
|
@@ -73,17 +76,18 @@ export declare class FetchClient {
|
|
|
73
76
|
* @param url - The URL to send the request to.
|
|
74
77
|
* @param body - The request body, can be an object, a string, or FormData.
|
|
75
78
|
* @param options - Additional options for the request.
|
|
76
|
-
* @returns A
|
|
79
|
+
* @returns A ResponsePromise that resolves to the response. Can use `.json<T>()` for typed JSON.
|
|
77
80
|
*/
|
|
78
|
-
post(url: string, body?: object | string | FormData, options?: RequestOptions):
|
|
81
|
+
post(url: string, body?: object | string | FormData, options?: RequestOptions): ResponsePromise<unknown>;
|
|
79
82
|
/**
|
|
80
83
|
* Sends a POST request with JSON payload to the specified URL.
|
|
84
|
+
* The response will have the parsed JSON in `response.data`.
|
|
81
85
|
*
|
|
82
86
|
* @template T - The type of the response data.
|
|
83
87
|
* @param {string} url - The URL to send the request to.
|
|
84
88
|
* @param {object | string | FormData} [body] - The JSON payload or form data to send with the request.
|
|
85
89
|
* @param {RequestOptions} [options] - Additional options for the request.
|
|
86
|
-
* @returns
|
|
90
|
+
* @returns A promise that resolves to the response with parsed JSON in `data`.
|
|
87
91
|
*/
|
|
88
92
|
postJSON<T>(url: string, body?: object | string | FormData, options?: RequestOptions): Promise<FetchClientResponse<T>>;
|
|
89
93
|
/**
|
|
@@ -91,17 +95,18 @@ export declare class FetchClient {
|
|
|
91
95
|
* @param url - The URL to send the request to.
|
|
92
96
|
* @param body - The request body, can be an object, a string, or FormData.
|
|
93
97
|
* @param options - The request options.
|
|
94
|
-
* @returns A
|
|
98
|
+
* @returns A ResponsePromise that resolves to the response. Can use `.json<T>()` for typed JSON.
|
|
95
99
|
*/
|
|
96
|
-
put(url: string, body?: object | string | FormData, options?: RequestOptions):
|
|
100
|
+
put(url: string, body?: object | string | FormData, options?: RequestOptions): ResponsePromise<unknown>;
|
|
97
101
|
/**
|
|
98
102
|
* Sends a PUT request with JSON payload to the specified URL.
|
|
103
|
+
* The response will have the parsed JSON in `response.data`.
|
|
99
104
|
*
|
|
100
105
|
* @template T - The type of the response data.
|
|
101
106
|
* @param {string} url - The URL to send the request to.
|
|
102
107
|
* @param {object | string} [body] - The JSON payload to send with the request.
|
|
103
108
|
* @param {RequestOptions} [options] - Additional options for the request.
|
|
104
|
-
* @returns
|
|
109
|
+
* @returns A promise that resolves to the response with parsed JSON in `data`.
|
|
105
110
|
*/
|
|
106
111
|
putJSON<T>(url: string, body?: object | string, options?: RequestOptions): Promise<FetchClientResponse<T>>;
|
|
107
112
|
/**
|
|
@@ -109,17 +114,18 @@ export declare class FetchClient {
|
|
|
109
114
|
* @param url - The URL to send the PATCH request to.
|
|
110
115
|
* @param body - The body of the request. It can be an object, a string, or FormData.
|
|
111
116
|
* @param options - The options for the request.
|
|
112
|
-
* @returns A
|
|
117
|
+
* @returns A ResponsePromise that resolves to the response. Can use `.json<T>()` for typed JSON.
|
|
113
118
|
*/
|
|
114
|
-
patch(url: string, body?: object | string | FormData, options?: RequestOptions):
|
|
119
|
+
patch(url: string, body?: object | string | FormData, options?: RequestOptions): ResponsePromise<unknown>;
|
|
115
120
|
/**
|
|
116
121
|
* Sends a PATCH request with JSON payload to the specified URL.
|
|
122
|
+
* The response will have the parsed JSON in `response.data`.
|
|
117
123
|
*
|
|
118
124
|
* @template T - The type of the response data.
|
|
119
125
|
* @param {string} url - The URL to send the request to.
|
|
120
126
|
* @param {object | string} [body] - The JSON payload to send with the request.
|
|
121
127
|
* @param {RequestOptions} [options] - Additional options for the request.
|
|
122
|
-
* @returns
|
|
128
|
+
* @returns A promise that resolves to the response with parsed JSON in `data`.
|
|
123
129
|
*/
|
|
124
130
|
patchJSON<T>(url: string, body?: object | string, options?: RequestOptions): Promise<FetchClientResponse<T>>;
|
|
125
131
|
/**
|
|
@@ -127,18 +133,28 @@ export declare class FetchClient {
|
|
|
127
133
|
*
|
|
128
134
|
* @param url - The URL to send the DELETE request to.
|
|
129
135
|
* @param options - The options for the request.
|
|
130
|
-
* @returns A
|
|
136
|
+
* @returns A ResponsePromise that resolves to the response. Can use `.json<T>()` for typed JSON.
|
|
131
137
|
*/
|
|
132
|
-
delete(url: string, options?: RequestOptions):
|
|
138
|
+
delete(url: string, options?: RequestOptions): ResponsePromise<unknown>;
|
|
133
139
|
/**
|
|
134
140
|
* Sends a DELETE request with JSON payload to the specified URL.
|
|
141
|
+
* The response will have the parsed JSON in `response.data`.
|
|
135
142
|
*
|
|
136
143
|
* @template T - The type of the response data.
|
|
137
144
|
* @param {string} url - The URL to send the request to.
|
|
138
145
|
* @param {RequestOptions} [options] - Additional options for the request.
|
|
139
|
-
* @returns
|
|
146
|
+
* @returns A promise that resolves to the response with parsed JSON in `data`.
|
|
140
147
|
*/
|
|
141
148
|
deleteJSON<T>(url: string, options?: RequestOptions): Promise<FetchClientResponse<T>>;
|
|
149
|
+
/**
|
|
150
|
+
* Sends a HEAD request to the specified URL.
|
|
151
|
+
* HEAD requests are identical to GET requests but without the response body.
|
|
152
|
+
*
|
|
153
|
+
* @param url - The URL to send the HEAD request to.
|
|
154
|
+
* @param options - The optional request options.
|
|
155
|
+
* @returns A ResponsePromise that resolves to the response.
|
|
156
|
+
*/
|
|
157
|
+
head(url: string, options?: GetRequestOptions): ResponsePromise<void>;
|
|
142
158
|
private validate;
|
|
143
159
|
private fetchInternal;
|
|
144
160
|
private invokeMiddleware;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FetchClient.d.ts","sourceRoot":"","sources":["../../src/src/FetchClient.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAE7E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AACpE,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AAGxE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAC9D,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAE/D,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAClE,OAAO,EAAE,KAAK,YAAY,EAAe,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"FetchClient.d.ts","sourceRoot":"","sources":["../../src/src/FetchClient.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAE7E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AACpE,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AAGxE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAC9D,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAE/D,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAClE,OAAO,EAAE,KAAK,YAAY,EAAe,MAAM,kBAAkB,CAAC;AAClE,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAGvD,KAAK,KAAK,GAAG,OAAO,UAAU,CAAC,KAAK,CAAC;AAKrC;;GAEG;AACH,qBAAa,WAA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iBAAiB;IAiCzB,OAAO,CAAC,QAAQ;IAuDhB,OAAO,CAAC,gBAAgB;CAiCzB"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { FetchClientResponse } from "./FetchClientResponse.js";
|
|
2
|
+
/**
|
|
3
|
+
* Error wrapper for non-2xx responses.
|
|
4
|
+
* Exposes the underlying response for compatibility and debugging.
|
|
5
|
+
*/
|
|
6
|
+
export declare class FetchClientError extends Error {
|
|
7
|
+
readonly response: FetchClientResponse<unknown>;
|
|
8
|
+
constructor(response: FetchClientResponse<unknown>, message?: string);
|
|
9
|
+
get status(): number;
|
|
10
|
+
get statusText(): string;
|
|
11
|
+
get ok(): boolean;
|
|
12
|
+
get headers(): Headers;
|
|
13
|
+
get url(): string;
|
|
14
|
+
get redirected(): boolean;
|
|
15
|
+
get type(): ResponseType;
|
|
16
|
+
get body(): ReadableStream<Uint8Array> | null;
|
|
17
|
+
get bodyUsed(): boolean;
|
|
18
|
+
get data(): unknown;
|
|
19
|
+
get problem(): unknown;
|
|
20
|
+
get meta(): unknown;
|
|
21
|
+
json(): Promise<unknown>;
|
|
22
|
+
text(): Promise<string>;
|
|
23
|
+
arrayBuffer(): Promise<ArrayBuffer>;
|
|
24
|
+
blob(): Promise<Blob>;
|
|
25
|
+
formData(): Promise<FormData>;
|
|
26
|
+
bytes(): Promise<Uint8Array>;
|
|
27
|
+
clone(): Response;
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=FetchClientError.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FetchClientError.d.ts","sourceRoot":"","sources":["../../src/src/FetchClientError.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAEpE;;;GAGG;AACH,qBAAa,gBAAiB,SAAQ,KAAK;IACzC,SAAgB,QAAQ,EAAE,mBAAmB,CAAC,OAAO,CAAC,CAAC;gBAGrD,QAAQ,EAAE,mBAAmB,CAAC,OAAO,CAAC,EACtC,OAAO,CAAC,EAAE,MAAM;IAWlB,IAAI,MAAM,IAAI,MAAM,CAEnB;IAED,IAAI,UAAU,IAAI,MAAM,CAEvB;IAED,IAAI,EAAE,IAAI,OAAO,CAEhB;IAED,IAAI,OAAO,IAAI,OAAO,CAErB;IAED,IAAI,GAAG,IAAI,MAAM,CAEhB;IAED,IAAI,UAAU,IAAI,OAAO,CAExB;IAED,IAAI,IAAI,IAAI,YAAY,CAEvB;IAED,IAAI,IAAI,IAAI,cAAc,CAAC,UAAU,CAAC,GAAG,IAAI,CAE5C;IAED,IAAI,QAAQ,IAAI,OAAO,CAEtB;IAED,IAAI,IAAI,IAAI,OAAO,CAElB;IAED,IAAI,OAAO,IAAI,OAAO,CAErB;IAED,IAAI,IAAI,IAAI,OAAO,CAElB;IAED,IAAI,IAAI,OAAO,CAAC,OAAO,CAAC;IAIxB,IAAI,IAAI,OAAO,CAAC,MAAM,CAAC;IAIvB,WAAW,IAAI,OAAO,CAAC,WAAW,CAAC;IAInC,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAIrB,QAAQ,IAAI,OAAO,CAAC,QAAQ,CAAC;IAK7B,KAAK,IAAI,OAAO,CAAC,UAAU,CAAC;IAK5B,KAAK,IAAI,QAAQ;CAGlB"}
|
|
@@ -9,6 +9,7 @@ import { type RateLimitMiddlewareOptions } from "./RateLimitMiddleware.js";
|
|
|
9
9
|
import { type RateLimiter } from "./RateLimiter.js";
|
|
10
10
|
import { type CircuitBreakerMiddlewareOptions } from "./CircuitBreakerMiddleware.js";
|
|
11
11
|
import { type CircuitBreaker } from "./CircuitBreaker.js";
|
|
12
|
+
import { type RetryMiddlewareOptions } from "./RetryMiddleware.js";
|
|
12
13
|
type Fetch = typeof globalThis.fetch;
|
|
13
14
|
/**
|
|
14
15
|
* Represents a provider for creating instances of the FetchClient class with shared default options and cache.
|
|
@@ -127,6 +128,16 @@ export declare class FetchClientProvider {
|
|
|
127
128
|
* Removes the circuit breaker middleware from all FetchClient instances created by this provider.
|
|
128
129
|
*/
|
|
129
130
|
removeCircuitBreaker(): void;
|
|
131
|
+
/**
|
|
132
|
+
* Enables automatic retry for failed requests.
|
|
133
|
+
* Retries are performed with exponential backoff and jitter.
|
|
134
|
+
* @param options - The retry configuration options.
|
|
135
|
+
*/
|
|
136
|
+
useRetry(options?: RetryMiddlewareOptions): void;
|
|
137
|
+
/**
|
|
138
|
+
* Removes the retry middleware from all FetchClient instances created by this provider.
|
|
139
|
+
*/
|
|
140
|
+
removeRetry(): void;
|
|
130
141
|
}
|
|
131
142
|
export declare const defaultInstance: FetchClientProvider;
|
|
132
143
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FetchClientProvider.d.ts","sourceRoot":"","sources":["../../src/src/FetchClientProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AACxE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAC1D,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAClE,OAAO,EAAE,KAAK,YAAY,EAAe,MAAM,kBAAkB,CAAC;AAClE,OAAO,EAEL,KAAK,0BAA0B,EAChC,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAiB,KAAK,WAAW,EAAE,MAAM,kBAAkB,CAAC;AACnE,OAAO,EAEL,KAAK,+BAA+B,EACrC,MAAM,+BAA+B,CAAC;AACvC,OAAO,EACL,KAAK,cAAc,EAEpB,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"FetchClientProvider.d.ts","sourceRoot":"","sources":["../../src/src/FetchClientProvider.ts"],"names":[],"mappings":"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