@ahoo-wang/fetcher-react 3.3.9 → 3.4.1

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.
@@ -1,9 +1,16 @@
1
- export * from './usePagedQuery';
2
- export * from './useSingleQuery';
1
+ export * from './types';
3
2
  export * from './useCountQuery';
3
+ export * from './useFetcherCountQuery';
4
+ export * from './useFetcherListQuery';
5
+ export * from './useFetcherListStreamQuery';
6
+ export * from './useFetcherPagedQuery';
7
+ export * from './useFetcherQuery';
8
+ export * from './useFetcherSingleQuery';
4
9
  export * from './useListQuery';
5
10
  export * from './useListStreamQuery';
11
+ export * from './usePagedQuery';
6
12
  export * from './useQuery';
13
+ export * from './useQueryState';
14
+ export * from './useSingleQuery';
7
15
  export * from './debounce';
8
- export * from './types';
9
16
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/wow/index.ts"],"names":[],"mappings":"AAaA,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,sBAAsB,CAAC;AACrC,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,SAAS,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/wow/index.ts"],"names":[],"mappings":"AAaA,cAAc,SAAS,CAAC;AACxB,cAAc,iBAAiB,CAAC;AAChC,cAAc,wBAAwB,CAAC;AACvC,cAAc,uBAAuB,CAAC;AACtC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,wBAAwB,CAAC;AACvC,cAAc,mBAAmB,CAAC;AAClC,cAAc,yBAAyB,CAAC;AACxC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,sBAAsB,CAAC;AACrC,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,YAAY,CAAC"}
@@ -0,0 +1,67 @@
1
+ import { FetcherError } from '@ahoo-wang/fetcher';
2
+ import { UseQueryReturn } from './useQuery';
3
+ import { UseFetcherQueryOptions } from './useFetcherQuery';
4
+ import { Condition } from '@ahoo-wang/fetcher-wow';
5
+ /**
6
+ * Options for configuring the useFetcherCountQuery hook.
7
+ *
8
+ * This interface extends UseFetcherQueryOptions and is specifically tailored for count queries
9
+ * that use a Condition object to filter results and return a numeric count.
10
+ *
11
+ * @template FIELDS - A string union type representing the fields that can be used in the condition.
12
+ * @template E - The type of error that may be thrown, defaults to FetcherError.
13
+ */
14
+ export interface UseFetcherCountQueryOptions<FIELDS extends string = string, E = FetcherError> extends UseFetcherQueryOptions<Condition<FIELDS>, number, E> {
15
+ }
16
+ /**
17
+ * Return type for the useFetcherCountQuery hook.
18
+ *
19
+ * This interface extends UseQueryReturn and provides the structure for the hook's return value,
20
+ * including data (the count as a number), loading state, error state, and other query-related properties.
21
+ *
22
+ * @template FIELDS - A string union type representing the fields that can be used in the condition.
23
+ * @template E - The type of error that may be thrown, defaults to FetcherError.
24
+ */
25
+ export interface UseFetcherCountQueryReturn<FIELDS extends string = string, E = FetcherError> extends UseQueryReturn<Condition<FIELDS>, number, E> {
26
+ }
27
+ /**
28
+ * A React hook for performing count queries using the Fetcher library.
29
+ *
30
+ * This hook is designed for scenarios where you need to retrieve the count of records
31
+ * that match a specific condition. It wraps the useFetcherQuery hook and specializes
32
+ * it for count operations, returning a number representing the count.
33
+ *
34
+ * @template FIELDS - A string union type representing the fields that can be used in the condition.
35
+ * @template E - The type of error that may be thrown, defaults to FetcherError.
36
+ *
37
+ * @param options - Configuration options for the count query, including the condition, fetcher instance, and other query settings.
38
+ * @returns An object containing the query result (count as a number), loading state, error state, and utility functions.
39
+ *
40
+ * @throws {E} Throws an error of type E if the query fails, which could be due to network issues, invalid conditions, or server errors.
41
+ *
42
+ * @example
43
+ * ```typescript
44
+ * import { useFetcherCountQuery } from '@ahoo-wang/fetcher-react';
45
+ * import { all } from '@ahoo-wang/fetcher-wow';
46
+ *
47
+ * function UserCountComponent() {
48
+ * const { data: count, loading, error, execute } = useFetcherCountQuery({
49
+ * url: '/api/users/count',
50
+ * initialQuery: all(),
51
+ * autoExecute: true,
52
+ * });
53
+ *
54
+ * if (loading) return <div>Loading...</div>;
55
+ * if (error) return <div>Error: {error.message}</div>;
56
+ *
57
+ * return (
58
+ * <div>
59
+ * <div>Total active users: {count}</div>
60
+ * <button onClick={execute}>Refresh Count</button>
61
+ * </div>
62
+ * );
63
+ * }
64
+ * ```
65
+ */
66
+ export declare function useFetcherCountQuery<FIELDS extends string = string, E = FetcherError>(options: UseFetcherCountQueryOptions<FIELDS, E>): UseFetcherCountQueryReturn<FIELDS, E>;
67
+ //# sourceMappingURL=useFetcherCountQuery.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useFetcherCountQuery.d.ts","sourceRoot":"","sources":["../../src/wow/useFetcherCountQuery.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAC5C,OAAO,EAAmB,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AAC5E,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAEnD;;;;;;;;GAQG;AACH,MAAM,WAAW,2BAA2B,CAC1C,MAAM,SAAS,MAAM,GAAG,MAAM,EAC9B,CAAC,GAAG,YAAY,CAChB,SAAQ,sBAAsB,CAAC,SAAS,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC;CAAG;AAEjE;;;;;;;;GAQG;AACH,MAAM,WAAW,0BAA0B,CACzC,MAAM,SAAS,MAAM,GAAG,MAAM,EAC9B,CAAC,GAAG,YAAY,CAChB,SAAQ,cAAc,CAAC,SAAS,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC;CAAG;AAEzD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,wBAAgB,oBAAoB,CAClC,MAAM,SAAS,MAAM,GAAG,MAAM,EAC9B,CAAC,GAAG,YAAY,EAEhB,OAAO,EAAE,2BAA2B,CAAC,MAAM,EAAE,CAAC,CAAC,GAC9C,0BAA0B,CAAC,MAAM,EAAE,CAAC,CAAC,CAEvC"}
@@ -0,0 +1,100 @@
1
+ import { ListQuery } from '@ahoo-wang/fetcher-wow';
2
+ import { FetcherError } from '@ahoo-wang/fetcher';
3
+ import { UseQueryReturn } from './useQuery';
4
+ import { UseFetcherQueryOptions } from './useFetcherQuery';
5
+ /**
6
+ * Options for the useFetcherListQuery hook.
7
+ * Extends UseFetcherQueryOptions to provide configuration for list queries.
8
+ *
9
+ * @template R - The type of individual items in the result array.
10
+ * @template FIELDS - The fields available for filtering, sorting, and pagination in the list query.
11
+ * @template E - The error type, defaults to FetcherError.
12
+ */
13
+ export interface UseFetcherListQueryOptions<R, FIELDS extends string = string, E = FetcherError> extends UseFetcherQueryOptions<ListQuery<FIELDS>, R[], E> {
14
+ }
15
+ /**
16
+ * Return type of the useFetcherListQuery hook.
17
+ * Extends UseQueryReturn to provide state and methods for list query operations.
18
+ *
19
+ * @template R - The type of individual items in the result array.
20
+ * @template FIELDS - The fields available for filtering, sorting, and pagination in the list query.
21
+ * @template E - The error type, defaults to FetcherError.
22
+ */
23
+ export interface UseFetcherListQueryReturn<R, FIELDS extends string = string, E = FetcherError> extends UseQueryReturn<ListQuery<FIELDS>, R[], E> {
24
+ }
25
+ /**
26
+ * A React hook for executing list queries using the fetcher library within the wow framework.
27
+ *
28
+ * This hook is designed for fetching lists of items with support for filtering, sorting, and pagination
29
+ * through the ListQuery type. It returns an array of results and integrates seamlessly with the fetcher
30
+ * library for HTTP requests.
31
+ *
32
+ * @template R - The type of individual items in the result array (e.g., User, Product).
33
+ * @template FIELDS - The fields available for filtering, sorting, and pagination (e.g., 'name', 'createdAt').
34
+ * @template E - The error type, defaults to FetcherError.
35
+ * @param options - Configuration options including URL, initial list query parameters, and execution settings.
36
+ * @returns An object containing loading state, result array, error state, and query management functions.
37
+ *
38
+ * @example
39
+ * ```typescript
40
+ * import { useFetcherListQuery } from '@ahoo-wang/fetcher-react';
41
+ * import { listQuery, contains, desc } from '@ahoo-wang/fetcher-wow';
42
+ *
43
+ * interface User {
44
+ * id: string;
45
+ * name: string;
46
+ * email: string;
47
+ * createdAt: string;
48
+ * }
49
+ *
50
+ * function UserListComponent() {
51
+ * const {
52
+ * loading,
53
+ * result: users,
54
+ * error,
55
+ * execute,
56
+ * setQuery,
57
+ * getQuery,
58
+ * } = useFetcherListQuery<User, keyof User>({
59
+ * url: '/api/users/list',
60
+ * initialQuery: listQuery({
61
+ * condition: contains('name', 'John'),
62
+ * sort: [desc('createdAt')],
63
+ * limit: 10,
64
+ * }),
65
+ * autoExecute: true,
66
+ * });
67
+ *
68
+ * const loadMore = () => {
69
+ * const currentQuery = getQuery();
70
+ * setQuery({
71
+ * ...currentQuery,
72
+ * limit: (currentQuery.limit || 10) + 10,
73
+ * });
74
+ * };
75
+ *
76
+ * if (loading) return <div>Loading users...</div>;
77
+ * if (error) return <div>Error: {error.message}</div>;
78
+ *
79
+ * return (
80
+ * <div>
81
+ * <h2>Users ({users?.length || 0})</h2>
82
+ * <ul>
83
+ * {users?.map(user => (
84
+ * <li key={user.id}>
85
+ * {user.name} - {user.email}
86
+ * </li>
87
+ * ))}
88
+ * </ul>
89
+ * <button onClick={loadMore}>Load More</button>
90
+ * <button onClick={execute}>Refresh list</button>
91
+ * </div>
92
+ * );
93
+ * }
94
+ * ```
95
+ *
96
+ * @throws {FetcherError} When the HTTP request fails due to network issues, invalid responses, or server errors.
97
+ * @throws {Error} When invalid options are provided, such as malformed URLs or unsupported query parameters.
98
+ */
99
+ export declare function useFetcherListQuery<R, FIELDS extends string = string, E = FetcherError>(options: UseFetcherListQueryOptions<R, FIELDS, E>): UseFetcherListQueryReturn<R, FIELDS, E>;
100
+ //# sourceMappingURL=useFetcherListQuery.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useFetcherListQuery.d.ts","sourceRoot":"","sources":["../../src/wow/useFetcherListQuery.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACnD,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAC5C,OAAO,EAAmB,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AAE5E;;;;;;;GAOG;AACH,MAAM,WAAW,0BAA0B,CACzC,CAAC,EACD,MAAM,SAAS,MAAM,GAAG,MAAM,EAC9B,CAAC,GAAG,YAAY,CAChB,SAAQ,sBAAsB,CAAC,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC;CAAG;AAE9D;;;;;;;GAOG;AACH,MAAM,WAAW,yBAAyB,CACxC,CAAC,EACD,MAAM,SAAS,MAAM,GAAG,MAAM,EAC9B,CAAC,GAAG,YAAY,CAChB,SAAQ,cAAc,CAAC,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC;CAAG;AAEtD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyEG;AACH,wBAAgB,mBAAmB,CACjC,CAAC,EACD,MAAM,SAAS,MAAM,GAAG,MAAM,EAC9B,CAAC,GAAG,YAAY,EAEhB,OAAO,EAAE,0BAA0B,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,GAChD,yBAAyB,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,CAEzC"}
@@ -0,0 +1,107 @@
1
+ import { ListQuery } from '@ahoo-wang/fetcher-wow';
2
+ import { FetcherError } from '@ahoo-wang/fetcher';
3
+ import { UseQueryReturn } from './useQuery';
4
+ import { UseFetcherQueryOptions } from './useFetcherQuery';
5
+ import { JsonServerSentEvent } from '@ahoo-wang/fetcher-eventstream';
6
+ /**
7
+ * Options for configuring the useFetcherListStreamQuery hook.
8
+ *
9
+ * This interface extends UseFetcherQueryOptions and is specifically tailored for list stream queries
10
+ * that use a ListQuery to filter results and return a ReadableStream of JSON server-sent events.
11
+ *
12
+ * @template R - The type of the resource or entity contained in each event in the stream.
13
+ * @template FIELDS - A string union type representing the fields that can be used in the list query.
14
+ * @template E - The type of error that may be thrown, defaults to FetcherError.
15
+ */
16
+ export interface UseFetcherListStreamQueryOptions<R, FIELDS extends string = string, E = FetcherError> extends UseFetcherQueryOptions<ListQuery<FIELDS>, ReadableStream<JsonServerSentEvent<R>>, E> {
17
+ }
18
+ /**
19
+ * Return type for the useFetcherListStreamQuery hook.
20
+ *
21
+ * This interface extends UseQueryReturn and provides the structure for the hook's return value,
22
+ * including data (a ReadableStream of JSON server-sent events), loading state, error state, and other query-related properties.
23
+ *
24
+ * @template R - The type of the resource or entity contained in each event in the stream.
25
+ * @template FIELDS - A string union type representing the fields that can be used in the list query.
26
+ * @template E - The type of error that may be thrown, defaults to FetcherError.
27
+ */
28
+ export interface UseFetcherListStreamQueryReturn<R, FIELDS extends string = string, E = FetcherError> extends UseQueryReturn<ListQuery<FIELDS>, ReadableStream<JsonServerSentEvent<R>>, E> {
29
+ }
30
+ /**
31
+ * A React hook for performing list stream queries using the Fetcher library with server-sent events.
32
+ *
33
+ * This hook is designed for scenarios where you need to retrieve a stream of data that matches a list query condition.
34
+ * It returns a ReadableStream of JSON server-sent events, allowing for real-time data streaming.
35
+ * The hook automatically configures the JsonEventStreamResultExtractor for proper stream handling.
36
+ *
37
+ * @template R - The type of the resource or entity contained in each event in the stream.
38
+ * @template FIELDS - A string union type representing the fields that can be used in the list query.
39
+ * @template E - The type of error that may be thrown, defaults to FetcherError.
40
+ *
41
+ * @param options - Configuration options for the list stream query, including the list query, fetcher instance, and other query settings.
42
+ * @returns An object containing the query result (a ReadableStream of JSON server-sent events), loading state, error state, and utility functions.
43
+ *
44
+ * @throws {E} Throws an error of type E if the query fails, which could be due to network issues, invalid queries, or server errors.
45
+ *
46
+ * @example
47
+ * ```typescript
48
+ * import { useFetcherListStreamQuery } from '@ahoo-wang/fetcher-react';
49
+ * import { listQuery, contains } from '@ahoo-wang/fetcher-wow';
50
+ * import { JsonServerSentEvent } from '@ahoo-wang/fetcher-eventstream';
51
+ * import { useEffect, useRef } from 'react';
52
+ *
53
+ * interface User {
54
+ * id: number;
55
+ * name: string;
56
+ * }
57
+ *
58
+ * function UserStreamComponent() {
59
+ * const { data: stream, loading, error, execute } = useFetcherListStreamQuery<User, 'id' | 'name'>({
60
+ * url: '/api/users/stream',
61
+ * initialQuery: listQuery({
62
+ * condition: contains('name', 'John'),
63
+ * limit: 10,
64
+ * }),
65
+ * autoExecute: true,
66
+ * });
67
+ *
68
+ * const messagesRef = useRef<HTMLDivElement>(null);
69
+ *
70
+ * useEffect(() => {
71
+ * if (stream) {
72
+ * const reader = stream.getReader();
73
+ * const readStream = async () => {
74
+ * try {
75
+ * while (true) {
76
+ * const { done, value } = await reader.read();
77
+ * if (done) break;
78
+ * // Process the JsonServerSentEvent<User>
79
+ * const newUser = value.data;
80
+ * if (messagesRef.current) {
81
+ * const div = document.createElement('div');
82
+ * div.textContent = `New user: ${newUser.name}`;
83
+ * messagesRef.current.appendChild(div);
84
+ * }
85
+ * }
86
+ * } catch (err) {
87
+ * console.error('Stream error:', err);
88
+ * }
89
+ * };
90
+ * readStream();
91
+ * }
92
+ * }, [stream]);
93
+ *
94
+ * if (loading) return <div>Loading stream...</div>;
95
+ * if (error) return <div>Error: {error.message}</div>;
96
+ *
97
+ * return (
98
+ * <div>
99
+ * <div ref={messagesRef}></div>
100
+ * <button onClick={execute}>Restart Stream</button>
101
+ * </div>
102
+ * );
103
+ * }
104
+ * ```
105
+ */
106
+ export declare function useFetcherListStreamQuery<R, FIELDS extends string = string, E = FetcherError>(options: UseFetcherListStreamQueryOptions<R, FIELDS, E>): UseFetcherListStreamQueryReturn<R, FIELDS, E>;
107
+ //# sourceMappingURL=useFetcherListStreamQuery.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useFetcherListStreamQuery.d.ts","sourceRoot":"","sources":["../../src/wow/useFetcherListStreamQuery.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACnD,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAC5C,OAAO,EAAmB,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AAC5E,OAAO,EAEL,mBAAmB,EACpB,MAAM,gCAAgC,CAAC;AAExC;;;;;;;;;GASG;AACH,MAAM,WAAW,gCAAgC,CAC/C,CAAC,EACD,MAAM,SAAS,MAAM,GAAG,MAAM,EAC9B,CAAC,GAAG,YAAY,CAChB,SAAQ,sBAAsB,CAC9B,SAAS,CAAC,MAAM,CAAC,EACjB,cAAc,CAAC,mBAAmB,CAAC,CAAC,CAAC,CAAC,EACtC,CAAC,CACF;CAAG;AAEJ;;;;;;;;;GASG;AACH,MAAM,WAAW,+BAA+B,CAC9C,CAAC,EACD,MAAM,SAAS,MAAM,GAAG,MAAM,EAC9B,CAAC,GAAG,YAAY,CAChB,SAAQ,cAAc,CACtB,SAAS,CAAC,MAAM,CAAC,EACjB,cAAc,CAAC,mBAAmB,CAAC,CAAC,CAAC,CAAC,EACtC,CAAC,CACF;CAAG;AAEJ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2EG;AACH,wBAAgB,yBAAyB,CACvC,CAAC,EACD,MAAM,SAAS,MAAM,GAAG,MAAM,EAC9B,CAAC,GAAG,YAAY,EAEhB,OAAO,EAAE,gCAAgC,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,GACtD,+BAA+B,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,CAU/C"}
@@ -0,0 +1,111 @@
1
+ import { PagedList, PagedQuery } from '@ahoo-wang/fetcher-wow';
2
+ import { FetcherError } from '@ahoo-wang/fetcher';
3
+ import { UseQueryReturn } from './useQuery';
4
+ import { UseFetcherQueryOptions } from './useFetcherQuery';
5
+ /**
6
+ * Options for configuring the useFetcherPagedQuery hook.
7
+ *
8
+ * This interface extends UseFetcherQueryOptions and is specifically tailored for paged queries
9
+ * that use a PagedQuery to filter and paginate results, returning a PagedList of items.
10
+ *
11
+ * @template R - The type of the resource or entity contained in each item of the paged list.
12
+ * @template FIELDS - A string union type representing the fields that can be used in the paged query.
13
+ * @template E - The type of error that may be thrown, defaults to FetcherError.
14
+ */
15
+ export interface UseFetcherPagedQueryOptions<R, FIELDS extends string = string, E = FetcherError> extends UseFetcherQueryOptions<PagedQuery<FIELDS>, PagedList<R>, E> {
16
+ }
17
+ /**
18
+ * Return type for the useFetcherPagedQuery hook.
19
+ *
20
+ * This interface extends UseQueryReturn and provides the structure for the hook's return value,
21
+ * including data (a PagedList containing items and pagination metadata), loading state, error state, and other query-related properties.
22
+ *
23
+ * @template R - The type of the resource or entity contained in each item of the paged list.
24
+ * @template FIELDS - A string union type representing the fields that can be used in the paged query.
25
+ * @template E - The type of error that may be thrown, defaults to FetcherError.
26
+ */
27
+ export interface UseFetcherPagedQueryReturn<R, FIELDS extends string = string, E = FetcherError> extends UseQueryReturn<PagedQuery<FIELDS>, PagedList<R>, E> {
28
+ }
29
+ /**
30
+ * A React hook for performing paged queries using the Fetcher library.
31
+ *
32
+ * This hook is designed for scenarios where you need to retrieve paginated data that matches a query condition.
33
+ * It returns a PagedList containing the items for the current page along with pagination metadata such as total count and page information.
34
+ *
35
+ * @template R - The type of the resource or entity contained in each item of the paged list.
36
+ * @template FIELDS - A string union type representing the fields that can be used in the paged query.
37
+ * @template E - The type of error that may be thrown, defaults to FetcherError.
38
+ *
39
+ * @param options - Configuration options for the paged query, including the paged query parameters, fetcher instance, and other query settings.
40
+ * @returns An object containing the query result (a PagedList with items and pagination info), loading state, error state, and utility functions.
41
+ *
42
+ * @throws {E} Throws an error of type E if the query fails, which could be due to network issues, invalid queries, or server errors.
43
+ *
44
+ * @example
45
+ * ```typescript
46
+ * import { useFetcherPagedQuery } from '@ahoo-wang/fetcher-react';
47
+ * import { pagedQuery, contains, pagination, desc } from '@ahoo-wang/fetcher-wow';
48
+ *
49
+ * interface User {
50
+ * id: number;
51
+ * name: string;
52
+ * email: string;
53
+ * }
54
+ *
55
+ * function UserListComponent() {
56
+ * const {
57
+ * data: pagedList,
58
+ * loading,
59
+ * error,
60
+ * execute,
61
+ * setQuery,
62
+ * getQuery
63
+ * } = useFetcherPagedQuery<User, keyof User>({
64
+ * url: '/api/users/paged',
65
+ * initialQuery: pagedQuery({
66
+ * condition: contains('name', 'John'),
67
+ * sort: [desc('createdAt')],
68
+ * pagination: pagination({ index: 1, size: 10 })
69
+ * }),
70
+ * autoExecute: true,
71
+ * });
72
+ *
73
+ * const goToPage = (page: number) => {
74
+ * const currentQuery = getQuery();
75
+ * setQuery({
76
+ * ...currentQuery,
77
+ * pagination: { ...currentQuery.pagination, index: page }
78
+ * });
79
+ * };
80
+ *
81
+ * if (loading) return <div>Loading...</div>;
82
+ * if (error) return <div>Error: {error.message}</div>;
83
+ *
84
+ * return (
85
+ * <div>
86
+ * <h2>Users</h2>
87
+ * <ul>
88
+ * {pagedList.list.map(user => (
89
+ * <li key={user.id}>{user.name} - {user.email}</li>
90
+ * ))}
91
+ * </ul>
92
+ * <div>
93
+ * <span>Total: {pagedList.total} users</span>
94
+ * <button onClick={() => goToPage(1)} disabled={pagedList.pagination.index === 1}>
95
+ * First
96
+ * </button>
97
+ * <button onClick={() => goToPage(pagedList.pagination.index - 1)} disabled={pagedList.pagination.index === 1}>
98
+ * Previous
99
+ * </button>
100
+ * <span>Page {pagedList.pagination.index}</span>
101
+ * <button onClick={() => goToPage(pagedList.pagination.index + 1)}>
102
+ * Next
103
+ * </button>
104
+ * </div>
105
+ * </div>
106
+ * );
107
+ * }
108
+ * ```
109
+ */
110
+ export declare function useFetcherPagedQuery<R, FIELDS extends string = string, E = FetcherError>(options: UseFetcherPagedQueryOptions<R, FIELDS, E>): UseFetcherPagedQueryReturn<R, FIELDS, E>;
111
+ //# sourceMappingURL=useFetcherPagedQuery.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useFetcherPagedQuery.d.ts","sourceRoot":"","sources":["../../src/wow/useFetcherPagedQuery.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAC/D,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAC5C,OAAO,EAAmB,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AAE5E;;;;;;;;;GASG;AACH,MAAM,WAAW,2BAA2B,CAC1C,CAAC,EACD,MAAM,SAAS,MAAM,GAAG,MAAM,EAC9B,CAAC,GAAG,YAAY,CAChB,SAAQ,sBAAsB,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;CAAG;AAExE;;;;;;;;;GASG;AACH,MAAM,WAAW,0BAA0B,CACzC,CAAC,EACD,MAAM,SAAS,MAAM,GAAG,MAAM,EAC9B,CAAC,GAAG,YAAY,CAChB,SAAQ,cAAc,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;CAAG;AAEhE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgFG;AACH,wBAAgB,oBAAoB,CAClC,CAAC,EACD,MAAM,SAAS,MAAM,GAAG,MAAM,EAC9B,CAAC,GAAG,YAAY,EAEhB,OAAO,EAAE,2BAA2B,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,GACjD,0BAA0B,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,CAE1C"}
@@ -0,0 +1,106 @@
1
+ import { UseFetcherOptions, UseFetcherReturn } from '../fetcher';
2
+ import { FetcherError } from '@ahoo-wang/fetcher';
3
+ import { AutoExecuteCapable } from './types';
4
+ import { UseQueryStateReturn } from './useQueryState';
5
+ /**
6
+ * Configuration options for the useFetcherQuery hook
7
+ * @template Q - The type of the query parameters
8
+ * @template R - The type of the result value
9
+ * @template E - The type of the error value
10
+ */
11
+ export interface UseFetcherQueryOptions<Q, R, E = FetcherError> extends UseFetcherOptions<R, E>, AutoExecuteCapable {
12
+ /** The URL endpoint to send the POST request to */
13
+ url: string;
14
+ /** The initial query parameters to be sent as the request body */
15
+ initialQuery: Q;
16
+ }
17
+ /**
18
+ * Return type of the useFetcherQuery hook
19
+ * @template Q - The type of the query parameters
20
+ * @template R - The type of the result value
21
+ * @template E - The type of the error value
22
+ */
23
+ export interface UseFetcherQueryReturn<Q, R, E = FetcherError> extends UseFetcherReturn<R, E>, UseQueryStateReturn<Q> {
24
+ /** Function to execute the query with current parameters */
25
+ execute: () => Promise<void>;
26
+ }
27
+ /**
28
+ * A React hook for managing query-based HTTP requests with automatic execution
29
+ *
30
+ * This hook combines the fetcher functionality with query state management to provide
31
+ * a convenient way to make POST requests where query parameters are sent as the request body.
32
+ * It supports automatic execution on mount and when query parameters change.
33
+ *
34
+ * @template Q - The type of the query parameters
35
+ * @template R - The type of the result value
36
+ * @template E - The type of the error value (defaults to FetcherError)
37
+ * @param options - Configuration options for the hook
38
+ * @returns An object containing fetcher state, query management functions, and execution controls
39
+ *
40
+ * @example
41
+ * ```typescript
42
+ * import { useFetcherQuery } from '@ahoo-wang/fetcher-react';
43
+ *
44
+ * interface SearchQuery {
45
+ * keyword: string;
46
+ * limit: number;
47
+ * filters?: { category?: string };
48
+ * }
49
+ *
50
+ * interface SearchResult {
51
+ * items: Array<{ id: string; title: string }>;
52
+ * total: number;
53
+ * }
54
+ *
55
+ * function SearchComponent() {
56
+ * const {
57
+ * loading,
58
+ * result,
59
+ * error,
60
+ * execute,
61
+ * setQuery,
62
+ * getQuery,
63
+ * } = useFetcherQuery<SearchQuery, SearchResult>({
64
+ * url: '/api/search',
65
+ * initialQuery: { keyword: '', limit: 10 },
66
+ * autoExecute: false, // Don't execute on mount
67
+ * });
68
+ *
69
+ * const handleSearch = (keyword: string) => {
70
+ * setQuery({ keyword, limit: 10 }); // This will auto-execute if autoExecute was true
71
+ * };
72
+ *
73
+ * const handleManualSearch = () => {
74
+ * execute(); // Manual execution with current query
75
+ * };
76
+ *
77
+ * if (loading) return <div>Searching...</div>;
78
+ * if (error) return <div>Error: {error.message}</div>;
79
+ *
80
+ * return (
81
+ * <div>
82
+ * <input
83
+ * type="text"
84
+ * onChange={(e) => handleSearch(e.target.value)}
85
+ * placeholder="Search..."
86
+ * />
87
+ * <button onClick={handleManualSearch}>Search</button>
88
+ * {result && (
89
+ * <div>
90
+ * Found {result.total} items:
91
+ * {result.items.map(item => (
92
+ * <div key={item.id}>{item.title}</div>
93
+ * ))}
94
+ * </div>
95
+ * )}
96
+ * </div>
97
+ * );
98
+ * }
99
+ * ```
100
+ *
101
+ * @throws This hook may throw exceptions related to network requests, which should be
102
+ * handled by the caller. The execute function may throw FetcherError or other network-related errors.
103
+ * Invalid URL or malformed request options may also cause exceptions.
104
+ */
105
+ export declare function useFetcherQuery<Q, R, E = FetcherError>(options: UseFetcherQueryOptions<Q, R, E>): UseFetcherQueryReturn<Q, R, E>;
106
+ //# sourceMappingURL=useFetcherQuery.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useFetcherQuery.d.ts","sourceRoot":"","sources":["../../src/wow/useFetcherQuery.ts"],"names":[],"mappings":"AAaA,OAAO,EAAc,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAC7E,OAAO,EAAE,YAAY,EAAqC,MAAM,oBAAoB,CAAC;AAGrF,OAAO,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAC7C,OAAO,EAAiB,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAErE;;;;;GAKG;AACH,MAAM,WAAW,sBAAsB,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,YAAY,CAC5D,SAAQ,iBAAiB,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,kBAAkB;IACnD,mDAAmD;IACnD,GAAG,EAAE,MAAM,CAAC;IACZ,kEAAkE;IAClE,YAAY,EAAE,CAAC,CAAC;CACjB;AAED;;;;;GAKG;AACH,MAAM,WAAW,qBAAqB,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,YAAY,CAC3D,SAAQ,gBAAgB,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,mBAAmB,CAAC,CAAC,CAAC;IACtD,4DAA4D;IAC5D,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CAC9B;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6EG;AACH,wBAAgB,eAAe,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,YAAY,EACpD,OAAO,EAAE,sBAAsB,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,GACvC,qBAAqB,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CA6DhC"}
@@ -0,0 +1,85 @@
1
+ import { SingleQuery } from '@ahoo-wang/fetcher-wow';
2
+ import { FetcherError } from '@ahoo-wang/fetcher';
3
+ import { UseQueryReturn } from './useQuery';
4
+ import { UseFetcherQueryOptions } from './useFetcherQuery';
5
+ /**
6
+ * Configuration options for the useFetcherSingleQuery hook.
7
+ *
8
+ * Extends UseFetcherQueryOptions to provide configuration for single item queries.
9
+ *
10
+ * @template R - The type of the result item.
11
+ * @template FIELDS - The fields available for filtering and sorting in the single query.
12
+ * @template E - The error type, defaults to FetcherError.
13
+ */
14
+ export interface UseFetcherSingleQueryOptions<R, FIELDS extends string = string, E = FetcherError> extends UseFetcherQueryOptions<SingleQuery<FIELDS>, R, E> {
15
+ }
16
+ /**
17
+ * Return type of the useFetcherSingleQuery hook.
18
+ *
19
+ * Extends UseQueryReturn to provide state and methods for single item query operations.
20
+ *
21
+ * @template R - The type of the result item.
22
+ * @template FIELDS - The fields available for filtering and sorting in the single query.
23
+ * @template E - The error type, defaults to FetcherError.
24
+ */
25
+ export interface UseFetcherSingleQueryReturn<R, FIELDS extends string = string, E = FetcherError> extends UseQueryReturn<SingleQuery<FIELDS>, R, E> {
26
+ }
27
+ /**
28
+ * A React hook for executing single item queries using the fetcher library within the wow framework.
29
+ *
30
+ * This hook is designed for fetching a single item with support for filtering and sorting
31
+ * through the SingleQuery type. It returns a single result item and integrates seamlessly
32
+ * with the fetcher library for HTTP requests.
33
+ *
34
+ * @template R - The type of the result item (e.g., User, Product).
35
+ * @template FIELDS - The fields available for filtering and sorting (e.g., 'id', 'name', 'createdAt').
36
+ * @template E - The error type, defaults to FetcherError.
37
+ * @param options - Configuration options including URL, initial single query parameters, and execution settings.
38
+ * @returns An object containing loading state, result item, error state, and query management functions.
39
+ *
40
+ * @example
41
+ * ```typescript
42
+ * import { useFetcherSingleQuery } from '@ahoo-wang/fetcher-react';
43
+ * import { singleQuery, eq } from '@ahoo-wang/fetcher-wow';
44
+ *
45
+ * interface User {
46
+ * id: string;
47
+ * name: string;
48
+ * email: string;
49
+ * createdAt: string;
50
+ * }
51
+ *
52
+ * function UserProfileComponent({ userId }: { userId: string }) {
53
+ * const {
54
+ * loading,
55
+ * result: user,
56
+ * error,
57
+ * execute,
58
+ * } = useFetcherSingleQuery<User, keyof User>({
59
+ * url: `/api/users/${userId}`,
60
+ * initialQuery: singleQuery({
61
+ * condition: eq('id', userId),
62
+ * }),
63
+ * autoExecute: true,
64
+ * });
65
+ *
66
+ * if (loading) return <div>Loading user...</div>;
67
+ * if (error) return <div>Error: {error.message}</div>;
68
+ * if (!user) return <div>User not found</div>;
69
+ *
70
+ * return (
71
+ * <div>
72
+ * <h2>{user.name}</h2>
73
+ * <p>Email: {user.email}</p>
74
+ * <p>Created: {user.createdAt}</p>
75
+ * <button onClick={execute}>Refresh</button>
76
+ * </div>
77
+ * );
78
+ * }
79
+ * ```
80
+ *
81
+ * @throws {FetcherError} When the HTTP request fails due to network issues, invalid responses, or server errors.
82
+ * @throws {Error} When invalid options are provided, such as malformed URLs or unsupported query parameters.
83
+ */
84
+ export declare function useFetcherSingleQuery<R, FIELDS extends string = string, E = FetcherError>(options: UseFetcherSingleQueryOptions<R, FIELDS, E>): UseFetcherSingleQueryReturn<R, FIELDS, E>;
85
+ //# sourceMappingURL=useFetcherSingleQuery.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useFetcherSingleQuery.d.ts","sourceRoot":"","sources":["../../src/wow/useFetcherSingleQuery.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAC5C,OAAO,EAAmB,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AAE5E;;;;;;;;GAQG;AACH,MAAM,WAAW,4BAA4B,CAC3C,CAAC,EACD,MAAM,SAAS,MAAM,GAAG,MAAM,EAC9B,CAAC,GAAG,YAAY,CAChB,SAAQ,sBAAsB,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;CAAG;AAE9D;;;;;;;;GAQG;AACH,MAAM,WAAW,2BAA2B,CAC1C,CAAC,EACD,MAAM,SAAS,MAAM,GAAG,MAAM,EAC9B,CAAC,GAAG,YAAY,CAChB,SAAQ,cAAc,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;CAAG;AAEtD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwDG;AACH,wBAAgB,qBAAqB,CACnC,CAAC,EACD,MAAM,SAAS,MAAM,GAAG,MAAM,EAC9B,CAAC,GAAG,YAAY,EAEhB,OAAO,EAAE,4BAA4B,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,GAClD,2BAA2B,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,CAE3C"}
@@ -1,6 +1,7 @@
1
1
  import { UseExecutePromiseReturn, UseExecutePromiseOptions } from '../core';
2
2
  import { AttributesCapable, FetcherError } from '@ahoo-wang/fetcher';
3
3
  import { AutoExecuteCapable } from './types';
4
+ import { UseQueryStateReturn } from './useQueryState';
4
5
  /**
5
6
  * Configuration options for the useQuery hook
6
7
  * @template Q - The type of the query parameters
@@ -19,13 +20,7 @@ export interface UseQueryOptions<Q, R, E = FetcherError> extends UseExecutePromi
19
20
  * @template R - The type of the result value
20
21
  * @template E - The type of the error value
21
22
  */
22
- export interface UseQueryReturn<Q, R, E = FetcherError> extends UseExecutePromiseReturn<R, E> {
23
- /**
24
- * Get the current query parameters
25
- */
26
- getQuery: () => Q;
27
- /** Function to update the query parameters */
28
- setQuery: (query: Q) => void;
23
+ export interface UseQueryReturn<Q, R, E = FetcherError> extends UseExecutePromiseReturn<R, E>, UseQueryStateReturn<Q> {
29
24
  /** Function to execute the query with current parameters */
30
25
  execute: () => Promise<void>;
31
26
  }
@@ -1 +1 @@
1
- {"version":3,"file":"useQuery.d.ts","sourceRoot":"","sources":["../../src/wow/useQuery.ts"],"names":[],"mappings":"AAaA,OAAO,EAGL,uBAAuB,EACvB,wBAAwB,EAEzB,MAAM,SAAS,CAAC;AAEjB,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACrE,OAAO,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAE7C;;;;;GAKG;AACH,MAAM,WAAW,eAAe,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,YAAY,CACrD,SAAQ,wBAAwB,CAAC,CAAC,EAAE,CAAC,CAAC,EACpC,iBAAiB,EACjB,kBAAkB;IACpB,mCAAmC;IACnC,YAAY,EAAE,CAAC,CAAC;IAEhB,kFAAkF;IAClF,OAAO,EAAE,CACP,KAAK,EAAE,CAAC,EACR,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAChC,eAAe,CAAC,EAAE,eAAe,KAC9B,OAAO,CAAC,CAAC,CAAC,CAAC;CACjB;AAED;;;;;GAKG;AACH,MAAM,WAAW,cAAc,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,YAAY,CACpD,SAAQ,uBAAuB,CAAC,CAAC,EAAE,CAAC,CAAC;IACrC;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAC,CAAC;IAClB,8CAA8C;IAC9C,QAAQ,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC;IAC7B,4DAA4D;IAC5D,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CAC9B;AAGD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,YAAY,EAC7C,OAAO,EAAE,eAAe,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,GAChC,cAAc,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CA4DzB"}
1
+ {"version":3,"file":"useQuery.d.ts","sourceRoot":"","sources":["../../src/wow/useQuery.ts"],"names":[],"mappings":"AAaA,OAAO,EAGL,uBAAuB,EACvB,wBAAwB,EAEzB,MAAM,SAAS,CAAC;AAEjB,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACrE,OAAO,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAC7C,OAAO,EAAiB,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAErE;;;;;GAKG;AACH,MAAM,WAAW,eAAe,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,YAAY,CACrD,SAAQ,wBAAwB,CAAC,CAAC,EAAE,CAAC,CAAC,EACpC,iBAAiB,EACjB,kBAAkB;IACpB,mCAAmC;IACnC,YAAY,EAAE,CAAC,CAAC;IAEhB,kFAAkF;IAClF,OAAO,EAAE,CACP,KAAK,EAAE,CAAC,EACR,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAChC,eAAe,CAAC,EAAE,eAAe,KAC9B,OAAO,CAAC,CAAC,CAAC,CAAC;CACjB;AAED;;;;;GAKG;AACH,MAAM,WAAW,cAAc,CAC7B,CAAC,EACD,CAAC,EACD,CAAC,GAAG,YAAY,CAChB,SAAQ,uBAAuB,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,mBAAmB,CAAC,CAAC,CAAC;IAC7D,4DAA4D;IAC5D,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CAC9B;AAGD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,YAAY,EAC7C,OAAO,EAAE,eAAe,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,GAChC,cAAc,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CA8DzB"}