@ahoo-wang/fetcher-react 3.4.1 → 3.4.2
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 +306 -28
- package/README.zh-CN.md +291 -28
- package/dist/index.es.js +195 -162
- 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/index.d.ts +1 -0
- package/dist/wow/debounce/index.d.ts.map +1 -1
- package/dist/wow/debounce/useDebouncedFetcherQuery.d.ts +112 -0
- package/dist/wow/debounce/useDebouncedFetcherQuery.d.ts.map +1 -0
- package/dist/wow/debounce/useDebouncedQuery.d.ts +96 -24
- package/dist/wow/debounce/useDebouncedQuery.d.ts.map +1 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/wow/debounce/index.ts"],"names":[],"mappings":"AAaA,cAAc,qBAAqB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/wow/debounce/index.ts"],"names":[],"mappings":"AAaA,cAAc,4BAA4B,CAAC;AAC3C,cAAc,qBAAqB,CAAC"}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { FetcherError } from '@ahoo-wang/fetcher';
|
|
2
|
+
import { DebounceCapable, UseDebouncedCallbackReturn } from '../../core';
|
|
3
|
+
import { UseFetcherQueryOptions, UseFetcherQueryReturn } from '../useFetcherQuery';
|
|
4
|
+
/**
|
|
5
|
+
* Configuration options for the useDebouncedFetcherQuery hook
|
|
6
|
+
* @template Q - The type of the query parameters
|
|
7
|
+
* @template R - The type of the result value
|
|
8
|
+
* @template E - The type of the error value (defaults to FetcherError)
|
|
9
|
+
*/
|
|
10
|
+
export interface UseDebouncedFetcherQueryOptions<Q, R, E = FetcherError> extends UseFetcherQueryOptions<Q, R, E>, DebounceCapable {
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Return type of the useDebouncedFetcherQuery hook
|
|
14
|
+
* @template Q - The type of the query parameters
|
|
15
|
+
* @template R - The type of the result value
|
|
16
|
+
* @template E - The type of the error value (defaults to FetcherError)
|
|
17
|
+
*/
|
|
18
|
+
export interface UseDebouncedFetcherQueryReturn<Q, R, E = FetcherError> extends Omit<UseFetcherQueryReturn<Q, R, E>, 'execute'>, UseDebouncedCallbackReturn<UseFetcherQueryReturn<Q, R, E>['execute']> {
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* A React hook for managing debounced query-based HTTP requests with automatic execution
|
|
22
|
+
*
|
|
23
|
+
* This hook combines the fetcher query functionality with debouncing to provide
|
|
24
|
+
* a convenient way to make POST requests where query parameters are sent as the request body,
|
|
25
|
+
* with built-in debouncing to prevent excessive API calls during rapid user interactions.
|
|
26
|
+
*
|
|
27
|
+
* The hook supports automatic execution on mount and when query parameters change,
|
|
28
|
+
* but wraps the execution in a debounced callback to optimize performance.
|
|
29
|
+
*
|
|
30
|
+
* @template Q - The type of the query parameters
|
|
31
|
+
* @template R - The type of the result value
|
|
32
|
+
* @template E - The type of the error value (defaults to FetcherError)
|
|
33
|
+
* @param options - Configuration options for the hook
|
|
34
|
+
* @returns An object containing fetcher state, query management functions, and debounced execution controls
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* import { useDebouncedFetcherQuery } from '@ahoo-wang/fetcher-react';
|
|
39
|
+
*
|
|
40
|
+
* interface SearchQuery {
|
|
41
|
+
* keyword: string;
|
|
42
|
+
* limit: number;
|
|
43
|
+
* filters?: { category?: string };
|
|
44
|
+
* }
|
|
45
|
+
*
|
|
46
|
+
* interface SearchResult {
|
|
47
|
+
* items: Array<{ id: string; title: string }>;
|
|
48
|
+
* total: number;
|
|
49
|
+
* }
|
|
50
|
+
*
|
|
51
|
+
* function SearchComponent() {
|
|
52
|
+
* const {
|
|
53
|
+
* loading,
|
|
54
|
+
* result,
|
|
55
|
+
* error,
|
|
56
|
+
* run,
|
|
57
|
+
* cancel,
|
|
58
|
+
* isPending,
|
|
59
|
+
* setQuery,
|
|
60
|
+
* getQuery,
|
|
61
|
+
* } = useDebouncedFetcherQuery<SearchQuery, SearchResult>({
|
|
62
|
+
* url: '/api/search',
|
|
63
|
+
* initialQuery: { keyword: '', limit: 10 },
|
|
64
|
+
* debounce: { delay: 300 }, // Debounce for 300ms
|
|
65
|
+
* autoExecute: false, // Don't execute on mount
|
|
66
|
+
* });
|
|
67
|
+
*
|
|
68
|
+
* const handleSearch = (keyword: string) => {
|
|
69
|
+
* setQuery({ keyword, limit: 10 }); // This will trigger debounced execution if autoExecute was true
|
|
70
|
+
* };
|
|
71
|
+
*
|
|
72
|
+
* const handleManualSearch = () => {
|
|
73
|
+
* run(); // Manual debounced execution with current query
|
|
74
|
+
* };
|
|
75
|
+
*
|
|
76
|
+
* const handleCancel = () => {
|
|
77
|
+
* cancel(); // Cancel any pending debounced execution
|
|
78
|
+
* };
|
|
79
|
+
*
|
|
80
|
+
* if (loading) return <div>Searching...</div>;
|
|
81
|
+
* if (error) return <div>Error: {error.message}</div>;
|
|
82
|
+
*
|
|
83
|
+
* return (
|
|
84
|
+
* <div>
|
|
85
|
+
* <input
|
|
86
|
+
* type="text"
|
|
87
|
+
* onChange={(e) => handleSearch(e.target.value)}
|
|
88
|
+
* placeholder="Search..."
|
|
89
|
+
* />
|
|
90
|
+
* <button onClick={handleManualSearch} disabled={isPending()}>
|
|
91
|
+
* {isPending() ? 'Searching...' : 'Search'}
|
|
92
|
+
* </button>
|
|
93
|
+
* <button onClick={handleCancel}>Cancel</button>
|
|
94
|
+
* {result && (
|
|
95
|
+
* <div>
|
|
96
|
+
* Found {result.total} items:
|
|
97
|
+
* {result.items.map(item => (
|
|
98
|
+
* <div key={item.id}>{item.title}</div>
|
|
99
|
+
* ))}
|
|
100
|
+
* </div>
|
|
101
|
+
* )}
|
|
102
|
+
* </div>
|
|
103
|
+
* );
|
|
104
|
+
* }
|
|
105
|
+
* ```
|
|
106
|
+
*
|
|
107
|
+
* @throws This hook may throw exceptions related to network requests, which should be
|
|
108
|
+
* handled by the caller. The underlying fetcher may throw FetcherError or other network-related errors.
|
|
109
|
+
* Invalid URL or malformed request options may also cause exceptions.
|
|
110
|
+
*/
|
|
111
|
+
export declare function useDebouncedFetcherQuery<Q, R, E = FetcherError>(options: UseDebouncedFetcherQueryOptions<Q, R, E>): UseDebouncedFetcherQueryReturn<Q, R, E>;
|
|
112
|
+
//# sourceMappingURL=useDebouncedFetcherQuery.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useDebouncedFetcherQuery.d.ts","sourceRoot":"","sources":["../../../src/wow/debounce/useDebouncedFetcherQuery.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EACL,eAAe,EAEf,0BAA0B,EAC3B,MAAM,YAAY,CAAC;AAEpB,OAAO,EAEL,sBAAsB,EACtB,qBAAqB,EACtB,MAAM,oBAAoB,CAAC;AAE5B;;;;;GAKG;AACH,MAAM,WAAW,+BAA+B,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,YAAY,CACrE,SAAQ,sBAAsB,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,eAAe;CAAG;AAE7D;;;;;GAKG;AACH,MAAM,WAAW,8BAA8B,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,YAAY,CACpE,SACE,IAAI,CAAC,qBAAqB,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,SAAS,CAAC,EAC/C,0BAA0B,CAAC,qBAAqB,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;CAAG;AAE5E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0FG;AACH,wBAAgB,wBAAwB,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,YAAY,EAC7D,OAAO,EAAE,+BAA+B,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,GAChD,8BAA8B,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CA4CzC"}
|
|
@@ -2,46 +2,118 @@ import { FetcherError } from '@ahoo-wang/fetcher';
|
|
|
2
2
|
import { UseQueryOptions, UseQueryReturn } from '../useQuery';
|
|
3
3
|
import { DebounceCapable, UseDebouncedCallbackReturn } from '../../core';
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
* @template Q - The query
|
|
7
|
-
* @template R - The result
|
|
8
|
-
* @template E - The error
|
|
5
|
+
* Configuration options for the useDebouncedQuery hook
|
|
6
|
+
* @template Q - The type of the query parameters
|
|
7
|
+
* @template R - The type of the result value
|
|
8
|
+
* @template E - The type of the error value (defaults to FetcherError)
|
|
9
9
|
*/
|
|
10
10
|
export interface UseDebouncedQueryOptions<Q, R, E = FetcherError> extends UseQueryOptions<Q, R, E>, DebounceCapable {
|
|
11
11
|
}
|
|
12
12
|
/**
|
|
13
|
-
* Return type
|
|
14
|
-
* @template Q - The query
|
|
15
|
-
* @template R - The result
|
|
16
|
-
* @template E - The error
|
|
13
|
+
* Return type of the useDebouncedQuery hook
|
|
14
|
+
* @template Q - The type of the query parameters
|
|
15
|
+
* @template R - The type of the result value
|
|
16
|
+
* @template E - The type of the error value (defaults to FetcherError)
|
|
17
17
|
*/
|
|
18
18
|
export interface UseDebouncedQueryReturn<Q, R, E = FetcherError> extends Omit<UseQueryReturn<Q, R, E>, 'execute'>, UseDebouncedCallbackReturn<UseQueryReturn<Q, R, E>['execute']> {
|
|
19
19
|
}
|
|
20
20
|
/**
|
|
21
|
-
* A React hook
|
|
22
|
-
* to prevent excessive API calls during rapid query changes.
|
|
21
|
+
* A React hook for managing debounced query execution with automatic state management
|
|
23
22
|
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
23
|
+
* This hook combines the query functionality with debouncing to provide
|
|
24
|
+
* a convenient way to execute queries with built-in debouncing to prevent excessive
|
|
25
|
+
* operations during rapid query changes, such as in search inputs or dynamic filtering.
|
|
26
|
+
*
|
|
27
|
+
* The hook supports automatic execution on mount and when query parameters change,
|
|
28
|
+
* but wraps the execution in a debounced callback to optimize performance.
|
|
29
|
+
*
|
|
30
|
+
* @template Q - The type of the query parameters
|
|
31
|
+
* @template R - The type of the result value
|
|
32
|
+
* @template E - The type of the error value (defaults to FetcherError)
|
|
33
|
+
* @param options - Configuration options for the hook
|
|
34
|
+
* @returns An object containing query state, query management functions, and debounced execution controls
|
|
29
35
|
*
|
|
30
36
|
* @example
|
|
31
37
|
* ```typescript
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
38
|
+
* import { useDebouncedQuery } from '@ahoo-wang/fetcher-react';
|
|
39
|
+
*
|
|
40
|
+
* interface SearchQuery {
|
|
41
|
+
* keyword: string;
|
|
42
|
+
* limit: number;
|
|
43
|
+
* filters?: { category?: string };
|
|
44
|
+
* }
|
|
45
|
+
*
|
|
46
|
+
* interface SearchResult {
|
|
47
|
+
* items: Array<{ id: string; title: string }>;
|
|
48
|
+
* total: number;
|
|
49
|
+
* }
|
|
50
|
+
*
|
|
51
|
+
* function SearchComponent() {
|
|
52
|
+
* const {
|
|
53
|
+
* loading,
|
|
54
|
+
* result,
|
|
55
|
+
* error,
|
|
56
|
+
* run,
|
|
57
|
+
* cancel,
|
|
58
|
+
* isPending,
|
|
59
|
+
* setQuery,
|
|
60
|
+
* getQuery,
|
|
61
|
+
* } = useDebouncedQuery<SearchQuery, SearchResult>({
|
|
62
|
+
* initialQuery: { keyword: '', limit: 10 },
|
|
63
|
+
* execute: async (query) => {
|
|
64
|
+
* const response = await fetch('/api/search', {
|
|
65
|
+
* method: 'POST',
|
|
66
|
+
* body: JSON.stringify(query),
|
|
67
|
+
* headers: { 'Content-Type': 'application/json' },
|
|
68
|
+
* });
|
|
69
|
+
* return response.json();
|
|
70
|
+
* },
|
|
71
|
+
* debounce: { delay: 300 }, // Debounce for 300ms
|
|
72
|
+
* autoExecute: false, // Don't execute on mount
|
|
73
|
+
* });
|
|
74
|
+
*
|
|
75
|
+
* const handleSearch = (keyword: string) => {
|
|
76
|
+
* setQuery({ keyword, limit: 10 }); // This will trigger debounced execution if autoExecute was true
|
|
77
|
+
* };
|
|
78
|
+
*
|
|
79
|
+
* const handleManualSearch = () => {
|
|
80
|
+
* run(); // Manual debounced execution with current query
|
|
81
|
+
* };
|
|
82
|
+
*
|
|
83
|
+
* const handleCancel = () => {
|
|
84
|
+
* cancel(); // Cancel any pending debounced execution
|
|
85
|
+
* };
|
|
36
86
|
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
87
|
+
* if (loading) return <div>Searching...</div>;
|
|
88
|
+
* if (error) return <div>Error: {error.message}</div>;
|
|
39
89
|
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
90
|
+
* return (
|
|
91
|
+
* <div>
|
|
92
|
+
* <input
|
|
93
|
+
* type="text"
|
|
94
|
+
* onChange={(e) => handleSearch(e.target.value)}
|
|
95
|
+
* placeholder="Search..."
|
|
96
|
+
* />
|
|
97
|
+
* <button onClick={handleManualSearch} disabled={isPending()}>
|
|
98
|
+
* {isPending() ? 'Searching...' : 'Search'}
|
|
99
|
+
* </button>
|
|
100
|
+
* <button onClick={handleCancel}>Cancel</button>
|
|
101
|
+
* {result && (
|
|
102
|
+
* <div>
|
|
103
|
+
* Found {result.total} items:
|
|
104
|
+
* {result.items.map(item => (
|
|
105
|
+
* <div key={item.id}>{item.title}</div>
|
|
106
|
+
* ))}
|
|
107
|
+
* </div>
|
|
108
|
+
* )}
|
|
109
|
+
* </div>
|
|
110
|
+
* );
|
|
111
|
+
* }
|
|
42
112
|
* ```
|
|
43
113
|
*
|
|
44
|
-
* @throws
|
|
114
|
+
* @throws This hook may throw exceptions related to query execution, which should be
|
|
115
|
+
* handled by the caller. The execute function may throw FetcherError or other errors.
|
|
116
|
+
* Invalid query parameters or execution function errors may also cause exceptions.
|
|
45
117
|
*/
|
|
46
118
|
export declare function useDebouncedQuery<Q, R, E = FetcherError>(options: UseDebouncedQueryOptions<Q, R, E>): UseDebouncedQueryReturn<Q, R, E>;
|
|
47
119
|
//# sourceMappingURL=useDebouncedQuery.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useDebouncedQuery.d.ts","sourceRoot":"","sources":["../../../src/wow/debounce/useDebouncedQuery.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAY,eAAe,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AACxE,OAAO,EACL,eAAe,EAEf,0BAA0B,EAC3B,MAAM,YAAY,CAAC;AAGpB;;;;;GAKG;AACH,MAAM,WAAW,wBAAwB,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,YAAY,CAC9D,SAAQ,eAAe,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,
|
|
1
|
+
{"version":3,"file":"useDebouncedQuery.d.ts","sourceRoot":"","sources":["../../../src/wow/debounce/useDebouncedQuery.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAY,eAAe,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AACxE,OAAO,EACL,eAAe,EAEf,0BAA0B,EAC3B,MAAM,YAAY,CAAC;AAGpB;;;;;GAKG;AACH,MAAM,WAAW,wBAAwB,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,YAAY,CAC9D,SAAQ,eAAe,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,eAAe;CAAG;AAEtD;;;;;GAKG;AACH,MAAM,WAAW,uBAAuB,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,YAAY,CAC7D,SACE,IAAI,CAAC,cAAc,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,SAAS,CAAC,EACxC,0BAA0B,CAAC,cAAc,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;CAAG;AAErE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiGG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,YAAY,EACtD,OAAO,EAAE,wBAAwB,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,GACzC,uBAAuB,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CA4ClC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ahoo-wang/fetcher-react",
|
|
3
|
-
"version": "3.4.
|
|
3
|
+
"version": "3.4.2",
|
|
4
4
|
"description": "React integration for Fetcher HTTP client. Provides React Hooks and components for seamless data fetching with automatic re-rendering and loading states.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"fetch",
|