@superutils/fetch 1.1.3 → 1.1.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.d.ts +23 -17
- package/package.json +5 -5
package/dist/index.d.ts
CHANGED
|
@@ -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
|
-
*
|
|
422
|
-
*
|
|
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/
|
|
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
|
-
*
|
|
441
|
-
*
|
|
442
|
-
*
|
|
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
|
-
* //
|
|
445
|
-
* PromisE.delay(
|
|
446
|
-
* //
|
|
447
|
-
* PromisE.delay(
|
|
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
|
-
*
|
|
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: ({
|
|
468
|
+
* onResult: ({ refreshToken = '' }) => {
|
|
463
469
|
* console.log(
|
|
464
470
|
* `Auth token successfully refreshed at ${new Date().toISOString()}`,
|
|
465
471
|
* )
|
|
466
|
-
* currentRefreshToken =
|
|
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
|
|
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.
|
|
9
|
-
"@superutils/promise": "^1.1.
|
|
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.
|
|
28
|
-
"@superutils/promise": "^1.1.
|
|
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.
|
|
48
|
+
"version": "1.1.4"
|
|
49
49
|
}
|