@superutils/fetch 1.1.3 → 1.1.5

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 (2) hide show
  1. package/dist/index.d.ts +25 -19
  2. package/package.json +5 -5
package/dist/index.d.ts CHANGED
@@ -84,7 +84,7 @@ declare class FetchError extends Error {
84
84
  * // Interceptors can be async functions or just return a promise that resolves to the error.
85
85
  * // If the execution of the interceptor fails or promise rejects, it will be ignored.
86
86
  * // To transform the error it must directly return an error or a Promise that `resolves` with an error.
87
- * const transformError = async fetchErr => {
87
+ * const transformError = async (fetchErr, url, options) => {
88
88
  * fetchErr.message = 'Custom errormessage'
89
89
  * return Promise.resolve(fetchErr)
90
90
  * }
@@ -95,7 +95,7 @@ declare class FetchError extends Error {
95
95
  * })
96
96
  * ```
97
97
  */
98
- type FetchInterceptorError = Interceptor<FetchError, []>;
98
+ type FetchInterceptorError = Interceptor<FetchError, FetchArgsInterceptor>;
99
99
  /**
100
100
  *
101
101
  * Fetch request interceptor to be invoked before making a fetch request.
@@ -417,10 +417,10 @@ declare function fetchDeferred<ThisArg = unknown, Delay extends number = number,
417
417
  * If the same property is provided in both cases, defaults will be overriden by the callback.
418
418
  *
419
419
  *
420
+ * #### Example 1: Auto-saving form data with throttling
420
421
  *
421
- * @example Auto-saving form data with throttling
422
- * ```javascript
423
- * import { postDeferred } from '@superutils/fetch'
422
+ * ```typescript
423
+ * import fetch from '@superutils/fetch'
424
424
  * import PromisE from '@superutils/promise'
425
425
  *
426
426
  * // Create a throttled function to auto-save product updates.
@@ -431,23 +431,29 @@ declare function fetchDeferred<ThisArg = unknown, Delay extends number = number,
431
431
  * trailing: true, // Ensures the very last update is always saved
432
432
  * onResult: product => console.log(`[Saved] Product: ${product.title}`),
433
433
  * },
434
- * 'https://dummyjson.com/products/1', // Default URL
435
- * undefined, // No default data
436
- * { method: 'put' }, // Default method
434
+ * 'https://dummyjson.com/products/add', // Default URL
437
435
  * )
438
436
  * // Simulate a user typing quickly, triggering multiple saves.
439
437
  * console.log('User starts typing...')
440
- * // First call
441
- * saveProductThrottled({ title: 'iPhone' }) // Executed immediately (leading edge)
442
- * // Second call after 200ms => Ignored (within 1000ms throttle window)
438
+ *
439
+ * // Executed immediately (leading edge)
440
+ * saveProductThrottled({ title: 'iPhone' })
441
+ * // Ignored (within 1000ms throttle window)
443
442
  * PromisE.delay(200, () => saveProductThrottled({ title: 'iPhone 15' }))
444
- * // Third call 300ms after second call => Ignored
445
- * PromisE.delay(500, () => saveProductThrottled({ title: 'iPhone 15 Pro' }))
446
- * // Fourth call 400ms after third call => Queued to execute on the trailing edge
447
- * PromisE.delay(900, () => saveProductThrottled({ title: 'iPhone 15 Pro Max' }))
443
+ * // Ignored
444
+ * PromisE.delay(300, () => saveProductThrottled({ title: 'iPhone 15 Pro' }))
445
+ * // Queued to execute on the trailing edge
446
+ * PromisE.delay(400, () => saveProductThrottled({ title: 'iPhone 15 Pro Max' }))
447
+ * // Outcome:
448
+ * // The first call ('iPhone') is executed immediately.
449
+ * // The next two calls are ignored by the throttle.
450
+ * // The final call ('iPhone 15 Pro Max') is executed after the 1000ms throttle window closes,
451
+ * // thanks to `trailing: true`.
452
+ * // This results in only two network requests instead of four.
448
453
  * ```
449
454
  *
450
- * @example Advanced example: debouncing an authentication token refresh
455
+ * #### Example 2: debouncing an authentication token refresh
456
+ *
451
457
  * ```typescript
452
458
  * import fetch from '@superutils/fetch'
453
459
  * import PromisE from '@superutils/promise'
@@ -459,11 +465,11 @@ declare function fetchDeferred<ThisArg = unknown, Delay extends number = number,
459
465
  * const requestNewToken = fetch.post.deferred(
460
466
  * {
461
467
  * delayMs: 300, // debounce delay
462
- * onResult: ({ token = '' }) => {
468
+ * onResult: ({ refreshToken = '' }) => {
463
469
  * console.log(
464
470
  * `Auth token successfully refreshed at ${new Date().toISOString()}`,
465
471
  * )
466
- * currentRefreshToken = token
472
+ * currentRefreshToken = refreshToken
467
473
  * },
468
474
  * },
469
475
  * 'https://dummyjson.com/auth/refresh', // Default URL
@@ -497,7 +503,7 @@ declare function fetchDeferred<ThisArg = unknown, Delay extends number = number,
497
503
  * // The token is refreshed only once, preventing redundant network requests.
498
504
  * ```
499
505
  */
500
- declare function postDeferred<ThisArg, Delay extends number = number, DefaultUrl extends PostArgs[0] | undefined = undefined, DefaultData extends PostArgs[1] | undefined = undefined, CbArgs extends unknown[] = PostDeferredCbArgs<DefaultUrl, DefaultData, false>>(deferOptions?: DeferredAsyncOptions<ThisArg, Delay>, defaultUrl?: DefaultUrl, defaultData?: DefaultData, defaultOptions?: PostArgs[2]): <TResult = unknown>(...args: CbArgs) => _superutils_promise.IPromisE<TResult>;
506
+ declare function postDeferred<ThisArg, Delay = unknown, DefaultUrl extends PostArgs[0] | undefined = undefined, DefaultData extends PostArgs[1] | undefined = undefined, CbArgs extends unknown[] = PostDeferredCbArgs<DefaultUrl, DefaultData, false>>(deferOptions?: DeferredAsyncOptions<ThisArg, Delay>, defaultUrl?: DefaultUrl, defaultData?: DefaultData, defaultOptions?: PostArgs[2]): <TResult = unknown>(...args: CbArgs) => _superutils_promise.IPromisE<TResult>;
501
507
 
502
508
  /**
503
509
  * Merge one or more {@link FetchOptions}
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.1.1",
9
- "@superutils/promise": "^1.1.3"
8
+ "@superutils/core": "^1.1.4",
9
+ "@superutils/promise": "^1.1.4"
10
10
  },
11
11
  "files": [
12
12
  "dist",
@@ -24,8 +24,8 @@
24
24
  "main": "dist/index.js",
25
25
  "name": "@superutils/fetch",
26
26
  "peerDependencies": {
27
- "@superutils/core": "^1.1.1",
28
- "@superutils/promise": "^1.1.3"
27
+ "@superutils/core": "^1.1.4",
28
+ "@superutils/promise": "^1.1.4"
29
29
  },
30
30
  "publishConfig": {
31
31
  "access": "public"
@@ -45,5 +45,5 @@
45
45
  "sideEffects": false,
46
46
  "type": "module",
47
47
  "types": "dist/index.d.ts",
48
- "version": "1.1.3"
48
+ "version": "1.1.5"
49
49
  }