-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.ts
More file actions
179 lines (163 loc) · 5.62 KB
/
mod.ts
File metadata and controls
179 lines (163 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
export { FetchClient } from "./src/FetchClient.ts";
export type { FetchClientOptions } from "./src/FetchClientOptions.ts";
export type { FetchClientResponse } from "./src/FetchClientResponse.ts";
export { FetchClientError } from "./src/FetchClientError.ts";
export { getStatusText } from "./src/HttpStatusText.ts";
export { ResponsePromise } from "./src/ResponsePromise.ts";
export { ProblemDetails } from "./src/ProblemDetails.ts";
export {
type CacheKey,
type CacheTag,
FetchClientCache,
} from "./src/FetchClientCache.ts";
export type { RequestOptions } from "./src/RequestOptions.ts";
export type { FetchClientMiddleware } from "./src/FetchClientMiddleware.ts";
export type { FetchClientContext } from "./src/FetchClientContext.ts";
export {
defaultInstance as defaultProviderInstance,
FetchClientProvider,
} from "./src/FetchClientProvider.ts";
export * from "./src/DefaultHelpers.ts";
export {
CircuitBreaker,
type CircuitBreakerOptions,
type CircuitState,
groupByDomain as circuitBreakerGroupByDomain,
type GroupCircuitBreakerOptions,
} from "./src/CircuitBreaker.ts";
export {
CircuitBreakerMiddleware,
type CircuitBreakerMiddlewareOptions,
CircuitOpenError,
createCircuitBreakerMiddleware,
createPerDomainCircuitBreakerMiddleware,
} from "./src/CircuitBreakerMiddleware.ts";
export {
createRetryMiddleware,
RetryMiddleware,
type RetryMiddlewareOptions,
} from "./src/RetryMiddleware.ts";
export {
createPerDomainRateLimitMiddleware,
createRateLimitMiddleware,
RateLimitError,
RateLimitMiddleware,
type RateLimitMiddlewareOptions,
} from "./src/RateLimitMiddleware.ts";
export {
groupByDomain as rateLimiterGroupByDomain,
RateLimiter,
type RateLimiterOptions,
} from "./src/RateLimiter.ts";
import { createRetryMiddleware } from "./src/RetryMiddleware.ts";
import {
createPerDomainRateLimitMiddleware,
createRateLimitMiddleware,
} from "./src/RateLimitMiddleware.ts";
import {
createCircuitBreakerMiddleware,
createPerDomainCircuitBreakerMiddleware,
} from "./src/CircuitBreakerMiddleware.ts";
import {
deleteJSON,
getJSON,
patchJSON,
postJSON,
putJSON,
useFetchClient,
useMiddleware,
} from "./src/DefaultHelpers.ts";
import type {
GetRequestOptions,
RequestOptions,
} from "./src/RequestOptions.ts";
import type { ResponsePromise } from "./src/ResponsePromise.ts";
/**
* Convenience middleware factory functions for use with FetchClient.use()
*
* @example
* ```typescript
* import { FetchClient, middleware } from "@foundatiofx/fetchclient";
*
* const client = new FetchClient();
* client.use(
* middleware.retry({ limit: 3 }),
* middleware.rateLimit({ maxRequests: 100, windowSeconds: 60 }),
* middleware.circuitBreaker({ failureThreshold: 5 })
* );
* ```
*/
export const middleware = {
/** Retry failed requests with exponential backoff and jitter */
retry: createRetryMiddleware,
/** Rate limit requests to prevent overwhelming servers */
rateLimit: createRateLimitMiddleware,
/** Per-domain rate limit (each domain tracked separately) */
perDomainRateLimit: createPerDomainRateLimitMiddleware,
/** Circuit breaker for fault tolerance */
circuitBreaker: createCircuitBreakerMiddleware,
/** Per-domain circuit breaker (each domain tracked separately) */
perDomainCircuitBreaker: createPerDomainCircuitBreakerMiddleware,
};
/**
* Default export for convenient access to all HTTP methods.
*
* @example
* ```typescript
* import fc from "@foundatiofx/fetchclient";
*
* // Configure middleware
* fc.use(fc.middleware.retry({ limit: 3 }));
*
* // Use JSON methods (recommended)
* const { data: user } = await fc.getJSON<User>("/api/user/1");
* const { data: created } = await fc.postJSON<User>("/api/users", { name: "Alice" });
*
* // Or use fluent API for other response types
* const html = await fc.get("/page").text();
* ```
*/
const fetchClient = {
/** Sends a GET request. Use `.json<T>()` for typed JSON response. */
get: (url: string, options?: GetRequestOptions): ResponsePromise<unknown> =>
useFetchClient().get(url, options),
/** Sends a POST request. Use `.json<T>()` for typed JSON response. */
post: (
url: string,
body?: object | string | FormData,
options?: RequestOptions,
): ResponsePromise<unknown> => useFetchClient().post(url, body, options),
/** Sends a PUT request. Use `.json<T>()` for typed JSON response. */
put: (
url: string,
body?: object | string | FormData,
options?: RequestOptions,
): ResponsePromise<unknown> => useFetchClient().put(url, body, options),
/** Sends a PATCH request. Use `.json<T>()` for typed JSON response. */
patch: (
url: string,
body?: object | string | FormData,
options?: RequestOptions,
): ResponsePromise<unknown> => useFetchClient().patch(url, body, options),
/** Sends a DELETE request. Use `.json<T>()` for typed JSON response. */
delete: (url: string, options?: RequestOptions): ResponsePromise<unknown> =>
useFetchClient().delete(url, options),
/** Sends a HEAD request. */
head: (url: string, options?: GetRequestOptions): ResponsePromise<void> =>
useFetchClient().head(url, options),
/** Sends a GET request and returns parsed JSON in response.data */
getJSON,
/** Sends a POST request and returns parsed JSON in response.data */
postJSON,
/** Sends a PUT request and returns parsed JSON in response.data */
putJSON,
/** Sends a PATCH request and returns parsed JSON in response.data */
patchJSON,
/** Sends a DELETE request and returns parsed JSON in response.data */
deleteJSON,
/** Adds middleware to the default provider */
use: useMiddleware,
/** Middleware factory functions */
middleware,
};
export default fetchClient;