create-request 1.4.3-rc.4 → 1.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +184 -12
- package/dist/library/BaseRequest.d.ts +20 -5
- package/dist/library/RequestError.d.ts +2 -2
- package/dist/library/apiBuilder.d.ts +476 -115
- package/dist/library/index.cjs +192 -291
- package/dist/library/index.cjs.map +1 -1
- package/dist/library/index.d.ts +3 -3
- package/dist/library/index.esm.js +184 -283
- package/dist/library/index.esm.js.map +1 -1
- package/dist/library/index.esm.min.js +1 -1
- package/dist/library/index.esm.min.js.map +1 -1
- package/dist/library/index.min.cjs +1 -1
- package/dist/library/index.min.cjs.map +1 -1
- package/dist/library/requestFactories.d.ts +3 -3
- package/dist/library/requestMethods.d.ts +3 -3
- package/dist/library/types.d.ts +8 -8
- package/package.json +38 -36
|
@@ -1,180 +1,541 @@
|
|
|
1
|
-
import
|
|
2
|
-
import type {
|
|
3
|
-
|
|
4
|
-
withoutCsrfProtection(): ApiBuilder & ApiBuilderRequestMethods;
|
|
5
|
-
withAntiCsrfHeaders(): ApiBuilder & ApiBuilderRequestMethods;
|
|
6
|
-
withTimeout(timeout: number): ApiBuilder & ApiBuilderRequestMethods;
|
|
7
|
-
withReferrer(referrer: string): ApiBuilder & ApiBuilderRequestMethods;
|
|
8
|
-
withKeepAlive(keepalive: boolean): ApiBuilder & ApiBuilderRequestMethods;
|
|
9
|
-
withIntegrity(integrity: string): ApiBuilder & ApiBuilderRequestMethods;
|
|
10
|
-
withHeader(key: string, value: string): ApiBuilder & ApiBuilderRequestMethods;
|
|
11
|
-
withHeaders(headers: Record<string, string>): ApiBuilder & ApiBuilderRequestMethods;
|
|
12
|
-
withRetries(retries: number | RetryConfig): ApiBuilder & ApiBuilderRequestMethods;
|
|
13
|
-
withBearerToken(token: string): ApiBuilder & ApiBuilderRequestMethods;
|
|
14
|
-
withCookies(cookies: CookiesRecord): ApiBuilder & ApiBuilderRequestMethods;
|
|
15
|
-
withContentType(contentType: string): ApiBuilder & ApiBuilderRequestMethods;
|
|
16
|
-
withAuthorization(authValue: string): ApiBuilder & ApiBuilderRequestMethods;
|
|
17
|
-
withBasicAuth(username: string, password: string): ApiBuilder & ApiBuilderRequestMethods;
|
|
18
|
-
withCsrfToken(token: string, headerName?: string): ApiBuilder & ApiBuilderRequestMethods;
|
|
19
|
-
withErrorInterceptor(interceptor: ErrorInterceptor): ApiBuilder & ApiBuilderRequestMethods;
|
|
20
|
-
withCookie(name: string, value: string | CookieOptions): ApiBuilder & ApiBuilderRequestMethods;
|
|
21
|
-
withRequestInterceptor(interceptor: RequestInterceptor): ApiBuilder & ApiBuilderRequestMethods;
|
|
22
|
-
withResponseInterceptor(interceptor: ResponseInterceptor): ApiBuilder & ApiBuilderRequestMethods;
|
|
23
|
-
}
|
|
1
|
+
import { GetRequest, PostRequest, PutRequest, DeleteRequest, PatchRequest, HeadRequest, OptionsRequest } from "./requestMethods.js";
|
|
2
|
+
import type { RetryConfig, RetryCallback, CookiesRecord, CookieOptions, RequestInterceptor, ResponseInterceptor, ErrorInterceptor } from "./types.js";
|
|
3
|
+
import type { CredentialsPolicy, RedirectMode, RequestPriority, ReferrerPolicy, RequestMode } from "./enums.js";
|
|
24
4
|
/**
|
|
25
|
-
* API
|
|
26
|
-
* Allows you to set default headers, timeouts, authentication, and other options
|
|
27
|
-
* that will be applied to all requests created through this builder.
|
|
28
|
-
*
|
|
29
|
-
* @example
|
|
30
|
-
* ```typescript
|
|
31
|
-
* const api = create.api()
|
|
32
|
-
* .withBaseURL("https://api.example.com")
|
|
33
|
-
* .withBearerToken("token123")
|
|
34
|
-
* .withTimeout(5000);
|
|
35
|
-
*
|
|
36
|
-
* // All requests will use the base URL, bearer token, and timeout
|
|
37
|
-
* await api.get("/users").getJson();
|
|
38
|
-
* await api.post("/posts").withBody({ title: "Hello" }).getJson();
|
|
39
|
-
* ```
|
|
5
|
+
* API Builder for creating configured API instances with reusable default settings.
|
|
40
6
|
*/
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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;
|
|
44
110
|
/**
|
|
45
|
-
*
|
|
46
|
-
*
|
|
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
|
|
47
117
|
*
|
|
48
|
-
* @param baseURL - The base URL to use for all requests
|
|
49
|
-
* @returns The API builder instance for method chaining
|
|
50
118
|
* @example
|
|
51
119
|
* ```typescript
|
|
52
|
-
*
|
|
53
|
-
*
|
|
120
|
+
* api.onRetry((attempt, error, delay) => {
|
|
121
|
+
* console.log(`Retry attempt ${attempt} after ${delay}ms due to:`, error.message);
|
|
122
|
+
* });
|
|
54
123
|
* ```
|
|
55
124
|
*/
|
|
56
|
-
|
|
125
|
+
onRetry(callback: RetryCallback): ApiBuilder;
|
|
57
126
|
/**
|
|
58
|
-
*
|
|
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.
|
|
59
135
|
*
|
|
60
|
-
* @
|
|
61
|
-
* @
|
|
62
|
-
*
|
|
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
|
+
* ```
|
|
63
144
|
*/
|
|
64
|
-
|
|
145
|
+
withCredentials(credentials: CredentialsPolicy): ApiBuilder;
|
|
65
146
|
/**
|
|
66
|
-
*
|
|
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
|
|
67
192
|
*
|
|
68
|
-
* @param url - Optional URL path. If not provided, uses the base URL. If relative, resolves against base URL.
|
|
69
|
-
* @returns A GetRequest instance ready to be executed
|
|
70
193
|
* @example
|
|
71
194
|
* ```typescript
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
* await api.get("https://other.com/data").getJson(); // Absolute URL overrides base
|
|
195
|
+
* api.withRedirect('error'); // Throw an error on redirect
|
|
196
|
+
* api.withRedirect(RedirectMode.ERROR); // Using enum
|
|
75
197
|
* ```
|
|
76
198
|
*/
|
|
77
|
-
|
|
199
|
+
withRedirect(redirect: RedirectMode): ApiBuilder;
|
|
78
200
|
/**
|
|
79
|
-
*
|
|
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
|
|
80
206
|
*
|
|
81
|
-
* @param url - Optional URL path. If not provided, uses the base URL. If relative, resolves against base URL.
|
|
82
|
-
* @returns A PostRequest instance ready to be executed
|
|
83
207
|
* @example
|
|
84
208
|
* ```typescript
|
|
85
|
-
*
|
|
86
|
-
* await api.post("/users").withBody({ name: "John" }).getJson();
|
|
209
|
+
* api.withKeepAlive(true);
|
|
87
210
|
* ```
|
|
88
211
|
*/
|
|
89
|
-
|
|
212
|
+
withKeepAlive(keepalive: boolean): ApiBuilder;
|
|
90
213
|
/**
|
|
91
|
-
*
|
|
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
|
|
92
223
|
*
|
|
93
|
-
* @param url - Optional URL path. If not provided, uses the base URL. If relative, resolves against base URL.
|
|
94
|
-
* @returns A PutRequest instance ready to be executed
|
|
95
224
|
* @example
|
|
96
225
|
* ```typescript
|
|
97
|
-
*
|
|
98
|
-
*
|
|
226
|
+
* api.withPriority('high'); // Mark as high priority
|
|
227
|
+
* api.withPriority(RequestPriority.HIGH); // Using enum
|
|
99
228
|
* ```
|
|
100
229
|
*/
|
|
101
|
-
|
|
230
|
+
withPriority(priority: RequestPriority): ApiBuilder;
|
|
102
231
|
/**
|
|
103
|
-
*
|
|
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
|
|
104
237
|
*
|
|
105
|
-
* @param url - Optional URL path. If not provided, uses the base URL. If relative, resolves against base URL.
|
|
106
|
-
* @returns A DeleteRequest instance ready to be executed
|
|
107
238
|
* @example
|
|
108
239
|
* ```typescript
|
|
109
|
-
*
|
|
110
|
-
* await api.del("/users/123").getResponse();
|
|
240
|
+
* api.withIntegrity('sha384-oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8wC');
|
|
111
241
|
* ```
|
|
112
242
|
*/
|
|
113
|
-
|
|
243
|
+
withIntegrity(integrity: string): ApiBuilder;
|
|
114
244
|
/**
|
|
115
|
-
*
|
|
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
|
|
116
258
|
*
|
|
117
|
-
* @param url - Optional URL path. If not provided, uses the base URL. If relative, resolves against base URL.
|
|
118
|
-
* @returns A PatchRequest instance ready to be executed
|
|
119
259
|
* @example
|
|
120
260
|
* ```typescript
|
|
121
|
-
*
|
|
122
|
-
*
|
|
261
|
+
* api.withCache('no-store'); // Don't cache this request
|
|
262
|
+
* api.withCache('force-cache'); // Use cache even if stale
|
|
123
263
|
* ```
|
|
124
264
|
*/
|
|
125
|
-
|
|
265
|
+
withCache(cache: RequestCache): ApiBuilder;
|
|
126
266
|
/**
|
|
127
|
-
*
|
|
267
|
+
* Set the request mode.
|
|
268
|
+
* Controls CORS behavior and what types of responses are allowed.
|
|
269
|
+
*
|
|
270
|
+
* Request modes:
|
|
271
|
+
* - `cors` (default): Allow cross-origin requests, follow CORS protocol
|
|
272
|
+
* - `no-cors`: Make cross-origin request without CORS headers (limited response access)
|
|
273
|
+
* - `same-origin`: Only allow same-origin requests, reject cross-origin
|
|
274
|
+
* - `navigate`: Used for navigation requests (usually not needed for fetch)
|
|
275
|
+
*
|
|
276
|
+
* @param mode - The request mode
|
|
277
|
+
* @returns The API builder instance for chaining
|
|
128
278
|
*
|
|
129
|
-
* @param url - Optional URL path. If not provided, uses the base URL. If relative, resolves against base URL.
|
|
130
|
-
* @returns A HeadRequest instance ready to be executed
|
|
131
279
|
* @example
|
|
132
280
|
* ```typescript
|
|
133
|
-
*
|
|
134
|
-
*
|
|
281
|
+
* api.withMode('same-origin'); // Only allow same-origin requests
|
|
282
|
+
* api.withMode(RequestMode.SAME_ORIGIN); // Using enum
|
|
135
283
|
* ```
|
|
136
284
|
*/
|
|
137
|
-
|
|
285
|
+
withMode(mode: RequestMode): ApiBuilder;
|
|
138
286
|
/**
|
|
139
|
-
*
|
|
287
|
+
* Sets the Content-Type header for the request.
|
|
288
|
+
* Shorthand for `withHeader('Content-Type', contentType)`.
|
|
289
|
+
*
|
|
290
|
+
* @param contentType - The MIME type (e.g., 'application/json', 'text/plain', 'application/xml')
|
|
291
|
+
* @returns The API builder instance for chaining
|
|
140
292
|
*
|
|
141
|
-
* @param url - Optional URL path. If not provided, uses the base URL. If relative, resolves against base URL.
|
|
142
|
-
* @returns An OptionsRequest instance ready to be executed
|
|
143
293
|
* @example
|
|
144
294
|
* ```typescript
|
|
145
|
-
*
|
|
146
|
-
*
|
|
295
|
+
* api.withContentType('application/json');
|
|
296
|
+
* ```
|
|
297
|
+
*
|
|
298
|
+
* @example
|
|
299
|
+
* ```typescript
|
|
300
|
+
* api.withContentType('application/xml');
|
|
147
301
|
* ```
|
|
148
302
|
*/
|
|
149
|
-
|
|
303
|
+
withContentType(contentType: string): ApiBuilder;
|
|
150
304
|
/**
|
|
151
|
-
*
|
|
152
|
-
*
|
|
153
|
-
*
|
|
305
|
+
* Sets the Authorization header for the request.
|
|
306
|
+
* Shorthand for `withHeader('Authorization', authValue)`.
|
|
307
|
+
* For Bearer tokens, use `withBearerToken()` instead. For Basic auth, use `withBasicAuth()`.
|
|
308
|
+
*
|
|
309
|
+
* @param authValue - The full authorization header value (e.g., `'Bearer token123'`, `'Basic base64string'`)
|
|
310
|
+
* @returns The API builder instance for chaining
|
|
154
311
|
*
|
|
155
|
-
* @
|
|
312
|
+
* @example
|
|
313
|
+
* ```typescript
|
|
314
|
+
* api.withAuthorization('Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...');
|
|
315
|
+
* ```
|
|
316
|
+
*
|
|
317
|
+
* @example
|
|
318
|
+
* ```typescript
|
|
319
|
+
* api.withAuthorization('CustomScheme customToken');
|
|
320
|
+
* ```
|
|
156
321
|
*/
|
|
157
|
-
|
|
158
|
-
|
|
322
|
+
withAuthorization(authValue: string): ApiBuilder;
|
|
323
|
+
/**
|
|
324
|
+
* Sets up HTTP Basic Authentication.
|
|
325
|
+
* Encodes the username and password in base64 and sets the Authorization header.
|
|
326
|
+
*
|
|
327
|
+
* @param username - The username for Basic authentication
|
|
328
|
+
* @param password - The password for Basic authentication
|
|
329
|
+
* @returns The API builder instance for chaining
|
|
330
|
+
*
|
|
331
|
+
* @example
|
|
332
|
+
* ```typescript
|
|
333
|
+
* api.withBasicAuth('myuser', 'mypassword');
|
|
334
|
+
* // Sets: Authorization: Basic bXl1c2VyOm15cGFzc3dvcmQ=
|
|
335
|
+
* ```
|
|
336
|
+
*/
|
|
337
|
+
withBasicAuth(username: string, password: string): ApiBuilder;
|
|
338
|
+
/**
|
|
339
|
+
* Sets a Bearer token for authentication.
|
|
340
|
+
* Shorthand for `withAuthorization('Bearer ' + token)`.
|
|
341
|
+
*
|
|
342
|
+
* @param token - The Bearer token (JWT, OAuth token, etc.)
|
|
343
|
+
* @returns The API builder instance for chaining
|
|
344
|
+
*
|
|
345
|
+
* @example
|
|
346
|
+
* ```typescript
|
|
347
|
+
* api.withBearerToken('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...');
|
|
348
|
+
* // Sets: Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
|
|
349
|
+
* ```
|
|
350
|
+
*/
|
|
351
|
+
withBearerToken(token: string): ApiBuilder;
|
|
352
|
+
/**
|
|
353
|
+
* Sets cookies for the request.
|
|
354
|
+
* Cookies are sent in the Cookie header. Multiple calls will merge cookies.
|
|
355
|
+
* Cookie values can be simple strings or objects with additional cookie options.
|
|
356
|
+
*
|
|
357
|
+
* @param cookies - An object where keys are cookie names and values are either:
|
|
358
|
+
* - A string (the cookie value)
|
|
359
|
+
* - A CookieOptions object with `value` and optional properties (secure, httpOnly, sameSite, expires, path, domain, maxAge)
|
|
360
|
+
* @returns The API builder instance for chaining
|
|
361
|
+
*
|
|
362
|
+
* @example
|
|
363
|
+
* ```typescript
|
|
364
|
+
* // Simple string cookies
|
|
365
|
+
* api.withCookies({ sessionId: 'abc123', userId: '456' });
|
|
366
|
+
* ```
|
|
367
|
+
*
|
|
368
|
+
* @example
|
|
369
|
+
* ```typescript
|
|
370
|
+
* // Cookies with options (note: options are for documentation only in request cookies)
|
|
371
|
+
* api.withCookies({
|
|
372
|
+
* sessionId: 'abc123',
|
|
373
|
+
* token: { value: 'xyz789', secure: true }
|
|
374
|
+
* });
|
|
375
|
+
* ```
|
|
376
|
+
*/
|
|
377
|
+
withCookies(cookies: CookiesRecord): ApiBuilder;
|
|
378
|
+
/**
|
|
379
|
+
* Sets a single cookie for the request.
|
|
380
|
+
* Convenience method for adding one cookie at a time.
|
|
381
|
+
*
|
|
382
|
+
* @param name - The cookie name
|
|
383
|
+
* @param value - The cookie value as a string, or a CookieOptions object with `value` and optional properties
|
|
384
|
+
* @returns The API builder instance for chaining
|
|
385
|
+
*
|
|
386
|
+
* @example
|
|
387
|
+
* ```typescript
|
|
388
|
+
* api.withCookie('sessionId', 'abc123');
|
|
389
|
+
* ```
|
|
390
|
+
*
|
|
391
|
+
* @example
|
|
392
|
+
* ```typescript
|
|
393
|
+
* api.withCookie('token', { value: 'xyz789', secure: true });
|
|
394
|
+
* ```
|
|
395
|
+
*/
|
|
396
|
+
withCookie(name: string, value: string | CookieOptions): ApiBuilder;
|
|
397
|
+
/**
|
|
398
|
+
* Sets a CSRF (Cross-Site Request Forgery) token in the request headers.
|
|
399
|
+
* This is commonly used to protect against CSRF attacks in web applications.
|
|
400
|
+
*
|
|
401
|
+
* @param token - The CSRF token value
|
|
402
|
+
* @param headerName - The name of the header to use. Defaults to `'X-CSRF-Token'`.
|
|
403
|
+
* @returns The API builder instance for chaining
|
|
404
|
+
*
|
|
405
|
+
* @example
|
|
406
|
+
* ```typescript
|
|
407
|
+
* api.withCsrfToken('csrf-token-123');
|
|
408
|
+
* // Sets: X-CSRF-Token: csrf-token-123
|
|
409
|
+
* ```
|
|
410
|
+
*
|
|
411
|
+
* @example
|
|
412
|
+
* ```typescript
|
|
413
|
+
* api.withCsrfToken('token', 'X-Custom-CSRF-Header');
|
|
414
|
+
* // Sets: X-Custom-CSRF-Header: token
|
|
415
|
+
* ```
|
|
416
|
+
*/
|
|
417
|
+
withCsrfToken(token: string, headerName?: string): ApiBuilder;
|
|
418
|
+
/**
|
|
419
|
+
* Disables automatic anti-CSRF protection.
|
|
420
|
+
* By default, X-Requested-With: XMLHttpRequest header is sent with all requests.
|
|
421
|
+
* @returns The API builder instance for chaining
|
|
422
|
+
*/
|
|
423
|
+
withoutCsrfProtection(): ApiBuilder;
|
|
424
|
+
/**
|
|
425
|
+
* Sets common security headers to help prevent CSRF attacks
|
|
426
|
+
* @returns The API builder instance for chaining
|
|
427
|
+
*/
|
|
428
|
+
withAntiCsrfHeaders(): ApiBuilder;
|
|
429
|
+
/**
|
|
430
|
+
* Add a request interceptor for this specific request
|
|
431
|
+
* Request interceptors can modify the request configuration or return an early response
|
|
432
|
+
*
|
|
433
|
+
* @param interceptor - The request interceptor function
|
|
434
|
+
* @returns The API builder instance for chaining
|
|
435
|
+
*
|
|
436
|
+
* @example
|
|
437
|
+
* api.withRequestInterceptor((config) => {
|
|
438
|
+
* config.headers['X-Custom'] = 'value';
|
|
439
|
+
* return config;
|
|
440
|
+
* });
|
|
441
|
+
*/
|
|
442
|
+
withRequestInterceptor(interceptor: RequestInterceptor): ApiBuilder;
|
|
443
|
+
/**
|
|
444
|
+
* Add a response interceptor for this specific request
|
|
445
|
+
* Response interceptors can transform the response
|
|
446
|
+
*
|
|
447
|
+
* @param interceptor - The response interceptor function
|
|
448
|
+
* @returns The API builder instance for chaining
|
|
449
|
+
*
|
|
450
|
+
* @example
|
|
451
|
+
* api.withResponseInterceptor((response) => {
|
|
452
|
+
* console.log('Status:', response.status);
|
|
453
|
+
* return response;
|
|
454
|
+
* });
|
|
455
|
+
*/
|
|
456
|
+
withResponseInterceptor(interceptor: ResponseInterceptor): ApiBuilder;
|
|
457
|
+
/**
|
|
458
|
+
* Add an error interceptor for this specific request
|
|
459
|
+
* Error interceptors can handle or transform errors
|
|
460
|
+
*
|
|
461
|
+
* @param interceptor - The error interceptor function
|
|
462
|
+
* @returns The API builder instance for chaining
|
|
463
|
+
*
|
|
464
|
+
* @example
|
|
465
|
+
* api.withErrorInterceptor((error) => {
|
|
466
|
+
* console.error('Request failed:', error);
|
|
467
|
+
* throw error;
|
|
468
|
+
* });
|
|
469
|
+
*/
|
|
470
|
+
withErrorInterceptor(interceptor: ErrorInterceptor): ApiBuilder;
|
|
471
|
+
/**
|
|
472
|
+
* Adds default query parameters to all requests made through this API instance.
|
|
473
|
+
*
|
|
474
|
+
* @param params - An object containing query parameter key-value pairs
|
|
475
|
+
* @returns The API builder instance for chaining
|
|
476
|
+
*
|
|
477
|
+
* @example
|
|
478
|
+
* ```typescript
|
|
479
|
+
* const api = createApi()
|
|
480
|
+
* .withBaseURL('https://api.example.com')
|
|
481
|
+
* .withQueryParams({ apiVersion: 'v2', format: 'json' });
|
|
482
|
+
* // All requests will include ?apiVersion=v2&format=json
|
|
483
|
+
* ```
|
|
484
|
+
*/
|
|
485
|
+
withQueryParams(params: Record<string, string | string[] | number | boolean | null | undefined>): ApiBuilder;
|
|
486
|
+
/**
|
|
487
|
+
* Adds a single default query parameter to all requests made through this API instance.
|
|
488
|
+
*
|
|
489
|
+
* @param key - The query parameter name
|
|
490
|
+
* @param value - The query parameter value
|
|
491
|
+
* @returns The API builder instance for chaining
|
|
492
|
+
*
|
|
493
|
+
* @example
|
|
494
|
+
* ```typescript
|
|
495
|
+
* const api = createApi()
|
|
496
|
+
* .withBaseURL('https://api.example.com')
|
|
497
|
+
* .withQueryParam('apiKey', 'abc123');
|
|
498
|
+
* ```
|
|
499
|
+
*/
|
|
500
|
+
withQueryParam(key: string, value: string | string[] | number | boolean | null | undefined): ApiBuilder;
|
|
501
|
+
};
|
|
159
502
|
/**
|
|
160
|
-
* Creates a new API builder
|
|
161
|
-
* The builder allows you to set base
|
|
162
|
-
* and other options that will be applied to all requests
|
|
503
|
+
* Creates a new API builder for configuring default request settings.
|
|
504
|
+
* The API builder allows you to set up a base URL, default headers, timeout,
|
|
505
|
+
* and other configuration options that will be applied to all requests made through it.
|
|
506
|
+
*
|
|
507
|
+
* @returns A new API builder instance
|
|
163
508
|
*
|
|
164
|
-
* @returns A new ApiBuilder instance with all configuration methods available
|
|
165
509
|
* @example
|
|
166
510
|
* ```typescript
|
|
167
|
-
* // Create an API instance with
|
|
168
|
-
* const api =
|
|
169
|
-
* .withBaseURL(
|
|
170
|
-
* .withBearerToken(
|
|
171
|
-
* .withTimeout(5000)
|
|
172
|
-
* .withHeaders({ "X-Custom": "value" });
|
|
511
|
+
* // Create an API instance with defaults
|
|
512
|
+
* const api = api()
|
|
513
|
+
* .withBaseURL('https://api.example.com')
|
|
514
|
+
* .withBearerToken('token123')
|
|
515
|
+
* .withTimeout(5000);
|
|
173
516
|
*
|
|
174
517
|
* // All requests will use these defaults
|
|
175
|
-
* const users = await api.get(
|
|
176
|
-
* const newUser = await api.post(
|
|
518
|
+
* const users = await api.get('/users').getJson();
|
|
519
|
+
* const newUser = await api.post('/users').withBody({ name: 'John' }).getJson();
|
|
520
|
+
* ```
|
|
521
|
+
*
|
|
522
|
+
* @example
|
|
523
|
+
* ```typescript
|
|
524
|
+
* // Use without URL when baseURL is set
|
|
525
|
+
* const api = api().withBaseURL('https://api.example.com');
|
|
526
|
+
* const data = await api.get().getJson(); // Requests to https://api.example.com
|
|
527
|
+
* ```
|
|
528
|
+
*
|
|
529
|
+
* @example
|
|
530
|
+
* ```typescript
|
|
531
|
+
* // Override defaults per request
|
|
532
|
+
* const api = api()
|
|
533
|
+
* .withBaseURL('https://api.example.com')
|
|
534
|
+
* .withTimeout(5000);
|
|
535
|
+
*
|
|
536
|
+
* // This request uses a longer timeout
|
|
537
|
+
* await api.get('/slow-endpoint').withTimeout(30000).getJson();
|
|
177
538
|
* ```
|
|
178
539
|
*/
|
|
179
|
-
export declare function api(): ApiBuilder
|
|
540
|
+
export declare function api(): ApiBuilder;
|
|
180
541
|
export {};
|