@superutils/fetch 1.2.2 → 1.3.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.
Files changed (4) hide show
  1. package/README.md +130 -61
  2. package/dist/index.d.ts +462 -245
  3. package/dist/index.js +256 -170
  4. package/package.json +5 -5
package/dist/index.d.ts CHANGED
@@ -1,18 +1,24 @@
1
1
  import * as _superutils_promise from '@superutils/promise';
2
- import { RetryOptions, DeferredAsyncOptions, IPromisE } from '@superutils/promise';
2
+ import { RetryOptions, RetryIfFunc, DeferredAsyncOptions, IPromisE_Timeout } from '@superutils/promise';
3
3
  export { DeferredAsyncOptions, ResolveError, ResolveIgnored } from '@superutils/promise';
4
4
  import { ValueOrPromise } from '@superutils/core';
5
5
 
6
- type ExcludeOptions<Target, // options to exclude
7
- Options extends FetchOptions = FetchOptions> = Target extends FetchOptions ? {
8
- headers?: Options['headers'];
9
- } & Omit<Options, 'headers' | keyof Target> & Partial<Record<Exclude<keyof Target, 'headers'>, never>> : Options;
10
- type ExcludePostOptions<Target> = ExcludeOptions<Target, PostOptions>;
11
- type FetchArgs = [url: string | URL, options?: FetchOptions];
12
- type FetchArgsInterceptor = [
13
- url: string | URL,
14
- options: FetchOptionsInterceptor
15
- ];
6
+ /** Commonly used content types for easier access */
7
+ declare const ContentType: {
8
+ readonly APPLICATION_JAVASCRIPT: "application/javascript";
9
+ readonly APPLICATION_JSON: "application/json";
10
+ readonly APPLICATION_OCTET_STREAM: "application/octet-stream";
11
+ readonly APPLICATION_PDF: "application/pdf";
12
+ readonly APPLICATION_X_WWW_FORM_URLENCODED: "application/x-www-form-urlencoded";
13
+ readonly APPLICATION_XML: "application/xml";
14
+ readonly APPLICATION_ZIP: "application/zip";
15
+ readonly AUDIO_MPEG: "audio/mpeg";
16
+ readonly MULTIPART_FORM_DATA: "multipart/form-data";
17
+ readonly TEXT_CSS: "text/css";
18
+ readonly TEXT_HTML: "text/html";
19
+ readonly TEXT_PLAIN: "text/plain";
20
+ readonly VIDEO_MP4: "video/mp4";
21
+ };
16
22
  declare enum FetchAs {
17
23
  arrayBuffer = "arrayBuffer",
18
24
  blob = "blob",
@@ -22,53 +28,18 @@ declare enum FetchAs {
22
28
  response = "response",
23
29
  text = "text"
24
30
  }
25
- /** Extract `FetchAs` from `FetchOptions` */
26
- type FetchAsFromOptions<TOptions, TFallback = FetchAs.json> = TOptions extends {
27
- as: infer As;
28
- } ? As extends FetchAs ? As : TFallback : TFallback;
29
- /** Custom fetch options (not used by built-in `fetch()`*/
30
- type FetchCustomOptions = {
31
- /**
32
- * Specify how the parse the result. To get raw response use {@link FetchAs.response}.
33
- * Default: 'json'
34
- */
35
- as?: FetchAs;
36
- abortCtrl?: AbortController;
37
- body?: PostBody | (() => PostBody);
38
- errMsgs?: FetchErrMsgs;
39
- interceptors?: FetchInterceptors;
40
- /** Request timeout in milliseconds. */
41
- timeout?: number;
42
- validateUrl?: boolean;
43
- } & FetchRetryOptions;
44
- /** Default args */
45
- type FetchDeferredArgs<OmitMethod extends boolean = false> = [
46
- url?: string | URL,
47
- options?: Omit<FetchOptions, 'abortCtrl' | (OmitMethod extends true ? 'method' : never)>
48
- ];
49
- type FetchErrMsgs = {
50
- invalidUrl?: string;
51
- parseFailed?: string;
52
- reqTimedout?: string;
53
- requestFailed?: string;
54
- };
55
- /** Custom error message for fetch requests with more detailed info about the request URL, fetch options and response */
56
- declare class FetchError extends Error {
57
- options: FetchOptions;
58
- response?: Response;
59
- url: string | URL;
60
- constructor(message: string, options: {
61
- cause?: unknown;
62
- options: FetchOptions;
63
- response?: Response;
64
- url: string | URL;
65
- });
66
- clone: (newMessage: string) => FetchError;
67
- }
31
+
32
+ /**
33
+ * Generic definition for interceptor and transformer callbacks used throughout the fetch lifecycle.
34
+ */
35
+ type Interceptor<T, TArgs extends unknown[]> = (...args: [value: T, ...TArgs]) => ValueOrPromise<void> | ValueOrPromise<T>;
68
36
  /**
69
37
  * Fetch error interceptor to be invoked whenever an exception occurs.
70
38
  * This interceptor can also be used as the error transformer by returning {@link FetchError}.
71
39
  *
40
+ * Note: The error interceptor is only triggered if the request ultimately fails. If retries are enabled (`retry > 0`)
41
+ * and a subsequent attempt succeeds, this interceptor will not be invoked for the intermediate failures.
42
+ *
72
43
  * @param {FetchError} fetchError custom error that also contain URL, options & response
73
44
  *
74
45
  * @returns returning undefined or not returning anything will not override the error
@@ -201,10 +172,10 @@ type FetchInterceptorResult<Args extends unknown[] = FetchArgsInterceptor> = Int
201
172
  /**
202
173
  * All valid interceptors for fetch requests are:
203
174
  * ---
204
- * 1. error,
175
+ * 1. error
205
176
  * 2. request
206
177
  * 3. response
207
- * 4. result.
178
+ * 4. result
208
179
  *
209
180
  * An interceptor can be any of the following:
210
181
  * ---
@@ -223,10 +194,9 @@ type FetchInterceptorResult<Args extends unknown[] = FetchArgsInterceptor> = Int
223
194
  * 1. Any exception thrown by interceptors will gracefully ignored.
224
195
  * 2. Interceptors will be executed in the sequence they're given.
225
196
  * 3. Execution priority: global interceptors will always be executed before local interceptors.
197
+ * 4. The following options cannot be modified using interceptors: abortCtrl, as, signal, timeout
226
198
  *
227
- *
228
- *
229
- * More info & examples:
199
+ * More info about specific interceptor types & examples:
230
200
  * ---
231
201
  * See the following for more details and examples:
232
202
  *
@@ -236,24 +206,108 @@ type FetchInterceptorResult<Args extends unknown[] = FetchArgsInterceptor> = Int
236
206
  * - `result`: {@link FetchInterceptorResult}
237
207
  */
238
208
  type FetchInterceptors = {
209
+ /** Request error interceptors/transformers. See {@link FetchInterceptorError} for more details. */
239
210
  error?: FetchInterceptorError[];
211
+ /** Request request interceptors/transformers. See {@link FetchInterceptorRequest} for more details. */
240
212
  request?: FetchInterceptorRequest[];
213
+ /** Request response interceptors/transformers. See {@link FetchInterceptorResponse} for more details. */
241
214
  response?: FetchInterceptorResponse[];
215
+ /** Request result interceptors/transformers. See {@link FetchInterceptorResult} for more details. */
242
216
  result?: FetchInterceptorResult[];
243
217
  };
218
+
219
+ type ExcludeOptions<Target, // options to exclude
220
+ Options extends FetchOptions = FetchOptions> = Target extends FetchOptions ? {
221
+ headers?: Options['headers'];
222
+ } & Omit<Options, 'headers' | keyof Target> & Partial<Record<Exclude<keyof Target, 'headers'>, never>> : Options;
223
+ type ExcludePostOptions<Target> = ExcludeOptions<Target, PostOptions>;
224
+ type FetchArgs = [url: string | URL, options?: FetchOptions];
225
+ type FetchArgsInterceptor = [
226
+ url: string | URL,
227
+ options: FetchOptionsInterceptor
228
+ ];
229
+ /** Extract `FetchAs` from `FetchOptions` */
230
+ type FetchAsFromOptions<TOptions, TFallback = FetchAs.json> = TOptions extends {
231
+ as: infer As;
232
+ } ? As extends FetchAs ? As : TFallback : TFallback;
233
+ /** Custom fetch options (not used by built-in `fetch()`*/
234
+ type FetchCustomOptions = {
235
+ /**
236
+ * Specify how the parse the result. To get raw response use {@link FetchAs.response}.
237
+ * Default: 'json'
238
+ */
239
+ as?: FetchAs;
240
+ /**
241
+ * An `AbortController` instance to control the request.
242
+ *
243
+ * If not provided, a new instance is automatically created internally.
244
+ *
245
+ * It is supported in addition to the standard `signal`. If both are provided, `abortCtrl` will be aborted
246
+ * when the `signal` aborts or when the request times out.
247
+ *
248
+ * Recommendation:
249
+ * - For request timeout purposes, setting a timeout duration will be sufficient.
250
+ * Abort controller/signal is not needed.
251
+ * - Use `abortCtrl` instead of `signal` to prevent creating internal `AbortController` instance.
252
+ */
253
+ abortCtrl?: AbortController;
254
+ body?: PostBody | (() => PostBody);
255
+ /**
256
+ * Custom fetch function to use instead of the global `fetch`.
257
+ * Useful for testing or using a different fetch implementation (e.g. `node-fetch` in older Node versions).
258
+ *
259
+ * Default: `globalThis.fetch`
260
+ */
261
+ fetchFunc?: (...args: FetchArgs) => Promise<Response>;
262
+ errMsgs?: FetchErrMsgs;
263
+ /**
264
+ * Interceptor/transformer callback executed at different stages of the request.
265
+ * See {@link FetchInterceptors} for more details.
266
+ */
267
+ interceptors?: FetchInterceptors;
268
+ /** Request timeout in milliseconds */
269
+ timeout?: number;
270
+ /** Whether to validate URL before making the request. Default: `true` */
271
+ validateUrl?: boolean;
272
+ } & FetchRetryOptions;
273
+ /** Default args */
274
+ type FetchDeferredArgs<OmitMethod extends boolean = false> = [
275
+ url?: string | URL,
276
+ options?: Omit<FetchOptions, 'abortCtrl' | (OmitMethod extends true ? 'method' : never)>
277
+ ];
278
+ type FetchErrMsgs = {
279
+ /** Error message to be used when request is aborted without specifying a message */
280
+ aborted?: string;
281
+ /** Error message to be use when URL validation fails */
282
+ invalidUrl?: string;
283
+ /** Error message to be used when request parse failes */
284
+ parseFailed?: string;
285
+ /** Error message to be used when request times out */
286
+ timedout?: string;
287
+ /** Error message to be used when request fails */
288
+ requestFailed?: string;
289
+ };
244
290
  /**
245
291
  * Fetch request options
246
292
  */
247
293
  type FetchOptions = Omit<RequestInit, 'body'> & FetchCustomOptions;
248
- type FetchOptionsDefaults = Omit<FetchOptionsInterceptor, 'as' | 'method' | 'retryIf'>;
294
+ /** Default fetch options */
295
+ type FetchOptionsDefault = Omit<FetchOptionsInterceptor, 'abortCtrl' | 'as' | 'method' | 'signal' | 'timeout'> & {
296
+ /** Request timeout duration in milliseconds. Default: `30_000` (30 seconds) */
297
+ timeout: number;
298
+ };
249
299
  /**
250
- * Fetch options available to interceptors
300
+ * Fetch options available to interceptors.
301
+ *
251
302
  */
252
- type FetchOptionsInterceptor = Omit<FetchOptions, 'as' | 'errMsgs' | 'interceptors' | 'headers' | keyof FetchRetryOptions> & {
303
+ type FetchOptionsInterceptor = Omit<FetchOptions, 'as' | 'errMsgs' | 'interceptors' | 'headers' | 'timeout' | keyof FetchRetryOptions> & {
253
304
  as: FetchAs;
305
+ /** Error messages */
254
306
  errMsgs: Required<FetchErrMsgs>;
255
307
  headers: Headers;
308
+ /** Interceptors/transformers for fetch requests. See {@link FetchInterceptors} for more details. */
256
309
  interceptors: Required<FetchInterceptors>;
310
+ timeout: number;
257
311
  } & FetchRetryOptions;
258
312
  /**
259
313
  * Result types for specific parsers ("as": FetchAs)
@@ -279,12 +333,8 @@ type FetchRetryOptions = Omit<Partial<RetryOptions>, 'retry' | 'retryIf'> & {
279
333
  * Default: `0`
280
334
  */
281
335
  retry?: number;
282
- retryIf?: null | ((response: Response | undefined, retryCount: number, error?: unknown) => boolean | Promise<boolean>);
336
+ retryIf?: RetryIfFunc<Response>;
283
337
  };
284
- /**
285
- * Generic fetch interceptor type
286
- */
287
- type Interceptor<T, TArgs extends unknown[]> = (...args: [value: T, ...TArgs]) => ValueOrPromise<void> | ValueOrPromise<T>;
288
338
  type PostBody = Record<string, unknown> | BodyInit | null;
289
339
  type PostArgs<OmitMethod = false> = [
290
340
  url: string | URL,
@@ -324,45 +374,166 @@ type PostArgs<OmitMethod = false> = [
324
374
  * f4().then(console.log, console.warn)
325
375
  * ```
326
376
  */
327
- type PostDeferredCbArgs<TUrl = undefined, TData = undefined, Options = PostOptions, CbArgsReq extends unknown[] = Required<PostArgs>> = [TUrl, TData] extends [CbArgsReq[0], undefined] ? [data?: PostArgs[1], options?: Options] : [TUrl, TData] extends [undefined, CbArgsReq[1]] ? [url: PostArgs[0], options?: Options] : [TUrl, TData] extends [CbArgsReq[0], CbArgsReq[1]] ? [options?: Options] : PostArgs;
377
+ type PostDeferredCbArgs<TUrl = undefined, TData = undefined, Options = undefined, CbArgsReq extends unknown[] = Required<PostArgs>> = [TUrl, TData] extends [CbArgsReq[0], undefined] ? [
378
+ data?: PostArgs[1],
379
+ options?: PostArgs[2]
380
+ ] : [
381
+ TUrl,
382
+ TData
383
+ ] extends [undefined, CbArgsReq[1]] ? [url: PostArgs[0], options?: PostArgs[2]] : [
384
+ TUrl,
385
+ TData
386
+ ] extends [CbArgsReq[0], CbArgsReq[1]] ? [options?: PostArgs[2]] : [TUrl, TData, Options];
387
+ /** Request options for POST-like methods that allow "options.body" */
328
388
  type PostOptions = {
329
389
  /** Default: `'post'` */
330
390
  method?: 'post' | 'put' | 'patch' | 'delete' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
331
391
  } & Omit<FetchOptions, 'method'>;
332
392
 
393
+ /** Custom error message for fetch requests with more detailed info about the request URL, fetch options and response */
394
+ declare class FetchError extends Error {
395
+ /** Create a new `FetchError` with a new `message` while preserving all metadata */
396
+ clone: (newMessage: string) => FetchError;
397
+ options: FetchOptions;
398
+ response: Response | undefined;
399
+ url: string | URL;
400
+ constructor(message: string, options: {
401
+ cause?: unknown;
402
+ options: FetchOptions;
403
+ response?: Response;
404
+ url: string | URL;
405
+ });
406
+ }
407
+
333
408
  /**
334
409
  * Create a reusable fetch client with shared options. The returned function comes attached with a
335
- * `.deferred()` function for debounced and throttled request.
410
+ * `.deferred()` function for debounce and throttle behavior.
411
+ *
412
+ * The `createClient` utility streamlines the creation of dedicated API clients by generating pre-configured fetch
413
+ * functions. These functions can be equipped with default options like headers, timeouts, or a specific HTTP method,
414
+ * which minimizes code repetition across your application. If a method is not specified during creation, the client
415
+ * will default to `GET`.
416
+ *
417
+ * The returned client also includes a `.deferred()` method, providing the same debounce, throttle, and sequential
418
+ * execution capabilities found in functions like `fetch.get.deferred()`.
419
+ *
420
+ * @example create reusable clients
421
+ * ```javascript
422
+ * import { createClient } from '@superutils/fetch'
423
+ *
424
+ * // Create a "GET" client with default headers and a 5-second timeout
425
+ * const apiClient = createClient(
426
+ * {
427
+ * // fixed options cannot be overridden
428
+ * method: 'get',
429
+ * },
430
+ * {
431
+ * // default options can be overridden
432
+ * headers: {
433
+ * Authorization: 'Bearer my-secret-token',
434
+ * 'Content-Type': 'application/json',
435
+ * },
436
+ * timeout: 5000,
437
+ * },
438
+ * {
439
+ * // default defer options (can be overridden)
440
+ * delayMs: 300,
441
+ * retry: 2, // If request fails, retry up to two more times
442
+ * },
443
+ * )
444
+ *
445
+ * // Use it just like the standard fetch
446
+ * apiClient('https://dummyjson.com/products/1', {
447
+ * // The 'method' property cannot be overridden as it is used in the fixed options when creating the client.
448
+ * // In TypeScript, the compiler will not allow this property.
449
+ * // In Javascript, it will simply be ignored.
450
+ * // method: 'post',
451
+ * timeout: 3000, // The 'timeout' property can be overridden
452
+ * }).then(console.log, console.warn)
453
+ *
454
+ * // create a deferred client using "apiClient"
455
+ * const deferredClient = apiClient.deferred(
456
+ * { retry: 0 }, // disable retrying by overriding the `retry` defer option
457
+ * 'https://dummyjson.com/products/1',
458
+ * { timeout: 3000 },
459
+ * )
460
+ * deferredClient({ timeout: 10000 }) // timeout is overridden by individual request
461
+ * .then(console.log, console.warn)
462
+ * ```
336
463
  */
337
- declare const createClient: <MandatoryOpts extends FetchOptions | undefined, CommonOpts extends ExcludeOptions<MandatoryOpts> | undefined, MandatoryAs extends FetchAs | undefined = FetchAsFromOptions<MandatoryOpts, undefined>>(
464
+ declare const createClient: <FixedOpts extends FetchOptions | undefined, CommonOpts extends ExcludeOptions<FixedOpts> | undefined, FixedAs extends FetchAs | undefined = FetchAsFromOptions<FixedOpts, undefined>>(
338
465
  /** Mandatory fetch options that cannot be overriden by individual request */
339
- mandatoryOptions?: MandatoryOpts,
466
+ fixedOptions?: FixedOpts,
340
467
  /** Common fetch options that can be overriden by individual request */
341
468
  commonOptions?: FetchOptions & CommonOpts, commonDeferOptions?: DeferredAsyncOptions<unknown, unknown>) => {
342
- <T, TOptions extends ExcludeOptions<MandatoryOpts> | undefined = ExcludeOptions<MandatoryOpts> | undefined, TAs extends FetchAs = MandatoryAs extends FetchAs ? MandatoryAs : FetchAsFromOptions<TOptions, FetchAsFromOptions<CommonOpts>>, TReturn = FetchResult<T>[TAs]>(url: FetchArgs[0], options?: TOptions): IPromisE<TReturn>;
343
- deferred<ThisArg = unknown, TDelay extends number = number, DefaultUrl extends FetchArgs[0] | undefined = string | URL | undefined, DefaultOptions extends ExcludeOptions<MandatoryOpts> | undefined = ExcludeOptions<MandatoryOpts> | undefined>(deferOptions?: DeferredAsyncOptions<ThisArg, TDelay>, defaultUrl?: DefaultUrl, defaultOptions?: DefaultOptions): <TResult = unknown, TOptions_1 extends ExcludeOptions<MandatoryOpts> | undefined = ExcludeOptions<MandatoryOpts> | undefined, TAs_1 extends FetchAs = MandatoryAs extends FetchAs ? MandatoryAs : FetchAsFromOptions<TOptions_1, FetchAsFromOptions<CommonOpts>>, TReturn_1 = FetchResult<TResult>[TAs_1]>(...args: DefaultUrl extends undefined ? [url: FetchArgs[0], options?: TOptions_1] : [options?: TOptions_1]) => IPromisE<TReturn_1>;
469
+ <T, TOptions extends ExcludeOptions<FixedOpts> | undefined = ExcludeOptions<FixedOpts> | undefined, TAs extends FetchAs = FixedAs extends FetchAs ? FixedAs : FetchAsFromOptions<TOptions, FetchAsFromOptions<CommonOpts>>, TReturn = FetchResult<T>[TAs]>(url: FetchArgs[0], options?: TOptions): _superutils_promise.IPromisE_Timeout<TReturn>;
470
+ deferred<ThisArg = unknown, Delay extends number = number, DefaultUrl extends FetchArgs[0] | undefined = string | URL | undefined, DefaultOptions extends ExcludeOptions<FixedOpts> | undefined = ExcludeOptions<FixedOpts> | undefined>(deferOptions?: DeferredAsyncOptions<ThisArg, Delay>, defaultUrl?: DefaultUrl, defaultOptions?: DefaultOptions): <TResult = unknown, TOptions_1 extends ExcludeOptions<FixedOpts> | undefined = ExcludeOptions<FixedOpts> | undefined, TAs_1 extends FetchAs = FixedAs extends FetchAs ? FixedAs : FetchAsFromOptions<TOptions_1, FetchAsFromOptions<CommonOpts>>, TReturn_1 = FetchResult<TResult>[TAs_1]>(...args: DefaultUrl extends undefined ? [url: FetchArgs[0], options?: TOptions_1] : [options?: TOptions_1]) => _superutils_promise.IPromisE_Timeout<TReturn_1>;
344
471
  };
345
472
 
346
473
  /**
347
474
  * Create a reusable fetch client with shared options. The returned function comes attached with a
348
475
  * `.deferred()` function for debounced and throttled request.
476
+ * While `createClient()` is versatile enough for any HTTP method, `createPostClient()` is specifically designed for
477
+ * methods that require a request body, such as `DELETE`, `PATCH`, `POST`, and `PUT`. If a method is not provided, it
478
+ * defaults to `POST`. The generated client accepts an additional second parameter (`data`) for the request payload.
479
+ *
480
+ * Similar to `createClient`, the returned function comes equipped with a `.deferred()` method, enabling debounced,
481
+ * throttled, or sequential execution.
482
+ *
483
+ * @example create reusable clients
484
+ * ```javascript
485
+ * import { createPostClient, FetchAs } from '@superutils/fetch'
486
+ *
487
+ * // Create a POST client with 10-second as the default timeout
488
+ * const postClient = createPostClient(
489
+ * {
490
+ * method: 'post',
491
+ * headers: { 'content-type': 'application/json' },
492
+ * },
493
+ * { timeout: 10000 },
494
+ * )
495
+ *
496
+ * // Invoking `postClient()` automatically applies the pre-configured options
497
+ * postClient(
498
+ * 'https://dummyjson.com/products/add',
499
+ * { title: 'New Product' }, // data/body
500
+ * {}, // other options
501
+ * ).then(console.log)
502
+ *
503
+ * // create a deferred client using "postClient"
504
+ * const updateProduct = postClient.deferred(
505
+ * {
506
+ * delayMs: 300, // debounce duration
507
+ * onResult: console.log, // prints only successful results
508
+ * },
509
+ * 'https://dummyjson.com/products/add',
510
+ * {
511
+ * method: 'patch',
512
+ * timeout: 3000,
513
+ * },
514
+ * )
515
+ * updateProduct({ title: 'New title 1' }) // ignored by debounce
516
+ * updateProduct({ title: 'New title 2' }) // executed
517
+ * ```
349
518
  */
350
- declare const createPostClient: <MandatoryOpts extends PostOptions | undefined, CommonOpts extends ExcludePostOptions<MandatoryOpts> | undefined, MandatoryAs extends FetchAs | undefined = FetchAsFromOptions<MandatoryOpts, undefined>>(
519
+ declare const createPostClient: <FixedOpts extends PostOptions | undefined, CommonOpts extends ExcludePostOptions<FixedOpts> | undefined, FixedAs extends FetchAs | undefined = FetchAsFromOptions<FixedOpts, undefined>>(
351
520
  /** Mandatory fetch options that cannot be overriden by individual request */
352
- mandatoryOptions?: MandatoryOpts,
521
+ fixedOptions?: FixedOpts,
353
522
  /** Common fetch options that can be overriden by individual request */
354
523
  commonOptions?: PostOptions & CommonOpts, commonDeferOptions?: DeferredAsyncOptions<unknown, unknown>) => {
355
- <T, TOptions extends ExcludePostOptions<MandatoryOpts> | undefined, TAs extends FetchAs = MandatoryAs extends FetchAs ? MandatoryAs : FetchAsFromOptions<TOptions, FetchAsFromOptions<CommonOpts>>, TReturn = FetchResult<T>[TAs]>(url: PostArgs[0], data?: PostArgs[1], options?: TOptions): IPromisE<TReturn>;
356
- deferred<ThisArg = unknown, TDelay extends number = number, DefaultUrl extends PostArgs[0] | undefined = string | URL | undefined, DefaultData extends PostArgs[1] = PostBody | (() => PostBody) | undefined, DefaultOptions extends ExcludePostOptions<MandatoryOpts> | undefined = ExcludePostOptions<MandatoryOpts> | undefined>(deferOptions?: DeferredAsyncOptions<ThisArg, TDelay>, defaultUrl?: DefaultUrl, defaultData?: DefaultData, defaultOptions?: DefaultOptions): <TResult = unknown, TOptions_1 extends ExcludePostOptions<MandatoryOpts> | undefined = ExcludePostOptions<MandatoryOpts> | undefined, TAs_1 extends FetchAs = MandatoryAs extends FetchAs ? MandatoryAs : FetchAsFromOptions<TOptions_1, FetchAsFromOptions<CommonOpts>>, TReturn_1 = FetchResult<TResult>[TAs_1]>(...args: PostDeferredCbArgs<DefaultUrl, DefaultData, TOptions_1>) => IPromisE<TReturn_1>;
524
+ <T = unknown, TOptions extends ExcludePostOptions<FixedOpts> | undefined = ExcludePostOptions<FixedOpts> | undefined, TAs extends FetchAs = FixedAs extends FetchAs ? FixedAs : FetchAsFromOptions<TOptions, FetchAsFromOptions<CommonOpts>>, TReturn = FetchResult<T>[TAs]>(url: PostArgs[0], data?: PostArgs[1], options?: TOptions): IPromisE_Timeout<TReturn>;
525
+ deferred<ThisArg, Delay extends number, DefaultUrl extends PostArgs[0] | undefined, DefaultData extends PostArgs[1] = undefined, DefaultOptions extends ExcludePostOptions<FixedOpts> | undefined = undefined>(deferOptions?: DeferredAsyncOptions<ThisArg, Delay>, defaultUrl?: DefaultUrl, defaultData?: DefaultData, defaultOptions?: DefaultOptions): <TResult = unknown, TOptions_1 extends ExcludePostOptions<FixedOpts> | undefined = ExcludePostOptions<FixedOpts>, TAs_1 extends FetchAs = FixedAs extends FetchAs ? FixedAs : FetchAsFromOptions<TOptions_1, FetchAsFromOptions<CommonOpts>>, TReturn_1 = FetchResult<TResult>[TAs_1]>(...args: PostDeferredCbArgs<DefaultUrl, DefaultData, TOptions_1>) => IPromisE_Timeout<TReturn_1>;
357
526
  };
358
527
 
528
+ /** Node.js setTimeout limit is 2147483647 (2^31-1). Larger values fire immediately. */
529
+ declare const MAX_TIMEOUT = 2147483647;
359
530
  /**
360
531
  * Extended `fetch` with timeout, retry, and other options. Automatically parses as JSON by default on success.
361
532
  *
362
533
  * @param url request URL
363
534
  * @param options (optional) Standard `fetch` options extended with {@link FetchCustomOptions}.
364
- * Default content type is 'application/json'
365
- * @param options.as (optional) determines who to parse the result. Default: {@link FetchAs.json}
535
+ * Default "content-type" header is 'application/json'.
536
+ * @param options.as (optional) determines how to parse the result. Default: {@link FetchAs.json}
366
537
  * @param options.method (optional) fetch method. Default: `'get'`
367
538
  *
368
539
  * @example Make a simple HTTP requests
@@ -375,21 +546,19 @@ commonOptions?: PostOptions & CommonOpts, commonDeferOptions?: DeferredAsyncOpti
375
546
  * ```
376
547
  */
377
548
  declare const fetch: {
378
- <T, TOptions extends FetchOptions = FetchOptions, TAs extends FetchAs = TOptions["as"] extends FetchAs ? TOptions["as"] : FetchAs.json, TReturn = FetchResult<T>[TAs]>(url: string | URL, options?: TOptions): IPromisE<TReturn>;
549
+ <T, TOptions extends FetchOptions = FetchOptions, TAs extends FetchAs = TOptions["as"] extends FetchAs ? TOptions["as"] : FetchAs.json, TReturn = FetchResult<T>[TAs]>(url: string | URL, options?: FetchOptions & TOptions): IPromisE_Timeout<TReturn>;
379
550
  /** Default fetch options */
380
- defaults: FetchOptionsDefaults;
551
+ defaults: FetchOptionsDefault;
381
552
  };
382
553
 
383
554
  /**
384
555
  * Drop-in replacement for built-in `fetch()` with with timeout, retry etc options and Response as default return type.
385
556
  *
386
557
  * @param url request URL
387
- * @param options (optional) Standard `fetch` options extended with {@link FetchCustomOptions}
388
- * @param options.as (optional) determines who to parse the result. Default: {@link FetchAs.response}
389
- * @param options.method (optional) fetch method. Default: `'get'`
558
+ * @param options (optional) Standard `fetch` options extended with "FetchCustomOptions"
390
559
  *
391
560
  * @example Make a simple HTTP requests
392
- * ```typescript
561
+ * ```javascript
393
562
  * import { fetchResponse } from '@superutils/fetch'
394
563
  *
395
564
  * fetchResponse('https://dummyjson.com/products/1')
@@ -397,129 +566,123 @@ declare const fetch: {
397
566
  * .then(console.log, console.error)
398
567
  * ```
399
568
  */
400
- declare const fetchResponse: <T = Response, TOptions extends FetchOptions = FetchOptions, TAs extends FetchAs = TOptions["as"] extends FetchAs ? TOptions["as"] : FetchAs.response, TReturn = FetchResult<T>[TAs]>(...args: Parameters<typeof fetch<T, TOptions, TAs, TReturn>>) => IPromisE<TReturn>;
401
-
402
- declare const _get: {
403
- <T, TOptions extends ({
404
- headers?: HeadersInit | undefined;
405
- } & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
406
- headers?: HeadersInit | undefined;
407
- } & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined, TAs extends FetchAs = FetchAsFromOptions<TOptions, FetchAs.json>, TReturn = FetchResult<T>[TAs]>(url: FetchArgs[0], options?: TOptions | undefined): _superutils_promise.IPromisE<TReturn>;
408
- deferred<ThisArg = unknown, TDelay extends number = number, DefaultUrl extends FetchArgs[0] | undefined = string | URL | undefined, DefaultOptions extends ({
409
- headers?: HeadersInit | undefined;
410
- } & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
411
- headers?: HeadersInit | undefined;
412
- } & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined>(deferOptions?: _superutils_promise.DeferredAsyncOptions<ThisArg, TDelay>, defaultUrl?: DefaultUrl | undefined, defaultOptions?: DefaultOptions | undefined): <TResult = unknown, TOptions extends ({
413
- headers?: HeadersInit | undefined;
414
- } & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
415
- headers?: HeadersInit | undefined;
416
- } & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined, TAs extends FetchAs = FetchAsFromOptions<TOptions, FetchAs.json>, TReturn = FetchResult<TResult>[TAs]>(...args: DefaultUrl extends undefined ? [url: string | URL, options?: TOptions | undefined] : [options?: TOptions | undefined]) => _superutils_promise.IPromisE<TReturn>;
417
- };
418
- declare const _head: {
419
- <T, TOptions extends ({
420
- headers?: HeadersInit | undefined;
421
- } & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
422
- headers?: HeadersInit | undefined;
423
- } & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined, TAs extends FetchAs = FetchAsFromOptions<TOptions, FetchAs.json>, TReturn = FetchResult<T>[TAs]>(url: FetchArgs[0], options?: TOptions | undefined): _superutils_promise.IPromisE<TReturn>;
424
- deferred<ThisArg = unknown, TDelay extends number = number, DefaultUrl extends FetchArgs[0] | undefined = string | URL | undefined, DefaultOptions extends ({
425
- headers?: HeadersInit | undefined;
426
- } & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
427
- headers?: HeadersInit | undefined;
428
- } & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined>(deferOptions?: _superutils_promise.DeferredAsyncOptions<ThisArg, TDelay>, defaultUrl?: DefaultUrl | undefined, defaultOptions?: DefaultOptions | undefined): <TResult = unknown, TOptions extends ({
429
- headers?: HeadersInit | undefined;
430
- } & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
431
- headers?: HeadersInit | undefined;
432
- } & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined, TAs extends FetchAs = FetchAsFromOptions<TOptions, FetchAs.json>, TReturn = FetchResult<TResult>[TAs]>(...args: DefaultUrl extends undefined ? [url: string | URL, options?: TOptions | undefined] : [options?: TOptions | undefined]) => _superutils_promise.IPromisE<TReturn>;
433
- };
434
- declare const _options: {
435
- <T, TOptions extends ({
436
- headers?: HeadersInit | undefined;
437
- } & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
438
- headers?: HeadersInit | undefined;
439
- } & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined, TAs extends FetchAs = FetchAsFromOptions<TOptions, FetchAs.json>, TReturn = FetchResult<T>[TAs]>(url: FetchArgs[0], options?: TOptions | undefined): _superutils_promise.IPromisE<TReturn>;
440
- deferred<ThisArg = unknown, TDelay extends number = number, DefaultUrl extends FetchArgs[0] | undefined = string | URL | undefined, DefaultOptions extends ({
441
- headers?: HeadersInit | undefined;
442
- } & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
443
- headers?: HeadersInit | undefined;
444
- } & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined>(deferOptions?: _superutils_promise.DeferredAsyncOptions<ThisArg, TDelay>, defaultUrl?: DefaultUrl | undefined, defaultOptions?: DefaultOptions | undefined): <TResult = unknown, TOptions extends ({
445
- headers?: HeadersInit | undefined;
446
- } & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
447
- headers?: HeadersInit | undefined;
448
- } & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined, TAs extends FetchAs = FetchAsFromOptions<TOptions, FetchAs.json>, TReturn = FetchResult<TResult>[TAs]>(...args: DefaultUrl extends undefined ? [url: string | URL, options?: TOptions | undefined] : [options?: TOptions | undefined]) => _superutils_promise.IPromisE<TReturn>;
449
- };
450
- declare const _delete: {
451
- <T, TOptions extends ({
452
- headers?: HeadersInit | undefined;
453
- } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined, TAs extends FetchAs = FetchAsFromOptions<TOptions, FetchAs.json>, TReturn = FetchResult<T>[TAs]>(url: PostArgs[0], data?: PostArgs[1], options?: TOptions | undefined): _superutils_promise.IPromisE<TReturn>;
454
- deferred<ThisArg = unknown, TDelay extends number = number, DefaultUrl extends PostArgs[0] | undefined = string | URL | undefined, DefaultData extends PostArgs[1] = PostBody | (() => PostBody) | undefined, DefaultOptions extends ({
455
- headers?: HeadersInit | undefined;
456
- } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
457
- headers?: HeadersInit | undefined;
458
- } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined>(deferOptions?: _superutils_promise.DeferredAsyncOptions<ThisArg, TDelay>, defaultUrl?: DefaultUrl | undefined, defaultData?: DefaultData | undefined, defaultOptions?: DefaultOptions | undefined): <TResult = unknown, TOptions extends ({
459
- headers?: HeadersInit | undefined;
460
- } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
461
- headers?: HeadersInit | undefined;
462
- } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined, TAs extends FetchAs = FetchAsFromOptions<TOptions, FetchAs.json>, TReturn = FetchResult<TResult>[TAs]>(...args: PostDeferredCbArgs<DefaultUrl, DefaultData, TOptions, [url: string | URL, data: PostBody | (() => PostBody), options: PostOptions]>) => _superutils_promise.IPromisE<TReturn>;
569
+ declare const fetchResponse: {
570
+ <T = Response, TOptions extends FetchOptions = FetchOptions, TAs extends FetchAs = TOptions["as"] extends FetchAs ? TOptions["as"] : FetchAs.response, TReturn = FetchResult<T>[TAs]>(url: FetchArgs[0], options?: Parameters<typeof fetch<T, TOptions, TAs, TReturn>>[1]): IPromisE_Timeout<TReturn>;
571
+ defaults: FetchOptionsDefault;
463
572
  };
464
- declare const _patch: {
465
- <T, TOptions extends ({
466
- headers?: HeadersInit | undefined;
467
- } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined, TAs extends FetchAs = FetchAsFromOptions<TOptions, FetchAs.json>, TReturn = FetchResult<T>[TAs]>(url: PostArgs[0], data?: PostArgs[1], options?: TOptions | undefined): _superutils_promise.IPromisE<TReturn>;
468
- deferred<ThisArg = unknown, TDelay extends number = number, DefaultUrl extends PostArgs[0] | undefined = string | URL | undefined, DefaultData extends PostArgs[1] = PostBody | (() => PostBody) | undefined, DefaultOptions extends ({
469
- headers?: HeadersInit | undefined;
470
- } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
471
- headers?: HeadersInit | undefined;
472
- } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined>(deferOptions?: _superutils_promise.DeferredAsyncOptions<ThisArg, TDelay>, defaultUrl?: DefaultUrl | undefined, defaultData?: DefaultData | undefined, defaultOptions?: DefaultOptions | undefined): <TResult = unknown, TOptions extends ({
473
- headers?: HeadersInit | undefined;
474
- } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
475
- headers?: HeadersInit | undefined;
476
- } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined, TAs extends FetchAs = FetchAsFromOptions<TOptions, FetchAs.json>, TReturn = FetchResult<TResult>[TAs]>(...args: PostDeferredCbArgs<DefaultUrl, DefaultData, TOptions, [url: string | URL, data: PostBody | (() => PostBody), options: PostOptions]>) => _superutils_promise.IPromisE<TReturn>;
477
- };
478
- declare const _post: {
479
- <T, TOptions extends ({
480
- headers?: HeadersInit | undefined;
481
- } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined, TAs extends FetchAs = FetchAsFromOptions<TOptions, FetchAs.json>, TReturn = FetchResult<T>[TAs]>(url: PostArgs[0], data?: PostArgs[1], options?: TOptions | undefined): _superutils_promise.IPromisE<TReturn>;
482
- deferred<ThisArg = unknown, TDelay extends number = number, DefaultUrl extends PostArgs[0] | undefined = string | URL | undefined, DefaultData extends PostArgs[1] = PostBody | (() => PostBody) | undefined, DefaultOptions extends ({
483
- headers?: HeadersInit | undefined;
484
- } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
485
- headers?: HeadersInit | undefined;
486
- } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined>(deferOptions?: _superutils_promise.DeferredAsyncOptions<ThisArg, TDelay>, defaultUrl?: DefaultUrl | undefined, defaultData?: DefaultData | undefined, defaultOptions?: DefaultOptions | undefined): <TResult = unknown, TOptions extends ({
487
- headers?: HeadersInit | undefined;
488
- } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
489
- headers?: HeadersInit | undefined;
490
- } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined, TAs extends FetchAs = FetchAsFromOptions<TOptions, FetchAs.json>, TReturn = FetchResult<TResult>[TAs]>(...args: PostDeferredCbArgs<DefaultUrl, DefaultData, TOptions, [url: string | URL, data: PostBody | (() => PostBody), options: PostOptions]>) => _superutils_promise.IPromisE<TReturn>;
491
- };
492
- declare const _put: {
493
- <T, TOptions extends ({
494
- headers?: HeadersInit | undefined;
495
- } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined, TAs extends FetchAs = FetchAsFromOptions<TOptions, FetchAs.json>, TReturn = FetchResult<T>[TAs]>(url: PostArgs[0], data?: PostArgs[1], options?: TOptions | undefined): _superutils_promise.IPromisE<TReturn>;
496
- deferred<ThisArg = unknown, TDelay extends number = number, DefaultUrl extends PostArgs[0] | undefined = string | URL | undefined, DefaultData extends PostArgs[1] = PostBody | (() => PostBody) | undefined, DefaultOptions extends ({
497
- headers?: HeadersInit | undefined;
498
- } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
499
- headers?: HeadersInit | undefined;
500
- } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined>(deferOptions?: _superutils_promise.DeferredAsyncOptions<ThisArg, TDelay>, defaultUrl?: DefaultUrl | undefined, defaultData?: DefaultData | undefined, defaultOptions?: DefaultOptions | undefined): <TResult = unknown, TOptions extends ({
501
- headers?: HeadersInit | undefined;
502
- } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
503
- headers?: HeadersInit | undefined;
504
- } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined, TAs extends FetchAs = FetchAsFromOptions<TOptions, FetchAs.json>, TReturn = FetchResult<TResult>[TAs]>(...args: PostDeferredCbArgs<DefaultUrl, DefaultData, TOptions, [url: string | URL, data: PostBody | (() => PostBody), options: PostOptions]>) => _superutils_promise.IPromisE<TReturn>;
505
- };
506
- type FetchWithMethods = typeof fetchResponse & {
507
- /** Default options */
508
- defaults: typeof fetch.defaults;
509
- /** Make HTTP `DELETE` request, result automatically parsed as JSON */
510
- delete: typeof _delete;
511
- /** Make HTTP `GET` request, result automatically parsed as JSON */
512
- get: typeof _get;
513
- /** Make HTTP `HEAD` request, result automatically parsed as JSON */
514
- head: typeof _head;
515
- /** Make HTTP `OPTIONS` request, result automatically parsed as JSON */
516
- options: typeof _options;
517
- /** Make HTTP `PATCH` request, result automatically parsed as JSON */
518
- patch: typeof _patch;
519
- /** Make HTTP `POST` request, result automatically parsed as JSON */
520
- post: typeof _post;
521
- /** Make HTTP `PUT` request, result automatically parsed as JSON */
522
- put: typeof _put;
573
+
574
+ declare const methods: {
575
+ /** Make HTTP requests with method GET */
576
+ get: {
577
+ <T, TOptions extends ({
578
+ headers?: HeadersInit | undefined;
579
+ } & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
580
+ headers?: HeadersInit | undefined;
581
+ } & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined, TAs extends FetchAs = FetchAsFromOptions<TOptions, FetchAs.json>, TReturn = FetchResult<T>[TAs]>(url: FetchArgs[0], options?: TOptions | undefined): _superutils_promise.IPromisE_Timeout<TReturn>;
582
+ deferred<ThisArg = unknown, Delay extends number = number, DefaultUrl extends FetchArgs[0] | undefined = string | URL | undefined, DefaultOptions extends ({
583
+ headers?: HeadersInit | undefined;
584
+ } & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
585
+ headers?: HeadersInit | undefined;
586
+ } & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined>(deferOptions?: _superutils_promise.DeferredAsyncOptions<ThisArg, Delay>, defaultUrl?: DefaultUrl | undefined, defaultOptions?: DefaultOptions | undefined): <TResult = unknown, TOptions extends ({
587
+ headers?: HeadersInit | undefined;
588
+ } & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
589
+ headers?: HeadersInit | undefined;
590
+ } & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined, TAs extends FetchAs = FetchAsFromOptions<TOptions, FetchAs.json>, TReturn = FetchResult<TResult>[TAs]>(...args: DefaultUrl extends undefined ? [url: string | URL, options?: TOptions | undefined] : [options?: TOptions | undefined]) => _superutils_promise.IPromisE_Timeout<TReturn>;
591
+ };
592
+ /** Make HTTP requests with method HEAD */
593
+ head: {
594
+ <T, TOptions extends ({
595
+ headers?: HeadersInit | undefined;
596
+ } & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
597
+ headers?: HeadersInit | undefined;
598
+ } & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined, TAs extends FetchAs = FetchAsFromOptions<TOptions, FetchAs.json>, TReturn = FetchResult<T>[TAs]>(url: FetchArgs[0], options?: TOptions | undefined): _superutils_promise.IPromisE_Timeout<TReturn>;
599
+ deferred<ThisArg = unknown, Delay extends number = number, DefaultUrl extends FetchArgs[0] | undefined = string | URL | undefined, DefaultOptions extends ({
600
+ headers?: HeadersInit | undefined;
601
+ } & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
602
+ headers?: HeadersInit | undefined;
603
+ } & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined>(deferOptions?: _superutils_promise.DeferredAsyncOptions<ThisArg, Delay>, defaultUrl?: DefaultUrl | undefined, defaultOptions?: DefaultOptions | undefined): <TResult = unknown, TOptions extends ({
604
+ headers?: HeadersInit | undefined;
605
+ } & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
606
+ headers?: HeadersInit | undefined;
607
+ } & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined, TAs extends FetchAs = FetchAsFromOptions<TOptions, FetchAs.json>, TReturn = FetchResult<TResult>[TAs]>(...args: DefaultUrl extends undefined ? [url: string | URL, options?: TOptions | undefined] : [options?: TOptions | undefined]) => _superutils_promise.IPromisE_Timeout<TReturn>;
608
+ };
609
+ /** Make HTTP requests with method OPTIONS */
610
+ options: {
611
+ <T, TOptions extends ({
612
+ headers?: HeadersInit | undefined;
613
+ } & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
614
+ headers?: HeadersInit | undefined;
615
+ } & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined, TAs extends FetchAs = FetchAsFromOptions<TOptions, FetchAs.json>, TReturn = FetchResult<T>[TAs]>(url: FetchArgs[0], options?: TOptions | undefined): _superutils_promise.IPromisE_Timeout<TReturn>;
616
+ deferred<ThisArg = unknown, Delay extends number = number, DefaultUrl extends FetchArgs[0] | undefined = string | URL | undefined, DefaultOptions extends ({
617
+ headers?: HeadersInit | undefined;
618
+ } & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
619
+ headers?: HeadersInit | undefined;
620
+ } & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined>(deferOptions?: _superutils_promise.DeferredAsyncOptions<ThisArg, Delay>, defaultUrl?: DefaultUrl | undefined, defaultOptions?: DefaultOptions | undefined): <TResult = unknown, TOptions extends ({
621
+ headers?: HeadersInit | undefined;
622
+ } & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
623
+ headers?: HeadersInit | undefined;
624
+ } & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined, TAs extends FetchAs = FetchAsFromOptions<TOptions, FetchAs.json>, TReturn = FetchResult<TResult>[TAs]>(...args: DefaultUrl extends undefined ? [url: string | URL, options?: TOptions | undefined] : [options?: TOptions | undefined]) => _superutils_promise.IPromisE_Timeout<TReturn>;
625
+ };
626
+ /** Make HTTP requests with method DELETE */
627
+ delete: {
628
+ <T = unknown, TOptions extends ({
629
+ headers?: HeadersInit | undefined;
630
+ } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
631
+ headers?: HeadersInit | undefined;
632
+ } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined, TAs extends FetchAs = FetchAsFromOptions<TOptions, FetchAs.json>, TReturn = FetchResult<T>[TAs]>(url: PostArgs[0], data?: PostArgs[1], options?: TOptions | undefined): _superutils_promise.IPromisE_Timeout<TReturn>;
633
+ deferred<ThisArg, Delay extends number, DefaultUrl extends PostArgs[0] | undefined, DefaultData extends PostArgs[1] = undefined, DefaultOptions extends ({
634
+ headers?: HeadersInit | undefined;
635
+ } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = undefined>(deferOptions?: _superutils_promise.DeferredAsyncOptions<ThisArg, Delay>, defaultUrl?: DefaultUrl | undefined, defaultData?: DefaultData | undefined, defaultOptions?: DefaultOptions | undefined): <TResult = unknown, TOptions extends ({
636
+ headers?: HeadersInit | undefined;
637
+ } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = {
638
+ headers?: HeadersInit | undefined;
639
+ } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>, TAs extends FetchAs = FetchAsFromOptions<TOptions, FetchAs.json>, TReturn = FetchResult<TResult>[TAs]>(...args: PostDeferredCbArgs<DefaultUrl, DefaultData, TOptions, [url: string | URL, data: PostBody | (() => PostBody), options: PostOptions]>) => _superutils_promise.IPromisE_Timeout<TReturn>;
640
+ };
641
+ /** Make HTTP requests with method PATCH */
642
+ patch: {
643
+ <T = unknown, TOptions extends ({
644
+ headers?: HeadersInit | undefined;
645
+ } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
646
+ headers?: HeadersInit | undefined;
647
+ } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined, TAs extends FetchAs = FetchAsFromOptions<TOptions, FetchAs.json>, TReturn = FetchResult<T>[TAs]>(url: PostArgs[0], data?: PostArgs[1], options?: TOptions | undefined): _superutils_promise.IPromisE_Timeout<TReturn>;
648
+ deferred<ThisArg, Delay extends number, DefaultUrl extends PostArgs[0] | undefined, DefaultData extends PostArgs[1] = undefined, DefaultOptions extends ({
649
+ headers?: HeadersInit | undefined;
650
+ } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = undefined>(deferOptions?: _superutils_promise.DeferredAsyncOptions<ThisArg, Delay>, defaultUrl?: DefaultUrl | undefined, defaultData?: DefaultData | undefined, defaultOptions?: DefaultOptions | undefined): <TResult = unknown, TOptions extends ({
651
+ headers?: HeadersInit | undefined;
652
+ } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = {
653
+ headers?: HeadersInit | undefined;
654
+ } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>, TAs extends FetchAs = FetchAsFromOptions<TOptions, FetchAs.json>, TReturn = FetchResult<TResult>[TAs]>(...args: PostDeferredCbArgs<DefaultUrl, DefaultData, TOptions, [url: string | URL, data: PostBody | (() => PostBody), options: PostOptions]>) => _superutils_promise.IPromisE_Timeout<TReturn>;
655
+ };
656
+ /** Make HTTP requests with method POST */
657
+ post: {
658
+ <T = unknown, TOptions extends ({
659
+ headers?: HeadersInit | undefined;
660
+ } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
661
+ headers?: HeadersInit | undefined;
662
+ } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined, TAs extends FetchAs = FetchAsFromOptions<TOptions, FetchAs.json>, TReturn = FetchResult<T>[TAs]>(url: PostArgs[0], data?: PostArgs[1], options?: TOptions | undefined): _superutils_promise.IPromisE_Timeout<TReturn>;
663
+ deferred<ThisArg, Delay extends number, DefaultUrl extends PostArgs[0] | undefined, DefaultData extends PostArgs[1] = undefined, DefaultOptions extends ({
664
+ headers?: HeadersInit | undefined;
665
+ } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = undefined>(deferOptions?: _superutils_promise.DeferredAsyncOptions<ThisArg, Delay>, defaultUrl?: DefaultUrl | undefined, defaultData?: DefaultData | undefined, defaultOptions?: DefaultOptions | undefined): <TResult = unknown, TOptions extends ({
666
+ headers?: HeadersInit | undefined;
667
+ } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = {
668
+ headers?: HeadersInit | undefined;
669
+ } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>, TAs extends FetchAs = FetchAsFromOptions<TOptions, FetchAs.json>, TReturn = FetchResult<TResult>[TAs]>(...args: PostDeferredCbArgs<DefaultUrl, DefaultData, TOptions, [url: string | URL, data: PostBody | (() => PostBody), options: PostOptions]>) => _superutils_promise.IPromisE_Timeout<TReturn>;
670
+ };
671
+ /** Make HTTP requests with method PUT */
672
+ put: {
673
+ <T = unknown, TOptions extends ({
674
+ headers?: HeadersInit | undefined;
675
+ } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
676
+ headers?: HeadersInit | undefined;
677
+ } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined, TAs extends FetchAs = FetchAsFromOptions<TOptions, FetchAs.json>, TReturn = FetchResult<T>[TAs]>(url: PostArgs[0], data?: PostArgs[1], options?: TOptions | undefined): _superutils_promise.IPromisE_Timeout<TReturn>;
678
+ deferred<ThisArg, Delay extends number, DefaultUrl extends PostArgs[0] | undefined, DefaultData extends PostArgs[1] = undefined, DefaultOptions extends ({
679
+ headers?: HeadersInit | undefined;
680
+ } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = undefined>(deferOptions?: _superutils_promise.DeferredAsyncOptions<ThisArg, Delay>, defaultUrl?: DefaultUrl | undefined, defaultData?: DefaultData | undefined, defaultOptions?: DefaultOptions | undefined): <TResult = unknown, TOptions extends ({
681
+ headers?: HeadersInit | undefined;
682
+ } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = {
683
+ headers?: HeadersInit | undefined;
684
+ } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>, TAs extends FetchAs = FetchAsFromOptions<TOptions, FetchAs.json>, TReturn = FetchResult<TResult>[TAs]>(...args: PostDeferredCbArgs<DefaultUrl, DefaultData, TOptions, [url: string | URL, data: PostBody | (() => PostBody), options: PostOptions]>) => _superutils_promise.IPromisE_Timeout<TReturn>;
685
+ };
523
686
  };
524
687
  /**
525
688
  * A `fetch()` replacement that simplifies data fetching with automatic JSON parsing, request timeouts, retries,
@@ -562,49 +725,34 @@ type FetchWithMethods = typeof fetchResponse & {
562
725
  * - FetchAs.json: `unknown`
563
726
  * - FetchAs.text: `string`
564
727
  * - FetchAs.response: `Response`
728
+ *
565
729
  * @param url
566
730
  * @param options (optional) Standard `fetch` options extended with {@link FetchCustomOptions}
567
- * @param options.as (optional) determines who to parse the result. Default: {@link FetchAs.response}
731
+ * @param options.abortCtrl (optional) if not provided `AbortController` will be instantiated when `timeout` used.
732
+ *
733
+ * Default: `new AbortController()`
734
+ * @param options.as (optional) (optional) specify how to parse the result.
735
+ * For raw `Response` use {@link FetchAs.response}
736
+ *
737
+ * Default: {@link FetchAs.json}
568
738
  * @param options.headers (optional) request headers
569
- * Default: `{ 'content-type': `'application/json' }` (unless changed in the fetch.defaults).
570
- * @param options.method (optional) fetch method. Default: `'get'`
571
739
  *
572
- * Options' default values (excluding `as`, `method` and `retryIf`) can be configured to be EFFECTIVE GLOBALLY.
740
+ * Default: `{ 'content-type': 'application/json' }`
741
+ * @param options.interceptors (optional) request interceptor/transformer callbacks.
742
+ * See {@link FetchInterceptors} for details.
743
+ * @param options.method (optional) fetch method.
573
744
  *
574
- * ```typescript
575
- * import fetch from '@superutils/fetch'
745
+ * Default: `'get'`
746
+ * @param options.timeout (optional) duration in milliseconds to abort the request.
747
+ * This duration includes the execution of all interceptors/transformers.
576
748
  *
577
- * fetch.defaults = {
578
- * errMsgs: {
579
- * invalidUrl: 'Invalid URL',
580
- * parseFailed: 'Failed to parse response as',
581
- * reqTimedout: 'Request timed out',
582
- * requestFailed: 'Request failed with status code:',
583
- * },
584
- * headers: new Headers([['content-type', 'application/json']]),
585
- * // Global/application-wide Interceptor & Transfermers
586
- * interceptors: {
587
- * error: [],
588
- * request: [],
589
- * response: [],
590
- * result: [],
591
- * },
592
- * timeout: 0,
593
- * //........
594
- * }
595
- * ```
749
+ * Default: `30_000`
596
750
  *
597
- * @property options.abortCtrl (optional) if not provided `AbortController` will be instantiated when `timeout` used.
598
- * @property options.headers (optional) request headers. Default: `{ 'content-type' : 'application/json'}`
599
- * @property options.interceptors (optional) request interceptor callbacks. See {@link FetchInterceptors} for details.
600
- * @property options.method (optional) Default: `"get"`
601
- * @property options.timeout (optional) duration in milliseconds to abort the request if it takes longer.
602
- * @property options.parse (optional) specify how to parse the result.
603
- * Default: {@link FetchAs.json}
604
- * For raw `Response` use {@link FetchAs.response}
751
+ * ---
605
752
  *
606
- * @example Drop-in replacement for built-in `fetch`
607
- * ```typescript
753
+ * @example Drop-in replacement for built-in fetch
754
+ *
755
+ * ```javascript
608
756
  * import fetch from '@superutils/fetch'
609
757
  *
610
758
  * fetch('https://dummyjson.com/products/1')
@@ -613,7 +761,7 @@ type FetchWithMethods = typeof fetchResponse & {
613
761
  * ```
614
762
  *
615
763
  * @example Method specific function with JSON parsing by default
616
- * ```typescript
764
+ * ```javascript
617
765
  * import fetch from '@superutils/fetch'
618
766
  *
619
767
  * // no need for `response.json()` or `result.data.data` drilling
@@ -626,8 +774,77 @@ type FetchWithMethods = typeof fetchResponse & {
626
774
  * fetch.post('https://dummyjson.com/products/add', { title: 'Product title' })
627
775
  * .then(product => console.log(product))
628
776
  * ```
777
+ *
778
+ *
779
+ * @example Set default options.
780
+ *
781
+ * Options' default values (excluding `as` and `method`) can be configured to be EFFECTIVE GLOBALLY.
782
+ *
783
+ * ```typescript
784
+ * import fetch from '@superutils/fetch'
785
+ *
786
+ * const { defaults, errorMsgs, interceptors } = fetch
787
+ *
788
+ * // set default request timeout duration in milliseconds
789
+ * defaults.timeout = 40_000
790
+ *
791
+ * // default headers
792
+ * defaults.headers = { 'content-type': 'text/plain' }
793
+ *
794
+ * // override error messages
795
+ * errorMsgs.invalidUrl = 'URL is not valid'
796
+ * errorMsgs.timedout = 'Request took longer than expected'
797
+ *
798
+ * // add an interceptor to log all request failures.
799
+ * const fetchLogger = (fetchErr, url, options) => console.log('Fetch error log', fetchErr)
800
+ * interceptors.error.push(fetchLogger)
801
+ *
802
+ * // add an interceptor to conditionally include header before making requests
803
+ * interceptors.request.push((url, options) => {
804
+ * // ignore login requests
805
+ * if (`${url}`.includes('/login')) return
806
+ *
807
+ * options.headers.set('x-auth-token', 'my-auth-token')
808
+ * })
809
+ * ```
810
+ */
811
+ declare const defaultFetch: typeof fetchResponse & typeof methods;
812
+
813
+ /**
814
+ * Gracefully executes interceptors and returns the processed value.
815
+ * If the value is not transformed (by returning a new value) by the interceptors,
816
+ * the original value is returned.
817
+ *
818
+ * @param value value to be passed to the interceptors
819
+ * @param signal The AbortController used to monitor the request status. If the signal is aborted (e.g. due to
820
+ * timeout or manual cancellation), the interceptor chain halts immediately. Note: This does not interrupt the
821
+ * currently executing interceptor, but prevents subsequent ones from running.
822
+ * @param interceptors interceptor/transformer callbacks
823
+ * @param args (optional) common arguments to be supplied to all the interceptors in addition to
824
+ * the `value' which will always be the first argument.
825
+ *
826
+ * Interceptor arguments: `[value, ...args]`
827
+ */
828
+ declare const executeInterceptors: <T, TArgs extends unknown[]>(value: T, signal?: AbortSignal, interceptors?: Interceptor<T, TArgs>[], ...args: TArgs) => Promise<T>;
829
+
830
+ /**
831
+ * Add AbortController to options if not present and propagate external abort signal if provided.
832
+ *
833
+ * @param options The fetch options object.
834
+ *
835
+ * @returns The AbortController instance associated with the options.
836
+ */
837
+ declare const getAbortCtrl: (options: Partial<FetchOptions>) => AbortController;
838
+
839
+ /**
840
+ * Execute built-in `fetch()` and retry if request fails and `options.retry > 0`.
841
+ *
842
+ * @param url request URL
843
+ * @param options (optional)
844
+ *
845
+ * @returns response
629
846
  */
630
- declare const fetchDefault: FetchWithMethods;
847
+ declare const getResponse: (url: FetchArgs[0], options?: FetchArgs[1]) => Promise<Response>;
631
848
 
632
849
  /**
633
850
  * Merge one or more {@link FetchOptions}
@@ -647,4 +864,4 @@ declare const mergeFetchOptions: (...allOptions: FetchOptions[]) => FetchOptions
647
864
  /** Merges partial fetch options ignoring empty or undefined. Otherwise, will return the first argument. */
648
865
  declare const mergePartialOptions: (...optionsAr: (Partial<FetchOptions> | undefined)[]) => Partial<FetchOptions> | undefined;
649
866
 
650
- export { type ExcludeOptions, type ExcludePostOptions, type FetchArgs, type FetchArgsInterceptor, FetchAs, type FetchAsFromOptions, type FetchCustomOptions, type FetchDeferredArgs, type FetchErrMsgs, FetchError, type FetchInterceptorError, type FetchInterceptorRequest, type FetchInterceptorResponse, type FetchInterceptorResult, type FetchInterceptors, type FetchOptions, type FetchOptionsDefaults, type FetchOptionsInterceptor, type FetchResult, type FetchRetryOptions, type FetchWithMethods, type Interceptor, type PostArgs, type PostBody, type PostDeferredCbArgs, type PostOptions, createClient, createPostClient, fetchDefault as default, fetch, fetchResponse, mergeFetchOptions, mergePartialOptions };
867
+ export { ContentType, type ExcludeOptions, type ExcludePostOptions, type FetchArgs, type FetchArgsInterceptor, FetchAs, type FetchAsFromOptions, type FetchCustomOptions, type FetchDeferredArgs, type FetchErrMsgs, FetchError, type FetchInterceptorError, type FetchInterceptorRequest, type FetchInterceptorResponse, type FetchInterceptorResult, type FetchInterceptors, type FetchOptions, type FetchOptionsDefault, type FetchOptionsInterceptor, type FetchResult, type FetchRetryOptions, type Interceptor, MAX_TIMEOUT, type PostArgs, type PostBody, type PostDeferredCbArgs, type PostOptions, createClient, createPostClient, defaultFetch as default, executeInterceptors, fetch, fetchResponse, getAbortCtrl, getResponse, mergeFetchOptions, mergePartialOptions };