create-request 1.4.3-rc.1 → 1.4.3-rc.3
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 +231 -120
- package/dist/library/BaseRequest.d.ts +182 -37
- package/dist/library/BodyRequest.d.ts +56 -10
- package/dist/library/RequestError.d.ts +99 -1
- package/dist/library/ResponseWrapper.d.ts +53 -10
- package/dist/library/apiBuilder.d.ts +0 -2
- package/dist/library/index.cjs +446 -69
- package/dist/library/index.cjs.map +1 -1
- package/dist/library/index.d.ts +23 -1
- package/dist/library/index.esm.js +439 -70
- 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/types.d.ts +233 -19
- package/package.json +7 -7
|
@@ -365,16 +365,56 @@ export declare abstract class BaseRequest {
|
|
|
365
365
|
ONLY_IF_CACHED: () => BaseRequest;
|
|
366
366
|
};
|
|
367
367
|
/**
|
|
368
|
-
* Adds query parameters to the request URL
|
|
369
|
-
*
|
|
370
|
-
*
|
|
368
|
+
* Adds query parameters to the request URL.
|
|
369
|
+
* Multiple calls will append parameters. Array values will create multiple query parameters with the same key.
|
|
370
|
+
* Null and undefined values are ignored.
|
|
371
|
+
*
|
|
372
|
+
* @param params - An object containing query parameter key-value pairs.
|
|
373
|
+
* Values can be strings, numbers, booleans, arrays (for multiple values), or null/undefined (ignored).
|
|
374
|
+
* @returns The request instance for chaining
|
|
375
|
+
*
|
|
376
|
+
* @example
|
|
377
|
+
* ```typescript
|
|
378
|
+
* // Simple parameters
|
|
379
|
+
* request.withQueryParams({ page: 1, limit: 10, active: true });
|
|
380
|
+
* // Results in: ?page=1&limit=10&active=true
|
|
381
|
+
* ```
|
|
382
|
+
*
|
|
383
|
+
* @example
|
|
384
|
+
* ```typescript
|
|
385
|
+
* // Array values create multiple parameters
|
|
386
|
+
* request.withQueryParams({ tags: ['js', 'ts', 'node'] });
|
|
387
|
+
* // Results in: ?tags=js&tags=ts&tags=node
|
|
388
|
+
* ```
|
|
389
|
+
*
|
|
390
|
+
* @example
|
|
391
|
+
* ```typescript
|
|
392
|
+
* // Null/undefined values are ignored
|
|
393
|
+
* request.withQueryParams({ page: 1, filter: null, sort: undefined });
|
|
394
|
+
* // Results in: ?page=1
|
|
395
|
+
* ```
|
|
371
396
|
*/
|
|
372
397
|
withQueryParams(params: Record<string, string | string[] | number | boolean | null | undefined>): this;
|
|
373
398
|
/**
|
|
374
|
-
* Adds a single query parameter
|
|
375
|
-
*
|
|
376
|
-
*
|
|
377
|
-
* @
|
|
399
|
+
* Adds a single query parameter to the request URL.
|
|
400
|
+
* Convenience method for adding one parameter at a time.
|
|
401
|
+
*
|
|
402
|
+
* @param key - The query parameter name
|
|
403
|
+
* @param value - The query parameter value. Can be a string, number, boolean, array (for multiple values), or null/undefined (ignored).
|
|
404
|
+
* @returns The request instance for chaining
|
|
405
|
+
*
|
|
406
|
+
* @example
|
|
407
|
+
* ```typescript
|
|
408
|
+
* request.withQueryParam('page', 1).withQueryParam('limit', 10);
|
|
409
|
+
* // Results in: ?page=1&limit=10
|
|
410
|
+
* ```
|
|
411
|
+
*
|
|
412
|
+
* @example
|
|
413
|
+
* ```typescript
|
|
414
|
+
* // Array values create multiple parameters
|
|
415
|
+
* request.withQueryParam('tags', ['js', 'ts']);
|
|
416
|
+
* // Results in: ?tags=js&tags=ts
|
|
417
|
+
* ```
|
|
378
418
|
*/
|
|
379
419
|
withQueryParam(key: string, value: string | string[] | number | boolean | null | undefined): this;
|
|
380
420
|
/**
|
|
@@ -412,22 +452,55 @@ export declare abstract class BaseRequest {
|
|
|
412
452
|
NAVIGATE: () => BaseRequest;
|
|
413
453
|
};
|
|
414
454
|
/**
|
|
415
|
-
*
|
|
416
|
-
*
|
|
417
|
-
*
|
|
455
|
+
* Sets the Content-Type header for the request.
|
|
456
|
+
* Shorthand for `withHeader('Content-Type', contentType)`.
|
|
457
|
+
*
|
|
458
|
+
* @param contentType - The MIME type (e.g., `'application/json'`, `'text/plain'`, `'multipart/form-data'`)
|
|
459
|
+
* @returns The request instance for chaining
|
|
460
|
+
*
|
|
461
|
+
* @example
|
|
462
|
+
* ```typescript
|
|
463
|
+
* request.withContentType('application/json');
|
|
464
|
+
* ```
|
|
465
|
+
*
|
|
466
|
+
* @example
|
|
467
|
+
* ```typescript
|
|
468
|
+
* request.withContentType('application/xml');
|
|
469
|
+
* ```
|
|
418
470
|
*/
|
|
419
471
|
withContentType(contentType: string): this;
|
|
420
472
|
/**
|
|
421
|
-
*
|
|
422
|
-
*
|
|
423
|
-
*
|
|
473
|
+
* Sets the Authorization header for the request.
|
|
474
|
+
* Shorthand for `withHeader('Authorization', authValue)`.
|
|
475
|
+
* For Bearer tokens, use `withBearerToken()` instead. For Basic auth, use `withBasicAuth()`.
|
|
476
|
+
*
|
|
477
|
+
* @param authValue - The full authorization header value (e.g., `'Bearer token123'`, `'Basic base64string'`)
|
|
478
|
+
* @returns The request instance for chaining
|
|
479
|
+
*
|
|
480
|
+
* @example
|
|
481
|
+
* ```typescript
|
|
482
|
+
* request.withAuthorization('Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...');
|
|
483
|
+
* ```
|
|
484
|
+
*
|
|
485
|
+
* @example
|
|
486
|
+
* ```typescript
|
|
487
|
+
* request.withAuthorization('CustomScheme customToken');
|
|
488
|
+
* ```
|
|
424
489
|
*/
|
|
425
490
|
withAuthorization(authValue: string): this;
|
|
426
491
|
/**
|
|
427
|
-
*
|
|
428
|
-
*
|
|
429
|
-
*
|
|
430
|
-
* @
|
|
492
|
+
* Sets up HTTP Basic Authentication.
|
|
493
|
+
* Encodes the username and password in base64 and sets the Authorization header.
|
|
494
|
+
*
|
|
495
|
+
* @param username - The username for Basic authentication
|
|
496
|
+
* @param password - The password for Basic authentication
|
|
497
|
+
* @returns The request instance for chaining
|
|
498
|
+
*
|
|
499
|
+
* @example
|
|
500
|
+
* ```typescript
|
|
501
|
+
* request.withBasicAuth('myuser', 'mypassword');
|
|
502
|
+
* // Sets: Authorization: Basic bXl1c2VyOm15cGFzc3dvcmQ=
|
|
503
|
+
* ```
|
|
431
504
|
*/
|
|
432
505
|
withBasicAuth(username: string, password: string): this;
|
|
433
506
|
/**
|
|
@@ -436,9 +509,17 @@ export declare abstract class BaseRequest {
|
|
|
436
509
|
*/
|
|
437
510
|
private encodeBase64;
|
|
438
511
|
/**
|
|
439
|
-
* Sets
|
|
440
|
-
*
|
|
441
|
-
*
|
|
512
|
+
* Sets a Bearer token for authentication.
|
|
513
|
+
* Shorthand for `withAuthorization('Bearer ' + token)`.
|
|
514
|
+
*
|
|
515
|
+
* @param token - The Bearer token (JWT, OAuth token, etc.)
|
|
516
|
+
* @returns The request instance for chaining
|
|
517
|
+
*
|
|
518
|
+
* @example
|
|
519
|
+
* ```typescript
|
|
520
|
+
* request.withBearerToken('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...');
|
|
521
|
+
* // Sets: Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
|
|
522
|
+
* ```
|
|
442
523
|
*/
|
|
443
524
|
withBearerToken(token: string): this;
|
|
444
525
|
/**
|
|
@@ -453,23 +534,69 @@ export declare abstract class BaseRequest {
|
|
|
453
534
|
*/
|
|
454
535
|
private hasHeader;
|
|
455
536
|
/**
|
|
456
|
-
* Sets cookies for the request
|
|
457
|
-
*
|
|
458
|
-
*
|
|
537
|
+
* Sets cookies for the request.
|
|
538
|
+
* Cookies are sent in the Cookie header. Multiple calls will merge cookies.
|
|
539
|
+
* Cookie values can be simple strings or objects with additional cookie options.
|
|
540
|
+
*
|
|
541
|
+
* @param cookies - An object where keys are cookie names and values are either:
|
|
542
|
+
* - A string (the cookie value)
|
|
543
|
+
* - A CookieOptions object with `value` and optional properties (secure, httpOnly, sameSite, expires, path, domain, maxAge)
|
|
544
|
+
* @returns The request instance for chaining
|
|
545
|
+
*
|
|
546
|
+
* @example
|
|
547
|
+
* ```typescript
|
|
548
|
+
* // Simple string cookies
|
|
549
|
+
* request.withCookies({ sessionId: 'abc123', userId: '456' });
|
|
550
|
+
* ```
|
|
551
|
+
*
|
|
552
|
+
* @example
|
|
553
|
+
* ```typescript
|
|
554
|
+
* // Cookies with options (note: options are for documentation only in request cookies)
|
|
555
|
+
* request.withCookies({
|
|
556
|
+
* sessionId: 'abc123',
|
|
557
|
+
* token: { value: 'xyz789', secure: true }
|
|
558
|
+
* });
|
|
559
|
+
* ```
|
|
459
560
|
*/
|
|
460
561
|
withCookies(cookies: CookiesRecord): this;
|
|
461
562
|
/**
|
|
462
|
-
*
|
|
463
|
-
*
|
|
464
|
-
*
|
|
465
|
-
* @
|
|
563
|
+
* Sets a single cookie for the request.
|
|
564
|
+
* Convenience method for adding one cookie at a time.
|
|
565
|
+
*
|
|
566
|
+
* @param name - The cookie name
|
|
567
|
+
* @param value - The cookie value as a string, or a CookieOptions object with `value` and optional properties
|
|
568
|
+
* @returns The request instance for chaining
|
|
569
|
+
*
|
|
570
|
+
* @example
|
|
571
|
+
* ```typescript
|
|
572
|
+
* request.withCookie('sessionId', 'abc123');
|
|
573
|
+
* ```
|
|
574
|
+
*
|
|
575
|
+
* @example
|
|
576
|
+
* ```typescript
|
|
577
|
+
* request.withCookie('token', { value: 'xyz789', secure: true });
|
|
578
|
+
* ```
|
|
466
579
|
*/
|
|
467
580
|
withCookie(name: string, value: string | CookieOptions): this;
|
|
468
581
|
/**
|
|
469
|
-
* Sets a CSRF token in the request headers
|
|
470
|
-
*
|
|
471
|
-
*
|
|
472
|
-
* @
|
|
582
|
+
* Sets a CSRF (Cross-Site Request Forgery) token in the request headers.
|
|
583
|
+
* This is commonly used to protect against CSRF attacks in web applications.
|
|
584
|
+
*
|
|
585
|
+
* @param token - The CSRF token value
|
|
586
|
+
* @param headerName - The name of the header to use. Defaults to `'X-CSRF-Token'`.
|
|
587
|
+
* @returns The request instance for chaining
|
|
588
|
+
*
|
|
589
|
+
* @example
|
|
590
|
+
* ```typescript
|
|
591
|
+
* request.withCsrfToken('csrf-token-123');
|
|
592
|
+
* // Sets: X-CSRF-Token: csrf-token-123
|
|
593
|
+
* ```
|
|
594
|
+
*
|
|
595
|
+
* @example
|
|
596
|
+
* ```typescript
|
|
597
|
+
* request.withCsrfToken('token', 'X-Custom-CSRF-Header');
|
|
598
|
+
* // Sets: X-Custom-CSRF-Header: token
|
|
599
|
+
* ```
|
|
473
600
|
*/
|
|
474
601
|
withCsrfToken(token: string, headerName?: string): this;
|
|
475
602
|
/**
|
|
@@ -557,30 +684,48 @@ export declare abstract class BaseRequest {
|
|
|
557
684
|
*/
|
|
558
685
|
getJson<T = unknown>(): Promise<T>;
|
|
559
686
|
/**
|
|
560
|
-
* Execute the request and get the response as text
|
|
687
|
+
* Execute the request and get the response body as text.
|
|
561
688
|
*
|
|
562
|
-
* @returns A promise that resolves to the response
|
|
689
|
+
* @returns A promise that resolves to the response body as a string
|
|
690
|
+
* @throws {RequestError} When the request fails or reading the response fails
|
|
563
691
|
*
|
|
564
692
|
* @example
|
|
693
|
+
* ```typescript
|
|
565
694
|
* const text = await request.getText();
|
|
695
|
+
* console.log(text); // "Hello, world!"
|
|
696
|
+
* ```
|
|
566
697
|
*/
|
|
567
698
|
getText(): Promise<string>;
|
|
568
699
|
/**
|
|
569
|
-
* Execute the request and get the response as a Blob
|
|
700
|
+
* Execute the request and get the response body as a Blob.
|
|
701
|
+
* Useful for downloading files or handling binary data.
|
|
570
702
|
*
|
|
571
|
-
* @returns A promise that resolves to the response Blob
|
|
703
|
+
* @returns A promise that resolves to the response body as a Blob
|
|
704
|
+
* @throws {RequestError} When the request fails or reading the response fails
|
|
572
705
|
*
|
|
573
706
|
* @example
|
|
707
|
+
* ```typescript
|
|
574
708
|
* const blob = await request.getBlob();
|
|
709
|
+
* const url = URL.createObjectURL(blob);
|
|
710
|
+
* // Use the blob URL (e.g., for downloading or displaying)
|
|
711
|
+
* ```
|
|
575
712
|
*/
|
|
576
713
|
getBlob(): Promise<Blob>;
|
|
577
714
|
/**
|
|
578
|
-
* Execute the request and get the response body as a ReadableStream
|
|
715
|
+
* Execute the request and get the response body as a ReadableStream.
|
|
716
|
+
* Note: Unlike other methods, streams cannot be cached. The body can only be consumed once.
|
|
579
717
|
*
|
|
580
|
-
* @returns A promise that resolves to the response body
|
|
718
|
+
* @returns A promise that resolves to the response body as a ReadableStream, or `null` if the body is not available
|
|
719
|
+
* @throws {RequestError} When the request fails or the body has already been consumed
|
|
581
720
|
*
|
|
582
721
|
* @example
|
|
722
|
+
* ```typescript
|
|
583
723
|
* const stream = await request.getBody();
|
|
724
|
+
* if (stream) {
|
|
725
|
+
* const reader = stream.getReader();
|
|
726
|
+
* // Process the stream chunk by chunk
|
|
727
|
+
* }
|
|
728
|
+
* ```
|
|
584
729
|
*/
|
|
585
730
|
getBody(): Promise<ReadableStream<Uint8Array> | null>;
|
|
586
731
|
/**
|
|
@@ -10,31 +10,77 @@ export declare abstract class BodyRequest extends BaseRequest {
|
|
|
10
10
|
private graphQLOptions;
|
|
11
11
|
constructor(url: string);
|
|
12
12
|
/**
|
|
13
|
-
* Sets the body
|
|
14
|
-
*
|
|
13
|
+
* Sets the request body. Automatically detects the body type and sets appropriate Content-Type header.
|
|
14
|
+
* Supports JSON objects/arrays, strings, FormData, Blob, ArrayBuffer, URLSearchParams, and ReadableStream.
|
|
15
|
+
*
|
|
16
|
+
* @param body - The request body. Can be:
|
|
17
|
+
* - A JSON-serializable object or array (automatically stringified)
|
|
18
|
+
* - A string (sets Content-Type to `text/plain` if not already set)
|
|
19
|
+
* - FormData, Blob, File, ArrayBuffer, TypedArray, URLSearchParams, or ReadableStream
|
|
20
|
+
* @returns The request instance for chaining
|
|
21
|
+
* @throws {RequestError} If the body is a JSON object that cannot be stringified
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* // JSON object (automatically stringified)
|
|
26
|
+
* request.withBody({ name: 'John', age: 30 });
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* // JSON array
|
|
30
|
+
* request.withBody([1, 2, 3]);
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* // String
|
|
34
|
+
* request.withBody('plain text');
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* // FormData
|
|
38
|
+
* const formData = new FormData();
|
|
39
|
+
* formData.append('file', fileBlob);
|
|
40
|
+
* request.withBody(formData);
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* // Blob
|
|
44
|
+
* request.withBody(new Blob(['content'], { type: 'text/plain' }));
|
|
45
|
+
* ```
|
|
15
46
|
*/
|
|
16
47
|
withBody(body: Body): this;
|
|
17
48
|
/**
|
|
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
|
|
49
|
+
* Sets a GraphQL query or mutation as the request body.
|
|
50
|
+
* Automatically formats the body as JSON and sets Content-Type to `application/json`.
|
|
51
|
+
* If `throwOnError` is enabled in options, the response will be checked for GraphQL errors
|
|
52
|
+
* and a RequestError will be thrown if any are found.
|
|
20
53
|
*
|
|
21
|
-
* @param query - The GraphQL query or mutation string
|
|
22
|
-
* @param variables - Optional variables object to pass with the query
|
|
54
|
+
* @param query - The GraphQL query or mutation string (e.g., `'query { user { id } }'`)
|
|
55
|
+
* @param variables - Optional variables object to pass with the query. Must be a plain object.
|
|
23
56
|
* @param options - Optional GraphQL-specific options
|
|
57
|
+
* @param options.throwOnError - If `true`, throws a RequestError when the GraphQL response contains errors
|
|
24
58
|
* @returns The request instance for chaining
|
|
59
|
+
* @throws {RequestError} If the query is empty, variables is invalid, or JSON stringification fails
|
|
25
60
|
*
|
|
26
61
|
* @example
|
|
27
|
-
*
|
|
62
|
+
* ```typescript
|
|
63
|
+
* // Simple query with variables
|
|
64
|
+
* const request = create.post('/graphql')
|
|
28
65
|
* .withGraphQL('query { user(id: $id) { name email } }', { id: '123' });
|
|
66
|
+
* const data = await request.getJson();
|
|
67
|
+
* ```
|
|
29
68
|
*
|
|
30
69
|
* @example
|
|
31
|
-
*
|
|
70
|
+
* ```typescript
|
|
71
|
+
* // Mutation with variables
|
|
72
|
+
* const request = create.post('/graphql')
|
|
32
73
|
* .withGraphQL('mutation { createUser(name: $name) { id } }', { name: 'John' });
|
|
74
|
+
* ```
|
|
33
75
|
*
|
|
34
76
|
* @example
|
|
35
|
-
*
|
|
36
|
-
*
|
|
77
|
+
* ```typescript
|
|
78
|
+
* // Throw error if GraphQL response contains errors
|
|
79
|
+
* const request = create.post('/graphql')
|
|
37
80
|
* .withGraphQL('query { user { id } }', undefined, { throwOnError: true });
|
|
81
|
+
* // If the response has errors, this will throw a RequestError
|
|
82
|
+
* const data = await request.getJson();
|
|
83
|
+
* ```
|
|
38
84
|
*/
|
|
39
85
|
withGraphQL(query: string, variables?: Record<string, unknown>, options?: GraphQLOptions): this;
|
|
40
86
|
/**
|
|
@@ -1,10 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error class for HTTP request failures.
|
|
3
|
+
* Extends the standard Error class with additional context about the failed request.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```typescript
|
|
7
|
+
* try {
|
|
8
|
+
* await create.get('/api/users').getJson();
|
|
9
|
+
* } catch (error) {
|
|
10
|
+
* console.log(`Request failed: ${error.message}`);
|
|
11
|
+
* console.log(`URL: ${error.url}`);
|
|
12
|
+
* console.log(`Method: ${error.method}`);
|
|
13
|
+
* console.log(`Status: ${error.status}`);
|
|
14
|
+
* console.log(`Is timeout: ${error.isTimeout}`);
|
|
15
|
+
* console.log(`Is aborted: ${error.isAborted}`);
|
|
16
|
+
* }
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
1
19
|
export declare class RequestError extends Error {
|
|
20
|
+
/** HTTP status code if the request received a response (e.g., 404, 500) */
|
|
2
21
|
readonly status?: number;
|
|
22
|
+
/** The Response object if the request received a response before failing */
|
|
3
23
|
readonly response?: Response;
|
|
24
|
+
/** The URL that was requested */
|
|
4
25
|
readonly url: string;
|
|
26
|
+
/** The HTTP method that was used (e.g., 'GET', 'POST') */
|
|
5
27
|
readonly method: string;
|
|
28
|
+
/** Whether the request failed due to a timeout */
|
|
6
29
|
readonly isTimeout?: boolean;
|
|
30
|
+
/** Whether the request was aborted (cancelled) */
|
|
7
31
|
readonly isAborted?: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Creates a new RequestError instance.
|
|
34
|
+
*
|
|
35
|
+
* @param message - Error message describing what went wrong
|
|
36
|
+
* @param url - The URL that was requested
|
|
37
|
+
* @param method - The HTTP method that was used
|
|
38
|
+
* @param options - Additional error context
|
|
39
|
+
* @param options.status - HTTP status code if available
|
|
40
|
+
* @param options.response - The Response object if available
|
|
41
|
+
* @param options.isTimeout - Whether this was a timeout error
|
|
42
|
+
* @param options.isAborted - Whether the request was aborted
|
|
43
|
+
* @param options.cause - The underlying error that caused this error
|
|
44
|
+
*/
|
|
8
45
|
constructor(message: string, url: string, method: string, options?: {
|
|
9
46
|
status?: number;
|
|
10
47
|
response?: Response;
|
|
@@ -13,10 +50,71 @@ export declare class RequestError extends Error {
|
|
|
13
50
|
cause?: Error;
|
|
14
51
|
});
|
|
15
52
|
/**
|
|
16
|
-
*
|
|
53
|
+
* Creates a RequestError for a timeout failure.
|
|
54
|
+
*
|
|
55
|
+
* @param url - The URL that timed out
|
|
56
|
+
* @param method - The HTTP method that was used
|
|
57
|
+
* @param timeoutMs - The timeout duration in milliseconds
|
|
58
|
+
* @returns A RequestError with `isTimeout` set to `true`
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```typescript
|
|
62
|
+
* throw RequestError.timeout('/api/data', 'GET', 5000);
|
|
63
|
+
* ```
|
|
17
64
|
*/
|
|
18
65
|
static timeout(url: string, method: string, timeoutMs: number): RequestError;
|
|
66
|
+
/**
|
|
67
|
+
* Creates a RequestError from an HTTP error response.
|
|
68
|
+
* Used when the server returns a non-2xx status code.
|
|
69
|
+
*
|
|
70
|
+
* @param response - The Response object from the failed request
|
|
71
|
+
* @param url - The URL that was requested
|
|
72
|
+
* @param method - The HTTP method that was used
|
|
73
|
+
* @returns A RequestError with the status code and response object
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```typescript
|
|
77
|
+
* const response = await fetch('/api/users');
|
|
78
|
+
* if (!response.ok) {
|
|
79
|
+
* throw RequestError.fromResponse(response, '/api/users', 'GET');
|
|
80
|
+
* }
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
19
83
|
static fromResponse(response: Response, url: string, method: string): RequestError;
|
|
84
|
+
/**
|
|
85
|
+
* Creates a RequestError from a network-level error.
|
|
86
|
+
* Automatically detects and categorizes common network errors (timeouts, DNS errors, connection errors).
|
|
87
|
+
*
|
|
88
|
+
* @param url - The URL that failed
|
|
89
|
+
* @param method - The HTTP method that was used
|
|
90
|
+
* @param originalError - The original error that occurred (e.g., from fetch)
|
|
91
|
+
* @returns A RequestError with enhanced error message and context
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```typescript
|
|
95
|
+
* try {
|
|
96
|
+
* await fetch('/api/data');
|
|
97
|
+
* } catch (error) {
|
|
98
|
+
* if (error instanceof Error) {
|
|
99
|
+
* throw RequestError.networkError('/api/data', 'GET', error);
|
|
100
|
+
* }
|
|
101
|
+
* }
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
20
104
|
static networkError(url: string, method: string, originalError: Error): RequestError;
|
|
105
|
+
/**
|
|
106
|
+
* Creates a RequestError for an aborted (cancelled) request.
|
|
107
|
+
*
|
|
108
|
+
* @param url - The URL that was aborted
|
|
109
|
+
* @param method - The HTTP method that was used
|
|
110
|
+
* @returns A RequestError with `isAborted` set to `true`
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```typescript
|
|
114
|
+
* const controller = new AbortController();
|
|
115
|
+
* controller.abort();
|
|
116
|
+
* throw RequestError.abortError('/api/data', 'GET');
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
21
119
|
static abortError(url: string, method: string): RequestError;
|
|
22
120
|
}
|
|
@@ -1,9 +1,22 @@
|
|
|
1
1
|
import type { GraphQLOptions } from "./types.js";
|
|
2
2
|
/**
|
|
3
|
-
* Wrapper for HTTP responses with methods to transform the response data
|
|
3
|
+
* Wrapper for HTTP responses with methods to transform the response data.
|
|
4
|
+
* Provides convenient methods to parse the response body in different formats.
|
|
5
|
+
* Response bodies are cached after the first read, so you can call multiple methods
|
|
6
|
+
* (e.g., `getJson()` and `getText()`) on the same response.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* const response = await create.get('/api/users').getResponse();
|
|
11
|
+
* console.log(response.status); // 200
|
|
12
|
+
* console.log(response.ok); // true
|
|
13
|
+
* const data = await response.getJson();
|
|
14
|
+
* ```
|
|
4
15
|
*/
|
|
5
16
|
export declare class ResponseWrapper {
|
|
17
|
+
/** The URL that was requested (if available) */
|
|
6
18
|
readonly url?: string;
|
|
19
|
+
/** The HTTP method that was used (if available) */
|
|
7
20
|
readonly method?: string;
|
|
8
21
|
private readonly response;
|
|
9
22
|
private graphQLOptions?;
|
|
@@ -12,10 +25,26 @@ export declare class ResponseWrapper {
|
|
|
12
25
|
private cachedJson?;
|
|
13
26
|
private cachedArrayBuffer?;
|
|
14
27
|
constructor(response: Response, url?: string, method?: string, graphQLOptions?: GraphQLOptions);
|
|
28
|
+
/**
|
|
29
|
+
* HTTP status code (e.g., 200, 404, 500)
|
|
30
|
+
*/
|
|
15
31
|
get status(): number;
|
|
32
|
+
/**
|
|
33
|
+
* HTTP status text (e.g., "OK", "Not Found", "Internal Server Error")
|
|
34
|
+
*/
|
|
16
35
|
get statusText(): string;
|
|
36
|
+
/**
|
|
37
|
+
* Response headers as a Headers object
|
|
38
|
+
*/
|
|
17
39
|
get headers(): Headers;
|
|
40
|
+
/**
|
|
41
|
+
* Whether the response status is in the 200-299 range (successful)
|
|
42
|
+
*/
|
|
18
43
|
get ok(): boolean;
|
|
44
|
+
/**
|
|
45
|
+
* The raw Response object from the fetch API.
|
|
46
|
+
* Use this if you need direct access to the underlying Response.
|
|
47
|
+
*/
|
|
19
48
|
get raw(): Response;
|
|
20
49
|
/**
|
|
21
50
|
* Check if the response body has already been consumed and throw an error if so
|
|
@@ -51,35 +80,49 @@ export declare class ResponseWrapper {
|
|
|
51
80
|
*/
|
|
52
81
|
getJson<T = unknown>(): Promise<T>;
|
|
53
82
|
/**
|
|
54
|
-
* Get the response body as text
|
|
83
|
+
* Get the response body as text.
|
|
84
|
+
* The result is cached, so subsequent calls return the same value without re-reading the body.
|
|
55
85
|
*
|
|
56
|
-
* @returns
|
|
57
|
-
* @throws {RequestError} When reading fails
|
|
86
|
+
* @returns A promise that resolves to the response body as a string
|
|
87
|
+
* @throws {RequestError} When the body has already been consumed or reading fails
|
|
58
88
|
*
|
|
59
89
|
* @example
|
|
90
|
+
* ```typescript
|
|
60
91
|
* const text = await response.getText();
|
|
92
|
+
* console.log(text); // "Hello, world!"
|
|
93
|
+
* ```
|
|
61
94
|
*/
|
|
62
95
|
getText(): Promise<string>;
|
|
63
96
|
/**
|
|
64
|
-
* Get the response body as a Blob
|
|
97
|
+
* Get the response body as a Blob.
|
|
98
|
+
* Useful for downloading files or handling binary data.
|
|
99
|
+
* The result is cached, so subsequent calls return the same value without re-reading the body.
|
|
65
100
|
*
|
|
66
|
-
* @returns
|
|
67
|
-
* @throws {RequestError} When reading fails
|
|
101
|
+
* @returns A promise that resolves to the response body as a Blob
|
|
102
|
+
* @throws {RequestError} When the body has already been consumed or reading fails
|
|
68
103
|
*
|
|
69
104
|
* @example
|
|
105
|
+
* ```typescript
|
|
70
106
|
* const blob = await response.getBlob();
|
|
71
107
|
* const url = URL.createObjectURL(blob);
|
|
108
|
+
* // Use the blob URL for downloading or displaying
|
|
109
|
+
* ```
|
|
72
110
|
*/
|
|
73
111
|
getBlob(): Promise<Blob>;
|
|
74
112
|
/**
|
|
75
|
-
* Get the response body as an ArrayBuffer
|
|
113
|
+
* Get the response body as an ArrayBuffer.
|
|
114
|
+
* Useful for processing binary data at a low level.
|
|
115
|
+
* The result is cached, so subsequent calls return the same value without re-reading the body.
|
|
76
116
|
*
|
|
77
|
-
* @returns
|
|
78
|
-
* @throws {RequestError} When reading fails
|
|
117
|
+
* @returns A promise that resolves to the response body as an ArrayBuffer
|
|
118
|
+
* @throws {RequestError} When the body has already been consumed or reading fails
|
|
79
119
|
*
|
|
80
120
|
* @example
|
|
121
|
+
* ```typescript
|
|
81
122
|
* const buffer = await response.getArrayBuffer();
|
|
82
123
|
* const uint8Array = new Uint8Array(buffer);
|
|
124
|
+
* // Process the binary data
|
|
125
|
+
* ```
|
|
83
126
|
*/
|
|
84
127
|
getArrayBuffer(): Promise<ArrayBuffer>;
|
|
85
128
|
/**
|
|
@@ -20,8 +20,6 @@ interface ApiBuilderRequestMethods {
|
|
|
20
20
|
withCookie(name: string, value: string | CookieOptions): ApiBuilder;
|
|
21
21
|
withRequestInterceptor(interceptor: RequestInterceptor): ApiBuilder;
|
|
22
22
|
withResponseInterceptor(interceptor: ResponseInterceptor): ApiBuilder;
|
|
23
|
-
withQueryParam(key: string, value: string | string[] | number | boolean | null | undefined): ApiBuilder;
|
|
24
|
-
withQueryParams(params: Record<string, string | string[] | number | boolean | null | undefined>): ApiBuilder;
|
|
25
23
|
}
|
|
26
24
|
/**
|
|
27
25
|
* API builder for creating configured API instances with default settings.
|