create-request 1.4.3-rc.2 → 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 +150 -20
- 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/index.cjs +435 -62
- package/dist/library/index.cjs.map +1 -1
- package/dist/library/index.d.ts +21 -1
- package/dist/library/index.esm.js +435 -62
- 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
|
@@ -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
|
/**
|