@ahoo-wang/fetcher-react 3.1.5 → 3.1.8
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 +35 -5
- package/dist/core/index.d.ts +1 -0
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/useDebouncedExecutePromise.d.ts.map +1 -1
- package/dist/core/useExecutePromise.d.ts +86 -20
- package/dist/core/useExecutePromise.d.ts.map +1 -1
- package/dist/core/useForceUpdate.d.ts +31 -0
- package/dist/core/useForceUpdate.d.ts.map +1 -0
- package/dist/fetcher/useDebouncedFetcher.d.ts.map +1 -1
- package/dist/fetcher/useFetcher.d.ts +12 -9
- package/dist/fetcher/useFetcher.d.ts.map +1 -1
- package/dist/index.es.js +303 -276
- package/dist/index.es.js.map +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/index.umd.js.map +1 -1
- package/dist/wow/debounce/useDebouncedQuery.d.ts.map +1 -1
- package/dist/wow/useQuery.d.ts +2 -2
- package/dist/wow/useQuery.d.ts.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -87,17 +87,25 @@ function App() {
|
|
|
87
87
|
### useFetcher Hook
|
|
88
88
|
|
|
89
89
|
The `useFetcher` hook provides complete data fetching capabilities with automatic state management, race condition
|
|
90
|
-
protection, and flexible configuration options.
|
|
90
|
+
protection, and flexible configuration options. It includes built-in AbortController support inherited from `useExecutePromise`.
|
|
91
91
|
|
|
92
92
|
```typescript jsx
|
|
93
93
|
import { useFetcher } from '@ahoo-wang/fetcher-react';
|
|
94
94
|
|
|
95
95
|
const MyComponent = () => {
|
|
96
|
-
const { loading, error, result, execute } = useFetcher<string>(
|
|
96
|
+
const { loading, error, result, execute, abort } = useFetcher<string>({
|
|
97
|
+
onAbort: () => {
|
|
98
|
+
console.log('Fetch operation was aborted');
|
|
99
|
+
}
|
|
100
|
+
});
|
|
97
101
|
|
|
98
102
|
const handleFetch = () => {
|
|
99
103
|
execute({ url: '/api/users', method: 'GET' });
|
|
100
|
-
};
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const handleAbort = () => {
|
|
107
|
+
abort(); // Cancel the current fetch operation
|
|
108
|
+
};
|
|
101
109
|
```
|
|
102
110
|
|
|
103
111
|
#### Auto Execute Example
|
|
@@ -261,13 +269,17 @@ const SearchInput = () => {
|
|
|
261
269
|
### useExecutePromise Hook
|
|
262
270
|
|
|
263
271
|
The `useExecutePromise` hook manages asynchronous operations with automatic state handling, built-in race condition
|
|
264
|
-
protection, and support for promise state options.
|
|
272
|
+
protection, and support for promise state options. It includes automatic AbortController support for canceling operations.
|
|
265
273
|
|
|
266
274
|
```typescript jsx
|
|
267
275
|
import { useExecutePromise } from '@ahoo-wang/fetcher-react';
|
|
268
276
|
|
|
269
277
|
const MyComponent = () => {
|
|
270
|
-
const { loading, result, error, execute, reset } = useExecutePromise<string>(
|
|
278
|
+
const { loading, result, error, execute, reset, abort } = useExecutePromise<string>({
|
|
279
|
+
onAbort: () => {
|
|
280
|
+
console.log('Operation was aborted');
|
|
281
|
+
}
|
|
282
|
+
});
|
|
271
283
|
|
|
272
284
|
const fetchData = async () => {
|
|
273
285
|
const response = await fetch('/api/data');
|
|
@@ -283,12 +295,17 @@ const MyComponent = () => {
|
|
|
283
295
|
execute(promise); // Using a direct promise
|
|
284
296
|
};
|
|
285
297
|
|
|
298
|
+
const handleAbort = () => {
|
|
299
|
+
abort(); // Manually abort the current operation
|
|
300
|
+
};
|
|
301
|
+
|
|
286
302
|
if (loading) return <div>Loading...</div>;
|
|
287
303
|
if (error) return <div>Error: {error.message}</div>;
|
|
288
304
|
return (
|
|
289
305
|
<div>
|
|
290
306
|
<button onClick={handleFetch}>Fetch with Supplier</button>
|
|
291
307
|
<button onClick={handleDirectPromise}>Fetch with Promise</button>
|
|
308
|
+
<button onClick={handleAbort} disabled={!loading}>Abort</button>
|
|
292
309
|
<button onClick={reset}>Reset</button>
|
|
293
310
|
{result && <p>{result}</p>}
|
|
294
311
|
</div>
|
|
@@ -296,6 +313,15 @@ const MyComponent = () => {
|
|
|
296
313
|
};
|
|
297
314
|
```
|
|
298
315
|
|
|
316
|
+
#### Abort Controller Support
|
|
317
|
+
|
|
318
|
+
The hook automatically creates an AbortController for each operation and provides methods to manage cancellation:
|
|
319
|
+
|
|
320
|
+
- **Automatic Cleanup**: Operations are automatically aborted when the component unmounts
|
|
321
|
+
- **Manual Abort**: Use the `abort()` method to cancel ongoing operations
|
|
322
|
+
- **onAbort Callback**: Configure a callback that fires when an operation is aborted (manually or automatically)
|
|
323
|
+
- **AbortController Access**: The AbortController is passed to promise suppliers for advanced cancellation handling
|
|
324
|
+
|
|
299
325
|
### usePromiseState Hook
|
|
300
326
|
|
|
301
327
|
The `usePromiseState` hook provides state management for promise operations without execution logic. Supports both
|
|
@@ -1527,6 +1553,8 @@ An object containing:
|
|
|
1527
1553
|
- `error`: The error value
|
|
1528
1554
|
- `exchange`: The FetchExchange object representing the ongoing fetch operation
|
|
1529
1555
|
- `execute`: Function to execute a fetch request
|
|
1556
|
+
- `abort`: Function to manually abort the current fetch operation
|
|
1557
|
+
- `onAbort`: Callback function invoked when fetch operation is aborted (configured via options)
|
|
1530
1558
|
|
|
1531
1559
|
### useExecutePromise
|
|
1532
1560
|
|
|
@@ -1561,6 +1589,8 @@ An object containing:
|
|
|
1561
1589
|
- `error`: The error value
|
|
1562
1590
|
- `execute`: Function to execute a promise supplier or promise
|
|
1563
1591
|
- `reset`: Function to reset the state to initial values
|
|
1592
|
+
- `abort`: Function to manually abort the current operation
|
|
1593
|
+
- `onAbort`: Callback function invoked when operation is aborted (configured via options)
|
|
1564
1594
|
|
|
1565
1595
|
### usePromiseState
|
|
1566
1596
|
|
package/dist/core/index.d.ts
CHANGED
package/dist/core/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAaA,cAAc,qBAAqB,CAAC;AACpC,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC;AAC1B,cAAc,wBAAwB,CAAC;AACvC,cAAc,8BAA8B,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAaA,cAAc,qBAAqB,CAAC;AACpC,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC;AAC1B,cAAc,wBAAwB,CAAC;AACvC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,kBAAkB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useDebouncedExecutePromise.d.ts","sourceRoot":"","sources":["../../src/core/useDebouncedExecutePromise.ts"],"names":[],"mappings":"AAaA,OAAO,EAEL,wBAAwB,EACxB,uBAAuB,EACxB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAEL,2BAA2B,EAC3B,0BAA0B,EAC3B,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAGlD;;;;GAIG;AACH,MAAM,WAAW,eAAe;IAC9B;;;;OAIG;IACH,QAAQ,EAAE,2BAA2B,CAAC;CACvC;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,iCAAiC,CAAC,CAAC,EAAE,CAAC,GAAG,OAAO,CAC/D,SAAQ,wBAAwB,CAAC,CAAC,EAAE,CAAC,CAAC,EACpC,eAAe;
|
|
1
|
+
{"version":3,"file":"useDebouncedExecutePromise.d.ts","sourceRoot":"","sources":["../../src/core/useDebouncedExecutePromise.ts"],"names":[],"mappings":"AAaA,OAAO,EAEL,wBAAwB,EACxB,uBAAuB,EACxB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAEL,2BAA2B,EAC3B,0BAA0B,EAC3B,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAGlD;;;;GAIG;AACH,MAAM,WAAW,eAAe;IAC9B;;;;OAIG;IACH,QAAQ,EAAE,2BAA2B,CAAC;CACvC;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,iCAAiC,CAAC,CAAC,EAAE,CAAC,GAAG,OAAO,CAC/D,SAAQ,wBAAwB,CAAC,CAAC,EAAE,CAAC,CAAC,EACpC,eAAe;CAClB;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,gCAAgC,CAAC,CAAC,EAAE,CAAC,GAAG,OAAO,CAC9D,SAAQ,IAAI,CAAC,uBAAuB,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,SAAS,CAAC,EACpD,0BAA0B,CAAC,uBAAuB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;CACvE;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqDG;AACH,wBAAgB,0BAA0B,CAAC,CAAC,GAAG,OAAO,EAAE,CAAC,GAAG,YAAY,EACtE,OAAO,EAAE,iCAAiC,CAAC,CAAC,EAAE,CAAC,CAAC,GAC/C,gCAAgC,CAAC,CAAC,EAAE,CAAC,CAAC,CAsBxC"}
|
|
@@ -13,13 +13,19 @@ export interface UseExecutePromiseOptions<R, E = unknown> extends UsePromiseStat
|
|
|
13
13
|
* @default false
|
|
14
14
|
*/
|
|
15
15
|
propagateError?: boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Callback function called when the current operation is aborted.
|
|
18
|
+
* This includes both automatic cancellation (when a new operation starts) and manual abortion.
|
|
19
|
+
*/
|
|
20
|
+
onAbort?: () => void | Promise<void>;
|
|
16
21
|
}
|
|
17
22
|
/**
|
|
18
|
-
* Type definition for a function that returns a Promise.
|
|
23
|
+
* Type definition for a function that returns a Promise with optional abort controller support.
|
|
19
24
|
* This is used as input to the execute function, allowing lazy evaluation of promises.
|
|
25
|
+
* The abort controller can be used to cancel the promise if needed.
|
|
20
26
|
* @template R - The type of value the promise will resolve to.
|
|
21
27
|
*/
|
|
22
|
-
export type PromiseSupplier<R> = () => Promise<R>;
|
|
28
|
+
export type PromiseSupplier<R> = (abortController: AbortController) => Promise<R>;
|
|
23
29
|
/**
|
|
24
30
|
* Interface defining the return type of the useExecutePromise hook.
|
|
25
31
|
* Provides state management and control functions for asynchronous operations.
|
|
@@ -28,24 +34,39 @@ export type PromiseSupplier<R> = () => Promise<R>;
|
|
|
28
34
|
*/
|
|
29
35
|
export interface UseExecutePromiseReturn<R, E = FetcherError> extends PromiseState<R, E> {
|
|
30
36
|
/**
|
|
31
|
-
* Function to execute a promise supplier
|
|
37
|
+
* Function to execute a promise supplier with automatic abort support.
|
|
38
|
+
* Automatically cancels any previous ongoing request before starting a new one.
|
|
32
39
|
* Manages the loading state, handles errors, and updates the result state.
|
|
33
|
-
* @param input - A function that returns a Promise
|
|
34
|
-
* @returns A Promise that resolves to the result on success, or the error if propagateError is false.
|
|
40
|
+
* @param input - A function that returns a Promise, optionally receiving an AbortController.
|
|
35
41
|
* @throws {Error} If the component is unmounted when execute is called.
|
|
36
42
|
* @throws {E} If propagateError is true and the promise rejects.
|
|
37
43
|
*/
|
|
38
|
-
execute: (input: PromiseSupplier<R>
|
|
44
|
+
execute: (input: PromiseSupplier<R>) => Promise<void>;
|
|
39
45
|
/**
|
|
40
46
|
* Function to reset the state to initial values.
|
|
41
47
|
* Clears loading, result, error, and sets status to idle.
|
|
42
48
|
*/
|
|
43
49
|
reset: () => void;
|
|
50
|
+
/**
|
|
51
|
+
* Function to manually abort the current ongoing operation.
|
|
52
|
+
* This will cancel the current promise execution and set the state to idle.
|
|
53
|
+
* Safe to call even when no operation is currently running.
|
|
54
|
+
*/
|
|
55
|
+
abort: () => void;
|
|
44
56
|
}
|
|
45
57
|
/**
|
|
46
|
-
* A React hook for managing asynchronous operations with proper state handling.
|
|
58
|
+
* A React hook for managing asynchronous operations with proper state handling and abort support.
|
|
47
59
|
* Provides a way to execute promises while automatically managing loading states,
|
|
48
|
-
* handling errors,
|
|
60
|
+
* handling errors, preventing state updates on unmounted components or stale requests,
|
|
61
|
+
* and supporting request cancellation through AbortController.
|
|
62
|
+
*
|
|
63
|
+
* Key features:
|
|
64
|
+
* - Automatic request cancellation when new requests are initiated
|
|
65
|
+
* - Manual abort control with dedicated abort() method
|
|
66
|
+
* - AbortController integration for promise-level cancellation
|
|
67
|
+
* - Race condition protection using request IDs
|
|
68
|
+
* - Comprehensive state management (idle, loading, success, error)
|
|
69
|
+
* - Memory leak prevention with automatic cleanup on unmount
|
|
49
70
|
*
|
|
50
71
|
* @template R - The type of the result value, defaults to unknown.
|
|
51
72
|
* @template E - The type of the error value, defaults to FetcherError.
|
|
@@ -56,20 +77,71 @@ export interface UseExecutePromiseReturn<R, E = FetcherError> extends PromiseSta
|
|
|
56
77
|
* @throws {E} When propagateError is true and the executed promise rejects.
|
|
57
78
|
*
|
|
58
79
|
* @example
|
|
59
|
-
* Basic usage with
|
|
80
|
+
* Basic usage with automatic abort support and onAbort callback:
|
|
81
|
+
* ```typescript
|
|
82
|
+
* import { useExecutePromise } from '@ahoo-wang/fetcher-react';
|
|
83
|
+
*
|
|
84
|
+
* function MyComponent() {
|
|
85
|
+
* const { loading, result, error, execute, reset, abort } = useExecutePromise<string>({
|
|
86
|
+
* onAbort: () => {
|
|
87
|
+
* console.log('Request was cancelled');
|
|
88
|
+
* // Handle abort (e.g., update UI, cleanup resources)
|
|
89
|
+
* }
|
|
90
|
+
* });
|
|
91
|
+
*
|
|
92
|
+
* const fetchData = async (abortController?: AbortController) => {
|
|
93
|
+
* const response = await fetch('/api/data', {
|
|
94
|
+
* signal: abortController?.signal,
|
|
95
|
+
* });
|
|
96
|
+
* return response.text();
|
|
97
|
+
* };
|
|
98
|
+
*
|
|
99
|
+
* const handleFetch = () => {
|
|
100
|
+
* execute(fetchData); // Automatically cancels previous request and calls onAbort
|
|
101
|
+
* };
|
|
102
|
+
*
|
|
103
|
+
* const handleCancel = () => {
|
|
104
|
+
* abort(); // Manually cancel current request and calls onAbort
|
|
105
|
+
* };
|
|
106
|
+
*
|
|
107
|
+
* const handleReset = () => {
|
|
108
|
+
* reset();
|
|
109
|
+
* };
|
|
110
|
+
*
|
|
111
|
+
* if (loading) return <div>Loading...</div>;
|
|
112
|
+
* if (error) return <div>Error: {error.message}</div>;
|
|
113
|
+
* return (
|
|
114
|
+
* <div>
|
|
115
|
+
* <button onClick={handleFetch}>Fetch Data</button>
|
|
116
|
+
* <button onClick={handleCancel} disabled={!loading}>Cancel</button>
|
|
117
|
+
* <button onClick={handleReset}>Reset</button>
|
|
118
|
+
* {result && <p>{result}</p>}
|
|
119
|
+
* </div>
|
|
120
|
+
* );
|
|
121
|
+
* }
|
|
122
|
+
* ```
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* Basic usage with automatic abort support:
|
|
60
126
|
* ```typescript
|
|
61
127
|
* import { useExecutePromise } from '@ahoo-wang/fetcher-react';
|
|
62
128
|
*
|
|
63
129
|
* function MyComponent() {
|
|
64
|
-
* const { loading, result, error, execute, reset } = useExecutePromise<string>();
|
|
130
|
+
* const { loading, result, error, execute, reset, abort } = useExecutePromise<string>();
|
|
65
131
|
*
|
|
66
|
-
* const fetchData = async () => {
|
|
67
|
-
* const response = await fetch('/api/data'
|
|
132
|
+
* const fetchData = async (abortController?: AbortController) => {
|
|
133
|
+
* const response = await fetch('/api/data', {
|
|
134
|
+
* signal: abortController?.signal, // Optional: use abort signal
|
|
135
|
+
* });
|
|
68
136
|
* return response.text();
|
|
69
137
|
* };
|
|
70
138
|
*
|
|
71
139
|
* const handleFetch = () => {
|
|
72
|
-
* execute(fetchData);
|
|
140
|
+
* execute(fetchData); // Automatically cancels previous request
|
|
141
|
+
* };
|
|
142
|
+
*
|
|
143
|
+
* const handleCancel = () => {
|
|
144
|
+
* abort(); // Manually cancel current request
|
|
73
145
|
* };
|
|
74
146
|
*
|
|
75
147
|
* const handleReset = () => {
|
|
@@ -81,6 +153,7 @@ export interface UseExecutePromiseReturn<R, E = FetcherError> extends PromiseSta
|
|
|
81
153
|
* return (
|
|
82
154
|
* <div>
|
|
83
155
|
* <button onClick={handleFetch}>Fetch Data</button>
|
|
156
|
+
* <button onClick={handleCancel} disabled={!loading}>Cancel</button>
|
|
84
157
|
* <button onClick={handleReset}>Reset</button>
|
|
85
158
|
* {result && <p>{result}</p>}
|
|
86
159
|
* </div>
|
|
@@ -103,13 +176,6 @@ export interface UseExecutePromiseReturn<R, E = FetcherError> extends PromiseSta
|
|
|
103
176
|
* };
|
|
104
177
|
* ```
|
|
105
178
|
*
|
|
106
|
-
* @example
|
|
107
|
-
* Executing a promise directly instead of a supplier function:
|
|
108
|
-
* ```typescript
|
|
109
|
-
* const { execute } = useExecutePromise<number>();
|
|
110
|
-
* const promise = fetch('/api/count').then(r => r.json());
|
|
111
|
-
* const result = await execute(promise);
|
|
112
|
-
* ```
|
|
113
179
|
*/
|
|
114
180
|
export declare function useExecutePromise<R = unknown, E = FetcherError>(options?: UseExecutePromiseOptions<R, E>): UseExecutePromiseReturn<R, E>;
|
|
115
181
|
//# sourceMappingURL=useExecutePromise.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useExecutePromise.d.ts","sourceRoot":"","sources":["../../src/core/useExecutePromise.ts"],"names":[],"mappings":"AAeA,OAAO,EAEL,YAAY,EACZ,sBAAsB,EACvB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAElD;;;;GAIG;AACH,MAAM,WAAW,wBAAwB,CAAC,CAAC,EAAE,CAAC,GAAG,OAAO,CACtD,SAAQ,sBAAsB,CAAC,CAAC,EAAE,CAAC,CAAC;IACpC;;;;;OAKG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;
|
|
1
|
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ACvC,uBAAuB,CAAC,CAAC,EAAE,CAAC,CAAC,CA0H/B"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A React hook that returns a function to force a component to re-render.
|
|
3
|
+
* This is useful when you need to trigger a re-render imperatively, such as
|
|
4
|
+
* when dealing with external state changes or imperative updates.
|
|
5
|
+
*
|
|
6
|
+
* @returns A function that when called, forces the component to re-render
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { useForceUpdate } from '@ahoo-wang/fetcher-react';
|
|
11
|
+
*
|
|
12
|
+
* const MyComponent = () => {
|
|
13
|
+
* const forceUpdate = useForceUpdate();
|
|
14
|
+
*
|
|
15
|
+
* const handleExternalChange = () => {
|
|
16
|
+
* // Some external state change that doesn't trigger React re-render
|
|
17
|
+
* externalLibrary.updateSomething();
|
|
18
|
+
* forceUpdate(); // Force re-render to reflect changes
|
|
19
|
+
* };
|
|
20
|
+
*
|
|
21
|
+
* return (
|
|
22
|
+
* <div>
|
|
23
|
+
* <p>Component state: {someValue}</p>
|
|
24
|
+
* <button onClick={handleExternalChange}>Update External State</button>
|
|
25
|
+
* </div>
|
|
26
|
+
* );
|
|
27
|
+
* };
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export declare function useForceUpdate(): () => void;
|
|
31
|
+
//# sourceMappingURL=useForceUpdate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useForceUpdate.d.ts","sourceRoot":"","sources":["../../src/core/useForceUpdate.ts"],"names":[],"mappings":"AAeA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,cAAc,IAAI,MAAM,IAAI,CAG3C"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useDebouncedFetcher.d.ts","sourceRoot":"","sources":["../../src/fetcher/useDebouncedFetcher.ts"],"names":[],"mappings":"AAaA,OAAO,EAAc,iBAAiB,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CAAC,CAwBjC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { FetcherCapable, FetchExchange, FetchRequest, RequestOptions, FetcherError } from '@ahoo-wang/fetcher';
|
|
2
|
-
import {
|
|
2
|
+
import { UseExecutePromiseOptions, UseExecutePromiseReturn } from '../core';
|
|
3
3
|
/**
|
|
4
4
|
* Configuration options for the useFetcher hook.
|
|
5
5
|
* Combines request configuration, fetcher selection, and promise state management options.
|
|
@@ -7,7 +7,7 @@ import { PromiseState, UsePromiseStateOptions } from '../core';
|
|
|
7
7
|
* @template R - The type of the expected result from the fetch operation
|
|
8
8
|
* @template E - The type of error that may be thrown (defaults to FetcherError)
|
|
9
9
|
*/
|
|
10
|
-
export interface UseFetcherOptions<R, E = FetcherError> extends RequestOptions, FetcherCapable,
|
|
10
|
+
export interface UseFetcherOptions<R, E = FetcherError> extends RequestOptions, FetcherCapable, UseExecutePromiseOptions<R, E> {
|
|
11
11
|
}
|
|
12
12
|
/**
|
|
13
13
|
* Return type of the useFetcher hook.
|
|
@@ -16,7 +16,7 @@ export interface UseFetcherOptions<R, E = FetcherError> extends RequestOptions,
|
|
|
16
16
|
* @template R - The type of the expected result from the fetch operation
|
|
17
17
|
* @template E - The type of error that may be thrown (defaults to FetcherError)
|
|
18
18
|
*/
|
|
19
|
-
export interface UseFetcherReturn<R, E = FetcherError> extends
|
|
19
|
+
export interface UseFetcherReturn<R, E = FetcherError> extends Omit<UseExecutePromiseReturn<R, E>, 'execute'> {
|
|
20
20
|
/**
|
|
21
21
|
* The FetchExchange object representing the current or most recent fetch operation.
|
|
22
22
|
* Contains request/response details, timing information, and extracted data.
|
|
@@ -24,8 +24,9 @@ export interface UseFetcherReturn<R, E = FetcherError> extends PromiseState<R, E
|
|
|
24
24
|
*/
|
|
25
25
|
exchange?: FetchExchange;
|
|
26
26
|
/**
|
|
27
|
-
* Function to execute a fetch request.
|
|
28
|
-
* Automatically cancels any ongoing request before starting a new one.
|
|
27
|
+
* Function to execute a fetch request with automatic abort support.
|
|
28
|
+
* Automatically cancels any ongoing request before starting a new one using AbortController.
|
|
29
|
+
* The abort controller is automatically passed to the underlying fetch operation.
|
|
29
30
|
*
|
|
30
31
|
* @param request - The fetch request configuration including URL, method, headers, etc.
|
|
31
32
|
* @returns Promise that resolves when the fetch operation completes (success or failure)
|
|
@@ -34,15 +35,16 @@ export interface UseFetcherReturn<R, E = FetcherError> extends PromiseState<R, E
|
|
|
34
35
|
}
|
|
35
36
|
/**
|
|
36
37
|
* A React hook for managing asynchronous HTTP fetch operations with comprehensive state handling,
|
|
37
|
-
* race condition protection, and
|
|
38
|
-
* with loading states, error handling, and request cancellation.
|
|
38
|
+
* race condition protection, automatic cleanup, and built-in abort support. Provides a clean interface
|
|
39
|
+
* for making API calls with loading states, error handling, and request cancellation.
|
|
39
40
|
*
|
|
40
41
|
* Key features:
|
|
41
|
-
* - Automatic request cancellation on component unmount or new requests
|
|
42
|
+
* - Automatic request cancellation on component unmount or new requests via AbortController
|
|
42
43
|
* - Race condition protection using request IDs
|
|
43
44
|
* - Comprehensive state management (idle, loading, success, error)
|
|
44
45
|
* - Type-safe result and error handling
|
|
45
46
|
* - Integration with fetcher ecosystem for advanced features
|
|
47
|
+
* - Memory leak prevention with automatic abort controller cleanup
|
|
46
48
|
*
|
|
47
49
|
* @template R - The type of the expected result from the fetch operation
|
|
48
50
|
* @template E - The type of error that may be thrown (defaults to FetcherError)
|
|
@@ -55,7 +57,7 @@ export interface UseFetcherReturn<R, E = FetcherError> extends PromiseState<R, E
|
|
|
55
57
|
* HTTP errors, or result extraction problems
|
|
56
58
|
* @throws {Error} When invalid options are provided or fetcher configuration is incorrect
|
|
57
59
|
*
|
|
58
|
-
* @example Basic GET request
|
|
60
|
+
* @example Basic GET request with automatic abort
|
|
59
61
|
* ```typescript
|
|
60
62
|
* import { useFetcher } from '@ahoo-wang/fetcher-react';
|
|
61
63
|
* import { ResultExtractors } from '@ahoo-wang/fetcher';
|
|
@@ -72,6 +74,7 @@ export interface UseFetcherReturn<R, E = FetcherError> extends PromiseState<R, E
|
|
|
72
74
|
* });
|
|
73
75
|
* };
|
|
74
76
|
*
|
|
77
|
+
* // Multiple calls to fetchUser() will automatically cancel previous requests
|
|
75
78
|
* // Handle loading, error, and success states in your component
|
|
76
79
|
* }
|
|
77
80
|
* ```
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useFetcher.d.ts","sourceRoot":"","sources":["../../src/fetcher/useFetcher.ts"],"names":[],"mappings":"AAaA,OAAO,EAEL,cAAc,EACd,aAAa,EACb,YAAY,EAEZ,cAAc,EACd,YAAY,EACb,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"useFetcher.d.ts","sourceRoot":"","sources":["../../src/fetcher/useFetcher.ts"],"names":[],"mappings":"AAaA,OAAO,EAEL,cAAc,EACd,aAAa,EACb,YAAY,EAEZ,cAAc,EACd,YAAY,EACb,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAEL,wBAAwB,EACxB,uBAAuB,EACxB,MAAM,SAAS,CAAC;AAIjB;;;;;;GAMG;AACH,MAAM,WAAW,iBAAiB,CAAC,CAAC,EAAE,CAAC,GAAG,YAAY,CACpD,SAAQ,cAAc,EACpB,cAAc,EACd,wBAAwB,CAAC,CAAC,EAAE,CAAC,CAAC;CACjC;AAED;;;;;;GAMG;AACH,MAAM,WAAW,gBAAgB,CAAC,CAAC,EAAE,CAAC,GAAG,YAAY,CACnD,SAAQ,IAAI,CAAC,uBAAuB,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,SAAS,CAAC;IACtD;;;;OAIG;IACH,QAAQ,CAAC,EAAE,aAAa,CAAC;IAEzB;;;;;;;OAOG;IACH,OAAO,EAAE,CAAC,OAAO,EAAE,YAAY,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CACnD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4FG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAAE,CAAC,GAAG,YAAY,EAC5C,OAAO,CAAC,EAAE,iBAAiB,CAAC,CAAC,EAAE,CAAC,CAAC,GAChC,gBAAgB,CAAC,CAAC,EAAE,CAAC,CAAC,CA8DxB"}
|