create-request 1.6.0 → 2.0.0-next.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/CHANGELOG.md +58 -0
- package/MIGRATION.md +158 -0
- package/README.md +422 -1292
- package/dist/index.cjs +730 -0
- package/dist/index.d.cts +851 -0
- package/dist/index.d.ts +851 -0
- package/dist/index.js +717 -0
- package/package.json +63 -67
- package/dist/library/BaseRequest.d.ts +0 -870
- package/dist/library/BodyRequest.d.ts +0 -101
- package/dist/library/RequestError.d.ts +0 -171
- package/dist/library/ResponseWrapper.d.ts +0 -182
- package/dist/library/apiBuilder.d.ts +0 -560
- package/dist/library/enums.d.ts +0 -85
- package/dist/library/index.cjs +0 -3160
- package/dist/library/index.cjs.map +0 -1
- package/dist/library/index.d.ts +0 -47
- package/dist/library/index.esm.js +0 -3140
- package/dist/library/index.esm.js.map +0 -1
- package/dist/library/index.esm.min.js +0 -1
- package/dist/library/index.esm.min.js.map +0 -1
- package/dist/library/index.min.cjs +0 -1
- package/dist/library/index.min.cjs.map +0 -1
- package/dist/library/requestFactories.d.ts +0 -91
- package/dist/library/requestMethods.d.ts +0 -91
- package/dist/library/types.d.ts +0 -329
- package/dist/library/utils/Config.d.ts +0 -221
- package/dist/library/utils/CookieUtils.d.ts +0 -9
- package/dist/library/utils/CsrfUtils.d.ts +0 -24
|
@@ -1,560 +0,0 @@
|
|
|
1
|
-
import { GetRequest, PostRequest, PutRequest, DeleteRequest, PatchRequest, HeadRequest, OptionsRequest } from "./requestMethods.js";
|
|
2
|
-
import type { RetryConfig, RetryCallback, CookiesRecord, CookieOptions, FetchFunction, RequestInterceptor, ResponseInterceptor, ErrorInterceptor } from "./types.js";
|
|
3
|
-
import type { CredentialsPolicy, RedirectMode, RequestPriority, ReferrerPolicy, RequestMode } from "./enums.js";
|
|
4
|
-
/**
|
|
5
|
-
* API Builder for creating configured API instances with reusable default settings.
|
|
6
|
-
*/
|
|
7
|
-
type ApiBuilder = {
|
|
8
|
-
withBaseURL(baseURL: string): ApiBuilder;
|
|
9
|
-
get(path?: string): GetRequest;
|
|
10
|
-
post(path?: string): PostRequest;
|
|
11
|
-
put(path?: string): PutRequest;
|
|
12
|
-
del(path?: string): DeleteRequest;
|
|
13
|
-
patch(path?: string): PatchRequest;
|
|
14
|
-
head(path?: string): HeadRequest;
|
|
15
|
-
options(path?: string): OptionsRequest;
|
|
16
|
-
/**
|
|
17
|
-
* Add multiple HTTP headers to the request.
|
|
18
|
-
* Null and undefined header values are ignored.
|
|
19
|
-
*
|
|
20
|
-
* @param headers - An object containing key-value pairs of headers
|
|
21
|
-
* @returns The API builder instance for chaining
|
|
22
|
-
*
|
|
23
|
-
* @example
|
|
24
|
-
* ```typescript
|
|
25
|
-
* api.withHeaders({
|
|
26
|
-
* 'Content-Type': 'application/json',
|
|
27
|
-
* 'Authorization': 'Bearer token123'
|
|
28
|
-
* });
|
|
29
|
-
* ```
|
|
30
|
-
*/
|
|
31
|
-
withHeaders(headers: Record<string, string>): ApiBuilder;
|
|
32
|
-
/**
|
|
33
|
-
* Add a single HTTP header to the request.
|
|
34
|
-
*
|
|
35
|
-
* @param name - The header name
|
|
36
|
-
* @param value - The header value
|
|
37
|
-
* @returns The API builder instance for chaining
|
|
38
|
-
*
|
|
39
|
-
* @example
|
|
40
|
-
* ```typescript
|
|
41
|
-
* api.withHeader('Content-Type', 'application/json');
|
|
42
|
-
* ```
|
|
43
|
-
*/
|
|
44
|
-
withHeader(name: string, value: string): ApiBuilder;
|
|
45
|
-
/**
|
|
46
|
-
* Set a timeout for the request.
|
|
47
|
-
* If the request takes longer than the specified time, it will be aborted and a RequestError will be thrown.
|
|
48
|
-
*
|
|
49
|
-
* @param timeout - The timeout in milliseconds (must be a positive finite number)
|
|
50
|
-
* @returns The API builder instance for chaining
|
|
51
|
-
* @throws {RequestError} If timeout is not a positive finite number
|
|
52
|
-
*
|
|
53
|
-
* @example
|
|
54
|
-
* ```typescript
|
|
55
|
-
* api.withTimeout(5000); // 5 second timeout
|
|
56
|
-
* ```
|
|
57
|
-
*/
|
|
58
|
-
withTimeout(timeout: number): ApiBuilder;
|
|
59
|
-
/**
|
|
60
|
-
* Configure automatic retry behavior for failed requests.
|
|
61
|
-
* By default, retries only network errors. For more control, pass a RetryConfig object.
|
|
62
|
-
*
|
|
63
|
-
* @param retries - Either:
|
|
64
|
-
* - A number: number of retry attempts (fixed delay of 1000ms between retries)
|
|
65
|
-
* - A RetryConfig object with optional properties:
|
|
66
|
-
* - maxRetries: number of retry attempts
|
|
67
|
-
* - delay: a function (attempt, error?) => number that returns delay in ms
|
|
68
|
-
* - retryOn: array of status codes to retry on (in addition to network errors)
|
|
69
|
-
* - shouldRetry: custom function to decide if a retry should happen
|
|
70
|
-
* @returns The API builder instance for chaining
|
|
71
|
-
*
|
|
72
|
-
* @example
|
|
73
|
-
* ```typescript
|
|
74
|
-
* // Simple retry with fixed delay
|
|
75
|
-
* api.withRetries(3);
|
|
76
|
-
* ```
|
|
77
|
-
*
|
|
78
|
-
* @example
|
|
79
|
-
* ```typescript
|
|
80
|
-
* // Exponential backoff
|
|
81
|
-
* api.withRetries({
|
|
82
|
-
* maxRetries: 3,
|
|
83
|
-
* delay: (attempt) => Math.pow(2, attempt) * 1000
|
|
84
|
-
* });
|
|
85
|
-
* ```
|
|
86
|
-
*
|
|
87
|
-
* @example
|
|
88
|
-
* ```typescript
|
|
89
|
-
* // Retry specific status codes
|
|
90
|
-
* api.withRetries({
|
|
91
|
-
* maxRetries: 2,
|
|
92
|
-
* retryOn: [408, 429, 500, 502, 503, 504]
|
|
93
|
-
* });
|
|
94
|
-
* ```
|
|
95
|
-
*
|
|
96
|
-
* @example
|
|
97
|
-
* ```typescript
|
|
98
|
-
* // Custom retry logic with error-aware delay
|
|
99
|
-
* api.withRetries({
|
|
100
|
-
* maxRetries: 3,
|
|
101
|
-
* delay: (attempt, error) => {
|
|
102
|
-
* if (error?.status === 429) return 5000; // Rate limited
|
|
103
|
-
* return attempt * 1000; // Linear backoff
|
|
104
|
-
* },
|
|
105
|
-
* shouldRetry: (error) => error.status === 429 || error.status >= 500
|
|
106
|
-
* });
|
|
107
|
-
* ```
|
|
108
|
-
*/
|
|
109
|
-
withRetries(retries: number | RetryConfig): ApiBuilder;
|
|
110
|
-
/**
|
|
111
|
-
* Register a callback to be invoked before each retry attempt.
|
|
112
|
-
* The callback receives the attempt number (1-indexed), the error that caused the retry,
|
|
113
|
-
* and the delay before the next retry.
|
|
114
|
-
*
|
|
115
|
-
* @param callback - A function that receives (attempt: number, error: RequestError, delay: number)
|
|
116
|
-
* @returns The API builder instance for chaining
|
|
117
|
-
*
|
|
118
|
-
* @example
|
|
119
|
-
* ```typescript
|
|
120
|
-
* api.onRetry((attempt, error, delay) => {
|
|
121
|
-
* console.log(`Retry attempt ${attempt} after ${delay}ms due to:`, error.message);
|
|
122
|
-
* });
|
|
123
|
-
* ```
|
|
124
|
-
*/
|
|
125
|
-
onRetry(callback: RetryCallback): ApiBuilder;
|
|
126
|
-
/**
|
|
127
|
-
* Set the credentials policy for the request.
|
|
128
|
-
* Controls whether cookies and HTTP authentication are sent with cross-origin requests.
|
|
129
|
-
*
|
|
130
|
-
* - `include`: Send credentials with both same-origin and cross-origin requests
|
|
131
|
-
* - `omit`: Never send credentials
|
|
132
|
-
* - `same-origin` (default): Only send credentials with same-origin requests
|
|
133
|
-
*
|
|
134
|
-
* Note: Use direct call pattern. Fluent API (e.g., `.withCredentials.INCLUDE()`) is not supported in ApiBuilder.
|
|
135
|
-
*
|
|
136
|
-
* @param credentials - The credentials policy
|
|
137
|
-
* @returns The API builder instance for chaining
|
|
138
|
-
*
|
|
139
|
-
* @example
|
|
140
|
-
* ```typescript
|
|
141
|
-
* api.withCredentials('include'); // Send cookies with cross-origin requests
|
|
142
|
-
* api.withCredentials(CredentialsPolicy.INCLUDE);
|
|
143
|
-
* ```
|
|
144
|
-
*/
|
|
145
|
-
withCredentials(credentials: CredentialsPolicy): ApiBuilder;
|
|
146
|
-
/**
|
|
147
|
-
* Set the referrer URL for the request.
|
|
148
|
-
* This specifies the referrer to send in the Referer header.
|
|
149
|
-
*
|
|
150
|
-
* @param referrer - The referrer URL
|
|
151
|
-
* @returns The API builder instance for chaining
|
|
152
|
-
*
|
|
153
|
-
* @example
|
|
154
|
-
* ```typescript
|
|
155
|
-
* api.withReferrer('https://example.com/page');
|
|
156
|
-
* ```
|
|
157
|
-
*/
|
|
158
|
-
withReferrer(referrer: string): ApiBuilder;
|
|
159
|
-
/**
|
|
160
|
-
* Set the referrer policy for the request.
|
|
161
|
-
* Controls how much referrer information is included with requests.
|
|
162
|
-
*
|
|
163
|
-
* Available policies:
|
|
164
|
-
* - `no-referrer`: Never send referrer
|
|
165
|
-
* - `no-referrer-when-downgrade` (default): Send referrer except when going from HTTPS to HTTP
|
|
166
|
-
* - `origin`: Send only the origin (scheme, host, port)
|
|
167
|
-
* - `origin-when-cross-origin`: Full URL for same-origin, only origin for cross-origin
|
|
168
|
-
* - `same-origin`: Send referrer only for same-origin requests
|
|
169
|
-
* - `strict-origin`: Send origin, but not when going from HTTPS to HTTP
|
|
170
|
-
* - `strict-origin-when-cross-origin`: Full URL for same-origin, origin for cross-origin HTTPS, nothing for HTTP
|
|
171
|
-
* - `unsafe-url`: Always send full URL (may leak sensitive information)
|
|
172
|
-
*
|
|
173
|
-
* @param policy - The referrer policy
|
|
174
|
-
* @returns The API builder instance for chaining
|
|
175
|
-
*
|
|
176
|
-
* @example
|
|
177
|
-
* ```typescript
|
|
178
|
-
* api.withReferrerPolicy('no-referrer');
|
|
179
|
-
* api.withReferrerPolicy(ReferrerPolicy.NO_REFERRER); // Using enum
|
|
180
|
-
* ```
|
|
181
|
-
*/
|
|
182
|
-
withReferrerPolicy(policy: ReferrerPolicy): ApiBuilder;
|
|
183
|
-
/**
|
|
184
|
-
* Set the redirect behavior for the request.
|
|
185
|
-
*
|
|
186
|
-
* - `follow` (default): Automatically follow redirects
|
|
187
|
-
* - `error`: Treat redirects as errors
|
|
188
|
-
* - `manual`: Handle redirects manually (response will have type 'opaqueredirect')
|
|
189
|
-
*
|
|
190
|
-
* @param redirect - The redirect mode
|
|
191
|
-
* @returns The API builder instance for chaining
|
|
192
|
-
*
|
|
193
|
-
* @example
|
|
194
|
-
* ```typescript
|
|
195
|
-
* api.withRedirect('error'); // Throw an error on redirect
|
|
196
|
-
* api.withRedirect(RedirectMode.ERROR); // Using enum
|
|
197
|
-
* ```
|
|
198
|
-
*/
|
|
199
|
-
withRedirect(redirect: RedirectMode): ApiBuilder;
|
|
200
|
-
/**
|
|
201
|
-
* Enable or disable HTTP keep-alive for the request.
|
|
202
|
-
* When enabled, the connection can be reused for multiple requests.
|
|
203
|
-
*
|
|
204
|
-
* @param keepalive - Whether to use keep-alive (default is false)
|
|
205
|
-
* @returns The API builder instance for chaining
|
|
206
|
-
*
|
|
207
|
-
* @example
|
|
208
|
-
* ```typescript
|
|
209
|
-
* api.withKeepAlive(true);
|
|
210
|
-
* ```
|
|
211
|
-
*/
|
|
212
|
-
withKeepAlive(keepalive: boolean): ApiBuilder;
|
|
213
|
-
/**
|
|
214
|
-
* Set the priority hint for the request.
|
|
215
|
-
* This provides a hint to the browser about the relative priority of this request.
|
|
216
|
-
*
|
|
217
|
-
* - `high`: High priority (e.g., critical resources)
|
|
218
|
-
* - `low`: Low priority (e.g., prefetch, background tasks)
|
|
219
|
-
* - `auto` (default): Browser decides the priority
|
|
220
|
-
*
|
|
221
|
-
* @param priority - The request priority
|
|
222
|
-
* @returns The API builder instance for chaining
|
|
223
|
-
*
|
|
224
|
-
* @example
|
|
225
|
-
* ```typescript
|
|
226
|
-
* api.withPriority('high'); // Mark as high priority
|
|
227
|
-
* api.withPriority(RequestPriority.HIGH); // Using enum
|
|
228
|
-
* ```
|
|
229
|
-
*/
|
|
230
|
-
withPriority(priority: RequestPriority): ApiBuilder;
|
|
231
|
-
/**
|
|
232
|
-
* Set the Subresource Integrity (SRI) value for the request.
|
|
233
|
-
* Used to verify that a fetched resource hasn't been tampered with.
|
|
234
|
-
*
|
|
235
|
-
* @param integrity - The integrity hash (e.g., 'sha384-...')
|
|
236
|
-
* @returns The API builder instance for chaining
|
|
237
|
-
*
|
|
238
|
-
* @example
|
|
239
|
-
* ```typescript
|
|
240
|
-
* api.withIntegrity('sha384-oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8wC');
|
|
241
|
-
* ```
|
|
242
|
-
*/
|
|
243
|
-
withIntegrity(integrity: string): ApiBuilder;
|
|
244
|
-
/**
|
|
245
|
-
* Set the cache mode for the request.
|
|
246
|
-
* Controls how the request interacts with the browser's HTTP cache.
|
|
247
|
-
*
|
|
248
|
-
* Cache modes:
|
|
249
|
-
* - `default`: Use the standard HTTP cache behavior (check freshness, use cached response if valid)
|
|
250
|
-
* - `no-store`: Bypass cache completely, don't store the response
|
|
251
|
-
* - `reload`: Bypass cache for this request, but store the response
|
|
252
|
-
* - `no-cache`: Use cached response only after revalidation with the server
|
|
253
|
-
* - `force-cache`: Use cached response even if stale, only fetch if not cached
|
|
254
|
-
* - `only-if-cached`: Use cached response or fail (must use with same-origin mode)
|
|
255
|
-
*
|
|
256
|
-
* @param cache - The cache mode
|
|
257
|
-
* @returns The API builder instance for chaining
|
|
258
|
-
*
|
|
259
|
-
* @example
|
|
260
|
-
* ```typescript
|
|
261
|
-
* api.withCache('no-store'); // Don't cache this request
|
|
262
|
-
* api.withCache('force-cache'); // Use cache even if stale
|
|
263
|
-
* ```
|
|
264
|
-
*/
|
|
265
|
-
withCache(cache: RequestCache): ApiBuilder;
|
|
266
|
-
/**
|
|
267
|
-
* Sets a custom fetch implementation for all requests created through this API instance.
|
|
268
|
-
* Useful for injecting test stubs, undici agents/dispatchers, or framework-patched
|
|
269
|
-
* fetch functions (e.g. Next.js caching options). Individual requests can still
|
|
270
|
-
* override it with their own `withFetch` call.
|
|
271
|
-
*
|
|
272
|
-
* @param fetchFn - A fetch-compatible function
|
|
273
|
-
* @returns The API builder instance for chaining
|
|
274
|
-
*
|
|
275
|
-
* @example
|
|
276
|
-
* ```typescript
|
|
277
|
-
* import { fetch as undiciFetch, Agent } from 'undici';
|
|
278
|
-
* const agent = new Agent({ connections: 10 });
|
|
279
|
-
* const api = createApi()
|
|
280
|
-
* .withBaseURL('https://api.example.com')
|
|
281
|
-
* .withFetch((url, init) => undiciFetch(url, { ...init, dispatcher: agent }));
|
|
282
|
-
* ```
|
|
283
|
-
*/
|
|
284
|
-
withFetch(fetchFn: FetchFunction): ApiBuilder;
|
|
285
|
-
/**
|
|
286
|
-
* Set the request mode.
|
|
287
|
-
* Controls CORS behavior and what types of responses are allowed.
|
|
288
|
-
*
|
|
289
|
-
* Request modes:
|
|
290
|
-
* - `cors` (default): Allow cross-origin requests, follow CORS protocol
|
|
291
|
-
* - `no-cors`: Make cross-origin request without CORS headers (limited response access)
|
|
292
|
-
* - `same-origin`: Only allow same-origin requests, reject cross-origin
|
|
293
|
-
* - `navigate`: Used for navigation requests (usually not needed for fetch)
|
|
294
|
-
*
|
|
295
|
-
* @param mode - The request mode
|
|
296
|
-
* @returns The API builder instance for chaining
|
|
297
|
-
*
|
|
298
|
-
* @example
|
|
299
|
-
* ```typescript
|
|
300
|
-
* api.withMode('same-origin'); // Only allow same-origin requests
|
|
301
|
-
* api.withMode(RequestMode.SAME_ORIGIN); // Using enum
|
|
302
|
-
* ```
|
|
303
|
-
*/
|
|
304
|
-
withMode(mode: RequestMode): ApiBuilder;
|
|
305
|
-
/**
|
|
306
|
-
* Sets the Content-Type header for the request.
|
|
307
|
-
* Shorthand for `withHeader('Content-Type', contentType)`.
|
|
308
|
-
*
|
|
309
|
-
* @param contentType - The MIME type (e.g., 'application/json', 'text/plain', 'application/xml')
|
|
310
|
-
* @returns The API builder instance for chaining
|
|
311
|
-
*
|
|
312
|
-
* @example
|
|
313
|
-
* ```typescript
|
|
314
|
-
* api.withContentType('application/json');
|
|
315
|
-
* ```
|
|
316
|
-
*
|
|
317
|
-
* @example
|
|
318
|
-
* ```typescript
|
|
319
|
-
* api.withContentType('application/xml');
|
|
320
|
-
* ```
|
|
321
|
-
*/
|
|
322
|
-
withContentType(contentType: string): ApiBuilder;
|
|
323
|
-
/**
|
|
324
|
-
* Sets the Authorization header for the request.
|
|
325
|
-
* Shorthand for `withHeader('Authorization', authValue)`.
|
|
326
|
-
* For Bearer tokens, use `withBearerToken()` instead. For Basic auth, use `withBasicAuth()`.
|
|
327
|
-
*
|
|
328
|
-
* @param authValue - The full authorization header value (e.g., `'Bearer token123'`, `'Basic base64string'`)
|
|
329
|
-
* @returns The API builder instance for chaining
|
|
330
|
-
*
|
|
331
|
-
* @example
|
|
332
|
-
* ```typescript
|
|
333
|
-
* api.withAuthorization('Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...');
|
|
334
|
-
* ```
|
|
335
|
-
*
|
|
336
|
-
* @example
|
|
337
|
-
* ```typescript
|
|
338
|
-
* api.withAuthorization('CustomScheme customToken');
|
|
339
|
-
* ```
|
|
340
|
-
*/
|
|
341
|
-
withAuthorization(authValue: string): ApiBuilder;
|
|
342
|
-
/**
|
|
343
|
-
* Sets up HTTP Basic Authentication.
|
|
344
|
-
* Encodes the username and password in base64 and sets the Authorization header.
|
|
345
|
-
*
|
|
346
|
-
* @param username - The username for Basic authentication
|
|
347
|
-
* @param password - The password for Basic authentication
|
|
348
|
-
* @returns The API builder instance for chaining
|
|
349
|
-
*
|
|
350
|
-
* @example
|
|
351
|
-
* ```typescript
|
|
352
|
-
* api.withBasicAuth('myuser', 'mypassword');
|
|
353
|
-
* // Sets: Authorization: Basic bXl1c2VyOm15cGFzc3dvcmQ=
|
|
354
|
-
* ```
|
|
355
|
-
*/
|
|
356
|
-
withBasicAuth(username: string, password: string): ApiBuilder;
|
|
357
|
-
/**
|
|
358
|
-
* Sets a Bearer token for authentication.
|
|
359
|
-
* Shorthand for `withAuthorization('Bearer ' + token)`.
|
|
360
|
-
*
|
|
361
|
-
* @param token - The Bearer token (JWT, OAuth token, etc.)
|
|
362
|
-
* @returns The API builder instance for chaining
|
|
363
|
-
*
|
|
364
|
-
* @example
|
|
365
|
-
* ```typescript
|
|
366
|
-
* api.withBearerToken('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...');
|
|
367
|
-
* // Sets: Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
|
|
368
|
-
* ```
|
|
369
|
-
*/
|
|
370
|
-
withBearerToken(token: string): ApiBuilder;
|
|
371
|
-
/**
|
|
372
|
-
* Sets cookies for the request.
|
|
373
|
-
* Cookies are sent in the Cookie header. Multiple calls will merge cookies.
|
|
374
|
-
* Cookie values can be simple strings or objects with additional cookie options.
|
|
375
|
-
*
|
|
376
|
-
* @param cookies - An object where keys are cookie names and values are either:
|
|
377
|
-
* - A string (the cookie value)
|
|
378
|
-
* - A CookieOptions object with `value` and optional properties (secure, httpOnly, sameSite, expires, path, domain, maxAge)
|
|
379
|
-
* @returns The API builder instance for chaining
|
|
380
|
-
*
|
|
381
|
-
* @example
|
|
382
|
-
* ```typescript
|
|
383
|
-
* // Simple string cookies
|
|
384
|
-
* api.withCookies({ sessionId: 'abc123', userId: '456' });
|
|
385
|
-
* ```
|
|
386
|
-
*
|
|
387
|
-
* @example
|
|
388
|
-
* ```typescript
|
|
389
|
-
* // Cookies with options (note: options are for documentation only in request cookies)
|
|
390
|
-
* api.withCookies({
|
|
391
|
-
* sessionId: 'abc123',
|
|
392
|
-
* token: { value: 'xyz789', secure: true }
|
|
393
|
-
* });
|
|
394
|
-
* ```
|
|
395
|
-
*/
|
|
396
|
-
withCookies(cookies: CookiesRecord): ApiBuilder;
|
|
397
|
-
/**
|
|
398
|
-
* Sets a single cookie for the request.
|
|
399
|
-
* Convenience method for adding one cookie at a time.
|
|
400
|
-
*
|
|
401
|
-
* @param name - The cookie name
|
|
402
|
-
* @param value - The cookie value as a string, or a CookieOptions object with `value` and optional properties
|
|
403
|
-
* @returns The API builder instance for chaining
|
|
404
|
-
*
|
|
405
|
-
* @example
|
|
406
|
-
* ```typescript
|
|
407
|
-
* api.withCookie('sessionId', 'abc123');
|
|
408
|
-
* ```
|
|
409
|
-
*
|
|
410
|
-
* @example
|
|
411
|
-
* ```typescript
|
|
412
|
-
* api.withCookie('token', { value: 'xyz789', secure: true });
|
|
413
|
-
* ```
|
|
414
|
-
*/
|
|
415
|
-
withCookie(name: string, value: string | CookieOptions): ApiBuilder;
|
|
416
|
-
/**
|
|
417
|
-
* Sets a CSRF (Cross-Site Request Forgery) token in the request headers.
|
|
418
|
-
* This is commonly used to protect against CSRF attacks in web applications.
|
|
419
|
-
*
|
|
420
|
-
* @param token - The CSRF token value
|
|
421
|
-
* @param headerName - The name of the header to use. Defaults to `'X-CSRF-Token'`.
|
|
422
|
-
* @returns The API builder instance for chaining
|
|
423
|
-
*
|
|
424
|
-
* @example
|
|
425
|
-
* ```typescript
|
|
426
|
-
* api.withCsrfToken('csrf-token-123');
|
|
427
|
-
* // Sets: X-CSRF-Token: csrf-token-123
|
|
428
|
-
* ```
|
|
429
|
-
*
|
|
430
|
-
* @example
|
|
431
|
-
* ```typescript
|
|
432
|
-
* api.withCsrfToken('token', 'X-Custom-CSRF-Header');
|
|
433
|
-
* // Sets: X-Custom-CSRF-Header: token
|
|
434
|
-
* ```
|
|
435
|
-
*/
|
|
436
|
-
withCsrfToken(token: string, headerName?: string): ApiBuilder;
|
|
437
|
-
/**
|
|
438
|
-
* Disables automatic anti-CSRF protection.
|
|
439
|
-
* By default, X-Requested-With: XMLHttpRequest header is sent with all requests.
|
|
440
|
-
* @returns The API builder instance for chaining
|
|
441
|
-
*/
|
|
442
|
-
withoutCsrfProtection(): ApiBuilder;
|
|
443
|
-
/**
|
|
444
|
-
* Sets common security headers to help prevent CSRF attacks
|
|
445
|
-
* @returns The API builder instance for chaining
|
|
446
|
-
*/
|
|
447
|
-
withAntiCsrfHeaders(): ApiBuilder;
|
|
448
|
-
/**
|
|
449
|
-
* Add a request interceptor for this specific request
|
|
450
|
-
* Request interceptors can modify the request configuration or return an early response
|
|
451
|
-
*
|
|
452
|
-
* @param interceptor - The request interceptor function
|
|
453
|
-
* @returns The API builder instance for chaining
|
|
454
|
-
*
|
|
455
|
-
* @example
|
|
456
|
-
* api.withRequestInterceptor((config) => {
|
|
457
|
-
* config.headers['X-Custom'] = 'value';
|
|
458
|
-
* return config;
|
|
459
|
-
* });
|
|
460
|
-
*/
|
|
461
|
-
withRequestInterceptor(interceptor: RequestInterceptor): ApiBuilder;
|
|
462
|
-
/**
|
|
463
|
-
* Add a response interceptor for this specific request
|
|
464
|
-
* Response interceptors can transform the response
|
|
465
|
-
*
|
|
466
|
-
* @param interceptor - The response interceptor function
|
|
467
|
-
* @returns The API builder instance for chaining
|
|
468
|
-
*
|
|
469
|
-
* @example
|
|
470
|
-
* api.withResponseInterceptor((response) => {
|
|
471
|
-
* console.log('Status:', response.status);
|
|
472
|
-
* return response;
|
|
473
|
-
* });
|
|
474
|
-
*/
|
|
475
|
-
withResponseInterceptor(interceptor: ResponseInterceptor): ApiBuilder;
|
|
476
|
-
/**
|
|
477
|
-
* Add an error interceptor for this specific request
|
|
478
|
-
* Error interceptors can handle or transform errors
|
|
479
|
-
*
|
|
480
|
-
* @param interceptor - The error interceptor function
|
|
481
|
-
* @returns The API builder instance for chaining
|
|
482
|
-
*
|
|
483
|
-
* @example
|
|
484
|
-
* api.withErrorInterceptor((error) => {
|
|
485
|
-
* console.error('Request failed:', error);
|
|
486
|
-
* throw error;
|
|
487
|
-
* });
|
|
488
|
-
*/
|
|
489
|
-
withErrorInterceptor(interceptor: ErrorInterceptor): ApiBuilder;
|
|
490
|
-
/**
|
|
491
|
-
* Adds default query parameters to all requests made through this API instance.
|
|
492
|
-
*
|
|
493
|
-
* @param params - An object containing query parameter key-value pairs
|
|
494
|
-
* @returns The API builder instance for chaining
|
|
495
|
-
*
|
|
496
|
-
* @example
|
|
497
|
-
* ```typescript
|
|
498
|
-
* const api = createApi()
|
|
499
|
-
* .withBaseURL('https://api.example.com')
|
|
500
|
-
* .withQueryParams({ apiVersion: 'v2', format: 'json' });
|
|
501
|
-
* // All requests will include ?apiVersion=v2&format=json
|
|
502
|
-
* ```
|
|
503
|
-
*/
|
|
504
|
-
withQueryParams(params: Record<string, string | string[] | number | boolean | null | undefined>): ApiBuilder;
|
|
505
|
-
/**
|
|
506
|
-
* Adds a single default query parameter to all requests made through this API instance.
|
|
507
|
-
*
|
|
508
|
-
* @param key - The query parameter name
|
|
509
|
-
* @param value - The query parameter value
|
|
510
|
-
* @returns The API builder instance for chaining
|
|
511
|
-
*
|
|
512
|
-
* @example
|
|
513
|
-
* ```typescript
|
|
514
|
-
* const api = createApi()
|
|
515
|
-
* .withBaseURL('https://api.example.com')
|
|
516
|
-
* .withQueryParam('apiKey', 'abc123');
|
|
517
|
-
* ```
|
|
518
|
-
*/
|
|
519
|
-
withQueryParam(key: string, value: string | string[] | number | boolean | null | undefined): ApiBuilder;
|
|
520
|
-
};
|
|
521
|
-
/**
|
|
522
|
-
* Creates a new API builder for configuring default request settings.
|
|
523
|
-
* The API builder allows you to set up a base URL, default headers, timeout,
|
|
524
|
-
* and other configuration options that will be applied to all requests made through it.
|
|
525
|
-
*
|
|
526
|
-
* @returns A new API builder instance
|
|
527
|
-
*
|
|
528
|
-
* @example
|
|
529
|
-
* ```typescript
|
|
530
|
-
* // Create an API instance with defaults
|
|
531
|
-
* const api = api()
|
|
532
|
-
* .withBaseURL('https://api.example.com')
|
|
533
|
-
* .withBearerToken('token123')
|
|
534
|
-
* .withTimeout(5000);
|
|
535
|
-
*
|
|
536
|
-
* // All requests will use these defaults
|
|
537
|
-
* const users = await api.get('/users').getJson();
|
|
538
|
-
* const newUser = await api.post('/users').withBody({ name: 'John' }).getJson();
|
|
539
|
-
* ```
|
|
540
|
-
*
|
|
541
|
-
* @example
|
|
542
|
-
* ```typescript
|
|
543
|
-
* // Use without URL when baseURL is set
|
|
544
|
-
* const api = api().withBaseURL('https://api.example.com');
|
|
545
|
-
* const data = await api.get().getJson(); // Requests to https://api.example.com
|
|
546
|
-
* ```
|
|
547
|
-
*
|
|
548
|
-
* @example
|
|
549
|
-
* ```typescript
|
|
550
|
-
* // Override defaults per request
|
|
551
|
-
* const api = api()
|
|
552
|
-
* .withBaseURL('https://api.example.com')
|
|
553
|
-
* .withTimeout(5000);
|
|
554
|
-
*
|
|
555
|
-
* // This request uses a longer timeout
|
|
556
|
-
* await api.get('/slow-endpoint').withTimeout(30000).getJson();
|
|
557
|
-
* ```
|
|
558
|
-
*/
|
|
559
|
-
export declare function api(): ApiBuilder;
|
|
560
|
-
export {};
|
package/dist/library/enums.d.ts
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Enum for HTTP methods
|
|
3
|
-
*/
|
|
4
|
-
export declare enum HttpMethod {
|
|
5
|
-
GET = "GET",
|
|
6
|
-
PUT = "PUT",
|
|
7
|
-
POST = "POST",
|
|
8
|
-
HEAD = "HEAD",
|
|
9
|
-
PATCH = "PATCH",
|
|
10
|
-
DELETE = "DELETE",
|
|
11
|
-
OPTIONS = "OPTIONS"
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Enum for request priorities
|
|
15
|
-
*/
|
|
16
|
-
export declare enum RequestPriority {
|
|
17
|
-
LOW = "low",
|
|
18
|
-
HIGH = "high",
|
|
19
|
-
AUTO = "auto"
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Enum for credentials policies
|
|
23
|
-
*/
|
|
24
|
-
export declare enum CredentialsPolicy {
|
|
25
|
-
OMIT = "omit",
|
|
26
|
-
INCLUDE = "include",
|
|
27
|
-
SAME_ORIGIN = "same-origin"
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Enum for request modes
|
|
31
|
-
*/
|
|
32
|
-
export declare enum RequestMode {
|
|
33
|
-
CORS = "cors",
|
|
34
|
-
NO_CORS = "no-cors",
|
|
35
|
-
SAME_ORIGIN = "same-origin",
|
|
36
|
-
NAVIGATE = "navigate"
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Enum for redirect modes
|
|
40
|
-
*/
|
|
41
|
-
export declare enum RedirectMode {
|
|
42
|
-
ERROR = "error",
|
|
43
|
-
FOLLOW = "follow",
|
|
44
|
-
MANUAL = "manual"
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Enum for cookie SameSite policies
|
|
48
|
-
*/
|
|
49
|
-
export declare enum SameSitePolicy {
|
|
50
|
-
LAX = "Lax",
|
|
51
|
-
NONE = "None",
|
|
52
|
-
STRICT = "Strict"
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Enum for body types
|
|
56
|
-
*/
|
|
57
|
-
export declare enum BodyType {
|
|
58
|
-
JSON = "json",
|
|
59
|
-
STRING = "string",
|
|
60
|
-
BINARY = "binary"
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* Referrer policies for fetch requests
|
|
64
|
-
*/
|
|
65
|
-
export declare enum ReferrerPolicy {
|
|
66
|
-
ORIGIN = "origin",
|
|
67
|
-
UNSAFE_URL = "unsafe-url",
|
|
68
|
-
SAME_ORIGIN = "same-origin",
|
|
69
|
-
NO_REFERRER = "no-referrer",
|
|
70
|
-
STRICT_ORIGIN = "strict-origin",
|
|
71
|
-
ORIGIN_WHEN_CROSS_ORIGIN = "origin-when-cross-origin",
|
|
72
|
-
NO_REFERRER_WHEN_DOWNGRADE = "no-referrer-when-downgrade",
|
|
73
|
-
STRICT_ORIGIN_WHEN_CROSS_ORIGIN = "strict-origin-when-cross-origin"
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Cache modes for fetch requests
|
|
77
|
-
*/
|
|
78
|
-
export declare enum CacheMode {
|
|
79
|
-
RELOAD = "reload",
|
|
80
|
-
DEFAULT = "default",
|
|
81
|
-
NO_CACHE = "no-cache",
|
|
82
|
-
NO_STORE = "no-store",
|
|
83
|
-
FORCE_CACHE = "force-cache",
|
|
84
|
-
ONLY_IF_CACHED = "only-if-cached"
|
|
85
|
-
}
|