create-request 1.2.3 → 1.3.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 CHANGED
@@ -360,6 +360,7 @@ const jsonData = await create.get("https://api.example.com/endpoint").getJson();
360
360
  const textData = await create.get("https://api.example.com/endpoint").getText();
361
361
  const blobData = await create.get("https://api.example.com/endpoint").getBlob();
362
362
  const bodyStream = await create.get("https://api.example.com/endpoint").getBody();
363
+ const arrayBuffer = await create.get("https://api.example.com/endpoint").getArrayBuffer();
363
364
 
364
365
  // Using the data selector API to extract specific data
365
366
  const userData = await create
@@ -387,10 +388,11 @@ console.log(response.method); // HTTP method
387
388
  console.log(response.raw); // Raw Response object from fetch
388
389
 
389
390
  // Use wrapper methods for body parsing
391
+ const stream = response.getBody(); // ReadableStream or null
390
392
  const json = await response.getJson();
391
393
  const text = await response.getText();
392
394
  const blob = await response.getBlob();
393
- const stream = response.getBody(); // ReadableStream or null
395
+ const arrayBuffer = await response.getArrayBuffer();
394
396
  ```
395
397
 
396
398
  ### Error Handling
@@ -89,10 +89,26 @@ export declare abstract class BaseRequest {
89
89
  */
90
90
  onRetry(callback: RetryCallback): this;
91
91
  /**
92
- * Sets credentials policy - supports both direct call and fluent API
92
+ * Sets the credentials policy for the request, controlling whether cookies and authentication
93
+ * headers are sent with cross-origin requests.
94
+ *
95
+ * @param credentialsPolicy - The credentials policy to use:
96
+ * - `"include"` or `CredentialsPolicy.INCLUDE`: Always send credentials (cookies, authorization headers) with the request, even for cross-origin requests.
97
+ * - `"omit"` or `CredentialsPolicy.OMIT`: Never send credentials, even for same-origin requests.
98
+ * - `"same-origin"` or `CredentialsPolicy.SAME_ORIGIN`: Only send credentials for same-origin requests (default behavior in most browsers).
99
+ *
100
+ * @returns The request instance for chaining
101
+ *
93
102
  * @example
103
+ * // Using string values
94
104
  * request.withCredentials("include")
105
+ *
106
+ * @example
107
+ * // Using enum values
95
108
  * request.withCredentials(CredentialsPolicy.INCLUDE)
109
+ *
110
+ * @example
111
+ * // Using fluent API
96
112
  * request.withCredentials.INCLUDE()
97
113
  */
98
114
  get withCredentials(): ((credentialsPolicy: CredentialsPolicy | string) => BaseRequest) & {
@@ -101,22 +117,77 @@ export declare abstract class BaseRequest {
101
117
  SAME_ORIGIN: () => BaseRequest;
102
118
  };
103
119
  /**
104
- * Allows providing an external AbortController to cancel the request
105
- * @param controller - The AbortController to use for this request
106
- * @returns The instance for chaining
120
+ * Allows providing an external AbortController to cancel the request.
121
+ * This is useful when you need to cancel a request from outside the request chain,
122
+ *
123
+ * @param controller - The AbortController to use for this request. When `controller.abort()` is called,
124
+ * the request will be cancelled and throw an abort error.
125
+ *
126
+ * @returns The request instance for chaining
127
+ *
128
+ * @example
129
+ * const controller = new AbortController();
130
+ * const request = createRequest('/api/data')
131
+ * .withAbortController(controller)
132
+ * .getJson();
133
+ *
134
+ * // Later, cancel the request
135
+ * controller.abort();
136
+ *
137
+ * @example
138
+ * // Share abort controller across multiple requests
139
+ * const controller = new AbortController();
140
+ * request1.withAbortController(controller).getJson();
141
+ * request2.withAbortController(controller).getJson();
142
+ * // Aborting will cancel both requests
143
+ * controller.abort();
107
144
  */
108
145
  withAbortController(controller: AbortController): this;
109
146
  /**
110
- * Sets the referrer for the request
111
- * @param referrer - The referrer URL or policy
112
- * @returns The instance for chaining
147
+ * Sets the referrer URL for the request. The referrer is the URL of the page that initiated the request.
148
+ * This can be used to override the default referrer that the browser would normally send.
149
+ *
150
+ * @param referrer - The referrer URL to send with the request. Can be:
151
+ * - A full URL (e.g., "https://example.com/page")
152
+ * - An empty string to omit the referrer
153
+ * - A relative URL (will be resolved relative to the current page)
154
+ *
155
+ * @returns The request instance for chaining
156
+ *
157
+ * @example
158
+ * request.withReferrer("https://example.com/previous-page")
159
+ *
160
+ * @example
161
+ * // Omit referrer
162
+ * request.withReferrer("")
113
163
  */
114
164
  withReferrer(referrer: string): this;
115
165
  /**
116
- * Sets referrer policy - supports both direct call and fluent API
166
+ * Sets the referrer policy for the request, controlling how much referrer information
167
+ * is sent with the request. This helps balance privacy and functionality.
168
+ *
169
+ * @param policy - The referrer policy to use:
170
+ * - `"no-referrer"` or `ReferrerPolicy.NO_REFERRER`: Never send the referrer header.
171
+ * - `"no-referrer-when-downgrade"` or `ReferrerPolicy.NO_REFERRER_WHEN_DOWNGRADE`: Send full referrer for same-origin or HTTPS→HTTPS, omit for HTTPS→HTTP (default in most browsers).
172
+ * - `"origin"` or `ReferrerPolicy.ORIGIN`: Only send the origin (scheme, host, port), not the full URL.
173
+ * - `"origin-when-cross-origin"` or `ReferrerPolicy.ORIGIN_WHEN_CROSS_ORIGIN`: Send full referrer for same-origin, only origin for cross-origin.
174
+ * - `"same-origin"` or `ReferrerPolicy.SAME_ORIGIN`: Send full referrer for same-origin requests only, omit for cross-origin.
175
+ * - `"strict-origin"` or `ReferrerPolicy.STRICT_ORIGIN`: Send origin for HTTPS→HTTPS or HTTP→HTTP, omit for HTTPS→HTTP.
176
+ * - `"strict-origin-when-cross-origin"` or `ReferrerPolicy.STRICT_ORIGIN_WHEN_CROSS_ORIGIN`: Send full referrer for same-origin, origin for cross-origin HTTPS→HTTPS, omit for HTTPS→HTTP.
177
+ * - `"unsafe-url"` or `ReferrerPolicy.UNSAFE_URL`: Always send the full referrer URL (may leak sensitive information).
178
+ *
179
+ * @returns The request instance for chaining
180
+ *
117
181
  * @example
182
+ * // Using string values
118
183
  * request.withReferrerPolicy("no-referrer")
184
+ *
185
+ * @example
186
+ * // Using enum values
119
187
  * request.withReferrerPolicy(ReferrerPolicy.NO_REFERRER)
188
+ *
189
+ * @example
190
+ * // Using fluent API
120
191
  * request.withReferrerPolicy.NO_REFERRER()
121
192
  */
122
193
  get withReferrerPolicy(): ((policy: ReferrerPolicy | string) => BaseRequest) & {
@@ -130,11 +201,30 @@ export declare abstract class BaseRequest {
130
201
  STRICT_ORIGIN_WHEN_CROSS_ORIGIN: () => BaseRequest;
131
202
  };
132
203
  /**
133
- * Sets redirect mode - supports both direct call and fluent API
204
+ * Sets how the request handles HTTP redirects (3xx status codes).
205
+ *
206
+ * @param redirect - The redirect handling mode:
207
+ * - `"follow"` or `RedirectMode.FOLLOW`: Automatically follow redirects. The fetch will transparently follow redirects and return the final response (default behavior).
208
+ * - `"error"` or `RedirectMode.ERROR`: Treat redirects as errors. If a redirect occurs, the request will fail with an error.
209
+ * - `"manual"` or `RedirectMode.MANUAL`: Return the redirect response without following it. The response will have a `type` of "opaqueredirect" and you can manually handle the redirect.
210
+ *
211
+ * @returns The request instance for chaining
212
+ *
134
213
  * @example
214
+ * // Using string values
135
215
  * request.withRedirect("follow")
216
+ *
217
+ * @example
218
+ * // Using enum values
136
219
  * request.withRedirect(RedirectMode.FOLLOW)
220
+ *
221
+ * @example
222
+ * // Using fluent API
137
223
  * request.withRedirect.FOLLOW()
224
+ *
225
+ * @example
226
+ * // Fail on redirects
227
+ * request.withRedirect.ERROR()
138
228
  */
139
229
  get withRedirect(): ((redirect: RedirectMode | string) => BaseRequest) & {
140
230
  FOLLOW: () => BaseRequest;
@@ -142,17 +232,47 @@ export declare abstract class BaseRequest {
142
232
  MANUAL: () => BaseRequest;
143
233
  };
144
234
  /**
145
- * Sets the keepalive flag for the request
146
- * @param keepalive - Whether to allow the request to outlive the page
147
- * @returns The instance for chaining
235
+ * Sets the keepalive flag for the request. When enabled, the request can continue
236
+ * even after the page that initiated it is closed. This is useful for analytics,
237
+ * logging, or other background requests that should complete even if the user navigates away.
238
+ *
239
+ * @param keepalive - Whether to allow the request to outlive the page:
240
+ * - `true`: The request will continue even if the page is closed or navigated away.
241
+ * - `false`: The request will be cancelled if the page is closed (default).
242
+ *
243
+ * @returns The request instance for chaining
244
+ *
245
+ * @example
246
+ * // Send analytics event that should complete even if user navigates away
247
+ * request.withKeepAlive(true)
148
248
  */
149
249
  withKeepAlive(keepalive: boolean): this;
150
250
  /**
151
- * Sets request priority - supports both direct call and fluent API
251
+ * Sets the priority hint for the request, indicating to the browser how important
252
+ * this request is relative to other requests. This helps the browser optimize resource loading.
253
+ *
254
+ * @param priority - The request priority:
255
+ * - `"high"` or `RequestPriority.HIGH`: High priority - the browser should prioritize this request.
256
+ * - `"low"` or `RequestPriority.LOW`: Low priority - the browser can defer this request if needed.
257
+ * - `"auto"` or `RequestPriority.AUTO`: Automatic priority based on the request type (default).
258
+ *
259
+ * @returns The request instance for chaining
260
+ *
152
261
  * @example
262
+ * // Using string values
153
263
  * request.withPriority("high")
264
+ *
265
+ * @example
266
+ * // Using enum values
154
267
  * request.withPriority(RequestPriority.HIGH)
268
+ *
269
+ * @example
270
+ * // Using fluent API
155
271
  * request.withPriority.HIGH()
272
+ *
273
+ * @example
274
+ * // Low priority for non-critical requests
275
+ * request.withPriority.LOW()
156
276
  */
157
277
  get withPriority(): ((priority: RequestPriority | string) => BaseRequest) & {
158
278
  HIGH: () => BaseRequest;
@@ -160,19 +280,59 @@ export declare abstract class BaseRequest {
160
280
  AUTO: () => BaseRequest;
161
281
  };
162
282
  /**
163
- * Sets the integrity hash for subresource integrity verification
164
- * @param integrity - The integrity hash string (e.g., "sha256-...")
165
- * @returns The instance for chaining
283
+ * Sets the integrity hash for Subresource Integrity (SRI) verification.
284
+ * This allows the browser to verify that the fetched resource hasn't been tampered with
285
+ * by comparing its hash against the provided value. If the hashes don't match, the request fails.
286
+ *
287
+ * @param integrity - The integrity hash string in the format `"algorithm-hash"`:
288
+ * - Example: `"sha256-abcdef1234567890..."` (SHA-256 hash)
289
+ * - Example: `"sha384-abcdef1234567890..."` (SHA-384 hash)
290
+ * - Example: `"sha512-abcdef1234567890..."` (SHA-512 hash)
291
+ * - Multiple hashes can be separated by spaces: `"sha256-... sha384-..."`
292
+ *
293
+ * @returns The request instance for chaining
294
+ *
166
295
  * @example
167
296
  * request.withIntegrity("sha256-abcdef1234567890...")
297
+ *
298
+ * @example
299
+ * // Multiple algorithms for better compatibility
300
+ * request.withIntegrity("sha256-... sha384-...")
168
301
  */
169
302
  withIntegrity(integrity: string): this;
170
303
  /**
171
- * Sets cache mode - supports both direct call and fluent API
304
+ * Sets the cache mode for the request, controlling how the browser's HTTP cache
305
+ * is used for this request.
306
+ *
307
+ * @param cache - The cache mode:
308
+ * - `"default"` or `CacheMode.DEFAULT`: Use the browser's default cache behavior. The browser will check the cache and use it if valid, otherwise fetch from network.
309
+ * - `"no-store"` or `CacheMode.NO_STORE`: Never use the cache and don't store the response in cache. Always fetch from network.
310
+ * - `"reload"` or `CacheMode.RELOAD`: Bypass the cache but store the response. Always fetch from network, ignoring cached responses.
311
+ * - `"no-cache"` or `CacheMode.NO_CACHE`: Check the cache but revalidate with the server. Use cached response only if server confirms it's still valid.
312
+ * - `"force-cache"` or `CacheMode.FORCE_CACHE`: Use the cache if available, even if stale. Only fetch from network if not in cache.
313
+ * - `"only-if-cached"` or `CacheMode.ONLY_IF_CACHED`: Only use the cache. If not in cache, return an error. Never fetch from network.
314
+ *
315
+ * @returns The request instance for chaining
316
+ *
172
317
  * @example
318
+ * // Using string values
173
319
  * request.withCache("no-cache")
320
+ *
321
+ * @example
322
+ * // Using enum values
174
323
  * request.withCache(CacheMode.NO_CACHE)
324
+ *
325
+ * @example
326
+ * // Using fluent API
175
327
  * request.withCache.NO_CACHE()
328
+ *
329
+ * @example
330
+ * // Always fetch fresh data
331
+ * request.withCache.RELOAD()
332
+ *
333
+ * @example
334
+ * // Use cache only, fail if not cached
335
+ * request.withCache.ONLY_IF_CACHED()
176
336
  */
177
337
  get withCache(): ((cache: string) => BaseRequest) & {
178
338
  DEFAULT: () => BaseRequest;
@@ -196,11 +356,32 @@ export declare abstract class BaseRequest {
196
356
  */
197
357
  withQueryParam(key: string, value: string | string[] | number | boolean | null | undefined): this;
198
358
  /**
199
- * Sets request mode - supports both direct call and fluent API
359
+ * Sets the request mode, which determines the CORS (Cross-Origin Resource Sharing) behavior
360
+ * for the request. This controls how the browser handles cross-origin requests.
361
+ *
362
+ * @param mode - The request mode:
363
+ * - `"cors"` or `RequestMode.CORS`: Enable CORS. The browser will send CORS headers and enforce CORS rules. This is the default for most cross-origin requests.
364
+ * - `"no-cors"` or `RequestMode.NO_CORS`: Disable CORS. The request is sent as a "simple" request without CORS headers. The response will be opaque (you can't read it).
365
+ * - `"same-origin"` or `RequestMode.SAME_ORIGIN`: Only allow same-origin requests. Cross-origin requests will fail.
366
+ * - `"navigate"` or `RequestMode.NAVIGATE`: Used for navigation requests (typically only used by the browser itself).
367
+ *
368
+ * @returns The request instance for chaining
369
+ *
200
370
  * @example
371
+ * // Using string values
201
372
  * request.withMode("cors")
373
+ *
374
+ * @example
375
+ * // Using enum values
202
376
  * request.withMode(RequestMode.CORS)
377
+ *
378
+ * @example
379
+ * // Using fluent API
203
380
  * request.withMode.CORS()
381
+ *
382
+ * @example
383
+ * // Restrict to same-origin only
384
+ * request.withMode.SAME_ORIGIN()
204
385
  */
205
386
  get withMode(): ((mode: RequestMode | string) => BaseRequest) & {
206
387
  CORS: () => BaseRequest;
@@ -1,16 +1,11 @@
1
1
  import type { GraphQLOptions } from "./types.js";
2
2
  /**
3
3
  * Wrapper for HTTP responses with methods to transform the response data
4
- * Provides caching and conversion between different response formats.
5
4
  */
6
5
  export declare class ResponseWrapper {
7
6
  private readonly response;
8
7
  readonly url?: string;
9
8
  readonly method?: string;
10
- private textCache;
11
- private jsonCache;
12
- private blobCache;
13
- private bodyUsed;
14
9
  private graphQLOptions?;
15
10
  constructor(response: Response, url?: string, method?: string, graphQLOptions?: GraphQLOptions);
16
11
  get status(): number;
@@ -26,7 +21,7 @@ export declare class ResponseWrapper {
26
21
  private checkGraphQLErrors;
27
22
  /**
28
23
  * Parse the response body as JSON
29
- * Caches the result for subsequent calls.
24
+ * Note: This consumes the response body and can only be called once.
30
25
  * If GraphQL options are set with throwOnError=true, will check for GraphQL errors and throw.
31
26
  *
32
27
  * @returns The parsed JSON data
@@ -49,11 +44,10 @@ export declare class ResponseWrapper {
49
44
  getJson<T = unknown>(): Promise<T>;
50
45
  /**
51
46
  * 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.
47
+ * Note: This consumes the response body and can only be called once.
54
48
  *
55
49
  * @returns The response text
56
- * @throws {RequestError} When reading fails or the response has already been consumed in an incompatible format
50
+ * @throws {RequestError} When reading fails or the response has already been consumed
57
51
  *
58
52
  * @example
59
53
  * const text = await response.getText();
@@ -61,20 +55,31 @@ export declare class ResponseWrapper {
61
55
  getText(): Promise<string>;
62
56
  /**
63
57
  * 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.
58
+ * Note: This consumes the response body and can only be called once.
66
59
  *
67
60
  * @returns The response as a Blob
68
- * @throws {RequestError} When reading fails or the response has already been consumed in an incompatible format
61
+ * @throws {RequestError} When reading fails or the response has already been consumed
69
62
  *
70
63
  * @example
71
64
  * const blob = await response.getBlob();
72
65
  * const url = URL.createObjectURL(blob);
73
66
  */
74
67
  getBlob(): Promise<Blob>;
68
+ /**
69
+ * Get the response body as an ArrayBuffer
70
+ * Note: This consumes the response body and can only be called once.
71
+ *
72
+ * @returns The response as an ArrayBuffer
73
+ * @throws {RequestError} When reading fails or the response has already been consumed
74
+ *
75
+ * @example
76
+ * const buffer = await response.getArrayBuffer();
77
+ * const uint8Array = new Uint8Array(buffer);
78
+ */
79
+ getArrayBuffer(): Promise<ArrayBuffer>;
75
80
  /**
76
81
  * Get the raw response body as a ReadableStream
77
- * This method consumes the body and should only be called once.
82
+ * Note: This consumes the response body and should only be called once.
78
83
  *
79
84
  * @returns The response body as a ReadableStream or null
80
85
  * @throws {RequestError} When the response body has already been consumed