create-request 0.0.1-beta.9 → 1.1.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/README.md +508 -303
- package/dist/library/BaseRequest.d.ts +301 -41
- package/dist/library/BodyRequest.d.ts +39 -4
- package/dist/library/RequestError.d.ts +5 -2
- package/dist/library/ResponseWrapper.d.ts +96 -13
- package/dist/library/enums.d.ts +0 -11
- package/dist/library/index.cjs +2201 -0
- package/dist/library/index.cjs.map +1 -0
- package/dist/library/index.d.ts +12 -12
- package/dist/library/index.esm.js +1416 -728
- package/dist/library/index.esm.js.map +1 -0
- package/dist/library/index.esm.min.js +1 -0
- package/dist/library/index.esm.min.js.map +1 -0
- package/dist/library/index.min.cjs +1 -0
- package/dist/library/index.min.cjs.map +1 -0
- package/dist/library/requestFactories.d.ts +70 -9
- package/dist/library/requestMethods.d.ts +70 -3
- package/dist/library/types.d.ts +47 -5
- package/dist/library/utils/Config.d.ts +164 -12
- package/dist/library/utils/CookieUtils.d.ts +1 -1
- package/dist/library/utils/sizeUtils.d.ts +8 -0
- package/package.json +74 -16
- package/dist/library/index.js +0 -1525
- package/dist/library/types/cache.d.ts +0 -62
- package/dist/library/utils/CacheManager.d.ts +0 -35
- package/dist/library/utils/CacheStorage.d.ts +0 -57
- package/dist/library/utils/StorageProvider.d.ts +0 -33
|
@@ -1,28 +1,105 @@
|
|
|
1
|
-
import { type HttpMethod,
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { type HttpMethod, RedirectMode, RequestMode, RequestPriority, ReferrerPolicy, CredentialsPolicy } from "./enums.js";
|
|
2
|
+
import type { RetryCallback, CookiesRecord, CookieOptions, RequestOptions, GraphQLOptions, ErrorInterceptor, RequestInterceptor, ResponseInterceptor } from "./types.js";
|
|
3
|
+
import { ResponseWrapper } from "./ResponseWrapper.js";
|
|
4
|
+
/**
|
|
5
|
+
* Base class with common functionality for all request types
|
|
6
|
+
* Provides the core request building and execution capabilities.
|
|
7
|
+
*/
|
|
6
8
|
export declare abstract class BaseRequest {
|
|
7
9
|
protected abstract method: HttpMethod;
|
|
10
|
+
protected url: string;
|
|
8
11
|
protected requestOptions: RequestOptions;
|
|
9
12
|
protected abortController?: AbortController;
|
|
10
13
|
protected queryParams: URLSearchParams;
|
|
11
14
|
protected autoApplyCsrfProtection: boolean;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
+
private requestInterceptors;
|
|
16
|
+
private responseInterceptors;
|
|
17
|
+
private errorInterceptors;
|
|
18
|
+
constructor(url: string);
|
|
19
|
+
/**
|
|
20
|
+
* Get GraphQL options if set (only for BodyRequest subclasses)
|
|
21
|
+
* @returns GraphQL options or undefined
|
|
22
|
+
*/
|
|
23
|
+
protected getGraphQLOptions(): GraphQLOptions | undefined;
|
|
24
|
+
/**
|
|
25
|
+
* Creates a fluent API for setting enum-based options
|
|
26
|
+
* Combines direct setter with convenience methods
|
|
27
|
+
*/
|
|
28
|
+
private createFluentSetter;
|
|
29
|
+
private validateUrl;
|
|
30
|
+
/**
|
|
31
|
+
* Add multiple HTTP headers to the request
|
|
32
|
+
*
|
|
33
|
+
* @param headers - Key-value pairs of header names and values
|
|
34
|
+
* @returns The request instance for chaining
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* request.withHeaders({
|
|
38
|
+
* 'Accept': 'application/json',
|
|
39
|
+
* 'X-Custom-Header': 'value'
|
|
40
|
+
* });
|
|
41
|
+
*/
|
|
15
42
|
withHeaders(headers: Record<string, string>): this;
|
|
43
|
+
/**
|
|
44
|
+
* Add a single HTTP header to the request
|
|
45
|
+
*
|
|
46
|
+
* @param key - The header name
|
|
47
|
+
* @param value - The header value
|
|
48
|
+
* @returns The request instance for chaining
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* request.withHeader('Accept', 'application/json');
|
|
52
|
+
*/
|
|
16
53
|
withHeader(key: string, value: string): this;
|
|
54
|
+
/**
|
|
55
|
+
* Set a timeout for the request
|
|
56
|
+
* If the request takes longer than the specified timeout, it will be aborted.
|
|
57
|
+
*
|
|
58
|
+
* @param timeout - The timeout in milliseconds
|
|
59
|
+
* @returns The request instance for chaining
|
|
60
|
+
* @throws RequestError if timeout is not a positive number
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* request.withTimeout(5000); // 5 seconds timeout
|
|
64
|
+
*/
|
|
17
65
|
withTimeout(timeout: number): this;
|
|
66
|
+
/**
|
|
67
|
+
* Configure automatic retry behavior for failed requests
|
|
68
|
+
*
|
|
69
|
+
* @param retries - Number of retry attempts before failing
|
|
70
|
+
* @returns The request instance for chaining
|
|
71
|
+
* @throws RequestError if retries is not a non-negative integer
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* request.withRetries(3); // Retry up to 3 times
|
|
75
|
+
*/
|
|
18
76
|
withRetries(retries: number): this;
|
|
77
|
+
/**
|
|
78
|
+
* Set a callback to be invoked before each retry attempt
|
|
79
|
+
* Useful for implementing backoff strategies or logging retry attempts.
|
|
80
|
+
*
|
|
81
|
+
* @param callback - Function to call before retrying
|
|
82
|
+
* @returns The request instance for chaining
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* request.onRetry(({ attempt, error }) => {
|
|
86
|
+
* console.log(`Retry attempt ${attempt} after error: ${error.message}`);
|
|
87
|
+
* return new Promise(resolve => setTimeout(resolve, attempt * 1000));
|
|
88
|
+
* });
|
|
89
|
+
*/
|
|
19
90
|
onRetry(callback: RetryCallback): this;
|
|
20
91
|
/**
|
|
21
|
-
* Sets
|
|
22
|
-
* @
|
|
23
|
-
*
|
|
92
|
+
* Sets credentials policy - supports both direct call and fluent API
|
|
93
|
+
* @example
|
|
94
|
+
* request.withCredentials("include")
|
|
95
|
+
* request.withCredentials(CredentialsPolicy.INCLUDE)
|
|
96
|
+
* request.withCredentials.INCLUDE()
|
|
24
97
|
*/
|
|
25
|
-
withCredentials(credentialsPolicy
|
|
98
|
+
get withCredentials(): ((credentialsPolicy: CredentialsPolicy | string) => BaseRequest) & {
|
|
99
|
+
INCLUDE: () => BaseRequest;
|
|
100
|
+
OMIT: () => BaseRequest;
|
|
101
|
+
SAME_ORIGIN: () => BaseRequest;
|
|
102
|
+
};
|
|
26
103
|
/**
|
|
27
104
|
* Allows providing an external AbortController to cancel the request
|
|
28
105
|
* @param controller - The AbortController to use for this request
|
|
@@ -36,17 +113,34 @@ export declare abstract class BaseRequest {
|
|
|
36
113
|
*/
|
|
37
114
|
withReferrer(referrer: string): this;
|
|
38
115
|
/**
|
|
39
|
-
* Sets
|
|
40
|
-
* @
|
|
41
|
-
*
|
|
116
|
+
* Sets referrer policy - supports both direct call and fluent API
|
|
117
|
+
* @example
|
|
118
|
+
* request.withReferrerPolicy("no-referrer")
|
|
119
|
+
* request.withReferrerPolicy(ReferrerPolicy.NO_REFERRER)
|
|
120
|
+
* request.withReferrerPolicy.NO_REFERRER()
|
|
42
121
|
*/
|
|
43
|
-
withReferrerPolicy(policy: ReferrerPolicy)
|
|
122
|
+
get withReferrerPolicy(): ((policy: ReferrerPolicy | string) => BaseRequest) & {
|
|
123
|
+
ORIGIN: () => BaseRequest;
|
|
124
|
+
UNSAFE_URL: () => BaseRequest;
|
|
125
|
+
SAME_ORIGIN: () => BaseRequest;
|
|
126
|
+
NO_REFERRER: () => BaseRequest;
|
|
127
|
+
STRICT_ORIGIN: () => BaseRequest;
|
|
128
|
+
ORIGIN_WHEN_CROSS_ORIGIN: () => BaseRequest;
|
|
129
|
+
NO_REFERRER_WHEN_DOWNGRADE: () => BaseRequest;
|
|
130
|
+
STRICT_ORIGIN_WHEN_CROSS_ORIGIN: () => BaseRequest;
|
|
131
|
+
};
|
|
44
132
|
/**
|
|
45
|
-
* Sets
|
|
46
|
-
* @
|
|
47
|
-
*
|
|
133
|
+
* Sets redirect mode - supports both direct call and fluent API
|
|
134
|
+
* @example
|
|
135
|
+
* request.withRedirect("follow")
|
|
136
|
+
* request.withRedirect(RedirectMode.FOLLOW)
|
|
137
|
+
* request.withRedirect.FOLLOW()
|
|
48
138
|
*/
|
|
49
|
-
withRedirect(redirect: RedirectMode)
|
|
139
|
+
get withRedirect(): ((redirect: RedirectMode | string) => BaseRequest) & {
|
|
140
|
+
FOLLOW: () => BaseRequest;
|
|
141
|
+
ERROR: () => BaseRequest;
|
|
142
|
+
MANUAL: () => BaseRequest;
|
|
143
|
+
};
|
|
50
144
|
/**
|
|
51
145
|
* Sets the keepalive flag for the request
|
|
52
146
|
* @param keepalive - Whether to allow the request to outlive the page
|
|
@@ -54,11 +148,17 @@ export declare abstract class BaseRequest {
|
|
|
54
148
|
*/
|
|
55
149
|
withKeepAlive(keepalive: boolean): this;
|
|
56
150
|
/**
|
|
57
|
-
* Sets
|
|
58
|
-
* @
|
|
59
|
-
*
|
|
151
|
+
* Sets request priority - supports both direct call and fluent API
|
|
152
|
+
* @example
|
|
153
|
+
* request.withPriority("high")
|
|
154
|
+
* request.withPriority(RequestPriority.HIGH)
|
|
155
|
+
* request.withPriority.HIGH()
|
|
60
156
|
*/
|
|
61
|
-
withPriority(priority: RequestPriority)
|
|
157
|
+
get withPriority(): ((priority: RequestPriority | string) => BaseRequest) & {
|
|
158
|
+
HIGH: () => BaseRequest;
|
|
159
|
+
LOW: () => BaseRequest;
|
|
160
|
+
AUTO: () => BaseRequest;
|
|
161
|
+
};
|
|
62
162
|
/**
|
|
63
163
|
* Adds query parameters to the request URL
|
|
64
164
|
* @param params - An object containing the query parameters
|
|
@@ -73,11 +173,18 @@ export declare abstract class BaseRequest {
|
|
|
73
173
|
*/
|
|
74
174
|
withQueryParam(key: string, value: string | string[] | number | boolean | null | undefined): this;
|
|
75
175
|
/**
|
|
76
|
-
* Sets
|
|
77
|
-
* @
|
|
78
|
-
*
|
|
176
|
+
* Sets request mode - supports both direct call and fluent API
|
|
177
|
+
* @example
|
|
178
|
+
* request.withMode("cors")
|
|
179
|
+
* request.withMode(RequestMode.CORS)
|
|
180
|
+
* request.withMode.CORS()
|
|
79
181
|
*/
|
|
80
|
-
withMode(mode: RequestMode)
|
|
182
|
+
get withMode(): ((mode: RequestMode | string) => BaseRequest) & {
|
|
183
|
+
CORS: () => BaseRequest;
|
|
184
|
+
NO_CORS: () => BaseRequest;
|
|
185
|
+
SAME_ORIGIN: () => BaseRequest;
|
|
186
|
+
NAVIGATE: () => BaseRequest;
|
|
187
|
+
};
|
|
81
188
|
/**
|
|
82
189
|
* Shorthand for setting the Content-Type header
|
|
83
190
|
* @param contentType - The content type
|
|
@@ -108,9 +215,13 @@ export declare abstract class BaseRequest {
|
|
|
108
215
|
* @returns The instance for chaining
|
|
109
216
|
*/
|
|
110
217
|
withBearerToken(token: string): this;
|
|
218
|
+
/**
|
|
219
|
+
* Safely get headers as a Record<string, string>
|
|
220
|
+
* @returns The headers object
|
|
221
|
+
*/
|
|
222
|
+
private getHeadersRecord;
|
|
111
223
|
/**
|
|
112
224
|
* Helper function to check for header presence in a case-insensitive way
|
|
113
|
-
* @param headers Headers object
|
|
114
225
|
* @param headerName Header name to check
|
|
115
226
|
* @returns Boolean indicating if the header exists (case-insensitive)
|
|
116
227
|
*/
|
|
@@ -147,16 +258,135 @@ export declare abstract class BaseRequest {
|
|
|
147
258
|
*/
|
|
148
259
|
withAntiCsrfHeaders(): this;
|
|
149
260
|
/**
|
|
150
|
-
*
|
|
151
|
-
*
|
|
261
|
+
* Add a request interceptor for this specific request
|
|
262
|
+
* Request interceptors can modify the request configuration or return an early response
|
|
263
|
+
*
|
|
264
|
+
* @param interceptor - The request interceptor function
|
|
152
265
|
* @returns The instance for chaining
|
|
266
|
+
*
|
|
267
|
+
* @example
|
|
268
|
+
* request.withRequestInterceptor((config) => {
|
|
269
|
+
* config.headers['X-Custom'] = 'value';
|
|
270
|
+
* return config;
|
|
271
|
+
* });
|
|
153
272
|
*/
|
|
154
|
-
|
|
273
|
+
withRequestInterceptor(interceptor: RequestInterceptor): this;
|
|
155
274
|
/**
|
|
156
|
-
*
|
|
157
|
-
*
|
|
275
|
+
* Add a response interceptor for this specific request
|
|
276
|
+
* Response interceptors can transform the response
|
|
277
|
+
*
|
|
278
|
+
* @param interceptor - The response interceptor function
|
|
279
|
+
* @returns The instance for chaining
|
|
280
|
+
*
|
|
281
|
+
* @example
|
|
282
|
+
* request.withResponseInterceptor((response) => {
|
|
283
|
+
* console.log('Status:', response.status);
|
|
284
|
+
* return response;
|
|
285
|
+
* });
|
|
286
|
+
*/
|
|
287
|
+
withResponseInterceptor(interceptor: ResponseInterceptor): this;
|
|
288
|
+
/**
|
|
289
|
+
* Add an error interceptor for this specific request
|
|
290
|
+
* Error interceptors can handle or transform errors
|
|
291
|
+
*
|
|
292
|
+
* @param interceptor - The error interceptor function
|
|
293
|
+
* @returns The instance for chaining
|
|
294
|
+
*
|
|
295
|
+
* @example
|
|
296
|
+
* request.withErrorInterceptor((error) => {
|
|
297
|
+
* console.error('Request failed:', error);
|
|
298
|
+
* throw error;
|
|
299
|
+
* });
|
|
158
300
|
*/
|
|
159
|
-
|
|
301
|
+
withErrorInterceptor(interceptor: ErrorInterceptor): this;
|
|
302
|
+
/**
|
|
303
|
+
* Execute the request and return the ResponseWrapper
|
|
304
|
+
* This is the base method for getting the full response.
|
|
305
|
+
*
|
|
306
|
+
* @returns A promise that resolves to the ResponseWrapper
|
|
307
|
+
*
|
|
308
|
+
* @example
|
|
309
|
+
* const response = await request.getResponse();
|
|
310
|
+
* console.log(response.status);
|
|
311
|
+
*/
|
|
312
|
+
getResponse(): Promise<ResponseWrapper>;
|
|
313
|
+
/**
|
|
314
|
+
* Execute the request and parse the response as JSON
|
|
315
|
+
*
|
|
316
|
+
* @returns A promise that resolves to the parsed JSON data
|
|
317
|
+
* @throws {RequestError} When the request fails, JSON parsing fails, GraphQL errors occur (if throwOnError enabled), or body is already consumed
|
|
318
|
+
*
|
|
319
|
+
* @example
|
|
320
|
+
* const users = await request.getJson<User[]>();
|
|
321
|
+
*
|
|
322
|
+
* @example
|
|
323
|
+
* // Error handling - errors are always RequestError
|
|
324
|
+
* try {
|
|
325
|
+
* const data = await request.getJson();
|
|
326
|
+
* } catch (error) {
|
|
327
|
+
* if (error instanceof RequestError) {
|
|
328
|
+
* console.log(error.status, error.url, error.method);
|
|
329
|
+
* }
|
|
330
|
+
* }
|
|
331
|
+
*/
|
|
332
|
+
getJson<T = unknown>(): Promise<T>;
|
|
333
|
+
/**
|
|
334
|
+
* Execute the request and get the response as text
|
|
335
|
+
*
|
|
336
|
+
* @returns A promise that resolves to the response text
|
|
337
|
+
*
|
|
338
|
+
* @example
|
|
339
|
+
* const text = await request.getText();
|
|
340
|
+
*/
|
|
341
|
+
getText(): Promise<string>;
|
|
342
|
+
/**
|
|
343
|
+
* Execute the request and get the response as a Blob
|
|
344
|
+
*
|
|
345
|
+
* @returns A promise that resolves to the response Blob
|
|
346
|
+
*
|
|
347
|
+
* @example
|
|
348
|
+
* const blob = await request.getBlob();
|
|
349
|
+
*/
|
|
350
|
+
getBlob(): Promise<Blob>;
|
|
351
|
+
/**
|
|
352
|
+
* Execute the request and get the response body as a ReadableStream
|
|
353
|
+
*
|
|
354
|
+
* @returns A promise that resolves to the response body stream
|
|
355
|
+
*
|
|
356
|
+
* @example
|
|
357
|
+
* const stream = await request.getBody();
|
|
358
|
+
*/
|
|
359
|
+
getBody(): Promise<ReadableStream<Uint8Array> | null>;
|
|
360
|
+
/**
|
|
361
|
+
* Execute the request and extract specific data using a selector function
|
|
362
|
+
* If no selector is provided, returns the full JSON response.
|
|
363
|
+
*
|
|
364
|
+
* @param selector - Optional function to extract and transform data
|
|
365
|
+
* @returns A promise that resolves to the selected data
|
|
366
|
+
* @throws {RequestError} When the request fails, JSON parsing fails, or the selector throws an error
|
|
367
|
+
*
|
|
368
|
+
* @example
|
|
369
|
+
* // Get full response
|
|
370
|
+
* const data = await request.getData();
|
|
371
|
+
*
|
|
372
|
+
* // Extract specific data
|
|
373
|
+
* const users = await request.getData(data => data.results.users);
|
|
374
|
+
*
|
|
375
|
+
* @example
|
|
376
|
+
* // Error handling - errors are always RequestError
|
|
377
|
+
* try {
|
|
378
|
+
* const data = await request.getData();
|
|
379
|
+
* } catch (error) {
|
|
380
|
+
* if (error instanceof RequestError) {
|
|
381
|
+
* console.log(error.status, error.url, error.method);
|
|
382
|
+
* }
|
|
383
|
+
* }
|
|
384
|
+
*/
|
|
385
|
+
getData<T = unknown, R = T>(selector?: (data: T) => R): Promise<T | R>;
|
|
386
|
+
/**
|
|
387
|
+
* Apply CSRF protection headers based on configuration
|
|
388
|
+
*/
|
|
389
|
+
private applyCsrfProtection;
|
|
160
390
|
/**
|
|
161
391
|
* Formats the URL with any query parameters
|
|
162
392
|
* @param url The base URL
|
|
@@ -168,14 +398,44 @@ export declare abstract class BaseRequest {
|
|
|
168
398
|
* @param url The formatted URL to send the request to
|
|
169
399
|
* @param fetchOptions The fetch options to use
|
|
170
400
|
* @returns A wrapped response object
|
|
401
|
+
* @throws RequestError if the request fails after all retries
|
|
171
402
|
*/
|
|
172
403
|
private executeWithRetries;
|
|
173
|
-
private executeRequest;
|
|
174
404
|
/**
|
|
175
|
-
*
|
|
176
|
-
* @param
|
|
177
|
-
* @
|
|
178
|
-
* @param cacheKey The cache key for this request
|
|
405
|
+
* Run request interceptors in order: global interceptors first, then per-request
|
|
406
|
+
* @param configParam - The request configuration
|
|
407
|
+
* @returns Modified config or a Response to short-circuit
|
|
179
408
|
*/
|
|
180
|
-
private
|
|
409
|
+
private runRequestInterceptors;
|
|
410
|
+
/**
|
|
411
|
+
* Run response interceptors in reverse order: per-request interceptors first, then global in reverse
|
|
412
|
+
* @param response - The response wrapper
|
|
413
|
+
* @returns Modified response wrapper
|
|
414
|
+
*/
|
|
415
|
+
private runResponseInterceptors;
|
|
416
|
+
/**
|
|
417
|
+
* Run error interceptors in reverse order: per-request interceptors first, then global in reverse
|
|
418
|
+
* @param error - The error that occurred
|
|
419
|
+
* @returns Modified error or a ResponseWrapper to recover
|
|
420
|
+
*/
|
|
421
|
+
private runErrorInterceptors;
|
|
422
|
+
/**
|
|
423
|
+
* Helper to create an abort signal with timeout support
|
|
424
|
+
* Handles various AbortSignal API levels gracefully
|
|
425
|
+
*/
|
|
426
|
+
private createAbortSignal;
|
|
427
|
+
/**
|
|
428
|
+
* Manually combine two abort signals for older environments
|
|
429
|
+
* Returns the first signal and listens to the second
|
|
430
|
+
*/
|
|
431
|
+
private combineSignalsManually;
|
|
432
|
+
/**
|
|
433
|
+
* Convert fetchOptions to RequestConfig with proper typing
|
|
434
|
+
*/
|
|
435
|
+
private createRequestConfig;
|
|
436
|
+
/**
|
|
437
|
+
* Apply interceptor results back to fetchOptions
|
|
438
|
+
*/
|
|
439
|
+
private applyRequestConfig;
|
|
440
|
+
private executeRequest;
|
|
181
441
|
}
|
|
@@ -1,20 +1,55 @@
|
|
|
1
|
-
import { BaseRequest } from "./BaseRequest";
|
|
2
|
-
import type { Body } from "./types";
|
|
1
|
+
import { BaseRequest } from "./BaseRequest.js";
|
|
2
|
+
import type { Body, GraphQLOptions } from "./types.js";
|
|
3
|
+
import type { ResponseWrapper } from "./ResponseWrapper.js";
|
|
3
4
|
/**
|
|
4
5
|
* Base class for requests that can have a body (POST, PUT, PATCH)
|
|
5
6
|
*/
|
|
6
7
|
export declare abstract class BodyRequest extends BaseRequest {
|
|
7
8
|
protected body?: Body;
|
|
8
9
|
private bodyType?;
|
|
10
|
+
private graphQLOptions;
|
|
11
|
+
constructor(url: string);
|
|
9
12
|
/**
|
|
10
13
|
* Sets the body of the request
|
|
11
14
|
* @param body - The request body
|
|
12
15
|
*/
|
|
13
16
|
withBody(body: Body): this;
|
|
17
|
+
/**
|
|
18
|
+
* Sets a GraphQL query or mutation as the request body
|
|
19
|
+
* Automatically formats the body as JSON and sets Content-Type to application/json
|
|
20
|
+
*
|
|
21
|
+
* @param query - The GraphQL query or mutation string
|
|
22
|
+
* @param variables - Optional variables object to pass with the query
|
|
23
|
+
* @param options - Optional GraphQL-specific options
|
|
24
|
+
* @returns The request instance for chaining
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* const request = new PostRequest('/graphql')
|
|
28
|
+
* .withGraphQL('query { user(id: $id) { name email } }', { id: '123' });
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* const request = new PostRequest('/graphql')
|
|
32
|
+
* .withGraphQL('mutation { createUser(name: $name) { id } }', { name: 'John' });
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* // Throw an error if the GraphQL response contains errors
|
|
36
|
+
* const request = new PostRequest('/graphql')
|
|
37
|
+
* .withGraphQL('query { user { id } }', undefined, { throwOnError: true });
|
|
38
|
+
*/
|
|
39
|
+
withGraphQL(query: string, variables?: Record<string, unknown>, options?: GraphQLOptions): this;
|
|
40
|
+
/**
|
|
41
|
+
* Check if Content-Type header is already set (case-insensitive)
|
|
42
|
+
*/
|
|
43
|
+
private hasContentType;
|
|
14
44
|
private setContentTypeIfNeeded;
|
|
15
45
|
/**
|
|
16
|
-
*
|
|
46
|
+
* Get the GraphQL options if set
|
|
47
|
+
* @returns The GraphQL options or undefined
|
|
48
|
+
*/
|
|
49
|
+
protected getGraphQLOptions(): GraphQLOptions | undefined;
|
|
50
|
+
/**
|
|
51
|
+
* Execute the request and return the ResponseWrapper
|
|
17
52
|
* Overrides the base implementation to add body handling
|
|
18
53
|
*/
|
|
19
|
-
|
|
54
|
+
getResponse(): Promise<ResponseWrapper>;
|
|
20
55
|
}
|
|
@@ -3,11 +3,13 @@ export declare class RequestError extends Error {
|
|
|
3
3
|
readonly response?: Response;
|
|
4
4
|
readonly url: string;
|
|
5
5
|
readonly method: string;
|
|
6
|
-
readonly
|
|
6
|
+
readonly isTimeout?: boolean;
|
|
7
|
+
readonly isAborted?: boolean;
|
|
7
8
|
constructor(message: string, url: string, method: string, options?: {
|
|
8
9
|
status?: number;
|
|
9
10
|
response?: Response;
|
|
10
|
-
|
|
11
|
+
isTimeout?: boolean;
|
|
12
|
+
isAborted?: boolean;
|
|
11
13
|
});
|
|
12
14
|
/**
|
|
13
15
|
* Static methods for creating specific types of RequestError
|
|
@@ -15,4 +17,5 @@ export declare class RequestError extends Error {
|
|
|
15
17
|
static timeout(url: string, method: string, timeoutMs: number): RequestError;
|
|
16
18
|
static fromResponse(response: Response, url: string, method: string): RequestError;
|
|
17
19
|
static networkError(url: string, method: string, originalError: Error): RequestError;
|
|
20
|
+
static abortError(url: string, method: string): RequestError;
|
|
18
21
|
}
|
|
@@ -1,33 +1,116 @@
|
|
|
1
|
+
import type { GraphQLOptions } from "./types.js";
|
|
1
2
|
/**
|
|
2
3
|
* Wrapper for HTTP responses with methods to transform the response data
|
|
4
|
+
* Provides caching and conversion between different response formats.
|
|
3
5
|
*/
|
|
4
6
|
export declare class ResponseWrapper {
|
|
5
7
|
private readonly response;
|
|
8
|
+
readonly url?: string;
|
|
9
|
+
readonly method?: string;
|
|
6
10
|
private textCache;
|
|
7
11
|
private jsonCache;
|
|
8
12
|
private blobCache;
|
|
9
|
-
private arrayBufferCache;
|
|
10
13
|
private bodyUsed;
|
|
11
|
-
|
|
14
|
+
private graphQLOptions?;
|
|
15
|
+
constructor(response: Response, url?: string, method?: string, graphQLOptions?: GraphQLOptions);
|
|
12
16
|
get status(): number;
|
|
13
17
|
get statusText(): string;
|
|
14
18
|
get headers(): Headers;
|
|
15
19
|
get ok(): boolean;
|
|
16
20
|
get raw(): Response;
|
|
21
|
+
/**
|
|
22
|
+
* Check for GraphQL errors and throw if throwOnError is enabled
|
|
23
|
+
* @param data - The parsed JSON data
|
|
24
|
+
* @throws RequestError if GraphQL response contains errors and throwOnError is enabled
|
|
25
|
+
*/
|
|
26
|
+
private checkGraphQLErrors;
|
|
27
|
+
/**
|
|
28
|
+
* Parse the response body as JSON
|
|
29
|
+
* Caches the result for subsequent calls.
|
|
30
|
+
* If GraphQL options are set with throwOnError=true, will check for GraphQL errors and throw.
|
|
31
|
+
*
|
|
32
|
+
* @returns The parsed JSON data
|
|
33
|
+
* @throws {RequestError} When the request fails, JSON parsing fails, GraphQL errors occur (if throwOnError enabled), or body is already consumed
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* const data = await response.getJson();
|
|
37
|
+
* console.log(data.items);
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* // Error handling - errors are always RequestError
|
|
41
|
+
* try {
|
|
42
|
+
* const data = await response.getJson();
|
|
43
|
+
* } catch (error) {
|
|
44
|
+
* if (error instanceof RequestError) {
|
|
45
|
+
* console.log(error.status, error.url, error.method);
|
|
46
|
+
* }
|
|
47
|
+
* }
|
|
48
|
+
*/
|
|
17
49
|
getJson<T = unknown>(): Promise<T>;
|
|
50
|
+
/**
|
|
51
|
+
* Get the response body as text
|
|
52
|
+
* Caches the result for subsequent calls. Attempts to convert from JSON or Blob
|
|
53
|
+
* if the body has already been consumed in those formats.
|
|
54
|
+
*
|
|
55
|
+
* @returns The response text
|
|
56
|
+
* @throws {RequestError} When reading fails or the response has already been consumed in an incompatible format
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* const text = await response.getText();
|
|
60
|
+
*/
|
|
18
61
|
getText(): Promise<string>;
|
|
62
|
+
/**
|
|
63
|
+
* Get the response body as a Blob
|
|
64
|
+
* Caches the result for subsequent calls. Attempts to convert from text
|
|
65
|
+
* if the body has already been consumed as text.
|
|
66
|
+
*
|
|
67
|
+
* @returns The response as a Blob
|
|
68
|
+
* @throws {RequestError} When reading fails or the response has already been consumed in an incompatible format
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* const blob = await response.getBlob();
|
|
72
|
+
* const url = URL.createObjectURL(blob);
|
|
73
|
+
*/
|
|
19
74
|
getBlob(): Promise<Blob>;
|
|
20
|
-
|
|
75
|
+
/**
|
|
76
|
+
* Get the raw response body as a ReadableStream
|
|
77
|
+
* This method consumes the body and should only be called once.
|
|
78
|
+
*
|
|
79
|
+
* @returns The response body as a ReadableStream or null
|
|
80
|
+
* @throws {RequestError} When the response body has already been consumed
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* const stream = response.getBody();
|
|
84
|
+
* if (stream) {
|
|
85
|
+
* const reader = stream.getReader();
|
|
86
|
+
* // Process the stream
|
|
87
|
+
* }
|
|
88
|
+
*/
|
|
21
89
|
getBody(): ReadableStream<Uint8Array> | null;
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
90
|
+
/**
|
|
91
|
+
* Extract specific data using a selector function
|
|
92
|
+
* If no selector is provided, returns the full JSON response.
|
|
93
|
+
*
|
|
94
|
+
* @param selector - Optional function to extract and transform data
|
|
95
|
+
* @returns A promise that resolves to the selected data
|
|
96
|
+
* @throws {RequestError} When the request fails, JSON parsing fails, or the selector throws an error
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* // Get full response
|
|
100
|
+
* const data = await response.getData();
|
|
101
|
+
*
|
|
102
|
+
* // Extract specific data
|
|
103
|
+
* const users = await response.getData(data => data.results.users);
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* // Error handling - errors are always RequestError
|
|
107
|
+
* try {
|
|
108
|
+
* const data = await response.getData();
|
|
109
|
+
* } catch (error) {
|
|
110
|
+
* if (error instanceof RequestError) {
|
|
111
|
+
* console.log(error.status, error.url, error.method);
|
|
112
|
+
* }
|
|
113
|
+
* }
|
|
114
|
+
*/
|
|
32
115
|
getData<T = unknown, R = T>(selector?: (data: T) => R): Promise<T | R>;
|
|
33
116
|
}
|
package/dist/library/enums.d.ts
CHANGED
|
@@ -43,17 +43,6 @@ export declare enum RedirectMode {
|
|
|
43
43
|
FOLLOW = "follow",
|
|
44
44
|
MANUAL = "manual"
|
|
45
45
|
}
|
|
46
|
-
/**
|
|
47
|
-
* Enum for cache modes
|
|
48
|
-
*/
|
|
49
|
-
export declare enum CacheMode {
|
|
50
|
-
DEFAULT = "default",
|
|
51
|
-
RELOAD = "reload",
|
|
52
|
-
NO_STORE = "no-store",
|
|
53
|
-
NO_CACHE = "no-cache",
|
|
54
|
-
FORCE_CACHE = "force-cache",
|
|
55
|
-
ONLY_IF_CACHED = "only-if-cached"
|
|
56
|
-
}
|
|
57
46
|
/**
|
|
58
47
|
* Enum for cookie SameSite policies
|
|
59
48
|
*/
|