@fgrzl/fetch 1.4.0-alpha.2 → 1.4.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/CONTRIBUTING.md +2 -2
- package/LICENSE +201 -21
- package/README.md +88 -82
- package/dist/cjs/index.js +146 -76
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/index.min.js +1 -1
- package/dist/cjs/index.min.js.map +1 -1
- package/dist/index.d.ts +124 -77
- package/dist/index.js +127 -79
- package/dist/index.js.map +1 -1
- package/dist/index.min.js +1 -1
- package/dist/index.min.js.map +1 -1
- package/package.json +67 -16
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileoverview Type definitions for the HTTP client.
|
|
3
|
-
*
|
|
4
|
-
* This file contains core TypeScript interfaces and types for FetchClient.
|
|
5
|
-
* Designed for discoverability and type safety.
|
|
6
|
-
*/
|
|
7
1
|
/**
|
|
8
2
|
* Typed response wrapper with consistent shape.
|
|
9
3
|
*
|
|
@@ -73,6 +67,20 @@ interface FetchClientOptions {
|
|
|
73
67
|
* ```
|
|
74
68
|
*/
|
|
75
69
|
baseUrl?: string;
|
|
70
|
+
/**
|
|
71
|
+
* Default timeout for requests in milliseconds.
|
|
72
|
+
*
|
|
73
|
+
* When set, requests will automatically be aborted after this duration.
|
|
74
|
+
* Individual requests can override this by providing their own timeout or signal.
|
|
75
|
+
* Set to 0 or undefined for no timeout.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```typescript
|
|
79
|
+
* const client = new FetchClient({ timeout: 5000 }); // 5 second timeout
|
|
80
|
+
* await client.get('/api/users'); // Will timeout after 5 seconds
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
timeout?: number;
|
|
76
84
|
}
|
|
77
85
|
|
|
78
86
|
/**
|
|
@@ -130,6 +138,7 @@ declare class FetchClient {
|
|
|
130
138
|
private middlewares;
|
|
131
139
|
private credentials;
|
|
132
140
|
private baseUrl;
|
|
141
|
+
private defaultTimeout;
|
|
133
142
|
constructor(config?: FetchClientOptions);
|
|
134
143
|
use(middleware: FetchMiddleware): this;
|
|
135
144
|
/**
|
|
@@ -152,12 +161,15 @@ declare class FetchClient {
|
|
|
152
161
|
*
|
|
153
162
|
* @example Chain with middleware:
|
|
154
163
|
* ```typescript
|
|
155
|
-
* const client =
|
|
164
|
+
* const client = addProductionStack(new FetchClient())
|
|
156
165
|
* .setBaseUrl(process.env.API_BASE_URL);
|
|
157
166
|
* ```
|
|
158
167
|
*/
|
|
159
168
|
setBaseUrl(baseUrl?: string): this;
|
|
160
|
-
request<T = unknown>(url: string, init?: RequestInit
|
|
169
|
+
request<T = unknown>(url: string, init?: RequestInit, options?: {
|
|
170
|
+
signal?: AbortSignal;
|
|
171
|
+
timeout?: number;
|
|
172
|
+
}): Promise<FetchResponse<T>>;
|
|
161
173
|
private coreFetch;
|
|
162
174
|
private parseResponse;
|
|
163
175
|
private buildUrlWithParams;
|
|
@@ -178,6 +190,7 @@ declare class FetchClient {
|
|
|
178
190
|
* @template T - Expected response data type (will be null for HEAD requests)
|
|
179
191
|
* @param url - Request URL
|
|
180
192
|
* @param params - Query parameters to append to URL
|
|
193
|
+
* @param options - Request options (signal, timeout)
|
|
181
194
|
* @returns Promise resolving to typed response (data will always be null)
|
|
182
195
|
*
|
|
183
196
|
* @example Check if resource exists:
|
|
@@ -190,15 +203,17 @@ declare class FetchClient {
|
|
|
190
203
|
* }
|
|
191
204
|
* ```
|
|
192
205
|
*
|
|
193
|
-
* @example
|
|
206
|
+
* @example With cancellation:
|
|
194
207
|
* ```typescript
|
|
195
|
-
* const
|
|
196
|
-
*
|
|
197
|
-
*
|
|
198
|
-
* }
|
|
208
|
+
* const controller = new AbortController();
|
|
209
|
+
* const request = client.head('/api/users', { id: 123 }, { signal: controller.signal });
|
|
210
|
+
* controller.abort(); // Cancel the request
|
|
199
211
|
* ```
|
|
200
212
|
*/
|
|
201
|
-
head<T = null>(url: string, params?: Record<string, string | number | boolean | undefined
|
|
213
|
+
head<T = null>(url: string, params?: Record<string, string | number | boolean | undefined>, options?: {
|
|
214
|
+
signal?: AbortSignal;
|
|
215
|
+
timeout?: number;
|
|
216
|
+
}): Promise<FetchResponse<T>>;
|
|
202
217
|
/**
|
|
203
218
|
* HEAD request that returns useful metadata about a resource.
|
|
204
219
|
*
|
|
@@ -234,6 +249,7 @@ declare class FetchClient {
|
|
|
234
249
|
* @template T - Expected response data type
|
|
235
250
|
* @param url - Request URL
|
|
236
251
|
* @param params - Query parameters to append to URL
|
|
252
|
+
* @param options - Request options (signal, timeout)
|
|
237
253
|
* @returns Promise resolving to typed response
|
|
238
254
|
*
|
|
239
255
|
* @example
|
|
@@ -242,8 +258,16 @@ declare class FetchClient {
|
|
|
242
258
|
* const filteredUsers = await client.get<User[]>('/api/users', { status: 'active', limit: 10 });
|
|
243
259
|
* if (users.ok) console.log(users.data);
|
|
244
260
|
* ```
|
|
261
|
+
*
|
|
262
|
+
* @example With timeout:
|
|
263
|
+
* ```typescript
|
|
264
|
+
* const users = await client.get<User[]>('/api/users', {}, { timeout: 5000 });
|
|
265
|
+
* ```
|
|
245
266
|
*/
|
|
246
|
-
get<T>(url: string, params?: Record<string, string | number | boolean | undefined
|
|
267
|
+
get<T>(url: string, params?: Record<string, string | number | boolean | undefined>, options?: {
|
|
268
|
+
signal?: AbortSignal;
|
|
269
|
+
timeout?: number;
|
|
270
|
+
}): Promise<FetchResponse<T>>;
|
|
247
271
|
/**
|
|
248
272
|
* POST request with automatic JSON serialization.
|
|
249
273
|
*
|
|
@@ -251,14 +275,25 @@ declare class FetchClient {
|
|
|
251
275
|
* @param url - Request URL
|
|
252
276
|
* @param body - Request body (auto-serialized to JSON)
|
|
253
277
|
* @param headers - Additional headers (Content-Type: application/json is default)
|
|
278
|
+
* @param options - Request options (signal, timeout)
|
|
254
279
|
* @returns Promise resolving to typed response
|
|
255
280
|
*
|
|
256
281
|
* @example
|
|
257
282
|
* ```typescript
|
|
258
283
|
* const result = await client.post<User>('/api/users', { name: 'John' });
|
|
259
284
|
* ```
|
|
285
|
+
*
|
|
286
|
+
* @example With cancellation:
|
|
287
|
+
* ```typescript
|
|
288
|
+
* const controller = new AbortController();
|
|
289
|
+
* const result = client.post('/api/users', { name: 'John' }, {}, { signal: controller.signal });
|
|
290
|
+
* controller.abort();
|
|
291
|
+
* ```
|
|
260
292
|
*/
|
|
261
|
-
post<T>(url: string, body?: unknown, headers?: Record<string, string
|
|
293
|
+
post<T>(url: string, body?: unknown, headers?: Record<string, string>, options?: {
|
|
294
|
+
signal?: AbortSignal;
|
|
295
|
+
timeout?: number;
|
|
296
|
+
}): Promise<FetchResponse<T>>;
|
|
262
297
|
/**
|
|
263
298
|
* PUT request with automatic JSON serialization.
|
|
264
299
|
*
|
|
@@ -266,9 +301,13 @@ declare class FetchClient {
|
|
|
266
301
|
* @param url - Request URL
|
|
267
302
|
* @param body - Request body (auto-serialized to JSON)
|
|
268
303
|
* @param headers - Additional headers (Content-Type: application/json is default)
|
|
304
|
+
* @param options - Request options (signal, timeout)
|
|
269
305
|
* @returns Promise resolving to typed response
|
|
270
306
|
*/
|
|
271
|
-
put<T>(url: string, body?: unknown, headers?: Record<string, string
|
|
307
|
+
put<T>(url: string, body?: unknown, headers?: Record<string, string>, options?: {
|
|
308
|
+
signal?: AbortSignal;
|
|
309
|
+
timeout?: number;
|
|
310
|
+
}): Promise<FetchResponse<T>>;
|
|
272
311
|
/**
|
|
273
312
|
* PATCH request with automatic JSON serialization.
|
|
274
313
|
*
|
|
@@ -276,15 +315,20 @@ declare class FetchClient {
|
|
|
276
315
|
* @param url - Request URL
|
|
277
316
|
* @param body - Request body (auto-serialized to JSON)
|
|
278
317
|
* @param headers - Additional headers (Content-Type: application/json is default)
|
|
318
|
+
* @param options - Request options (signal, timeout)
|
|
279
319
|
* @returns Promise resolving to typed response
|
|
280
320
|
*/
|
|
281
|
-
patch<T>(url: string, body?: unknown, headers?: Record<string, string
|
|
321
|
+
patch<T>(url: string, body?: unknown, headers?: Record<string, string>, options?: {
|
|
322
|
+
signal?: AbortSignal;
|
|
323
|
+
timeout?: number;
|
|
324
|
+
}): Promise<FetchResponse<T>>;
|
|
282
325
|
/**
|
|
283
326
|
* DELETE request with query parameter support.
|
|
284
327
|
*
|
|
285
328
|
* @template T - Expected response data type
|
|
286
329
|
* @param url - Request URL
|
|
287
330
|
* @param params - Query parameters to append to URL
|
|
331
|
+
* @param options - Request options (signal, timeout)
|
|
288
332
|
* @returns Promise resolving to typed response
|
|
289
333
|
*
|
|
290
334
|
* @example
|
|
@@ -294,7 +338,10 @@ declare class FetchClient {
|
|
|
294
338
|
* if (result.ok) console.log('Deleted successfully');
|
|
295
339
|
* ```
|
|
296
340
|
*/
|
|
297
|
-
del<T>(url: string, params?: Record<string, string | number | boolean | undefined
|
|
341
|
+
del<T>(url: string, params?: Record<string, string | number | boolean | undefined>, options?: {
|
|
342
|
+
signal?: AbortSignal;
|
|
343
|
+
timeout?: number;
|
|
344
|
+
}): Promise<FetchResponse<T>>;
|
|
298
345
|
}
|
|
299
346
|
|
|
300
347
|
/**
|
|
@@ -533,14 +580,14 @@ interface AuthenticationOptions {
|
|
|
533
580
|
*
|
|
534
581
|
* @example Basic usage:
|
|
535
582
|
* ```typescript
|
|
536
|
-
* const authClient =
|
|
583
|
+
* const authClient = addAuthentication(client, {
|
|
537
584
|
* tokenProvider: () => localStorage.getItem('token') || ''
|
|
538
585
|
* });
|
|
539
586
|
* ```
|
|
540
587
|
*
|
|
541
588
|
* @example Async token provider:
|
|
542
589
|
* ```typescript
|
|
543
|
-
* const authClient =
|
|
590
|
+
* const authClient = addAuthentication(client, {
|
|
544
591
|
* tokenProvider: async () => {
|
|
545
592
|
* const token = await getAuthToken();
|
|
546
593
|
* return token || '';
|
|
@@ -564,14 +611,14 @@ declare function createAuthenticationMiddleware(options: AuthenticationOptions):
|
|
|
564
611
|
*
|
|
565
612
|
* @example Basic token from localStorage:
|
|
566
613
|
* ```typescript
|
|
567
|
-
* const authClient =
|
|
614
|
+
* const authClient = addAuthentication(client, {
|
|
568
615
|
* tokenProvider: () => localStorage.getItem('auth-token') || ''
|
|
569
616
|
* });
|
|
570
617
|
* ```
|
|
571
618
|
*
|
|
572
619
|
* @example Async token with refresh:
|
|
573
620
|
* ```typescript
|
|
574
|
-
* const authClient =
|
|
621
|
+
* const authClient = addAuthentication(client, {
|
|
575
622
|
* tokenProvider: async () => {
|
|
576
623
|
* let token = localStorage.getItem('auth-token');
|
|
577
624
|
* if (!token || isExpired(token)) {
|
|
@@ -582,7 +629,7 @@ declare function createAuthenticationMiddleware(options: AuthenticationOptions):
|
|
|
582
629
|
* });
|
|
583
630
|
* ```
|
|
584
631
|
*/
|
|
585
|
-
declare function
|
|
632
|
+
declare function addAuthentication(client: FetchClient, options: AuthenticationOptions): FetchClient;
|
|
586
633
|
|
|
587
634
|
/**
|
|
588
635
|
* @fileoverview Authorization middleware types and configuration.
|
|
@@ -698,13 +745,13 @@ interface AuthorizationOptions {
|
|
|
698
745
|
*
|
|
699
746
|
* @example Smart defaults (no configuration needed):
|
|
700
747
|
* ```typescript
|
|
701
|
-
* const authzClient =
|
|
748
|
+
* const authzClient = addAuthorization(client);
|
|
702
749
|
* // Redirects to '/login?return_url=current-page' on 401
|
|
703
750
|
* ```
|
|
704
751
|
*
|
|
705
752
|
* @example Custom redirect configuration:
|
|
706
753
|
* ```typescript
|
|
707
|
-
* const authzClient =
|
|
754
|
+
* const authzClient = addAuthorization(client, {
|
|
708
755
|
* redirectConfig: {
|
|
709
756
|
* redirectPath: '/signin',
|
|
710
757
|
* returnUrlParam: 'redirect_to'
|
|
@@ -714,14 +761,14 @@ interface AuthorizationOptions {
|
|
|
714
761
|
*
|
|
715
762
|
* @example Manual handler (full control):
|
|
716
763
|
* ```typescript
|
|
717
|
-
* const authzClient =
|
|
764
|
+
* const authzClient = addAuthorization(client, {
|
|
718
765
|
* onUnauthorized: () => window.location.href = '/login'
|
|
719
766
|
* });
|
|
720
767
|
* ```
|
|
721
768
|
*
|
|
722
769
|
* @example Handle both 401 and 403:
|
|
723
770
|
* ```typescript
|
|
724
|
-
* const authzClient =
|
|
771
|
+
* const authzClient = addAuthorization(client, {
|
|
725
772
|
* onForbidden: () => showAccessDeniedMessage(),
|
|
726
773
|
* statusCodes: [401, 403]
|
|
727
774
|
* });
|
|
@@ -743,20 +790,20 @@ declare function createAuthorizationMiddleware(options?: AuthorizationOptions):
|
|
|
743
790
|
*
|
|
744
791
|
* @example Smart defaults - no configuration needed:
|
|
745
792
|
* ```typescript
|
|
746
|
-
* const authzClient =
|
|
793
|
+
* const authzClient = addAuthorization(client);
|
|
747
794
|
* // Redirects to '/login?return_url=current-page' on 401
|
|
748
795
|
* ```
|
|
749
796
|
*
|
|
750
797
|
* @example Custom redirect path:
|
|
751
798
|
* ```typescript
|
|
752
|
-
* const authzClient =
|
|
799
|
+
* const authzClient = addAuthorization(client, {
|
|
753
800
|
* redirectConfig: { redirectPath: '/signin', returnUrlParam: 'redirect_to' }
|
|
754
801
|
* });
|
|
755
802
|
* ```
|
|
756
803
|
*
|
|
757
804
|
* @example Manual handler (full control):
|
|
758
805
|
* ```typescript
|
|
759
|
-
* const authzClient =
|
|
806
|
+
* const authzClient = addAuthorization(client, {
|
|
760
807
|
* onUnauthorized: () => {
|
|
761
808
|
* localStorage.removeItem('auth-token');
|
|
762
809
|
* window.location.href = '/login';
|
|
@@ -766,13 +813,13 @@ declare function createAuthorizationMiddleware(options?: AuthorizationOptions):
|
|
|
766
813
|
*
|
|
767
814
|
* @example Handle multiple status codes:
|
|
768
815
|
* ```typescript
|
|
769
|
-
* const authzClient =
|
|
816
|
+
* const authzClient = addAuthorization(client, {
|
|
770
817
|
* onForbidden: () => showAccessDenied(),
|
|
771
818
|
* statusCodes: [401, 403]
|
|
772
819
|
* });
|
|
773
820
|
* ```
|
|
774
821
|
*/
|
|
775
|
-
declare function
|
|
822
|
+
declare function addAuthorization(client: FetchClient, options?: AuthorizationOptions): FetchClient;
|
|
776
823
|
|
|
777
824
|
/**
|
|
778
825
|
* @fileoverview Cache middleware types and configuration.
|
|
@@ -875,13 +922,13 @@ interface CacheOptions {
|
|
|
875
922
|
*
|
|
876
923
|
* @example Basic caching:
|
|
877
924
|
* ```typescript
|
|
878
|
-
* const cachedClient =
|
|
925
|
+
* const cachedClient = addCache(client);
|
|
879
926
|
* // GET requests will be cached for 5 minutes
|
|
880
927
|
* ```
|
|
881
928
|
*
|
|
882
929
|
* @example Custom TTL:
|
|
883
930
|
* ```typescript
|
|
884
|
-
* const cachedClient =
|
|
931
|
+
* const cachedClient = addCache(client, {
|
|
885
932
|
* ttl: 10 * 60 * 1000 // 10 minutes
|
|
886
933
|
* });
|
|
887
934
|
* ```
|
|
@@ -902,7 +949,7 @@ declare function createCacheMiddleware(options?: CacheOptions): FetchMiddleware;
|
|
|
902
949
|
*
|
|
903
950
|
* @example Basic caching (5 minute TTL):
|
|
904
951
|
* ```typescript
|
|
905
|
-
* const cachedClient =
|
|
952
|
+
* const cachedClient = addCache(client);
|
|
906
953
|
*
|
|
907
954
|
* // First call hits the network
|
|
908
955
|
* await cachedClient.get('/api/data');
|
|
@@ -913,7 +960,7 @@ declare function createCacheMiddleware(options?: CacheOptions): FetchMiddleware;
|
|
|
913
960
|
*
|
|
914
961
|
* @example Custom TTL and methods:
|
|
915
962
|
* ```typescript
|
|
916
|
-
* const cachedClient =
|
|
963
|
+
* const cachedClient = addCache(client, {
|
|
917
964
|
* ttl: 10 * 60 * 1000, // 10 minutes
|
|
918
965
|
* methods: ['GET', 'HEAD']
|
|
919
966
|
* });
|
|
@@ -921,13 +968,13 @@ declare function createCacheMiddleware(options?: CacheOptions): FetchMiddleware;
|
|
|
921
968
|
*
|
|
922
969
|
* @example Stale-while-revalidate:
|
|
923
970
|
* ```typescript
|
|
924
|
-
* const cachedClient =
|
|
971
|
+
* const cachedClient = addCache(client, {
|
|
925
972
|
* staleWhileRevalidate: true
|
|
926
973
|
* });
|
|
927
974
|
* // Returns stale data immediately, updates cache in background
|
|
928
975
|
* ```
|
|
929
976
|
*/
|
|
930
|
-
declare function
|
|
977
|
+
declare function addCache(client: FetchClient, options?: CacheOptions): FetchClient;
|
|
931
978
|
|
|
932
979
|
/**
|
|
933
980
|
* @fileoverview CSRF protection middleware types and configuration.
|
|
@@ -1005,7 +1052,7 @@ interface CSRFOptions {
|
|
|
1005
1052
|
* @example Basic usage (automatic cookie-based CSRF):
|
|
1006
1053
|
* ```typescript
|
|
1007
1054
|
* const client = new FetchClient();
|
|
1008
|
-
* const protectedClient =
|
|
1055
|
+
* const protectedClient = addCSRF(client);
|
|
1009
1056
|
*
|
|
1010
1057
|
* // CSRF token automatically added to POST requests
|
|
1011
1058
|
* await protectedClient.post('/api/users', { name: 'John' });
|
|
@@ -1013,14 +1060,14 @@ interface CSRFOptions {
|
|
|
1013
1060
|
*
|
|
1014
1061
|
* @example Custom token provider:
|
|
1015
1062
|
* ```typescript
|
|
1016
|
-
* const protectedClient =
|
|
1063
|
+
* const protectedClient = addCSRF(client, {
|
|
1017
1064
|
* tokenProvider: () => localStorage.getItem('csrf-token') || ''
|
|
1018
1065
|
* });
|
|
1019
1066
|
* ```
|
|
1020
1067
|
*
|
|
1021
1068
|
* @example Custom header and cookie names:
|
|
1022
1069
|
* ```typescript
|
|
1023
|
-
* const protectedClient =
|
|
1070
|
+
* const protectedClient = addCSRF(client, {
|
|
1024
1071
|
* headerName: 'X-CSRF-Token',
|
|
1025
1072
|
* cookieName: 'csrf-token'
|
|
1026
1073
|
* });
|
|
@@ -1028,7 +1075,7 @@ interface CSRFOptions {
|
|
|
1028
1075
|
*
|
|
1029
1076
|
* @example Skip patterns for external APIs:
|
|
1030
1077
|
* ```typescript
|
|
1031
|
-
* const protectedClient =
|
|
1078
|
+
* const protectedClient = addCSRF(client, {
|
|
1032
1079
|
* skipPatterns: [
|
|
1033
1080
|
* /^https:\/\/api\.external\.com\//, // Skip external API
|
|
1034
1081
|
* '/webhook/', // Skip webhook endpoints
|
|
@@ -1037,7 +1084,7 @@ interface CSRFOptions {
|
|
|
1037
1084
|
* });
|
|
1038
1085
|
* ```
|
|
1039
1086
|
*/
|
|
1040
|
-
declare function
|
|
1087
|
+
declare function addCSRF(client: FetchClient, options?: CSRFOptions): FetchClient;
|
|
1041
1088
|
|
|
1042
1089
|
/**
|
|
1043
1090
|
* @fileoverview Logging middleware types and configuration.
|
|
@@ -1141,13 +1188,13 @@ interface LoggingOptions {
|
|
|
1141
1188
|
*
|
|
1142
1189
|
* @example Basic logging:
|
|
1143
1190
|
* ```typescript
|
|
1144
|
-
* const loggedClient =
|
|
1191
|
+
* const loggedClient = addLogging(client);
|
|
1145
1192
|
* // Logs all requests to console
|
|
1146
1193
|
* ```
|
|
1147
1194
|
*
|
|
1148
1195
|
* @example Custom logger:
|
|
1149
1196
|
* ```typescript
|
|
1150
|
-
* const loggedClient =
|
|
1197
|
+
* const loggedClient = addLogging(client, {
|
|
1151
1198
|
* logger: winston.createLogger({...}),
|
|
1152
1199
|
* level: 'debug',
|
|
1153
1200
|
* includeRequestHeaders: true
|
|
@@ -1170,7 +1217,7 @@ declare function createLoggingMiddleware(options?: LoggingOptions): FetchMiddlew
|
|
|
1170
1217
|
*
|
|
1171
1218
|
* @example Basic logging to console:
|
|
1172
1219
|
* ```typescript
|
|
1173
|
-
* const loggedClient =
|
|
1220
|
+
* const loggedClient = addLogging(client);
|
|
1174
1221
|
*
|
|
1175
1222
|
* // Logs: → GET /api/users
|
|
1176
1223
|
* // Logs: ← GET /api/users → 200 (245ms)
|
|
@@ -1179,7 +1226,7 @@ declare function createLoggingMiddleware(options?: LoggingOptions): FetchMiddlew
|
|
|
1179
1226
|
*
|
|
1180
1227
|
* @example Custom log level and headers:
|
|
1181
1228
|
* ```typescript
|
|
1182
|
-
* const loggedClient =
|
|
1229
|
+
* const loggedClient = addLogging(client, {
|
|
1183
1230
|
* level: 'debug',
|
|
1184
1231
|
* includeRequestHeaders: true,
|
|
1185
1232
|
* includeResponseHeaders: true
|
|
@@ -1188,12 +1235,12 @@ declare function createLoggingMiddleware(options?: LoggingOptions): FetchMiddlew
|
|
|
1188
1235
|
*
|
|
1189
1236
|
* @example Skip health check endpoints:
|
|
1190
1237
|
* ```typescript
|
|
1191
|
-
* const loggedClient =
|
|
1238
|
+
* const loggedClient = addLogging(client, {
|
|
1192
1239
|
* skipPatterns: ['/health', '/metrics', '/ping']
|
|
1193
1240
|
* });
|
|
1194
1241
|
* ```
|
|
1195
1242
|
*/
|
|
1196
|
-
declare function
|
|
1243
|
+
declare function addLogging(client: FetchClient, options?: LoggingOptions): FetchClient;
|
|
1197
1244
|
|
|
1198
1245
|
/**
|
|
1199
1246
|
* @fileoverview Rate limiting middleware types and configuration.
|
|
@@ -1315,7 +1362,7 @@ declare function createRateLimitMiddleware(options?: RateLimitOptions): FetchMid
|
|
|
1315
1362
|
* - Bulk operations that need throttling
|
|
1316
1363
|
* - Pay-per-request API cost management
|
|
1317
1364
|
*/
|
|
1318
|
-
declare function
|
|
1365
|
+
declare function addRateLimit(client: FetchClient, options?: RateLimitOptions): FetchClient;
|
|
1319
1366
|
|
|
1320
1367
|
/**
|
|
1321
1368
|
* @fileoverview Retry middleware types and configuration.
|
|
@@ -1415,7 +1462,7 @@ interface RetryOptions {
|
|
|
1415
1462
|
*/
|
|
1416
1463
|
declare function createRetryMiddleware(options?: RetryOptions): FetchMiddleware;
|
|
1417
1464
|
|
|
1418
|
-
declare function
|
|
1465
|
+
declare function addRetry(client: FetchClient, options?: RetryOptions): FetchClient;
|
|
1419
1466
|
|
|
1420
1467
|
/**
|
|
1421
1468
|
* @fileoverview Complete middleware collection for FetchClient - "pit of success" APIs.
|
|
@@ -1431,21 +1478,21 @@ declare function useRetry(client: FetchClient, options?: RetryOptions): FetchCli
|
|
|
1431
1478
|
*
|
|
1432
1479
|
* Each middleware follows the "pit of success" pattern with:
|
|
1433
1480
|
* - Smart defaults for common scenarios
|
|
1434
|
-
* - Simple `
|
|
1481
|
+
* - Simple `add{Middleware}()` convenience functions
|
|
1435
1482
|
* - Advanced `create{Middleware}Middleware()` for custom scenarios
|
|
1436
1483
|
* - Comprehensive TypeScript support
|
|
1437
1484
|
*
|
|
1438
1485
|
* @example Quick setup with multiple middleware:
|
|
1439
1486
|
* ```typescript
|
|
1440
1487
|
* import { FetchClient } from '@fgrzl/fetch';
|
|
1441
|
-
* import {
|
|
1488
|
+
* import { addAuthentication, addRetry, addLogging } from '@fgrzl/fetch/middleware';
|
|
1442
1489
|
*
|
|
1443
1490
|
* const client = new FetchClient();
|
|
1444
|
-
* const enhancedClient =
|
|
1491
|
+
* const enhancedClient = addAuthentication(client, {
|
|
1445
1492
|
* tokenProvider: () => localStorage.getItem('auth-token') || ''
|
|
1446
1493
|
* })
|
|
1447
|
-
* .pipe(
|
|
1448
|
-
* .pipe(
|
|
1494
|
+
* .pipe(addRetry, { retries: 3 })
|
|
1495
|
+
* .pipe(addLogging);
|
|
1449
1496
|
* ```
|
|
1450
1497
|
*/
|
|
1451
1498
|
|
|
@@ -1459,19 +1506,19 @@ declare function useRetry(client: FetchClient, options?: RetryOptions): FetchCli
|
|
|
1459
1506
|
*
|
|
1460
1507
|
* @example
|
|
1461
1508
|
* ```typescript
|
|
1462
|
-
* const apiClient =
|
|
1509
|
+
* const apiClient = addProductionStack(new FetchClient(), {
|
|
1463
1510
|
* auth: { tokenProvider: () => getAuthToken() },
|
|
1464
1511
|
* cache: { ttl: 5 * 60 * 1000 }, // 5 minutes
|
|
1465
1512
|
* logging: { level: 'info' }
|
|
1466
1513
|
* });
|
|
1467
1514
|
* ```
|
|
1468
1515
|
*/
|
|
1469
|
-
declare function
|
|
1470
|
-
auth?: Parameters<typeof
|
|
1471
|
-
retry?: Parameters<typeof
|
|
1472
|
-
cache?: Parameters<typeof
|
|
1473
|
-
logging?: Parameters<typeof
|
|
1474
|
-
rateLimit?: Parameters<typeof
|
|
1516
|
+
declare function addProductionStack(client: FetchClient, config?: {
|
|
1517
|
+
auth?: Parameters<typeof addAuthentication>[1];
|
|
1518
|
+
retry?: Parameters<typeof addRetry>[1];
|
|
1519
|
+
cache?: Parameters<typeof addCache>[1];
|
|
1520
|
+
logging?: Parameters<typeof addLogging>[1];
|
|
1521
|
+
rateLimit?: Parameters<typeof addRateLimit>[1];
|
|
1475
1522
|
}): FetchClient;
|
|
1476
1523
|
/**
|
|
1477
1524
|
* Development-friendly middleware stack with comprehensive logging and retries.
|
|
@@ -1483,13 +1530,13 @@ declare function useProductionStack(client: FetchClient, config?: {
|
|
|
1483
1530
|
*
|
|
1484
1531
|
* @example
|
|
1485
1532
|
* ```typescript
|
|
1486
|
-
* const devClient =
|
|
1533
|
+
* const devClient = addDevelopmentStack(new FetchClient(), {
|
|
1487
1534
|
* auth: { tokenProvider: () => 'dev-token' }
|
|
1488
1535
|
* });
|
|
1489
1536
|
* ```
|
|
1490
1537
|
*/
|
|
1491
|
-
declare function
|
|
1492
|
-
auth?: Parameters<typeof
|
|
1538
|
+
declare function addDevelopmentStack(client: FetchClient, config?: {
|
|
1539
|
+
auth?: Parameters<typeof addAuthentication>[1];
|
|
1493
1540
|
}): FetchClient;
|
|
1494
1541
|
/**
|
|
1495
1542
|
* Basic middleware stack with just authentication and retry.
|
|
@@ -1501,13 +1548,13 @@ declare function useDevelopmentStack(client: FetchClient, config?: {
|
|
|
1501
1548
|
*
|
|
1502
1549
|
* @example
|
|
1503
1550
|
* ```typescript
|
|
1504
|
-
* const basicClient =
|
|
1551
|
+
* const basicClient = addBasicStack(new FetchClient(), {
|
|
1505
1552
|
* auth: { tokenProvider: () => getToken() }
|
|
1506
1553
|
* });
|
|
1507
1554
|
* ```
|
|
1508
1555
|
*/
|
|
1509
|
-
declare function
|
|
1510
|
-
auth: Parameters<typeof
|
|
1556
|
+
declare function addBasicStack(client: FetchClient, config: {
|
|
1557
|
+
auth: Parameters<typeof addAuthentication>[1];
|
|
1511
1558
|
}): FetchClient;
|
|
1512
1559
|
|
|
1513
1560
|
/**
|
|
@@ -1559,18 +1606,18 @@ declare function useBasicStack(client: FetchClient, config: {
|
|
|
1559
1606
|
* @example Configure authentication:
|
|
1560
1607
|
* ```typescript
|
|
1561
1608
|
* import api from '@fgrzl/fetch';
|
|
1562
|
-
* import {
|
|
1609
|
+
* import { addAuthentication } from '@fgrzl/fetch/middleware';
|
|
1563
1610
|
*
|
|
1564
|
-
* const authClient =
|
|
1611
|
+
* const authClient = addAuthentication(api, {
|
|
1565
1612
|
* tokenProvider: () => localStorage.getItem('auth-token') || ''
|
|
1566
1613
|
* });
|
|
1567
1614
|
* ```
|
|
1568
1615
|
*
|
|
1569
1616
|
* @example For token-only auth (no cookies):
|
|
1570
1617
|
* ```typescript
|
|
1571
|
-
* import { FetchClient,
|
|
1618
|
+
* import { FetchClient, addAuthentication } from '@fgrzl/fetch';
|
|
1572
1619
|
*
|
|
1573
|
-
* const tokenClient =
|
|
1620
|
+
* const tokenClient = addAuthentication(new FetchClient({
|
|
1574
1621
|
* credentials: 'omit' // Don't send cookies
|
|
1575
1622
|
* }), {
|
|
1576
1623
|
* tokenProvider: () => getJWTToken()
|
|
@@ -1579,10 +1626,10 @@ declare function useBasicStack(client: FetchClient, config: {
|
|
|
1579
1626
|
*
|
|
1580
1627
|
* @example Production-ready API client with base URL:
|
|
1581
1628
|
* ```typescript
|
|
1582
|
-
* import { FetchClient,
|
|
1629
|
+
* import { FetchClient, addProductionStack } from '@fgrzl/fetch';
|
|
1583
1630
|
*
|
|
1584
1631
|
* // One-liner production setup with base URL
|
|
1585
|
-
* const apiClient =
|
|
1632
|
+
* const apiClient = addProductionStack(new FetchClient(), {
|
|
1586
1633
|
* auth: { tokenProvider: () => getAuthToken() },
|
|
1587
1634
|
* retry: { maxRetries: 3 },
|
|
1588
1635
|
* logging: { level: 'info' }
|
|
@@ -1594,4 +1641,4 @@ declare function useBasicStack(client: FetchClient, config: {
|
|
|
1594
1641
|
*/
|
|
1595
1642
|
declare const api: FetchClient;
|
|
1596
1643
|
|
|
1597
|
-
export { type AuthTokenProvider, type AuthenticationOptions, type AuthorizationOptions, type CacheEntry, type CacheKeyGenerator, type CacheOptions, type CacheStorage, FetchClient, type FetchClientOptions, FetchError, type FetchResponse, HttpError, type FetchMiddleware as InterceptMiddleware, type LogLevel, type Logger, type LoggingOptions, NetworkError, type QueryParams, type QueryValue, type RateLimitAlgorithm, type RateLimitOptions, type RetryOptions, type UnauthorizedHandler,
|
|
1644
|
+
export { type AuthTokenProvider, type AuthenticationOptions, type AuthorizationOptions, type CacheEntry, type CacheKeyGenerator, type CacheOptions, type CacheStorage, FetchClient, type FetchClientOptions, FetchError, type FetchResponse, HttpError, type FetchMiddleware as InterceptMiddleware, type LogLevel, type Logger, type LoggingOptions, NetworkError, type QueryParams, type QueryValue, type RateLimitAlgorithm, type RateLimitOptions, type RetryOptions, type UnauthorizedHandler, addAuthentication, addAuthorization, addBasicStack, addCSRF, addCache, addDevelopmentStack, addLogging, addProductionStack, addRateLimit, addRetry, appendQueryParams, buildQueryParams, createAuthenticationMiddleware, createAuthorizationMiddleware, createCacheMiddleware, createLoggingMiddleware, createRateLimitMiddleware, createRetryMiddleware, api as default };
|