@superutils/fetch 1.5.2 → 1.5.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -249,6 +249,11 @@ var fetch = (url, options = {}) => {
249
249
  return (0, import_promise2.timeout)(opts, async () => {
250
250
  var _a2, _b2, _c2, _d, _e;
251
251
  try {
252
+ opts.body = await (0, import_core4.fallbackIfFails)(
253
+ opts.body,
254
+ [],
255
+ (err) => Promise.reject(err)
256
+ );
252
257
  url = await executeInterceptors_default(
253
258
  url,
254
259
  abortCtrl.signal,
@@ -268,11 +273,7 @@ var fetch = (url, options = {}) => {
268
273
  const shouldStringifyBody = ["delete", "patch", "post", "put"].includes(
269
274
  `${opts.method}`.toLowerCase()
270
275
  ) && !["undefined", "string"].includes(typeof body) && (0, import_core4.isObj)(body, true) && contentType === ContentType.APPLICATION_JSON;
271
- if (shouldStringifyBody) {
272
- opts.body = JSON.stringify(
273
- (0, import_core4.isFn)(body) ? (0, import_core4.fallbackIfFails)(body, [], void 0) : body
274
- );
275
- }
276
+ if (shouldStringifyBody) opts.body = JSON.stringify(opts.body);
276
277
  }
277
278
  response = await getResponse_default(url, opts);
278
279
  response = await executeInterceptors_default(
@@ -417,7 +418,7 @@ var createPostClient = (fixedOptions, commonOptions, commonDeferOptions) => {
417
418
  // fixed options will always override other options
418
419
  );
419
420
  (_a = mergedOptions.as) != null ? _a : mergedOptions.as = "json" /* json */;
420
- mergedOptions.body = data;
421
+ mergedOptions.body = data != null ? data : mergedOptions.body;
421
422
  (_b = mergedOptions.method) != null ? _b : mergedOptions.method = "post";
422
423
  mergedOptions.fromPostClient = true;
423
424
  return fetch_default(url, mergedOptions);
package/dist/index.d.cts CHANGED
@@ -1,7 +1,8 @@
1
+ import * as _superutils_core from '@superutils/core';
2
+ import { ValueOrPromise, DropFirst } from '@superutils/core';
1
3
  import * as _superutils_promise from '@superutils/promise';
2
4
  import { RetryOptions, RetryIfFunc, TimeoutOptions, IPromisE_Timeout, DeferredAsyncOptions } from '@superutils/promise';
3
5
  export { DeferredAsyncOptions, OnEarlyFinalize, OnFinalize, ResolveError, ResolveIgnored, RetryIfFunc, RetryOptions, TIMEOUT_FALLBACK, TIMEOUT_MAX, TimeoutOptions, TimeoutPromise } from '@superutils/promise';
4
- import { ValueOrPromise, DropFirst } from '@superutils/core';
5
6
 
6
7
  /** Commonly used content types for easier access */
7
8
  declare const ContentType: {
@@ -51,7 +52,7 @@ type Interceptor<T, TArgs extends unknown[]> = (...args: [value: T, ...TArgs]) =
51
52
  *
52
53
  * // not returning anything or returning undefined will avoid transforming the error.
53
54
  * const logError = fetchErr => console.log(fetchErr)
54
- * const result = await fetch.get('https://dummyjson.com/http/400', {
55
+ * const result = await fetch.get('[DUMMYJSON-DOT-COM]/http/400', {
55
56
  * interceptors: {
56
57
  * error: [logError]
57
58
  * }
@@ -70,7 +71,7 @@ type Interceptor<T, TArgs extends unknown[]> = (...args: [value: T, ...TArgs]) =
70
71
  * fetchErr.message = 'Custom errormessage'
71
72
  * return Promise.resolve(fetchErr)
72
73
  * }
73
- * const result = await fetch.get('https://dummyjson.com/http/400', {
74
+ * const result = await fetch.get('[DUMMYJSON-DOT-COM]/http/400', {
74
75
  * interceptors: {
75
76
  * error: [transformError]
76
77
  * }
@@ -94,7 +95,7 @@ type FetchInterceptorError = Interceptor<FetchError, FetchArgsInterceptor>;
94
95
  * const includeAuthToken = (url, options) => {
95
96
  * options.headers.set('x-auth-token', 'my-auth-token')
96
97
  * }
97
- * const result = await fetch.get('https://dummyjson.com/products', {
98
+ * const result = await fetch.get('[DUMMYJSON-DOT-COM]/products', {
98
99
  * method: 'post',
99
100
  * interceptors: {
100
101
  * result: [apiV1ToV2, includeAuthToken]
@@ -120,12 +121,12 @@ type FetchInterceptorRequest = Interceptor<FetchArgs[0], [
120
121
  * // just a hypothetical scenario ;)
121
122
  * const getUser = async response => {
122
123
  * const authResult = await response.json()
123
- * const userDetails = await fetch.get('https://dummyjson.com/users/1')
124
+ * const userDetails = await fetch.get('[DUMMYJSON-DOT-COM]/users/1')
124
125
  * const userAuth = { ...userDetails, ...authResult }
125
126
  * return new Response(JSON.stringify(userAuth))
126
127
  * }
127
128
  * const user = await fetch.post(
128
- * 'https://dummyjson.com/user/login',
129
+ * '[DUMMYJSON-DOT-COM]/user/login',
129
130
  * { // data/request body
130
131
  * username: 'emilys',
131
132
  * password: 'emilyspass',
@@ -178,7 +179,7 @@ type FetchInterceptorResponse = Interceptor<Response, FetchArgsInterceptor>;
178
179
  * )
179
180
  * }
180
181
  * // now we make the actaul fetch request
181
- * const result = await fetch.get('https://dummyjson.com/users/1', {
182
+ * const result = await fetch.get('[DUMMYJSON-DOT-COM]/users/1', {
182
183
  * interceptors: {
183
184
  * result: [
184
185
  * ensureBalanceHex,
@@ -285,7 +286,7 @@ type FetchCustomOptions = {
285
286
  * - Use `abortCtrl` instead of `signal` to prevent creating internal `AbortController` instance.
286
287
  */
287
288
  abortCtrl?: AbortController;
288
- body?: PostBody | (() => PostBody);
289
+ body?: PostArgs[1];
289
290
  /**
290
291
  * Custom fetch function to use instead of the global `fetch`.
291
292
  * Useful for testing or using a different fetch implementation (e.g. `node-fetch` in older Node versions).
@@ -325,7 +326,7 @@ type FetchFunc = (...args: FetchArgs) => Promise<Response>;
325
326
  */
326
327
  type FetchOptions = Omit<RequestInit, 'body'> & FetchCustomOptions;
327
328
  /** Default fetch options */
328
- type FetchOptionsDefault = Omit<FetchOptionsInterceptor, 'abortCtrl' | 'as' | 'method' | 'signal' | 'timeout' | 'headers'> & {
329
+ type FetchOptionsDefault = Omit<FetchOptionsInterceptor, 'abortCtrl' | 'as' | 'body' | 'method' | 'signal' | 'timeout' | 'headers'> & {
329
330
  /**
330
331
  * Request headers.
331
332
  *
@@ -349,8 +350,9 @@ type FetchOptionsDefault = Omit<FetchOptionsInterceptor, 'abortCtrl' | 'as' | 'm
349
350
  * Fetch options available to interceptors.
350
351
  *
351
352
  */
352
- type FetchOptionsInterceptor = Omit<FetchOptions, 'as' | 'errMsgs' | 'interceptors' | 'headers' | 'timeout' | keyof FetchRetryOptions> & {
353
+ type FetchOptionsInterceptor = Omit<FetchOptions, 'as' | 'body' | 'errMsgs' | 'interceptors' | 'headers' | 'timeout' | keyof FetchRetryOptions> & {
353
354
  as: FetchAs;
355
+ body: PostBody;
354
356
  /** Error messages */
355
357
  errMsgs: Required<FetchErrMsgs>;
356
358
  headers: Headers;
@@ -388,7 +390,14 @@ type FetchRetryOptions = Omit<Partial<RetryOptions>, 'retry' | 'retryIf'> & {
388
390
  type PostBody = Record<string, unknown> | BodyInit | null;
389
391
  type PostArgs = [
390
392
  url: string | URL,
391
- data?: PostBody | (() => PostBody),
393
+ /**
394
+ * Post body or a function that returns/resolves post body.
395
+ *
396
+ * PS:
397
+ * - if function provided, it will be executed before executing any request interceptors
398
+ * - if function execution fails it will throw an error and avoid making the fetch request
399
+ */
400
+ data?: PostBody | (() => ValueOrPromise<PostBody>),
392
401
  options?: PostOptions
393
402
  ];
394
403
  /**
@@ -480,7 +489,7 @@ type ClientData<FixedOptions> = ExtractAs<[FixedOptions]> extends FetchAs.json ?
480
489
  * )
481
490
  *
482
491
  * // Use it just like the standard fetch
483
- * apiClient('https://dummyjson.com/products/1', {
492
+ * apiClient('[DUMMYJSON-DOT-COM]/products/1', {
484
493
  * // The 'method' property cannot be overridden as it is used in the fixed options when creating the client.
485
494
  * // In TypeScript, the compiler will not allow this property.
486
495
  * // In Javascript, it will simply be ignored.
@@ -491,7 +500,7 @@ type ClientData<FixedOptions> = ExtractAs<[FixedOptions]> extends FetchAs.json ?
491
500
  * // create a deferred client using "apiClient"
492
501
  * const deferredClient = apiClient.deferred(
493
502
  * { retry: 0 }, // disable retrying by overriding the `retry` defer option
494
- * 'https://dummyjson.com/products/1',
503
+ * '[DUMMYJSON-DOT-COM]/products/1',
495
504
  * { timeout: 3000 },
496
505
  * )
497
506
  * deferredClient({ timeout: 10000 }) // timeout is overridden by individual request
@@ -533,7 +542,7 @@ commonOptions?: FetchOptions & CommonOptions, commonDeferOptions?: DeferredAsync
533
542
  *
534
543
  * // Invoking `postClient()` automatically applies the pre-configured options
535
544
  * postClient(
536
- * 'https://dummyjson.com/products/add',
545
+ * '[DUMMYJSON-DOT-COM]/products/add',
537
546
  * { title: 'New Product' }, // data/body
538
547
  * {}, // other options
539
548
  * ).then(console.log)
@@ -544,7 +553,7 @@ commonOptions?: FetchOptions & CommonOptions, commonDeferOptions?: DeferredAsync
544
553
  * delay: 300, // debounce duration
545
554
  * onResult: console.log, // prints only successful results
546
555
  * },
547
- * 'https://dummyjson.com/products/add',
556
+ * '[DUMMYJSON-DOT-COM]/products/add',
548
557
  * { method: 'patch', timeout: 3000 },
549
558
  * )
550
559
  * updateProduct({ title: 'New title 1' }) // ignored by debounce
@@ -592,7 +601,7 @@ declare const executeInterceptors: <T, TArgs extends unknown[]>(value: T, signal
592
601
  * import { fetch } from '@superutils/fetch'
593
602
  *
594
603
  * // no need for `response.json()` or `result.data.data` drilling
595
- * fetch.get('https://dummyjson.com/products/1')
604
+ * fetch.get('[DUMMYJSON-DOT-COM]/products/1')
596
605
  * .then(product => console.log(product))
597
606
  * ```
598
607
  */
@@ -716,7 +725,7 @@ declare const methods: {
716
725
  method: "delete";
717
726
  }, Options, DefaultOptions, ({
718
727
  headers?: HeadersInit | undefined;
719
- } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined], T>>(...args: PostDeferredCbArgs<DefaultUrl, DefaultData, Options, [url: string | URL, data: PostBody | (() => PostBody), options: PostOptions], undefined extends DefaultUrl ? DefaultUrl & undefined : DefaultUrl, undefined extends DefaultData ? DefaultData & undefined : DefaultData>) => IPromise_Fetch<TReturn>;
728
+ } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined], T>>(...args: PostDeferredCbArgs<DefaultUrl, DefaultData, Options, [url: string | URL, data: PostBody | (() => _superutils_core.ValueOrPromise<PostBody>), options: PostOptions], undefined extends DefaultUrl ? DefaultUrl & undefined : DefaultUrl, undefined extends DefaultData ? DefaultData & undefined : DefaultData>) => IPromise_Fetch<TReturn>;
720
729
  };
721
730
  /** Make HTTP requests with method PATCH */
722
731
  patch: {
@@ -741,7 +750,7 @@ declare const methods: {
741
750
  method: "patch";
742
751
  }, Options, DefaultOptions, ({
743
752
  headers?: HeadersInit | undefined;
744
- } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined], T>>(...args: PostDeferredCbArgs<DefaultUrl, DefaultData, Options, [url: string | URL, data: PostBody | (() => PostBody), options: PostOptions], undefined extends DefaultUrl ? DefaultUrl & undefined : DefaultUrl, undefined extends DefaultData ? DefaultData & undefined : DefaultData>) => IPromise_Fetch<TReturn>;
753
+ } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined], T>>(...args: PostDeferredCbArgs<DefaultUrl, DefaultData, Options, [url: string | URL, data: PostBody | (() => _superutils_core.ValueOrPromise<PostBody>), options: PostOptions], undefined extends DefaultUrl ? DefaultUrl & undefined : DefaultUrl, undefined extends DefaultData ? DefaultData & undefined : DefaultData>) => IPromise_Fetch<TReturn>;
745
754
  };
746
755
  /** Make HTTP requests with method POST */
747
756
  post: {
@@ -766,7 +775,7 @@ declare const methods: {
766
775
  method: "post";
767
776
  }, Options, DefaultOptions, ({
768
777
  headers?: HeadersInit | undefined;
769
- } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined], T>>(...args: PostDeferredCbArgs<DefaultUrl, DefaultData, Options, [url: string | URL, data: PostBody | (() => PostBody), options: PostOptions], undefined extends DefaultUrl ? DefaultUrl & undefined : DefaultUrl, undefined extends DefaultData ? DefaultData & undefined : DefaultData>) => IPromise_Fetch<TReturn>;
778
+ } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined], T>>(...args: PostDeferredCbArgs<DefaultUrl, DefaultData, Options, [url: string | URL, data: PostBody | (() => _superutils_core.ValueOrPromise<PostBody>), options: PostOptions], undefined extends DefaultUrl ? DefaultUrl & undefined : DefaultUrl, undefined extends DefaultData ? DefaultData & undefined : DefaultData>) => IPromise_Fetch<TReturn>;
770
779
  };
771
780
  /** Make HTTP requests with method PUT */
772
781
  put: {
@@ -791,7 +800,7 @@ declare const methods: {
791
800
  method: "put";
792
801
  }, Options, DefaultOptions, ({
793
802
  headers?: HeadersInit | undefined;
794
- } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined], T>>(...args: PostDeferredCbArgs<DefaultUrl, DefaultData, Options, [url: string | URL, data: PostBody | (() => PostBody), options: PostOptions], undefined extends DefaultUrl ? DefaultUrl & undefined : DefaultUrl, undefined extends DefaultData ? DefaultData & undefined : DefaultData>) => IPromise_Fetch<TReturn>;
803
+ } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined], T>>(...args: PostDeferredCbArgs<DefaultUrl, DefaultData, Options, [url: string | URL, data: PostBody | (() => _superutils_core.ValueOrPromise<PostBody>), options: PostOptions], undefined extends DefaultUrl ? DefaultUrl & undefined : DefaultUrl, undefined extends DefaultData ? DefaultData & undefined : DefaultData>) => IPromise_Fetch<TReturn>;
795
804
  };
796
805
  };
797
806
  /**
@@ -868,7 +877,7 @@ declare const methods: {
868
877
  * ```javascript
869
878
  * import fetch from '@superutils/fetch'
870
879
  *
871
- * fetch('https://dummyjson.com/products/1')
880
+ * fetch('[DUMMYJSON-DOT-COM]/products/1')
872
881
  * .then(response => response.json())
873
882
  * .then(console.log, console.error)
874
883
  * ```
@@ -879,9 +888,9 @@ declare const methods: {
879
888
  * import fetch from '@superutils/fetch'
880
889
  *
881
890
  * // no need for `response.json()` or `result.data.data` drilling
882
- * fetch.get('https://dummyjson.com/products/1')
891
+ * fetch.get('[DUMMYJSON-DOT-COM]/products/1')
883
892
  * .then(product => console.log(product))
884
- * fetch.post('https://dummyjson.com/products/add', { title: 'Product title' })
893
+ * fetch.post('[DUMMYJSON-DOT-COM]/products/add', { title: 'Product title' })
885
894
  * .then(product => console.log(product))
886
895
  * ```
887
896
  *
package/dist/index.d.ts CHANGED
@@ -1,7 +1,8 @@
1
+ import * as _superutils_core from '@superutils/core';
2
+ import { ValueOrPromise, DropFirst } from '@superutils/core';
1
3
  import * as _superutils_promise from '@superutils/promise';
2
4
  import { RetryOptions, RetryIfFunc, TimeoutOptions, IPromisE_Timeout, DeferredAsyncOptions } from '@superutils/promise';
3
5
  export { DeferredAsyncOptions, OnEarlyFinalize, OnFinalize, ResolveError, ResolveIgnored, RetryIfFunc, RetryOptions, TIMEOUT_FALLBACK, TIMEOUT_MAX, TimeoutOptions, TimeoutPromise } from '@superutils/promise';
4
- import { ValueOrPromise, DropFirst } from '@superutils/core';
5
6
 
6
7
  /** Commonly used content types for easier access */
7
8
  declare const ContentType: {
@@ -51,7 +52,7 @@ type Interceptor<T, TArgs extends unknown[]> = (...args: [value: T, ...TArgs]) =
51
52
  *
52
53
  * // not returning anything or returning undefined will avoid transforming the error.
53
54
  * const logError = fetchErr => console.log(fetchErr)
54
- * const result = await fetch.get('https://dummyjson.com/http/400', {
55
+ * const result = await fetch.get('[DUMMYJSON-DOT-COM]/http/400', {
55
56
  * interceptors: {
56
57
  * error: [logError]
57
58
  * }
@@ -70,7 +71,7 @@ type Interceptor<T, TArgs extends unknown[]> = (...args: [value: T, ...TArgs]) =
70
71
  * fetchErr.message = 'Custom errormessage'
71
72
  * return Promise.resolve(fetchErr)
72
73
  * }
73
- * const result = await fetch.get('https://dummyjson.com/http/400', {
74
+ * const result = await fetch.get('[DUMMYJSON-DOT-COM]/http/400', {
74
75
  * interceptors: {
75
76
  * error: [transformError]
76
77
  * }
@@ -94,7 +95,7 @@ type FetchInterceptorError = Interceptor<FetchError, FetchArgsInterceptor>;
94
95
  * const includeAuthToken = (url, options) => {
95
96
  * options.headers.set('x-auth-token', 'my-auth-token')
96
97
  * }
97
- * const result = await fetch.get('https://dummyjson.com/products', {
98
+ * const result = await fetch.get('[DUMMYJSON-DOT-COM]/products', {
98
99
  * method: 'post',
99
100
  * interceptors: {
100
101
  * result: [apiV1ToV2, includeAuthToken]
@@ -120,12 +121,12 @@ type FetchInterceptorRequest = Interceptor<FetchArgs[0], [
120
121
  * // just a hypothetical scenario ;)
121
122
  * const getUser = async response => {
122
123
  * const authResult = await response.json()
123
- * const userDetails = await fetch.get('https://dummyjson.com/users/1')
124
+ * const userDetails = await fetch.get('[DUMMYJSON-DOT-COM]/users/1')
124
125
  * const userAuth = { ...userDetails, ...authResult }
125
126
  * return new Response(JSON.stringify(userAuth))
126
127
  * }
127
128
  * const user = await fetch.post(
128
- * 'https://dummyjson.com/user/login',
129
+ * '[DUMMYJSON-DOT-COM]/user/login',
129
130
  * { // data/request body
130
131
  * username: 'emilys',
131
132
  * password: 'emilyspass',
@@ -178,7 +179,7 @@ type FetchInterceptorResponse = Interceptor<Response, FetchArgsInterceptor>;
178
179
  * )
179
180
  * }
180
181
  * // now we make the actaul fetch request
181
- * const result = await fetch.get('https://dummyjson.com/users/1', {
182
+ * const result = await fetch.get('[DUMMYJSON-DOT-COM]/users/1', {
182
183
  * interceptors: {
183
184
  * result: [
184
185
  * ensureBalanceHex,
@@ -285,7 +286,7 @@ type FetchCustomOptions = {
285
286
  * - Use `abortCtrl` instead of `signal` to prevent creating internal `AbortController` instance.
286
287
  */
287
288
  abortCtrl?: AbortController;
288
- body?: PostBody | (() => PostBody);
289
+ body?: PostArgs[1];
289
290
  /**
290
291
  * Custom fetch function to use instead of the global `fetch`.
291
292
  * Useful for testing or using a different fetch implementation (e.g. `node-fetch` in older Node versions).
@@ -325,7 +326,7 @@ type FetchFunc = (...args: FetchArgs) => Promise<Response>;
325
326
  */
326
327
  type FetchOptions = Omit<RequestInit, 'body'> & FetchCustomOptions;
327
328
  /** Default fetch options */
328
- type FetchOptionsDefault = Omit<FetchOptionsInterceptor, 'abortCtrl' | 'as' | 'method' | 'signal' | 'timeout' | 'headers'> & {
329
+ type FetchOptionsDefault = Omit<FetchOptionsInterceptor, 'abortCtrl' | 'as' | 'body' | 'method' | 'signal' | 'timeout' | 'headers'> & {
329
330
  /**
330
331
  * Request headers.
331
332
  *
@@ -349,8 +350,9 @@ type FetchOptionsDefault = Omit<FetchOptionsInterceptor, 'abortCtrl' | 'as' | 'm
349
350
  * Fetch options available to interceptors.
350
351
  *
351
352
  */
352
- type FetchOptionsInterceptor = Omit<FetchOptions, 'as' | 'errMsgs' | 'interceptors' | 'headers' | 'timeout' | keyof FetchRetryOptions> & {
353
+ type FetchOptionsInterceptor = Omit<FetchOptions, 'as' | 'body' | 'errMsgs' | 'interceptors' | 'headers' | 'timeout' | keyof FetchRetryOptions> & {
353
354
  as: FetchAs;
355
+ body: PostBody;
354
356
  /** Error messages */
355
357
  errMsgs: Required<FetchErrMsgs>;
356
358
  headers: Headers;
@@ -388,7 +390,14 @@ type FetchRetryOptions = Omit<Partial<RetryOptions>, 'retry' | 'retryIf'> & {
388
390
  type PostBody = Record<string, unknown> | BodyInit | null;
389
391
  type PostArgs = [
390
392
  url: string | URL,
391
- data?: PostBody | (() => PostBody),
393
+ /**
394
+ * Post body or a function that returns/resolves post body.
395
+ *
396
+ * PS:
397
+ * - if function provided, it will be executed before executing any request interceptors
398
+ * - if function execution fails it will throw an error and avoid making the fetch request
399
+ */
400
+ data?: PostBody | (() => ValueOrPromise<PostBody>),
392
401
  options?: PostOptions
393
402
  ];
394
403
  /**
@@ -480,7 +489,7 @@ type ClientData<FixedOptions> = ExtractAs<[FixedOptions]> extends FetchAs.json ?
480
489
  * )
481
490
  *
482
491
  * // Use it just like the standard fetch
483
- * apiClient('https://dummyjson.com/products/1', {
492
+ * apiClient('[DUMMYJSON-DOT-COM]/products/1', {
484
493
  * // The 'method' property cannot be overridden as it is used in the fixed options when creating the client.
485
494
  * // In TypeScript, the compiler will not allow this property.
486
495
  * // In Javascript, it will simply be ignored.
@@ -491,7 +500,7 @@ type ClientData<FixedOptions> = ExtractAs<[FixedOptions]> extends FetchAs.json ?
491
500
  * // create a deferred client using "apiClient"
492
501
  * const deferredClient = apiClient.deferred(
493
502
  * { retry: 0 }, // disable retrying by overriding the `retry` defer option
494
- * 'https://dummyjson.com/products/1',
503
+ * '[DUMMYJSON-DOT-COM]/products/1',
495
504
  * { timeout: 3000 },
496
505
  * )
497
506
  * deferredClient({ timeout: 10000 }) // timeout is overridden by individual request
@@ -533,7 +542,7 @@ commonOptions?: FetchOptions & CommonOptions, commonDeferOptions?: DeferredAsync
533
542
  *
534
543
  * // Invoking `postClient()` automatically applies the pre-configured options
535
544
  * postClient(
536
- * 'https://dummyjson.com/products/add',
545
+ * '[DUMMYJSON-DOT-COM]/products/add',
537
546
  * { title: 'New Product' }, // data/body
538
547
  * {}, // other options
539
548
  * ).then(console.log)
@@ -544,7 +553,7 @@ commonOptions?: FetchOptions & CommonOptions, commonDeferOptions?: DeferredAsync
544
553
  * delay: 300, // debounce duration
545
554
  * onResult: console.log, // prints only successful results
546
555
  * },
547
- * 'https://dummyjson.com/products/add',
556
+ * '[DUMMYJSON-DOT-COM]/products/add',
548
557
  * { method: 'patch', timeout: 3000 },
549
558
  * )
550
559
  * updateProduct({ title: 'New title 1' }) // ignored by debounce
@@ -592,7 +601,7 @@ declare const executeInterceptors: <T, TArgs extends unknown[]>(value: T, signal
592
601
  * import { fetch } from '@superutils/fetch'
593
602
  *
594
603
  * // no need for `response.json()` or `result.data.data` drilling
595
- * fetch.get('https://dummyjson.com/products/1')
604
+ * fetch.get('[DUMMYJSON-DOT-COM]/products/1')
596
605
  * .then(product => console.log(product))
597
606
  * ```
598
607
  */
@@ -716,7 +725,7 @@ declare const methods: {
716
725
  method: "delete";
717
726
  }, Options, DefaultOptions, ({
718
727
  headers?: HeadersInit | undefined;
719
- } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined], T>>(...args: PostDeferredCbArgs<DefaultUrl, DefaultData, Options, [url: string | URL, data: PostBody | (() => PostBody), options: PostOptions], undefined extends DefaultUrl ? DefaultUrl & undefined : DefaultUrl, undefined extends DefaultData ? DefaultData & undefined : DefaultData>) => IPromise_Fetch<TReturn>;
728
+ } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined], T>>(...args: PostDeferredCbArgs<DefaultUrl, DefaultData, Options, [url: string | URL, data: PostBody | (() => _superutils_core.ValueOrPromise<PostBody>), options: PostOptions], undefined extends DefaultUrl ? DefaultUrl & undefined : DefaultUrl, undefined extends DefaultData ? DefaultData & undefined : DefaultData>) => IPromise_Fetch<TReturn>;
720
729
  };
721
730
  /** Make HTTP requests with method PATCH */
722
731
  patch: {
@@ -741,7 +750,7 @@ declare const methods: {
741
750
  method: "patch";
742
751
  }, Options, DefaultOptions, ({
743
752
  headers?: HeadersInit | undefined;
744
- } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined], T>>(...args: PostDeferredCbArgs<DefaultUrl, DefaultData, Options, [url: string | URL, data: PostBody | (() => PostBody), options: PostOptions], undefined extends DefaultUrl ? DefaultUrl & undefined : DefaultUrl, undefined extends DefaultData ? DefaultData & undefined : DefaultData>) => IPromise_Fetch<TReturn>;
753
+ } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined], T>>(...args: PostDeferredCbArgs<DefaultUrl, DefaultData, Options, [url: string | URL, data: PostBody | (() => _superutils_core.ValueOrPromise<PostBody>), options: PostOptions], undefined extends DefaultUrl ? DefaultUrl & undefined : DefaultUrl, undefined extends DefaultData ? DefaultData & undefined : DefaultData>) => IPromise_Fetch<TReturn>;
745
754
  };
746
755
  /** Make HTTP requests with method POST */
747
756
  post: {
@@ -766,7 +775,7 @@ declare const methods: {
766
775
  method: "post";
767
776
  }, Options, DefaultOptions, ({
768
777
  headers?: HeadersInit | undefined;
769
- } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined], T>>(...args: PostDeferredCbArgs<DefaultUrl, DefaultData, Options, [url: string | URL, data: PostBody | (() => PostBody), options: PostOptions], undefined extends DefaultUrl ? DefaultUrl & undefined : DefaultUrl, undefined extends DefaultData ? DefaultData & undefined : DefaultData>) => IPromise_Fetch<TReturn>;
778
+ } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined], T>>(...args: PostDeferredCbArgs<DefaultUrl, DefaultData, Options, [url: string | URL, data: PostBody | (() => _superutils_core.ValueOrPromise<PostBody>), options: PostOptions], undefined extends DefaultUrl ? DefaultUrl & undefined : DefaultUrl, undefined extends DefaultData ? DefaultData & undefined : DefaultData>) => IPromise_Fetch<TReturn>;
770
779
  };
771
780
  /** Make HTTP requests with method PUT */
772
781
  put: {
@@ -791,7 +800,7 @@ declare const methods: {
791
800
  method: "put";
792
801
  }, Options, DefaultOptions, ({
793
802
  headers?: HeadersInit | undefined;
794
- } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined], T>>(...args: PostDeferredCbArgs<DefaultUrl, DefaultData, Options, [url: string | URL, data: PostBody | (() => PostBody), options: PostOptions], undefined extends DefaultUrl ? DefaultUrl & undefined : DefaultUrl, undefined extends DefaultData ? DefaultData & undefined : DefaultData>) => IPromise_Fetch<TReturn>;
803
+ } & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined], T>>(...args: PostDeferredCbArgs<DefaultUrl, DefaultData, Options, [url: string | URL, data: PostBody | (() => _superutils_core.ValueOrPromise<PostBody>), options: PostOptions], undefined extends DefaultUrl ? DefaultUrl & undefined : DefaultUrl, undefined extends DefaultData ? DefaultData & undefined : DefaultData>) => IPromise_Fetch<TReturn>;
795
804
  };
796
805
  };
797
806
  /**
@@ -868,7 +877,7 @@ declare const methods: {
868
877
  * ```javascript
869
878
  * import fetch from '@superutils/fetch'
870
879
  *
871
- * fetch('https://dummyjson.com/products/1')
880
+ * fetch('[DUMMYJSON-DOT-COM]/products/1')
872
881
  * .then(response => response.json())
873
882
  * .then(console.log, console.error)
874
883
  * ```
@@ -879,9 +888,9 @@ declare const methods: {
879
888
  * import fetch from '@superutils/fetch'
880
889
  *
881
890
  * // no need for `response.json()` or `result.data.data` drilling
882
- * fetch.get('https://dummyjson.com/products/1')
891
+ * fetch.get('[DUMMYJSON-DOT-COM]/products/1')
883
892
  * .then(product => console.log(product))
884
- * fetch.post('https://dummyjson.com/products/add', { title: 'Product title' })
893
+ * fetch.post('[DUMMYJSON-DOT-COM]/products/add', { title: 'Product title' })
885
894
  * .then(product => console.log(product))
886
895
  * ```
887
896
  *
package/dist/index.js CHANGED
@@ -225,6 +225,11 @@ var fetch = (url, options = {}) => {
225
225
  return PromisE_timeout(opts, async () => {
226
226
  var _a2, _b2, _c2, _d, _e;
227
227
  try {
228
+ opts.body = await fallbackIfFails3(
229
+ opts.body,
230
+ [],
231
+ (err) => Promise.reject(err)
232
+ );
228
233
  url = await executeInterceptors_default(
229
234
  url,
230
235
  abortCtrl.signal,
@@ -244,11 +249,7 @@ var fetch = (url, options = {}) => {
244
249
  const shouldStringifyBody = ["delete", "patch", "post", "put"].includes(
245
250
  `${opts.method}`.toLowerCase()
246
251
  ) && !["undefined", "string"].includes(typeof body) && isObj2(body, true) && contentType === ContentType.APPLICATION_JSON;
247
- if (shouldStringifyBody) {
248
- opts.body = JSON.stringify(
249
- isFn4(body) ? fallbackIfFails3(body, [], void 0) : body
250
- );
251
- }
252
+ if (shouldStringifyBody) opts.body = JSON.stringify(opts.body);
252
253
  }
253
254
  response = await getResponse_default(url, opts);
254
255
  response = await executeInterceptors_default(
@@ -393,7 +394,7 @@ var createPostClient = (fixedOptions, commonOptions, commonDeferOptions) => {
393
394
  // fixed options will always override other options
394
395
  );
395
396
  (_a = mergedOptions.as) != null ? _a : mergedOptions.as = "json" /* json */;
396
- mergedOptions.body = data;
397
+ mergedOptions.body = data != null ? data : mergedOptions.body;
397
398
  (_b = mergedOptions.method) != null ? _b : mergedOptions.method = "post";
398
399
  mergedOptions.fromPostClient = true;
399
400
  return fetch_default(url, mergedOptions);
package/package.json CHANGED
@@ -5,8 +5,8 @@
5
5
  },
6
6
  "description": "A lightweight `fetch` wrapper for browsers and Node.js, designed to simplify data fetching and reduce boilerplate.",
7
7
  "dependencies": {
8
- "@superutils/core": "^1.2.4",
9
- "@superutils/promise": "^1.3.2"
8
+ "@superutils/core": "^1.2.6",
9
+ "@superutils/promise": "^1.3.4"
10
10
  },
11
11
  "files": [
12
12
  "dist",
@@ -53,6 +53,6 @@
53
53
  "module": "./dist/index.js",
54
54
  "type": "module",
55
55
  "types": "./dist/index.d.ts",
56
- "version": "1.5.2",
57
- "gitHead": "bdecb6b7e72557ac91be0b4e2b2cf47dc8dc15eb"
56
+ "version": "1.5.4",
57
+ "gitHead": "3635cab194aaa64c633839fdaab5b243a07d0f66"
58
58
  }