@superutils/fetch 1.2.2 → 1.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +155 -83
- package/dist/index.d.ts +317 -176
- package/dist/index.js +139 -82
- package/package.json +5 -5
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +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 } from '@superutils/promise';
|
|
3
3
|
export { DeferredAsyncOptions, ResolveError, ResolveIgnored } from '@superutils/promise';
|
|
4
4
|
import { ValueOrPromise } from '@superutils/core';
|
|
5
5
|
|
|
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
|
+
};
|
|
6
22
|
type ExcludeOptions<Target, // options to exclude
|
|
7
23
|
Options extends FetchOptions = FetchOptions> = Target extends FetchOptions ? {
|
|
8
24
|
headers?: Options['headers'];
|
|
@@ -245,7 +261,7 @@ type FetchInterceptors = {
|
|
|
245
261
|
* Fetch request options
|
|
246
262
|
*/
|
|
247
263
|
type FetchOptions = Omit<RequestInit, 'body'> & FetchCustomOptions;
|
|
248
|
-
type FetchOptionsDefaults = Omit<FetchOptionsInterceptor, 'as' | 'method'
|
|
264
|
+
type FetchOptionsDefaults = Omit<FetchOptionsInterceptor, 'as' | 'method'>;
|
|
249
265
|
/**
|
|
250
266
|
* Fetch options available to interceptors
|
|
251
267
|
*/
|
|
@@ -279,7 +295,7 @@ type FetchRetryOptions = Omit<Partial<RetryOptions>, 'retry' | 'retryIf'> & {
|
|
|
279
295
|
* Default: `0`
|
|
280
296
|
*/
|
|
281
297
|
retry?: number;
|
|
282
|
-
retryIf?:
|
|
298
|
+
retryIf?: RetryIfFunc<Response>;
|
|
283
299
|
};
|
|
284
300
|
/**
|
|
285
301
|
* Generic fetch interceptor type
|
|
@@ -324,7 +340,17 @@ type PostArgs<OmitMethod = false> = [
|
|
|
324
340
|
* f4().then(console.log, console.warn)
|
|
325
341
|
* ```
|
|
326
342
|
*/
|
|
327
|
-
type PostDeferredCbArgs<TUrl = undefined, TData = undefined, Options =
|
|
343
|
+
type PostDeferredCbArgs<TUrl = undefined, TData = undefined, Options = undefined, CbArgsReq extends unknown[] = Required<PostArgs>> = [TUrl, TData] extends [CbArgsReq[0], undefined] ? [
|
|
344
|
+
data?: PostArgs[1],
|
|
345
|
+
options?: PostArgs[2]
|
|
346
|
+
] : [
|
|
347
|
+
TUrl,
|
|
348
|
+
TData
|
|
349
|
+
] extends [undefined, CbArgsReq[1]] ? [url: PostArgs[0], options?: PostArgs[2]] : [
|
|
350
|
+
TUrl,
|
|
351
|
+
TData
|
|
352
|
+
] extends [CbArgsReq[0], CbArgsReq[1]] ? [options?: PostArgs[2]] : [TUrl, TData, Options];
|
|
353
|
+
/** Request options for POST-like methods that allow "options.body" */
|
|
328
354
|
type PostOptions = {
|
|
329
355
|
/** Default: `'post'` */
|
|
330
356
|
method?: 'post' | 'put' | 'patch' | 'delete' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
@@ -332,37 +358,144 @@ type PostOptions = {
|
|
|
332
358
|
|
|
333
359
|
/**
|
|
334
360
|
* Create a reusable fetch client with shared options. The returned function comes attached with a
|
|
335
|
-
* `.deferred()` function for
|
|
361
|
+
* `.deferred()` function for debounce and throttle behavior.
|
|
362
|
+
*
|
|
363
|
+
* The `createClient` utility streamlines the creation of dedicated API clients by generating pre-configured fetch
|
|
364
|
+
* functions. These functions can be equipped with default options like headers, timeouts, or a specific HTTP method,
|
|
365
|
+
* which minimizes code repetition across your application. If a method is not specified during creation, the client
|
|
366
|
+
* will default to `GET`.
|
|
367
|
+
*
|
|
368
|
+
* The returned client also includes a `.deferred()` method, providing the same debounce, throttle, and sequential
|
|
369
|
+
* execution capabilities found in functions like `fetch.get.deferred()`.
|
|
370
|
+
*
|
|
371
|
+
* @example create reusable clients
|
|
372
|
+
* ```javascript
|
|
373
|
+
* import { createClient } from '@superutils/fetch'
|
|
374
|
+
*
|
|
375
|
+
* // Create a "GET" client with default headers and a 5-second timeout
|
|
376
|
+
* const apiClient = createClient(
|
|
377
|
+
* {
|
|
378
|
+
* // fixed options cannot be overridden
|
|
379
|
+
* method: 'get',
|
|
380
|
+
* },
|
|
381
|
+
* {
|
|
382
|
+
* // default options can be overridden
|
|
383
|
+
* headers: {
|
|
384
|
+
* Authorization: 'Bearer my-secret-token',
|
|
385
|
+
* 'Content-Type': 'application/json',
|
|
386
|
+
* },
|
|
387
|
+
* timeout: 5000,
|
|
388
|
+
* },
|
|
389
|
+
* {
|
|
390
|
+
* // default defer options (can be overridden)
|
|
391
|
+
* delayMs: 300,
|
|
392
|
+
* retry: 2, // If request fails, retry up to two more times
|
|
393
|
+
* },
|
|
394
|
+
* )
|
|
395
|
+
*
|
|
396
|
+
* // Use it just like the standard fetch
|
|
397
|
+
* apiClient('https://dummyjson.com/products/1', {
|
|
398
|
+
* // The 'method' property cannot be overridden as it is used in the fixed options when creating the client.
|
|
399
|
+
* // In TypeScript, the compiler will not allow this property.
|
|
400
|
+
* // In Javascript, it will simply be ignored.
|
|
401
|
+
* // method: 'post',
|
|
402
|
+
* timeout: 3000, // The 'timeout' property can be overridden
|
|
403
|
+
* }).then(console.log, console.warn)
|
|
404
|
+
*
|
|
405
|
+
* // create a deferred client using "apiClient"
|
|
406
|
+
* const deferredClient = apiClient.deferred(
|
|
407
|
+
* { retry: 0 }, // disable retrying by overriding the `retry` defer option
|
|
408
|
+
* 'https://dummyjson.com/products/1',
|
|
409
|
+
* { timeout: 3000 },
|
|
410
|
+
* )
|
|
411
|
+
* deferredClient({ timeout: 10000 }) // timeout is overridden by individual request
|
|
412
|
+
* .then(console.log, console.warn)
|
|
413
|
+
* ```
|
|
336
414
|
*/
|
|
337
|
-
declare const createClient: <
|
|
415
|
+
declare const createClient: <FixedOpts extends FetchOptions | undefined, CommonOpts extends ExcludeOptions<FixedOpts> | undefined, FixedAs extends FetchAs | undefined = FetchAsFromOptions<FixedOpts, undefined>>(
|
|
338
416
|
/** Mandatory fetch options that cannot be overriden by individual request */
|
|
339
|
-
|
|
417
|
+
fixedOptions?: FixedOpts,
|
|
340
418
|
/** Common fetch options that can be overriden by individual request */
|
|
341
419
|
commonOptions?: FetchOptions & CommonOpts, commonDeferOptions?: DeferredAsyncOptions<unknown, unknown>) => {
|
|
342
|
-
<T, TOptions extends ExcludeOptions<
|
|
343
|
-
deferred<ThisArg = unknown,
|
|
420
|
+
<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): IPromisE<TReturn>;
|
|
421
|
+
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]) => IPromisE<TReturn_1>;
|
|
344
422
|
};
|
|
345
423
|
|
|
346
424
|
/**
|
|
347
425
|
* Create a reusable fetch client with shared options. The returned function comes attached with a
|
|
348
426
|
* `.deferred()` function for debounced and throttled request.
|
|
427
|
+
* While `createClient()` is versatile enough for any HTTP method, `createPostClient()` is specifically designed for
|
|
428
|
+
* methods that require a request body, such as `DELETE`, `PATCH`, `POST`, and `PUT`. If a method is not provided, it
|
|
429
|
+
* defaults to `POST`. The generated client accepts an additional second parameter (`data`) for the request payload.
|
|
430
|
+
*
|
|
431
|
+
* Similar to `createClient`, the returned function comes equipped with a `.deferred()` method, enabling debounced,
|
|
432
|
+
* throttled, or sequential execution.
|
|
433
|
+
*
|
|
434
|
+
* @example create reusable clients
|
|
435
|
+
* ```javascript
|
|
436
|
+
* import { createPostClient, FetchAs } from '@superutils/fetch'
|
|
437
|
+
*
|
|
438
|
+
* // Create a POST client with 10-second as the default timeout
|
|
439
|
+
* const postClient = createPostClient(
|
|
440
|
+
* {
|
|
441
|
+
* method: 'post',
|
|
442
|
+
* headers: { 'content-type': 'application/json' },
|
|
443
|
+
* },
|
|
444
|
+
* { timeout: 10000 },
|
|
445
|
+
* )
|
|
446
|
+
*
|
|
447
|
+
* // Invoking `postClient()` automatically applies the pre-configured options
|
|
448
|
+
* postClient(
|
|
449
|
+
* 'https://dummyjson.com/products/add',
|
|
450
|
+
* { title: 'New Product' }, // data/body
|
|
451
|
+
* {}, // other options
|
|
452
|
+
* ).then(console.log)
|
|
453
|
+
*
|
|
454
|
+
* // create a deferred client using "postClient"
|
|
455
|
+
* const updateProduct = postClient.deferred(
|
|
456
|
+
* {
|
|
457
|
+
* delayMs: 300, // debounce duration
|
|
458
|
+
* },
|
|
459
|
+
* 'https://dummyjson.com/products/1',
|
|
460
|
+
* {
|
|
461
|
+
* method: 'patch',
|
|
462
|
+
* timeout: 3000,
|
|
463
|
+
* },
|
|
464
|
+
* )
|
|
465
|
+
* updateProduct({ title: 'New title 1' }) // ignored by debounce
|
|
466
|
+
* updateProduct({ title: 'New title 2' }) // executed
|
|
467
|
+
* ```
|
|
349
468
|
*/
|
|
350
|
-
declare const createPostClient: <
|
|
469
|
+
declare const createPostClient: <FixedOpts extends PostOptions | undefined, CommonOpts extends ExcludePostOptions<FixedOpts> | undefined, FixedAs extends FetchAs | undefined = FetchAsFromOptions<FixedOpts, undefined>>(
|
|
351
470
|
/** Mandatory fetch options that cannot be overriden by individual request */
|
|
352
|
-
|
|
471
|
+
fixedOptions?: FixedOpts,
|
|
353
472
|
/** Common fetch options that can be overriden by individual request */
|
|
354
473
|
commonOptions?: PostOptions & CommonOpts, commonDeferOptions?: DeferredAsyncOptions<unknown, unknown>) => {
|
|
355
|
-
<T, TOptions extends ExcludePostOptions<
|
|
356
|
-
deferred<ThisArg
|
|
474
|
+
<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<TReturn>;
|
|
475
|
+
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<TReturn_1>;
|
|
357
476
|
};
|
|
358
477
|
|
|
478
|
+
/**
|
|
479
|
+
* Gracefully executes interceptors and returns the processed value.
|
|
480
|
+
* If the value is not transformed (by returning a new value) by the interceptors,
|
|
481
|
+
* the original value is returned.
|
|
482
|
+
*
|
|
483
|
+
* @param value value to be passed to the interceptors
|
|
484
|
+
* @param interceptors interceptor callbacks
|
|
485
|
+
* @param args (optional) common arguments to be supplied to all the interceptors in addition to
|
|
486
|
+
* the `value' which will always be the first argument.
|
|
487
|
+
*
|
|
488
|
+
* Interceptor arguments: `[value, ...args]`
|
|
489
|
+
*/
|
|
490
|
+
declare const executeInterceptors: <T, TArgs extends unknown[]>(value: T, interceptors?: Interceptor<T, TArgs>[], ...args: TArgs) => Promise<T>;
|
|
491
|
+
|
|
359
492
|
/**
|
|
360
493
|
* Extended `fetch` with timeout, retry, and other options. Automatically parses as JSON by default on success.
|
|
361
494
|
*
|
|
362
495
|
* @param url request URL
|
|
363
496
|
* @param options (optional) Standard `fetch` options extended with {@link FetchCustomOptions}.
|
|
364
|
-
* Default content
|
|
365
|
-
* @param options.as (optional) determines
|
|
497
|
+
* Default "content-type" header is 'application/json'.
|
|
498
|
+
* @param options.as (optional) determines how to parse the result. Default: {@link FetchAs.json}
|
|
366
499
|
* @param options.method (optional) fetch method. Default: `'get'`
|
|
367
500
|
*
|
|
368
501
|
* @example Make a simple HTTP requests
|
|
@@ -375,7 +508,7 @@ commonOptions?: PostOptions & CommonOpts, commonDeferOptions?: DeferredAsyncOpti
|
|
|
375
508
|
* ```
|
|
376
509
|
*/
|
|
377
510
|
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>;
|
|
511
|
+
<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<TReturn>;
|
|
379
512
|
/** Default fetch options */
|
|
380
513
|
defaults: FetchOptionsDefaults;
|
|
381
514
|
};
|
|
@@ -384,12 +517,10 @@ declare const fetch: {
|
|
|
384
517
|
* Drop-in replacement for built-in `fetch()` with with timeout, retry etc options and Response as default return type.
|
|
385
518
|
*
|
|
386
519
|
* @param url request URL
|
|
387
|
-
* @param options (optional) Standard `fetch` options extended with
|
|
388
|
-
* @param options.as (optional) determines who to parse the result. Default: {@link FetchAs.response}
|
|
389
|
-
* @param options.method (optional) fetch method. Default: `'get'`
|
|
520
|
+
* @param options (optional) Standard `fetch` options extended with "FetchCustomOptions"
|
|
390
521
|
*
|
|
391
522
|
* @example Make a simple HTTP requests
|
|
392
|
-
* ```
|
|
523
|
+
* ```javascript
|
|
393
524
|
* import { fetchResponse } from '@superutils/fetch'
|
|
394
525
|
*
|
|
395
526
|
* fetchResponse('https://dummyjson.com/products/1')
|
|
@@ -397,129 +528,120 @@ declare const fetch: {
|
|
|
397
528
|
* .then(console.log, console.error)
|
|
398
529
|
* ```
|
|
399
530
|
*/
|
|
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]>(
|
|
531
|
+
declare const fetchResponse: <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<TReturn>;
|
|
401
532
|
|
|
402
|
-
declare const
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
headers?:
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
headers
|
|
430
|
-
|
|
431
|
-
headers?:
|
|
432
|
-
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
headers
|
|
442
|
-
|
|
443
|
-
headers?:
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
headers?:
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
headers
|
|
474
|
-
|
|
475
|
-
headers?:
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
headers
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
headers
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
headers
|
|
504
|
-
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
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;
|
|
533
|
+
declare const methods: {
|
|
534
|
+
/** Make HTTP requests with method GET */
|
|
535
|
+
get: {
|
|
536
|
+
<T, TOptions extends ({
|
|
537
|
+
headers?: HeadersInit | undefined;
|
|
538
|
+
} & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
|
|
539
|
+
headers?: HeadersInit | undefined;
|
|
540
|
+
} & 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>;
|
|
541
|
+
deferred<ThisArg = unknown, Delay extends number = number, DefaultUrl extends FetchArgs[0] | undefined = string | URL | undefined, DefaultOptions extends ({
|
|
542
|
+
headers?: HeadersInit | undefined;
|
|
543
|
+
} & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
|
|
544
|
+
headers?: HeadersInit | undefined;
|
|
545
|
+
} & 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 ({
|
|
546
|
+
headers?: HeadersInit | undefined;
|
|
547
|
+
} & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
|
|
548
|
+
headers?: HeadersInit | undefined;
|
|
549
|
+
} & 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>;
|
|
550
|
+
};
|
|
551
|
+
/** Make HTTP requests with method HEAD */
|
|
552
|
+
head: {
|
|
553
|
+
<T, TOptions extends ({
|
|
554
|
+
headers?: HeadersInit | undefined;
|
|
555
|
+
} & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
|
|
556
|
+
headers?: HeadersInit | undefined;
|
|
557
|
+
} & 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>;
|
|
558
|
+
deferred<ThisArg = unknown, Delay extends number = number, DefaultUrl extends FetchArgs[0] | undefined = string | URL | undefined, DefaultOptions extends ({
|
|
559
|
+
headers?: HeadersInit | undefined;
|
|
560
|
+
} & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
|
|
561
|
+
headers?: HeadersInit | undefined;
|
|
562
|
+
} & 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 ({
|
|
563
|
+
headers?: HeadersInit | undefined;
|
|
564
|
+
} & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
|
|
565
|
+
headers?: HeadersInit | undefined;
|
|
566
|
+
} & 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>;
|
|
567
|
+
};
|
|
568
|
+
/** Make HTTP requests with method OPTIONS */
|
|
569
|
+
options: {
|
|
570
|
+
<T, TOptions extends ({
|
|
571
|
+
headers?: HeadersInit | undefined;
|
|
572
|
+
} & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
|
|
573
|
+
headers?: HeadersInit | undefined;
|
|
574
|
+
} & 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>;
|
|
575
|
+
deferred<ThisArg = unknown, Delay extends number = number, DefaultUrl extends FetchArgs[0] | undefined = string | URL | undefined, DefaultOptions extends ({
|
|
576
|
+
headers?: HeadersInit | undefined;
|
|
577
|
+
} & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
|
|
578
|
+
headers?: HeadersInit | undefined;
|
|
579
|
+
} & 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 ({
|
|
580
|
+
headers?: HeadersInit | undefined;
|
|
581
|
+
} & Omit<FetchOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
|
|
582
|
+
headers?: HeadersInit | undefined;
|
|
583
|
+
} & 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>;
|
|
584
|
+
};
|
|
585
|
+
/** Make HTTP requests with method DELETE */
|
|
586
|
+
delete: {
|
|
587
|
+
<T = unknown, TOptions extends ({
|
|
588
|
+
headers?: HeadersInit | undefined;
|
|
589
|
+
} & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
|
|
590
|
+
headers?: HeadersInit | undefined;
|
|
591
|
+
} & 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>;
|
|
592
|
+
deferred<ThisArg, Delay extends number, DefaultUrl extends PostArgs[0] | undefined, DefaultData extends PostArgs[1] = undefined, DefaultOptions extends ({
|
|
593
|
+
headers?: HeadersInit | undefined;
|
|
594
|
+
} & 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 ({
|
|
595
|
+
headers?: HeadersInit | undefined;
|
|
596
|
+
} & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = {
|
|
597
|
+
headers?: HeadersInit | undefined;
|
|
598
|
+
} & 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<TReturn>;
|
|
599
|
+
};
|
|
600
|
+
/** Make HTTP requests with method PATCH */
|
|
601
|
+
patch: {
|
|
602
|
+
<T = unknown, TOptions extends ({
|
|
603
|
+
headers?: HeadersInit | undefined;
|
|
604
|
+
} & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
|
|
605
|
+
headers?: HeadersInit | undefined;
|
|
606
|
+
} & 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>;
|
|
607
|
+
deferred<ThisArg, Delay extends number, DefaultUrl extends PostArgs[0] | undefined, DefaultData extends PostArgs[1] = undefined, DefaultOptions extends ({
|
|
608
|
+
headers?: HeadersInit | undefined;
|
|
609
|
+
} & 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 ({
|
|
610
|
+
headers?: HeadersInit | undefined;
|
|
611
|
+
} & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = {
|
|
612
|
+
headers?: HeadersInit | undefined;
|
|
613
|
+
} & 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<TReturn>;
|
|
614
|
+
};
|
|
615
|
+
/** Make HTTP requests with method POST */
|
|
616
|
+
post: {
|
|
617
|
+
<T = unknown, TOptions extends ({
|
|
618
|
+
headers?: HeadersInit | undefined;
|
|
619
|
+
} & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
|
|
620
|
+
headers?: HeadersInit | undefined;
|
|
621
|
+
} & 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>;
|
|
622
|
+
deferred<ThisArg, Delay extends number, DefaultUrl extends PostArgs[0] | undefined, DefaultData extends PostArgs[1] = undefined, DefaultOptions extends ({
|
|
623
|
+
headers?: HeadersInit | undefined;
|
|
624
|
+
} & 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 ({
|
|
625
|
+
headers?: HeadersInit | undefined;
|
|
626
|
+
} & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = {
|
|
627
|
+
headers?: HeadersInit | undefined;
|
|
628
|
+
} & 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<TReturn>;
|
|
629
|
+
};
|
|
630
|
+
/** Make HTTP requests with method PUT */
|
|
631
|
+
put: {
|
|
632
|
+
<T = unknown, TOptions extends ({
|
|
633
|
+
headers?: HeadersInit | undefined;
|
|
634
|
+
} & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = ({
|
|
635
|
+
headers?: HeadersInit | undefined;
|
|
636
|
+
} & 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>;
|
|
637
|
+
deferred<ThisArg, Delay extends number, DefaultUrl extends PostArgs[0] | undefined, DefaultData extends PostArgs[1] = undefined, DefaultOptions extends ({
|
|
638
|
+
headers?: HeadersInit | undefined;
|
|
639
|
+
} & 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 ({
|
|
640
|
+
headers?: HeadersInit | undefined;
|
|
641
|
+
} & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined = {
|
|
642
|
+
headers?: HeadersInit | undefined;
|
|
643
|
+
} & 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<TReturn>;
|
|
644
|
+
};
|
|
523
645
|
};
|
|
524
646
|
/**
|
|
525
647
|
* A `fetch()` replacement that simplifies data fetching with automatic JSON parsing, request timeouts, retries,
|
|
@@ -562,14 +684,47 @@ type FetchWithMethods = typeof fetchResponse & {
|
|
|
562
684
|
* - FetchAs.json: `unknown`
|
|
563
685
|
* - FetchAs.text: `string`
|
|
564
686
|
* - FetchAs.response: `Response`
|
|
687
|
+
*
|
|
565
688
|
* @param url
|
|
566
689
|
* @param options (optional) Standard `fetch` options extended with {@link FetchCustomOptions}
|
|
567
|
-
* @param options.
|
|
690
|
+
* @param options.abortCtrl (optional) if not provided `AbortController` will be instantiated when `timeout` used.
|
|
691
|
+
* @param options.as (optional) (optional) specify how to parse the result. Default: {@link FetchAs.json}
|
|
692
|
+
* For raw `Response` use {@link FetchAs.response}
|
|
568
693
|
* @param options.headers (optional) request headers
|
|
569
|
-
* Default: `{ 'content-type':
|
|
694
|
+
* Default: `{ 'content-type': 'application/json' }` (or whaterver is set in the `fetch.defaults`).
|
|
695
|
+
* @param options.interceptors (optional) request interceptor callbacks. See {@link FetchInterceptors} for details.
|
|
570
696
|
* @param options.method (optional) fetch method. Default: `'get'`
|
|
697
|
+
* @param options.timeout (optional) duration in milliseconds to abort the request if it takes longer.
|
|
698
|
+
*
|
|
699
|
+
*
|
|
700
|
+
* @example Drop-in replacement for built-in fetch
|
|
701
|
+
* ```javascript
|
|
702
|
+
* import fetch from '@superutils/fetch'
|
|
703
|
+
*
|
|
704
|
+
* fetch('https://dummyjson.com/products/1')
|
|
705
|
+
* .then(response => response.json())
|
|
706
|
+
* .then(console.log, console.error)
|
|
707
|
+
* ```
|
|
571
708
|
*
|
|
572
|
-
*
|
|
709
|
+
* @example Method specific function with JSON parsing by default
|
|
710
|
+
* ```javascript
|
|
711
|
+
* import fetch from '@superutils/fetch'
|
|
712
|
+
*
|
|
713
|
+
* // no need for `response.json()` or `result.data.data` drilling
|
|
714
|
+
* fetch.get('https://dummyjson.com/products/1')
|
|
715
|
+
* .then(product => console.log(product))
|
|
716
|
+
* fetch.get('https://dummyjson.com/products/1')
|
|
717
|
+
* .then(product => console.log(product))
|
|
718
|
+
*
|
|
719
|
+
*
|
|
720
|
+
* fetch.post('https://dummyjson.com/products/add', { title: 'Product title' })
|
|
721
|
+
* .then(product => console.log(product))
|
|
722
|
+
* ```
|
|
723
|
+
*
|
|
724
|
+
*
|
|
725
|
+
* @example Set default options.
|
|
726
|
+
*
|
|
727
|
+
* Options' default values (excluding `as` and `method`) can be configured to be EFFECTIVE GLOBALLY.
|
|
573
728
|
*
|
|
574
729
|
* ```typescript
|
|
575
730
|
* import fetch from '@superutils/fetch'
|
|
@@ -593,41 +748,27 @@ type FetchWithMethods = typeof fetchResponse & {
|
|
|
593
748
|
* //........
|
|
594
749
|
* }
|
|
595
750
|
* ```
|
|
751
|
+
*/
|
|
752
|
+
declare const fetchDefault: typeof fetchResponse & typeof methods;
|
|
753
|
+
|
|
754
|
+
/**
|
|
755
|
+
* Add AbortController to options if not present and propagate external abort signal if provided.
|
|
596
756
|
*
|
|
597
|
-
* @
|
|
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}
|
|
605
|
-
*
|
|
606
|
-
* @example Drop-in replacement for built-in `fetch`
|
|
607
|
-
* ```typescript
|
|
608
|
-
* import fetch from '@superutils/fetch'
|
|
609
|
-
*
|
|
610
|
-
* fetch('https://dummyjson.com/products/1')
|
|
611
|
-
* .then(response => response.json())
|
|
612
|
-
* .then(console.log, console.error)
|
|
613
|
-
* ```
|
|
614
|
-
*
|
|
615
|
-
* @example Method specific function with JSON parsing by default
|
|
616
|
-
* ```typescript
|
|
617
|
-
* import fetch from '@superutils/fetch'
|
|
757
|
+
* @param options The fetch options object.
|
|
618
758
|
*
|
|
619
|
-
*
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
759
|
+
* @returns The AbortController instance associated with the options.
|
|
760
|
+
*/
|
|
761
|
+
declare const getAbortCtrl: (options: Partial<FetchOptions>) => AbortController;
|
|
762
|
+
|
|
763
|
+
/**
|
|
764
|
+
* Execute built-in `fetch()` and retry if request fails and `options.retry > 0`.
|
|
624
765
|
*
|
|
766
|
+
* @param url request URL
|
|
767
|
+
* @param options (optional)
|
|
625
768
|
*
|
|
626
|
-
*
|
|
627
|
-
* .then(product => console.log(product))
|
|
628
|
-
* ```
|
|
769
|
+
* @returns response
|
|
629
770
|
*/
|
|
630
|
-
declare const
|
|
771
|
+
declare const getResponse: (url: FetchArgs[0], options?: FetchArgs[1]) => Promise<Response>;
|
|
631
772
|
|
|
632
773
|
/**
|
|
633
774
|
* Merge one or more {@link FetchOptions}
|
|
@@ -647,4 +788,4 @@ declare const mergeFetchOptions: (...allOptions: FetchOptions[]) => FetchOptions
|
|
|
647
788
|
/** Merges partial fetch options ignoring empty or undefined. Otherwise, will return the first argument. */
|
|
648
789
|
declare const mergePartialOptions: (...optionsAr: (Partial<FetchOptions> | undefined)[]) => Partial<FetchOptions> | undefined;
|
|
649
790
|
|
|
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
|
|
791
|
+
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 FetchOptionsDefaults, type FetchOptionsInterceptor, type FetchResult, type FetchRetryOptions, type Interceptor, type PostArgs, type PostBody, type PostDeferredCbArgs, type PostOptions, createClient, createPostClient, fetchDefault as default, executeInterceptors, fetch, fetchResponse, getAbortCtrl, getResponse, mergeFetchOptions, mergePartialOptions };
|