create-request 1.4.3-rc.2 → 1.4.3-rc.4

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.
@@ -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
- * Static methods for creating specific types of RequestError
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 The response text
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 The response as a Blob
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 The response as an ArrayBuffer
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
  /**
@@ -1,25 +1,25 @@
1
1
  import type { CookiesRecord, CookieOptions, RetryConfig, RequestInterceptor, ResponseInterceptor, ErrorInterceptor } from "./types.js";
2
2
  import type { GetRequest, PostRequest, PutRequest, DeleteRequest, PatchRequest, HeadRequest, OptionsRequest } from "./requestMethods.js";
3
3
  interface ApiBuilderRequestMethods {
4
- withoutCsrfProtection(): ApiBuilder;
5
- withAntiCsrfHeaders(): ApiBuilder;
6
- withTimeout(timeout: number): ApiBuilder;
7
- withReferrer(referrer: string): ApiBuilder;
8
- withKeepAlive(keepalive: boolean): ApiBuilder;
9
- withIntegrity(integrity: string): ApiBuilder;
10
- withHeader(key: string, value: string): ApiBuilder;
11
- withHeaders(headers: Record<string, string>): ApiBuilder;
12
- withRetries(retries: number | RetryConfig): ApiBuilder;
13
- withBearerToken(token: string): ApiBuilder;
14
- withCookies(cookies: CookiesRecord): ApiBuilder;
15
- withContentType(contentType: string): ApiBuilder;
16
- withAuthorization(authValue: string): ApiBuilder;
17
- withBasicAuth(username: string, password: string): ApiBuilder;
18
- withCsrfToken(token: string, headerName?: string): ApiBuilder;
19
- withErrorInterceptor(interceptor: ErrorInterceptor): ApiBuilder;
20
- withCookie(name: string, value: string | CookieOptions): ApiBuilder;
21
- withRequestInterceptor(interceptor: RequestInterceptor): ApiBuilder;
22
- withResponseInterceptor(interceptor: ResponseInterceptor): ApiBuilder;
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
23
  }
24
24
  /**
25
25
  * API builder for creating configured API instances with default settings.
@@ -53,7 +53,7 @@ export declare class ApiBuilder {
53
53
  * await api.get("/users").getJson(); // Requests https://api.example.com/users
54
54
  * ```
55
55
  */
56
- withBaseURL(baseURL: string): this;
56
+ withBaseURL(baseURL: string): ApiBuilder & ApiBuilderRequestMethods;
57
57
  /**
58
58
  * Adds a modifier function that will be applied to all requests created through this builder.
59
59
  *