@ahoo-wang/fetcher-react 2.13.10 → 2.15.0
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 +232 -0
- package/README.zh-CN.md +232 -0
- package/dist/core/index.d.ts +2 -0
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/useDebouncedCallback.d.ts +71 -0
- package/dist/core/useDebouncedCallback.d.ts.map +1 -0
- package/dist/core/useDebouncedExecutePromise.d.ts +93 -0
- package/dist/core/useDebouncedExecutePromise.d.ts.map +1 -0
- package/dist/core/useExecutePromise.d.ts +55 -17
- package/dist/core/useExecutePromise.d.ts.map +1 -1
- package/dist/fetcher/index.d.ts +1 -0
- package/dist/fetcher/index.d.ts.map +1 -1
- package/dist/fetcher/useDebouncedFetcher.d.ts +92 -0
- package/dist/fetcher/useDebouncedFetcher.d.ts.map +1 -0
- package/dist/fetcher/useFetcher.d.ts +103 -18
- package/dist/fetcher/useFetcher.d.ts.map +1 -1
- package/dist/index.es.js +291 -186
- package/dist/index.es.js.map +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/index.umd.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,39 +1,62 @@
|
|
|
1
1
|
import { PromiseState, UsePromiseStateOptions } from './usePromiseState';
|
|
2
2
|
import { FetcherError } from '@ahoo-wang/fetcher';
|
|
3
|
+
/**
|
|
4
|
+
* Configuration options for the useExecutePromise hook.
|
|
5
|
+
* @template R - The type of the resolved value from the promise.
|
|
6
|
+
* @template E - The type of the error value, defaults to unknown.
|
|
7
|
+
*/
|
|
3
8
|
export interface UseExecutePromiseOptions<R, E = unknown> extends UsePromiseStateOptions<R, E> {
|
|
4
9
|
/**
|
|
5
10
|
* Whether to propagate errors thrown by the promise.
|
|
6
11
|
* If true, the execute function will throw errors.
|
|
7
12
|
* If false (default), the execute function will return the error as the result instead of throwing.
|
|
13
|
+
* @default false
|
|
8
14
|
*/
|
|
9
15
|
propagateError?: boolean;
|
|
10
16
|
}
|
|
11
17
|
/**
|
|
12
|
-
* Type definition for a function that returns a Promise
|
|
13
|
-
*
|
|
18
|
+
* Type definition for a function that returns a Promise.
|
|
19
|
+
* This is used as input to the execute function, allowing lazy evaluation of promises.
|
|
20
|
+
* @template R - The type of value the promise will resolve to.
|
|
14
21
|
*/
|
|
15
22
|
export type PromiseSupplier<R> = () => Promise<R>;
|
|
16
23
|
/**
|
|
17
|
-
* Interface defining the return type of useExecutePromise hook
|
|
18
|
-
*
|
|
24
|
+
* Interface defining the return type of the useExecutePromise hook.
|
|
25
|
+
* Provides state management and control functions for asynchronous operations.
|
|
26
|
+
* @template R - The type of the result value.
|
|
27
|
+
* @template E - The type of the error value, defaults to FetcherError.
|
|
19
28
|
*/
|
|
20
29
|
export interface UseExecutePromiseReturn<R, E = FetcherError> extends PromiseState<R, E> {
|
|
21
30
|
/**
|
|
22
31
|
* Function to execute a promise supplier or promise.
|
|
23
|
-
*
|
|
32
|
+
* Manages the loading state, handles errors, and updates the result state.
|
|
33
|
+
* @param input - A function that returns a Promise or a Promise directly.
|
|
34
|
+
* @returns A Promise that resolves to the result on success, or the error if propagateError is false.
|
|
35
|
+
* @throws {Error} If the component is unmounted when execute is called.
|
|
36
|
+
* @throws {E} If propagateError is true and the promise rejects.
|
|
24
37
|
*/
|
|
25
38
|
execute: (input: PromiseSupplier<R> | Promise<R>) => Promise<R | E>;
|
|
26
|
-
/**
|
|
39
|
+
/**
|
|
40
|
+
* Function to reset the state to initial values.
|
|
41
|
+
* Clears loading, result, error, and sets status to idle.
|
|
42
|
+
*/
|
|
27
43
|
reset: () => void;
|
|
28
44
|
}
|
|
29
45
|
/**
|
|
30
|
-
* A React hook for managing asynchronous operations with proper state handling
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
* @
|
|
46
|
+
* A React hook for managing asynchronous operations with proper state handling.
|
|
47
|
+
* Provides a way to execute promises while automatically managing loading states,
|
|
48
|
+
* handling errors, and preventing state updates on unmounted components or stale requests.
|
|
49
|
+
*
|
|
50
|
+
* @template R - The type of the result value, defaults to unknown.
|
|
51
|
+
* @template E - The type of the error value, defaults to FetcherError.
|
|
52
|
+
* @param options - Optional configuration options for the hook behavior.
|
|
53
|
+
* @returns An object containing the current promise state and control functions.
|
|
54
|
+
*
|
|
55
|
+
* @throws {Error} When execute is called on an unmounted component.
|
|
56
|
+
* @throws {E} When propagateError is true and the executed promise rejects.
|
|
35
57
|
*
|
|
36
58
|
* @example
|
|
59
|
+
* Basic usage with state management:
|
|
37
60
|
* ```typescript
|
|
38
61
|
* import { useExecutePromise } from '@ahoo-wang/fetcher-react';
|
|
39
62
|
*
|
|
@@ -63,14 +86,29 @@ export interface UseExecutePromiseReturn<R, E = FetcherError> extends PromiseSta
|
|
|
63
86
|
* </div>
|
|
64
87
|
* );
|
|
65
88
|
* }
|
|
89
|
+
* ```
|
|
66
90
|
*
|
|
67
|
-
*
|
|
91
|
+
* @example
|
|
92
|
+
* Using propagateError for try/catch error handling:
|
|
93
|
+
* ```typescript
|
|
68
94
|
* const { execute } = useExecutePromise<string>({ propagateError: true });
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
95
|
+
*
|
|
96
|
+
* const handleSubmit = async () => {
|
|
97
|
+
* try {
|
|
98
|
+
* const data = await execute(fetchUserData);
|
|
99
|
+
* console.log('Success:', data);
|
|
100
|
+
* } catch (err) {
|
|
101
|
+
* console.error('Error occurred:', err);
|
|
102
|
+
* }
|
|
103
|
+
* };
|
|
104
|
+
* ```
|
|
105
|
+
*
|
|
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);
|
|
74
112
|
* ```
|
|
75
113
|
*/
|
|
76
114
|
export declare function useExecutePromise<R = unknown, E = FetcherError>(options?: UseExecutePromiseOptions<R, E>): UseExecutePromiseReturn<R, E>;
|
|
@@ -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,MAAM,WAAW,wBAAwB,CAAC,CAAC,EAAE,CAAC,GAAG,OAAO,CACtD,SAAQ,sBAAsB,CAAC,CAAC,EAAE,CAAC,CAAC;IACpC
|
|
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;CAC1B;AAED;;;;GAIG;AACH,MAAM,MAAM,eAAe,CAAC,CAAC,IAAI,MAAM,OAAO,CAAC,CAAC,CAAC,CAAC;AAElD;;;;;GAKG;AACH,MAAM,WAAW,uBAAuB,CAAC,CAAC,EAAE,CAAC,GAAG,YAAY,CAC1D,SAAQ,YAAY,CAAC,CAAC,EAAE,CAAC,CAAC;IAC1B;;;;;;;OAOG;IACH,OAAO,EAAE,CAAC,KAAK,EAAE,eAAe,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;IACpE;;;OAGG;IACH,KAAK,EAAE,MAAM,IAAI,CAAC;CACnB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoEG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,GAAG,OAAO,EAAE,CAAC,GAAG,YAAY,EAC7D,OAAO,CAAC,EAAE,wBAAwB,CAAC,CAAC,EAAE,CAAC,CAAC,GACvC,uBAAuB,CAAC,CAAC,EAAE,CAAC,CAAC,CAwE/B"}
|
package/dist/fetcher/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/fetcher/index.ts"],"names":[],"mappings":"AAaA,cAAc,cAAc,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/fetcher/index.ts"],"names":[],"mappings":"AAaA,cAAc,cAAc,CAAC;AAC7B,cAAc,uBAAuB,CAAC"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { UseFetcherOptions, UseFetcherReturn } from './useFetcher';
|
|
2
|
+
import { FetcherError } from '@ahoo-wang/fetcher';
|
|
3
|
+
import { DebounceCapable, UseDebouncedCallbackReturn } from '../core';
|
|
4
|
+
/**
|
|
5
|
+
* Configuration options for the useDebouncedFetcher hook.
|
|
6
|
+
* Extends UseFetcherOptions with debouncing capabilities to control the rate
|
|
7
|
+
* at which fetch requests are executed.
|
|
8
|
+
*
|
|
9
|
+
* @template R - The type of the fetch result
|
|
10
|
+
* @template E - The type of the error (defaults to FetcherError)
|
|
11
|
+
*/
|
|
12
|
+
export interface UseDebouncedFetcherOptions<R, E = FetcherError> extends UseFetcherOptions<R, E>, DebounceCapable {
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Return type of the useDebouncedFetcher hook.
|
|
16
|
+
* Provides the same state properties as useFetcher (except execute) along with
|
|
17
|
+
* debounced execution controls.
|
|
18
|
+
*
|
|
19
|
+
* @template R - The type of the fetch result
|
|
20
|
+
* @template E - The type of the error (defaults to FetcherError)
|
|
21
|
+
*/
|
|
22
|
+
export interface UseDebouncedFetcherReturn<R, E = FetcherError> extends Omit<UseFetcherReturn<R, E>, 'execute'>, UseDebouncedCallbackReturn<UseFetcherReturn<R, E>['execute']> {
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* A React hook that provides a debounced version of the useFetcher hook.
|
|
26
|
+
* This hook wraps the fetcher's execute function with debouncing to prevent
|
|
27
|
+
* excessive API calls during rapid user interactions, such as typing in a search field.
|
|
28
|
+
*
|
|
29
|
+
* The debouncing behavior is controlled by the `debounce` option, which specifies
|
|
30
|
+
* the delay and whether to execute on the leading edge, trailing edge, or both.
|
|
31
|
+
*
|
|
32
|
+
* @template R - The type of the fetch result
|
|
33
|
+
* @template E - The type of the error (defaults to FetcherError)
|
|
34
|
+
* @param options - Configuration options including fetcher settings and debounce parameters
|
|
35
|
+
* @returns An object containing the fetch state and debounced execution controls
|
|
36
|
+
*
|
|
37
|
+
* @throws {Error} If the debounce delay is not a positive number
|
|
38
|
+
* @throws {Error} If the fetcher configuration is invalid
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```typescript
|
|
42
|
+
* import { useDebouncedFetcher } from '@ahoo-wang/fetcher-react';
|
|
43
|
+
*
|
|
44
|
+
* function SearchComponent() {
|
|
45
|
+
* const { loading, result, error, run, cancel, isPending } = useDebouncedFetcher<string>({
|
|
46
|
+
* debounce: { delay: 300, trailing: true },
|
|
47
|
+
* onSuccess: (data) => console.log('Search results:', data),
|
|
48
|
+
* onError: (err) => console.error('Search failed:', err),
|
|
49
|
+
* });
|
|
50
|
+
*
|
|
51
|
+
* const handleSearch = (query: string) => {
|
|
52
|
+
* if (query.trim()) {
|
|
53
|
+
* run({ url: `/api/search?q=${encodeURIComponent(query)}`, method: 'GET' });
|
|
54
|
+
* } else {
|
|
55
|
+
* cancel(); // Cancel any pending search
|
|
56
|
+
* }
|
|
57
|
+
* };
|
|
58
|
+
*
|
|
59
|
+
* return (
|
|
60
|
+
* <div>
|
|
61
|
+
* <input
|
|
62
|
+
* type="text"
|
|
63
|
+
* placeholder="Search..."
|
|
64
|
+
* onChange={(e) => handleSearch(e.target.value)}
|
|
65
|
+
* />
|
|
66
|
+
* {isPending() && <div>Searching...</div>}
|
|
67
|
+
* {loading && <div>Loading...</div>}
|
|
68
|
+
* {error && <div>Error: {error.message}</div>}
|
|
69
|
+
* {result && <div>Results: {result}</div>}
|
|
70
|
+
* </div>
|
|
71
|
+
* );
|
|
72
|
+
* }
|
|
73
|
+
* ```
|
|
74
|
+
*
|
|
75
|
+
* @example Debounced form submission
|
|
76
|
+
* ```typescript
|
|
77
|
+
* const { run, cancel, isPending } = useDebouncedFetcher({
|
|
78
|
+
* debounce: { delay: 500, leading: false, trailing: true },
|
|
79
|
+
* onSuccess: () => console.log('Form submitted successfully'),
|
|
80
|
+
* });
|
|
81
|
+
*
|
|
82
|
+
* const handleSubmit = (formData: FormData) => {
|
|
83
|
+
* run({
|
|
84
|
+
* url: '/api/submit',
|
|
85
|
+
* method: 'POST',
|
|
86
|
+
* body: formData,
|
|
87
|
+
* });
|
|
88
|
+
* };
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
export declare function useDebouncedFetcher<R, E = FetcherError>(options: UseDebouncedFetcherOptions<R, E>): UseDebouncedFetcherReturn<R, E>;
|
|
92
|
+
//# sourceMappingURL=useDebouncedFetcher.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useDebouncedFetcher.d.ts","sourceRoot":"","sources":["../../src/fetcher/useDebouncedFetcher.ts"],"names":[],"mappings":"AAaA,OAAO,EAAc,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAC/E,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EACL,eAAe,EAEf,0BAA0B,EAC3B,MAAM,SAAS,CAAC;AAGjB;;;;;;;GAOG;AACH,MAAM,WAAW,0BAA0B,CAAC,CAAC,EAAE,CAAC,GAAG,YAAY,CAC7D,SAAQ,iBAAiB,CAAC,CAAC,EAAE,CAAC,CAAC,EAC7B,eAAe;CAAG;AAEtB;;;;;;;GAOG;AACH,MAAM,WAAW,yBAAyB,CAAC,CAAC,EAAE,CAAC,GAAG,YAAY,CAC5D,SAAQ,IAAI,CAAC,gBAAgB,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,SAAS,CAAC,EAC7C,0BAA0B,CAAC,gBAAgB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;CAAG;AAEpE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkEG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,CAAC,GAAG,YAAY,EACrD,OAAO,EAAE,0BAA0B,CAAC,CAAC,EAAE,CAAC,CAAC,GACxC,yBAAyB,CAAC,CAAC,EAAE,CAAC,CAAC,CAsBjC"}
|
|
@@ -2,39 +2,124 @@ import { FetcherCapable, FetchExchange, FetchRequest, RequestOptions, FetcherErr
|
|
|
2
2
|
import { PromiseState, UsePromiseStateOptions } from '../core';
|
|
3
3
|
/**
|
|
4
4
|
* Configuration options for the useFetcher hook.
|
|
5
|
-
*
|
|
5
|
+
* Combines request configuration, fetcher selection, and promise state management options.
|
|
6
|
+
*
|
|
7
|
+
* @template R - The type of the expected result from the fetch operation
|
|
8
|
+
* @template E - The type of error that may be thrown (defaults to FetcherError)
|
|
6
9
|
*/
|
|
7
10
|
export interface UseFetcherOptions<R, E = FetcherError> extends RequestOptions, FetcherCapable, UsePromiseStateOptions<R, E> {
|
|
8
11
|
}
|
|
12
|
+
/**
|
|
13
|
+
* Return type of the useFetcher hook.
|
|
14
|
+
* Provides access to the current fetch state, result data, and control functions.
|
|
15
|
+
*
|
|
16
|
+
* @template R - The type of the expected result from the fetch operation
|
|
17
|
+
* @template E - The type of error that may be thrown (defaults to FetcherError)
|
|
18
|
+
*/
|
|
9
19
|
export interface UseFetcherReturn<R, E = FetcherError> extends PromiseState<R, E> {
|
|
10
|
-
/**
|
|
20
|
+
/**
|
|
21
|
+
* The FetchExchange object representing the current or most recent fetch operation.
|
|
22
|
+
* Contains request/response details, timing information, and extracted data.
|
|
23
|
+
* Undefined when no fetch operation has been performed.
|
|
24
|
+
*/
|
|
11
25
|
exchange?: FetchExchange;
|
|
26
|
+
/**
|
|
27
|
+
* Function to execute a fetch request.
|
|
28
|
+
* Automatically cancels any ongoing request before starting a new one.
|
|
29
|
+
*
|
|
30
|
+
* @param request - The fetch request configuration including URL, method, headers, etc.
|
|
31
|
+
* @returns Promise that resolves when the fetch operation completes (success or failure)
|
|
32
|
+
*/
|
|
12
33
|
execute: (request: FetchRequest) => Promise<void>;
|
|
13
34
|
}
|
|
14
35
|
/**
|
|
15
|
-
* A React hook for managing asynchronous operations with
|
|
16
|
-
*
|
|
17
|
-
*
|
|
36
|
+
* A React hook for managing asynchronous HTTP fetch operations with comprehensive state handling,
|
|
37
|
+
* race condition protection, and automatic cleanup. Provides a clean interface for making API calls
|
|
38
|
+
* with loading states, error handling, and request cancellation.
|
|
39
|
+
*
|
|
40
|
+
* Key features:
|
|
41
|
+
* - Automatic request cancellation on component unmount or new requests
|
|
42
|
+
* - Race condition protection using request IDs
|
|
43
|
+
* - Comprehensive state management (idle, loading, success, error)
|
|
44
|
+
* - Type-safe result and error handling
|
|
45
|
+
* - Integration with fetcher ecosystem for advanced features
|
|
46
|
+
*
|
|
47
|
+
* @template R - The type of the expected result from the fetch operation
|
|
48
|
+
* @template E - The type of error that may be thrown (defaults to FetcherError)
|
|
49
|
+
* @param options - Configuration options for the fetcher including request settings,
|
|
50
|
+
* result extraction, error handling, and fetcher selection
|
|
51
|
+
* @returns An object containing the current fetch state, result data, error information,
|
|
52
|
+
* and the execute function to trigger fetch operations
|
|
18
53
|
*
|
|
19
|
-
* @
|
|
54
|
+
* @throws {FetcherError} When the fetch operation fails due to network issues,
|
|
55
|
+
* HTTP errors, or result extraction problems
|
|
56
|
+
* @throws {Error} When invalid options are provided or fetcher configuration is incorrect
|
|
57
|
+
*
|
|
58
|
+
* @example Basic GET request
|
|
20
59
|
* ```typescript
|
|
21
60
|
* import { useFetcher } from '@ahoo-wang/fetcher-react';
|
|
61
|
+
* import { ResultExtractors } from '@ahoo-wang/fetcher';
|
|
22
62
|
*
|
|
23
|
-
* function
|
|
24
|
-
* const { loading, result, error, execute } = useFetcher
|
|
63
|
+
* function UserProfile({ userId }: { userId: string }) {
|
|
64
|
+
* const { loading, result, error, execute } = useFetcher({
|
|
65
|
+
* resultExtractor: ResultExtractors.Json,
|
|
66
|
+
* });
|
|
25
67
|
*
|
|
26
|
-
* const
|
|
27
|
-
* execute({
|
|
68
|
+
* const fetchUser = () => {
|
|
69
|
+
* execute({
|
|
70
|
+
* url: `/api/users/${userId}`,
|
|
71
|
+
* method: 'GET'
|
|
72
|
+
* });
|
|
28
73
|
* };
|
|
29
74
|
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
75
|
+
* // Handle loading, error, and success states in your component
|
|
76
|
+
* }
|
|
77
|
+
* ```
|
|
78
|
+
*
|
|
79
|
+
* @example POST request with error handling
|
|
80
|
+
* ```typescript
|
|
81
|
+
* import { useFetcher } from '@ahoo-wang/fetcher-react';
|
|
82
|
+
*
|
|
83
|
+
* function CreatePost() {
|
|
84
|
+
* const { loading, result, error, execute } = useFetcher({
|
|
85
|
+
* onSuccess: (data) => {
|
|
86
|
+
* console.log('Post created:', data);
|
|
87
|
+
* // Handle success (e.g., redirect, show notification)
|
|
88
|
+
* },
|
|
89
|
+
* onError: (error) => {
|
|
90
|
+
* console.error('Failed to create post:', error);
|
|
91
|
+
* // Handle error (e.g., show error message)
|
|
92
|
+
* }
|
|
93
|
+
* });
|
|
94
|
+
*
|
|
95
|
+
* const handleSubmit = (postData: { title: string; content: string }) => {
|
|
96
|
+
* execute({
|
|
97
|
+
* url: '/api/posts',
|
|
98
|
+
* method: 'POST',
|
|
99
|
+
* body: JSON.stringify(postData),
|
|
100
|
+
* headers: {
|
|
101
|
+
* 'Content-Type': 'application/json'
|
|
102
|
+
* }
|
|
103
|
+
* });
|
|
104
|
+
* };
|
|
105
|
+
* }
|
|
106
|
+
* ```
|
|
107
|
+
*
|
|
108
|
+
* @example Using custom fetcher instance
|
|
109
|
+
* ```typescript
|
|
110
|
+
* import { useFetcher } from '@ahoo-wang/fetcher-react';
|
|
111
|
+
* import { getFetcher } from '@ahoo-wang/fetcher';
|
|
112
|
+
*
|
|
113
|
+
* // Using a named fetcher instance
|
|
114
|
+
* const customFetcher = getFetcher('my-custom-fetcher');
|
|
115
|
+
*
|
|
116
|
+
* function CustomApiComponent() {
|
|
117
|
+
* const { loading, result, execute } = useFetcher({
|
|
118
|
+
* fetcher: customFetcher,
|
|
119
|
+
* });
|
|
120
|
+
*
|
|
121
|
+
* // All requests will use the custom fetcher configuration
|
|
122
|
+
* const fetchData = () => execute({ url: '/data', method: 'GET' });
|
|
38
123
|
* }
|
|
39
124
|
* ```
|
|
40
125
|
*/
|
|
@@ -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,
|
|
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;AAG5B,OAAO,EACL,YAAY,EAGZ,sBAAsB,EAEvB,MAAM,SAAS,CAAC;AAEjB;;;;;;GAMG;AACH,MAAM,WAAW,iBAAiB,CAAC,CAAC,EAAE,CAAC,GAAG,YAAY,CACpD,SAAQ,cAAc,EACpB,cAAc,EACd,sBAAsB,CAAC,CAAC,EAAE,CAAC,CAAC;CAE/B;AAED;;;;;;GAMG;AACH,MAAM,WAAW,gBAAgB,CAAC,CAAC,EAAE,CAAC,GAAG,YAAY,CACnD,SAAQ,YAAY,CAAC,CAAC,EAAE,CAAC,CAAC;IAC1B;;;;OAIG;IACH,QAAQ,CAAC,EAAE,aAAa,CAAC;IAEzB;;;;;;OAMG;IACH,OAAO,EAAE,CAAC,OAAO,EAAE,YAAY,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CACnD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0FG;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,CA2FxB"}
|