@ahoo-wang/wow-react 10.0.0-alpha.6 → 10.0.0-beta.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.
- package/README.md +138 -105
- package/README.zh-CN.md +115 -85
- package/dist/hooks/useAggregateQuery.d.ts +69 -0
- package/dist/hooks/useCountQuery.d.ts +5 -8
- package/dist/hooks/useCursorQuery.d.ts +90 -0
- package/dist/hooks/useListQuery.d.ts +7 -10
- package/dist/hooks/usePagedQuery.d.ts +8 -11
- package/dist/hooks/useSingleQuery.d.ts +14 -13
- package/dist/hooks/useStreamQuery.d.ts +109 -0
- package/dist/index.d.ts +3 -6
- package/dist/index.es.js +158 -178
- package/dist/index.es.js.map +1 -1
- package/dist/internal/queryTransitions.d.ts +6 -6
- package/dist/internal/readStreamRows.d.ts +5 -3
- package/dist/internal/useQueryRunner.d.ts +11 -9
- package/dist/types.d.ts +34 -22
- package/package.json +7 -8
- package/dist/hooks/useFetcherCountQuery.d.ts +0 -66
- package/dist/hooks/useFetcherListQuery.d.ts +0 -80
- package/dist/hooks/useFetcherListStreamQuery.d.ts +0 -66
- package/dist/hooks/useFetcherPagedQuery.d.ts +0 -75
- package/dist/hooks/useFetcherSingleQuery.d.ts +0 -71
- package/dist/hooks/useListStreamQuery.d.ts +0 -93
- package/dist/internal/endpoint.d.ts +0 -38
- package/dist/internal/useEndpointRunner.d.ts +0 -8
- package/dist/internal/useListStream.d.ts +0 -11
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { AggregationQuery } from '@ahoo-wang/wow-client/dsl';
|
|
2
|
+
import { QueryHookOptions, QueryHookReturn } from '../types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Options of {@link useAggregateQuery}: an aggregation and an `execute` that
|
|
5
|
+
* resolves to its rows, as a query client's `aggregate` does.
|
|
6
|
+
*
|
|
7
|
+
* @template ROW - One row of the result: the groups and metrics by alias
|
|
8
|
+
* @template FIELDS - The field names the aggregation may use
|
|
9
|
+
* @template Q - The aggregation type: `AggregationQuery` by default
|
|
10
|
+
*/
|
|
11
|
+
export interface UseAggregateQueryOptions<ROW, FIELDS extends string = string, Q extends AggregationQuery<FIELDS, string> = AggregationQuery<FIELDS, string>> extends QueryHookOptions<Q, ROW[]> {
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* What {@link useAggregateQuery} returns: the rows as `result`.
|
|
15
|
+
*
|
|
16
|
+
* @template ROW - One row of the result
|
|
17
|
+
* @template FIELDS - The field names the aggregation may use
|
|
18
|
+
* @template Q - The aggregation type: `AggregationQuery` by default
|
|
19
|
+
*/
|
|
20
|
+
export interface UseAggregateQueryReturn<ROW, FIELDS extends string = string, Q extends AggregationQuery<FIELDS, string> = AggregationQuery<FIELDS, string>> extends QueryHookReturn<Q, ROW[]> {
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Runs an aggregation through your own `execute` function and keeps its rows
|
|
24
|
+
* as state: typically a query client's `aggregate`, one row per group.
|
|
25
|
+
*
|
|
26
|
+
* `execute` receives the aggregation and the request options of the run, its
|
|
27
|
+
* `signal` included; hand them on so that a newer query, `abort()`
|
|
28
|
+
* or an unmount cancels the request. The aggregation runs on mount and
|
|
29
|
+
* whenever `query` or `setQuery()` changes it; set `autoExecute: false` to
|
|
30
|
+
* run it only through `execute()`.
|
|
31
|
+
*
|
|
32
|
+
* Returns `result` (the rows, or `undefined` before the first success),
|
|
33
|
+
* `loading`, `error`, `problem`, `status`, `execute`, `abort`, `reset`,
|
|
34
|
+
* `getQuery` and `setQuery`.
|
|
35
|
+
*
|
|
36
|
+
* @template ROW - One row of the result: the groups and metrics by alias
|
|
37
|
+
* @template FIELDS - The field names the aggregation may use. With a client
|
|
38
|
+
* whose fields are narrower than `string`, as a generated client's are,
|
|
39
|
+
* pass them here: TypeScript does not infer them from `execute` once
|
|
40
|
+
* `ROW` is given
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```tsx
|
|
44
|
+
* import type { SnapshotQueryClient } from '@ahoo-wang/wow-client';
|
|
45
|
+
* import { aggregation } from '@ahoo-wang/wow-client/dsl';
|
|
46
|
+
* import { useAggregateQuery } from '@ahoo-wang/wow-react';
|
|
47
|
+
*
|
|
48
|
+
* type StatusCount = { status: string; orders: number };
|
|
49
|
+
*
|
|
50
|
+
* function OrdersByStatus({ client }: { client: SnapshotQueryClient<OrderState, OrderFields> }) {
|
|
51
|
+
* const { result, error } = useAggregateQuery<StatusCount, OrderFields>({
|
|
52
|
+
* query: {
|
|
53
|
+
* groupBy: [aggregation.terms('state.status', 'status')],
|
|
54
|
+
* metrics: [aggregation.count('orders')],
|
|
55
|
+
* },
|
|
56
|
+
* execute: (query, options) =>
|
|
57
|
+
* client.aggregate<StatusCount>(query, options),
|
|
58
|
+
* });
|
|
59
|
+
* if (error) return <p role="alert">{error.message}</p>;
|
|
60
|
+
* return <ul>{result?.map(row => <li key={row.status}>{row.status}: {row.orders}</li>)}</ul>;
|
|
61
|
+
* }
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
export declare function useAggregateQuery<ROW, FIELDS extends string = string>(options: UseAggregateQueryOptions<ROW, FIELDS, AggregationQuery<FIELDS, string>>): UseAggregateQueryReturn<ROW, FIELDS, AggregationQuery<FIELDS, string>>;
|
|
65
|
+
/**
|
|
66
|
+
* {@link useAggregateQuery} typed by an aggregation `Q` of your own, a
|
|
67
|
+
* subtype of `AggregationQuery`.
|
|
68
|
+
*/
|
|
69
|
+
export declare function useAggregateQuery<ROW, FIELDS extends string = string, Q extends AggregationQuery<FIELDS, string> = AggregationQuery<FIELDS, string>>(options: UseAggregateQueryOptions<ROW, FIELDS, Q>): UseAggregateQueryReturn<ROW, FIELDS, Q>;
|
|
@@ -5,19 +5,17 @@ import { QueryHookOptions, QueryHookReturn } from '../types.js';
|
|
|
5
5
|
* how many rows it matches.
|
|
6
6
|
*
|
|
7
7
|
* @template FIELDS - The field names the filter may use
|
|
8
|
-
* @template E - The error type, `Error` by default
|
|
9
8
|
* @template Q - The filter type: `FilterExpression` by default
|
|
10
9
|
*/
|
|
11
|
-
export interface UseCountQueryOptions<FIELDS extends string = string,
|
|
10
|
+
export interface UseCountQueryOptions<FIELDS extends string = string, Q extends CountQuery<FIELDS> = CountQuery<FIELDS>> extends QueryHookOptions<Q, CountResult> {
|
|
12
11
|
}
|
|
13
12
|
/**
|
|
14
13
|
* What {@link useCountQuery} returns: the count as `result`.
|
|
15
14
|
*
|
|
16
15
|
* @template FIELDS - The field names the filter may use
|
|
17
|
-
* @template E - The error type, `Error` by default
|
|
18
16
|
* @template Q - The filter type: `FilterExpression` by default
|
|
19
17
|
*/
|
|
20
|
-
export interface UseCountQueryReturn<FIELDS extends string = string,
|
|
18
|
+
export interface UseCountQueryReturn<FIELDS extends string = string, Q extends CountQuery<FIELDS> = CountQuery<FIELDS>> extends QueryHookReturn<Q, CountResult> {
|
|
21
19
|
}
|
|
22
20
|
/**
|
|
23
21
|
* Counts what a filter matches through your own `execute` function and keeps
|
|
@@ -30,14 +28,13 @@ export interface UseCountQueryReturn<FIELDS extends string = string, E = Error,
|
|
|
30
28
|
* it only through `execute()`.
|
|
31
29
|
*
|
|
32
30
|
* Returns `result` (the count, or `undefined` before the first success),
|
|
33
|
-
* `loading`, `error`, `status`, `execute`, `abort`, `reset`, `getQuery` and
|
|
31
|
+
* `loading`, `error`, `problem`, `status`, `execute`, `abort`, `reset`, `getQuery` and
|
|
34
32
|
* `setQuery`.
|
|
35
33
|
*
|
|
36
34
|
* @template FIELDS - The field names the filter may use. With a client whose
|
|
37
35
|
* fields are narrower than `string`, as a generated client's are, pass
|
|
38
36
|
* them here; inferred from the first filter, they would admit only its
|
|
39
37
|
* fields in `setQuery()`
|
|
40
|
-
* @template E - The error type, `Error` by default
|
|
41
38
|
*
|
|
42
39
|
* @example
|
|
43
40
|
* ```tsx
|
|
@@ -56,9 +53,9 @@ export interface UseCountQueryReturn<FIELDS extends string = string, E = Error,
|
|
|
56
53
|
* }
|
|
57
54
|
* ```
|
|
58
55
|
*/
|
|
59
|
-
export declare function useCountQuery<FIELDS extends string = string
|
|
56
|
+
export declare function useCountQuery<FIELDS extends string = string>(options: UseCountQueryOptions<FIELDS, CountQuery<FIELDS>>): UseCountQueryReturn<FIELDS, CountQuery<FIELDS>>;
|
|
60
57
|
/**
|
|
61
58
|
* {@link useCountQuery} typed by a query `Q` of your own, a subtype of
|
|
62
59
|
* `CountQuery`.
|
|
63
60
|
*/
|
|
64
|
-
export declare function useCountQuery<FIELDS extends string = string,
|
|
61
|
+
export declare function useCountQuery<FIELDS extends string = string, Q extends CountQuery<FIELDS> = CountQuery<FIELDS>>(options: UseCountQueryOptions<FIELDS, Q>): UseCountQueryReturn<FIELDS, Q>;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { CursorPage, CursorQuery } from '@ahoo-wang/wow-client/dsl';
|
|
2
|
+
import { QueryExecutor, QueryHookOptions, QueryHookReturn } from '../types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Options of {@link useCursorQuery}: a cursor query without its `cursor`,
|
|
5
|
+
* which the hook owns, and an `execute` that reads one page, as a query
|
|
6
|
+
* client's `cursor` does.
|
|
7
|
+
*
|
|
8
|
+
* `onSuccess` receives each page as it arrives.
|
|
9
|
+
*
|
|
10
|
+
* @template R - One row of a page
|
|
11
|
+
* @template FIELDS - The field names the query may use
|
|
12
|
+
* @template Q - The query type: `CursorQuery` without `cursor` by default
|
|
13
|
+
*/
|
|
14
|
+
export interface UseCursorQueryOptions<R, FIELDS extends string = string, Q extends Omit<CursorQuery<FIELDS>, 'cursor'> = Omit<CursorQuery<FIELDS>, 'cursor'>> extends Omit<QueryHookOptions<Q, CursorPage<R>>, 'execute'> {
|
|
15
|
+
/**
|
|
16
|
+
* Reads one page: the query with the `cursor` of the page, `null` for
|
|
17
|
+
* the first.
|
|
18
|
+
*/
|
|
19
|
+
execute: QueryExecutor<Q & {
|
|
20
|
+
cursor: string | null;
|
|
21
|
+
}, CursorPage<R>>;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* What {@link useCursorQuery} returns: the rows of every page read so far,
|
|
25
|
+
* and how to read the next one.
|
|
26
|
+
*
|
|
27
|
+
* - `items` — the rows of the pages read so far, in order. A new query and
|
|
28
|
+
* `execute()` start again from the first page with no rows; `reset()`
|
|
29
|
+
* empties them; `abort()` and an error keep them.
|
|
30
|
+
* - `hasMore` — whether a page follows the last one read.
|
|
31
|
+
* - `fetchNext()` — reads the next page and appends its rows; it does
|
|
32
|
+
* nothing while a page is loading or when no page follows. A failed
|
|
33
|
+
* `fetchNext()` keeps the rows, so calling it again retries that page.
|
|
34
|
+
*/
|
|
35
|
+
export interface UseCursorQueryReturn<R, FIELDS extends string = string, Q extends Omit<CursorQuery<FIELDS>, 'cursor'> = Omit<CursorQuery<FIELDS>, 'cursor'>> extends Omit<QueryHookReturn<Q, CursorPage<R>>, 'result'> {
|
|
36
|
+
/** The rows of every page read so far, a new array at every page. */
|
|
37
|
+
items: R[];
|
|
38
|
+
/** Whether a page follows the last one read. */
|
|
39
|
+
hasMore: boolean;
|
|
40
|
+
/** Reads the next page and appends its rows. */
|
|
41
|
+
fetchNext: () => Promise<void>;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Reads a forward-only cursor query page by page and keeps the rows as
|
|
45
|
+
* state: typically a query client's `cursor`, for a list that grows as the
|
|
46
|
+
* reader scrolls.
|
|
47
|
+
*
|
|
48
|
+
* The hook owns the cursor: every run of the query starts at the first page,
|
|
49
|
+
* and `fetchNext()` hands `execute` the `nextCursor` of the page before.
|
|
50
|
+
* `execute` receives the request options of the run, its `signal` included;
|
|
51
|
+
* hand them on so that a newer query, `abort()` or an unmount cancels the
|
|
52
|
+
* request. The first page is read on mount and whenever `query` or
|
|
53
|
+
* `setQuery()` changes the query; set `autoExecute: false` to read it only
|
|
54
|
+
* through `execute()`.
|
|
55
|
+
*
|
|
56
|
+
* @template R - One row of a page
|
|
57
|
+
* @template FIELDS - The field names the query may use. With a client whose
|
|
58
|
+
* fields are narrower than `string`, as a generated client's are, pass
|
|
59
|
+
* them here: TypeScript does not infer them from `execute` once `R` is
|
|
60
|
+
* given
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```tsx
|
|
64
|
+
* import type { SnapshotQueryClient, Snapshot } from '@ahoo-wang/wow-client';
|
|
65
|
+
* import { filter } from '@ahoo-wang/wow-client/dsl';
|
|
66
|
+
* import { useCursorQuery } from '@ahoo-wang/wow-react';
|
|
67
|
+
*
|
|
68
|
+
* function PaidOrders({ client }: { client: SnapshotQueryClient<OrderState, OrderFields> }) {
|
|
69
|
+
* const { items, hasMore, loading, error, fetchNext } = useCursorQuery<Snapshot<OrderState>, OrderFields>({
|
|
70
|
+
* query: { filter: filter.eq('state.status', 'PAID'), size: 50 },
|
|
71
|
+
* execute: (query, options) =>
|
|
72
|
+
* client.cursor(query, options),
|
|
73
|
+
* });
|
|
74
|
+
* if (error) return <p role="alert">{error.message}</p>;
|
|
75
|
+
* return (
|
|
76
|
+
* <>
|
|
77
|
+
* <ul>{items.map(order => <li key={order.aggregateId}>{order.state.status}</li>)}</ul>
|
|
78
|
+
* {hasMore && <button disabled={loading} onClick={() => void fetchNext()}>More</button>}
|
|
79
|
+
* </>
|
|
80
|
+
* );
|
|
81
|
+
* }
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
export declare function useCursorQuery<R, FIELDS extends string = string>(options: UseCursorQueryOptions<R, FIELDS, Omit<CursorQuery<FIELDS>, 'cursor'>>): UseCursorQueryReturn<R, FIELDS, Omit<CursorQuery<FIELDS>, 'cursor'>>;
|
|
85
|
+
/**
|
|
86
|
+
* {@link useCursorQuery} typed by a query `Q` of your own, a subtype of
|
|
87
|
+
* `CursorQuery` without `cursor`, such as one whose literal projection
|
|
88
|
+
* narrows the rows.
|
|
89
|
+
*/
|
|
90
|
+
export declare function useCursorQuery<R, FIELDS extends string = string, Q extends Omit<CursorQuery<FIELDS>, 'cursor'> = Omit<CursorQuery<FIELDS>, 'cursor'>>(options: UseCursorQueryOptions<R, FIELDS, Q>): UseCursorQueryReturn<R, FIELDS, Q>;
|
|
@@ -6,20 +6,18 @@ import { QueryHookOptions, QueryHookReturn } from '../types.js';
|
|
|
6
6
|
*
|
|
7
7
|
* @template R - One row of the list
|
|
8
8
|
* @template FIELDS - The field names the query may use
|
|
9
|
-
* @template E - The error type, `Error` by default
|
|
10
9
|
* @template Q - The query type: `FilterListQuery` by default
|
|
11
10
|
*/
|
|
12
|
-
export interface UseListQueryOptions<R, FIELDS extends string = string,
|
|
11
|
+
export interface UseListQueryOptions<R, FIELDS extends string = string, Q extends FilterListQuery<FIELDS> = FilterListQuery<FIELDS>> extends QueryHookOptions<Q, R[]> {
|
|
13
12
|
}
|
|
14
13
|
/**
|
|
15
14
|
* What {@link useListQuery} returns: the rows as `result`.
|
|
16
15
|
*
|
|
17
16
|
* @template R - One row of the list
|
|
18
17
|
* @template FIELDS - The field names the query may use
|
|
19
|
-
* @template E - The error type, `Error` by default
|
|
20
18
|
* @template Q - The query type: `FilterListQuery` by default
|
|
21
19
|
*/
|
|
22
|
-
export interface UseListQueryReturn<R, FIELDS extends string = string,
|
|
20
|
+
export interface UseListQueryReturn<R, FIELDS extends string = string, Q extends FilterListQuery<FIELDS> = FilterListQuery<FIELDS>> extends QueryHookReturn<Q, R[]> {
|
|
23
21
|
}
|
|
24
22
|
/**
|
|
25
23
|
* Runs a list query through your own `execute` function and keeps the rows
|
|
@@ -32,23 +30,22 @@ export interface UseListQueryReturn<R, FIELDS extends string = string, E = Error
|
|
|
32
30
|
* through `execute()`.
|
|
33
31
|
*
|
|
34
32
|
* Returns `result` (the rows, or `undefined` before the first success),
|
|
35
|
-
* `loading`, `error`, `status`, `execute`, `abort`, `reset`, `getQuery` and
|
|
33
|
+
* `loading`, `error`, `problem`, `status`, `execute`, `abort`, `reset`, `getQuery` and
|
|
36
34
|
* `setQuery`.
|
|
37
35
|
*
|
|
38
36
|
* @template R - One row of the list
|
|
39
37
|
* @template FIELDS - The field names the query may use. With a client whose
|
|
40
38
|
* fields are narrower than `string`, as a generated client's are, pass
|
|
41
39
|
* them here: TypeScript does not infer them from `execute` once `R` is given
|
|
42
|
-
* @template E - The error type, `Error` by default
|
|
43
40
|
*
|
|
44
41
|
* @example
|
|
45
42
|
* ```tsx
|
|
46
|
-
* import type { SnapshotQueryClient,
|
|
43
|
+
* import type { SnapshotQueryClient, Snapshot } from '@ahoo-wang/wow-client';
|
|
47
44
|
* import { desc, filter, listQuery } from '@ahoo-wang/wow-client/dsl';
|
|
48
45
|
* import { useListQuery } from '@ahoo-wang/wow-react';
|
|
49
46
|
*
|
|
50
47
|
* function LatestOrders({ client }: { client: SnapshotQueryClient<OrderState, OrderFields> }) {
|
|
51
|
-
* const { result, loading, error } = useListQuery<
|
|
48
|
+
* const { result, loading, error } = useListQuery<Snapshot<OrderState>, OrderFields>({
|
|
52
49
|
* initialQuery: listQuery({
|
|
53
50
|
* filter: filter.eq('state.status', 'PAID'),
|
|
54
51
|
* sort: [desc('createTime')],
|
|
@@ -63,9 +60,9 @@ export interface UseListQueryReturn<R, FIELDS extends string = string, E = Error
|
|
|
63
60
|
* }
|
|
64
61
|
* ```
|
|
65
62
|
*/
|
|
66
|
-
export declare function useListQuery<R, FIELDS extends string = string
|
|
63
|
+
export declare function useListQuery<R, FIELDS extends string = string>(options: UseListQueryOptions<R, FIELDS, FilterListQuery<FIELDS>>): UseListQueryReturn<R, FIELDS, FilterListQuery<FIELDS>>;
|
|
67
64
|
/**
|
|
68
65
|
* {@link useListQuery} typed by a query `Q` of your own, a subtype of
|
|
69
66
|
* `FilterListQuery`, such as one whose literal projection narrows the result.
|
|
70
67
|
*/
|
|
71
|
-
export declare function useListQuery<R, FIELDS extends string = string,
|
|
68
|
+
export declare function useListQuery<R, FIELDS extends string = string, Q extends FilterListQuery<FIELDS> = FilterListQuery<FIELDS>>(options: UseListQueryOptions<R, FIELDS, Q>): UseListQueryReturn<R, FIELDS, Q>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { PagedQuery,
|
|
1
|
+
import { PagedQuery, PagedList } from '@ahoo-wang/wow-client/dsl';
|
|
2
2
|
import { QueryHookOptions, QueryHookReturn } from '../types.js';
|
|
3
3
|
/**
|
|
4
4
|
* Options of {@link usePagedQuery}: a paged query and an `execute` that
|
|
@@ -6,10 +6,9 @@ import { QueryHookOptions, QueryHookReturn } from '../types.js';
|
|
|
6
6
|
*
|
|
7
7
|
* @template R - One row of the page
|
|
8
8
|
* @template FIELDS - The field names the query may use
|
|
9
|
-
* @template E - The error type, `Error` by default
|
|
10
9
|
* @template Q - The query type: `FilterPagedQuery` by default
|
|
11
10
|
*/
|
|
12
|
-
export interface UsePagedQueryOptions<R, FIELDS extends string = string,
|
|
11
|
+
export interface UsePagedQueryOptions<R, FIELDS extends string = string, Q extends PagedQuery<FIELDS> = PagedQuery<FIELDS>> extends QueryHookOptions<Q, PagedList<R>> {
|
|
13
12
|
}
|
|
14
13
|
/**
|
|
15
14
|
* What {@link usePagedQuery} returns: the page (`total` and `list`) as
|
|
@@ -17,10 +16,9 @@ export interface UsePagedQueryOptions<R, FIELDS extends string = string, E = Err
|
|
|
17
16
|
*
|
|
18
17
|
* @template R - One row of the page
|
|
19
18
|
* @template FIELDS - The field names the query may use
|
|
20
|
-
* @template E - The error type, `Error` by default
|
|
21
19
|
* @template Q - The query type: `FilterPagedQuery` by default
|
|
22
20
|
*/
|
|
23
|
-
export interface UsePagedQueryReturn<R, FIELDS extends string = string,
|
|
21
|
+
export interface UsePagedQueryReturn<R, FIELDS extends string = string, Q extends PagedQuery<FIELDS> = PagedQuery<FIELDS>> extends QueryHookReturn<Q, PagedList<R>> {
|
|
24
22
|
}
|
|
25
23
|
/**
|
|
26
24
|
* Runs a paged query through your own `execute` function and keeps the page
|
|
@@ -33,23 +31,22 @@ export interface UsePagedQueryReturn<R, FIELDS extends string = string, E = Erro
|
|
|
33
31
|
* `autoExecute: false` to run it only through `execute()`.
|
|
34
32
|
*
|
|
35
33
|
* Returns `result` (`{ total, list }`, or `undefined` before the first
|
|
36
|
-
* success), `loading`, `error`, `status`, `execute`, `abort`, `reset`,
|
|
34
|
+
* success), `loading`, `error`, `problem`, `status`, `execute`, `abort`, `reset`,
|
|
37
35
|
* `getQuery` and `setQuery`.
|
|
38
36
|
*
|
|
39
37
|
* @template R - One row of the page
|
|
40
38
|
* @template FIELDS - The field names the query may use. With a client whose
|
|
41
39
|
* fields are narrower than `string`, as a generated client's are, pass
|
|
42
40
|
* them here: TypeScript does not infer them from `execute` once `R` is given
|
|
43
|
-
* @template E - The error type, `Error` by default
|
|
44
41
|
*
|
|
45
42
|
* @example
|
|
46
43
|
* ```tsx
|
|
47
|
-
* import type { SnapshotQueryClient,
|
|
44
|
+
* import type { SnapshotQueryClient, Snapshot } from '@ahoo-wang/wow-client';
|
|
48
45
|
* import { filter, pagedQuery } from '@ahoo-wang/wow-client/dsl';
|
|
49
46
|
* import { usePagedQuery } from '@ahoo-wang/wow-react';
|
|
50
47
|
*
|
|
51
48
|
* function PaidOrders({ client, page }: { client: SnapshotQueryClient<OrderState, OrderFields>; page: number }) {
|
|
52
|
-
* const { result, loading, error } = usePagedQuery<
|
|
49
|
+
* const { result, loading, error } = usePagedQuery<Snapshot<OrderState>, OrderFields>({
|
|
53
50
|
* query: pagedQuery({
|
|
54
51
|
* filter: filter.eq('state.status', 'PAID'),
|
|
55
52
|
* pagination: { index: page, size: 20 },
|
|
@@ -63,9 +60,9 @@ export interface UsePagedQueryReturn<R, FIELDS extends string = string, E = Erro
|
|
|
63
60
|
* }
|
|
64
61
|
* ```
|
|
65
62
|
*/
|
|
66
|
-
export declare function usePagedQuery<R, FIELDS extends string = string
|
|
63
|
+
export declare function usePagedQuery<R, FIELDS extends string = string>(options: UsePagedQueryOptions<R, FIELDS, PagedQuery<FIELDS>>): UsePagedQueryReturn<R, FIELDS, PagedQuery<FIELDS>>;
|
|
67
64
|
/**
|
|
68
65
|
* {@link usePagedQuery} typed by a query `Q` of your own, a subtype of
|
|
69
66
|
* `PagedQuery`, such as one whose literal projection narrows the result.
|
|
70
67
|
*/
|
|
71
|
-
export declare function usePagedQuery<R, FIELDS extends string = string,
|
|
68
|
+
export declare function usePagedQuery<R, FIELDS extends string = string, Q extends PagedQuery<FIELDS> = PagedQuery<FIELDS>>(options: UsePagedQueryOptions<R, FIELDS, Q>): UsePagedQueryReturn<R, FIELDS, Q>;
|
|
@@ -2,24 +2,25 @@ import { FilterSingleQuery } from '@ahoo-wang/wow-client/dsl';
|
|
|
2
2
|
import { QueryHookOptions, QueryHookReturn } from '../types.js';
|
|
3
3
|
/**
|
|
4
4
|
* Options of {@link useSingleQuery}: a single query and an `execute` that
|
|
5
|
-
* resolves to one item
|
|
5
|
+
* resolves to one item, or `undefined` when nothing matches, as a query
|
|
6
|
+
* client's `single` does.
|
|
6
7
|
*
|
|
7
8
|
* @template R - The item the query returns
|
|
8
9
|
* @template FIELDS - The field names the query may use
|
|
9
|
-
* @template E - The error type, `Error` by default
|
|
10
10
|
* @template Q - The query type: `FilterSingleQuery` by default
|
|
11
11
|
*/
|
|
12
|
-
export interface UseSingleQueryOptions<R, FIELDS extends string = string,
|
|
12
|
+
export interface UseSingleQueryOptions<R, FIELDS extends string = string, Q extends FilterSingleQuery<FIELDS> = FilterSingleQuery<FIELDS>> extends QueryHookOptions<Q, R | undefined> {
|
|
13
13
|
}
|
|
14
14
|
/**
|
|
15
|
-
* What {@link useSingleQuery} returns: the item as `result
|
|
15
|
+
* What {@link useSingleQuery} returns: the item as `result`, `undefined`
|
|
16
|
+
* before the first run and when nothing matched (tell them apart by
|
|
17
|
+
* `status`).
|
|
16
18
|
*
|
|
17
19
|
* @template R - The item the query returns
|
|
18
20
|
* @template FIELDS - The field names the query may use
|
|
19
|
-
* @template E - The error type, `Error` by default
|
|
20
21
|
* @template Q - The query type: `FilterSingleQuery` by default
|
|
21
22
|
*/
|
|
22
|
-
export interface UseSingleQueryReturn<R, FIELDS extends string = string,
|
|
23
|
+
export interface UseSingleQueryReturn<R, FIELDS extends string = string, Q extends FilterSingleQuery<FIELDS> = FilterSingleQuery<FIELDS>> extends QueryHookReturn<Q, R | undefined> {
|
|
23
24
|
}
|
|
24
25
|
/**
|
|
25
26
|
* Runs a single query through your own `execute` function and keeps the
|
|
@@ -32,36 +33,36 @@ export interface UseSingleQueryReturn<R, FIELDS extends string = string, E = Err
|
|
|
32
33
|
* through `execute()`.
|
|
33
34
|
*
|
|
34
35
|
* Returns `result` (the item, or `undefined` before the first success),
|
|
35
|
-
* `loading`, `error`, `status`, `execute`, `abort`, `reset`, `getQuery` and
|
|
36
|
+
* `loading`, `error`, `problem`, `status`, `execute`, `abort`, `reset`, `getQuery` and
|
|
36
37
|
* `setQuery`.
|
|
37
38
|
*
|
|
38
39
|
* @template R - The item the query returns
|
|
39
40
|
* @template FIELDS - The field names the query may use. With a client whose
|
|
40
41
|
* fields are narrower than `string`, as a generated client's are, pass
|
|
41
42
|
* them here: TypeScript does not infer them from `execute` once `R` is given
|
|
42
|
-
* @template E - The error type, `Error` by default
|
|
43
43
|
*
|
|
44
44
|
* @example
|
|
45
45
|
* ```tsx
|
|
46
|
-
* import type { SnapshotQueryClient,
|
|
46
|
+
* import type { SnapshotQueryClient, Snapshot } from '@ahoo-wang/wow-client';
|
|
47
47
|
* import { filter, singleQuery } from '@ahoo-wang/wow-client/dsl';
|
|
48
48
|
* import { useSingleQuery } from '@ahoo-wang/wow-react';
|
|
49
49
|
*
|
|
50
50
|
* function OrderStatus({ client, id }: { client: SnapshotQueryClient<OrderState, OrderFields>; id: string }) {
|
|
51
|
-
* const { result, loading, error } = useSingleQuery<
|
|
51
|
+
* const { result, loading, error } = useSingleQuery<Snapshot<OrderState>, OrderFields>({
|
|
52
52
|
* query: singleQuery({ filter: filter.id(id) }),
|
|
53
53
|
* execute: (query, options) =>
|
|
54
54
|
* client.single(query, options),
|
|
55
55
|
* });
|
|
56
56
|
* if (error) return <p role="alert">{error.message}</p>;
|
|
57
|
-
* if (loading
|
|
57
|
+
* if (loading) return <p>Loading…</p>;
|
|
58
|
+
* if (!result) return <p>No such order.</p>;
|
|
58
59
|
* return <p>{result.state.status}</p>;
|
|
59
60
|
* }
|
|
60
61
|
* ```
|
|
61
62
|
*/
|
|
62
|
-
export declare function useSingleQuery<R, FIELDS extends string = string
|
|
63
|
+
export declare function useSingleQuery<R, FIELDS extends string = string>(options: UseSingleQueryOptions<R, FIELDS, FilterSingleQuery<FIELDS>>): UseSingleQueryReturn<R, FIELDS, FilterSingleQuery<FIELDS>>;
|
|
63
64
|
/**
|
|
64
65
|
* {@link useSingleQuery} typed by a query `Q` of your own, a subtype of
|
|
65
66
|
* `FilterSingleQuery`, such as one whose literal projection narrows the result.
|
|
66
67
|
*/
|
|
67
|
-
export declare function useSingleQuery<R, FIELDS extends string = string,
|
|
68
|
+
export declare function useSingleQuery<R, FIELDS extends string = string, Q extends FilterSingleQuery<FIELDS> = FilterSingleQuery<FIELDS>>(options: UseSingleQueryOptions<R, FIELDS, Q>): UseSingleQueryReturn<R, FIELDS, Q>;
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { StreamOptions } from '@ahoo-wang/wow-client';
|
|
2
|
+
import { FilterListQuery } from '@ahoo-wang/wow-client/dsl';
|
|
3
|
+
import { QueryHookOptions, QueryHookReturn, StreamExecutor } from '../types.js';
|
|
4
|
+
/**
|
|
5
|
+
* Options of {@link useStreamQuery}: those of every query hook, with an
|
|
6
|
+
* `execute` that opens the stream and `requestOptions` that take the
|
|
7
|
+
* stream's `resume`.
|
|
8
|
+
*
|
|
9
|
+
* `onSuccess` receives every record once the stream has ended.
|
|
10
|
+
*
|
|
11
|
+
* @template R - One record of the stream
|
|
12
|
+
* @template FIELDS - The field names the query may use
|
|
13
|
+
* @template Q - The query type: `FilterListQuery` by default
|
|
14
|
+
*/
|
|
15
|
+
export interface UseStreamQueryOptions<R, FIELDS extends string = string, Q extends FilterListQuery<FIELDS> = FilterListQuery<FIELDS>> extends Omit<QueryHookOptions<Q, R[], StreamOptions>, 'execute'> {
|
|
16
|
+
/**
|
|
17
|
+
* Opens the stream for a query: a query client's `streamRows`, which
|
|
18
|
+
* answers each record with its position.
|
|
19
|
+
*/
|
|
20
|
+
execute: StreamExecutor<R, Q>;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* What {@link useStreamQuery} returns: the records as they arrive, whether
|
|
24
|
+
* the stream has ended, and where it stopped.
|
|
25
|
+
*
|
|
26
|
+
* - `items` — the records of the current query received so far, in order,
|
|
27
|
+
* updated at most about once a frame (every 16 ms) while they arrive. A new
|
|
28
|
+
* query starts from an empty list; `reset()` empties it; `abort()` and an
|
|
29
|
+
* error keep the records received before them.
|
|
30
|
+
* - `position` — the position of the last record in `items` that had one.
|
|
31
|
+
* - `done` — the stream ended normally and `items` holds every record.
|
|
32
|
+
* - `loading` — from the request until the stream ends, fails or is aborted.
|
|
33
|
+
* - `error` — the request failed, or the server sent an error event in the
|
|
34
|
+
* stream; `problem` is then the `WowProblemError`, with the problem's
|
|
35
|
+
* `code`.
|
|
36
|
+
* - `status` — `idle`, `loading`, `success` (the same as `done`) or `error`.
|
|
37
|
+
* - `execute()` runs the current query again and aborts the stream in flight;
|
|
38
|
+
* `abort()` stops the stream; `reset()` stops it and empties `items`.
|
|
39
|
+
*/
|
|
40
|
+
export interface UseStreamQueryReturn<R, FIELDS extends string = string, Q extends FilterListQuery<FIELDS> = FilterListQuery<FIELDS>> extends Omit<QueryHookReturn<Q, R[]>, 'result'> {
|
|
41
|
+
/**
|
|
42
|
+
* The records of the current query received so far, a new array at every
|
|
43
|
+
* update.
|
|
44
|
+
*/
|
|
45
|
+
items: R[];
|
|
46
|
+
/**
|
|
47
|
+
* The position of the last record received that had one, or `undefined`
|
|
48
|
+
* before the first such record: hand it back as
|
|
49
|
+
* `requestOptions.resume.lastEventId` to go on after it, on a later mount.
|
|
50
|
+
* A new query and `reset()` clear it with `items`.
|
|
51
|
+
*/
|
|
52
|
+
position: string | undefined;
|
|
53
|
+
/** Whether the stream ended normally, so `items` holds every record. */
|
|
54
|
+
done: boolean;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Streams the records of a list query as server-sent events and keeps them
|
|
58
|
+
* as state.
|
|
59
|
+
*
|
|
60
|
+
* The hook owns the stream: it reads it, collects the records into `items`,
|
|
61
|
+
* and cancels it when a newer query starts, on `abort()` or `reset()`, and on
|
|
62
|
+
* unmount. Components render `items` and never hold a reader, so the hook is
|
|
63
|
+
* safe under StrictMode. `autoExecute` defaults to `true`.
|
|
64
|
+
*
|
|
65
|
+
* Pass a query client's `streamRows` as `execute`: it sends
|
|
66
|
+
* `Accept: text/event-stream`, resumes a stream the server ends to be
|
|
67
|
+
* resumed or that is cut off, turns error events into a `WowProblemError`,
|
|
68
|
+
* and answers each record with its position, which the hook keeps as
|
|
69
|
+
* `position`.
|
|
70
|
+
*
|
|
71
|
+
* To go on where a stream stopped after the component remounts (a page
|
|
72
|
+
* change, a reload), keep `position` somewhere that outlives it and hand it
|
|
73
|
+
* back as `requestOptions: { resume: { lastEventId: saved } }`; the first
|
|
74
|
+
* request then asks for the records after it.
|
|
75
|
+
*
|
|
76
|
+
* @template R - One record of the stream
|
|
77
|
+
* @template FIELDS - The field names the query may use. With a client whose
|
|
78
|
+
* fields are narrower than `string`, as a generated client's are, pass
|
|
79
|
+
* them here: TypeScript does not infer them from `execute` once `R` is given
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```tsx
|
|
83
|
+
* import type { SnapshotQueryClient, Snapshot } from '@ahoo-wang/wow-client';
|
|
84
|
+
* import { filter, listQuery } from '@ahoo-wang/wow-client/dsl';
|
|
85
|
+
* import { useStreamQuery } from '@ahoo-wang/wow-react';
|
|
86
|
+
*
|
|
87
|
+
* function PaidOrders({ client }: { client: SnapshotQueryClient<OrderState, OrderFields> }) {
|
|
88
|
+
* const { items, done, loading, error, abort } = useStreamQuery<Snapshot<OrderState>, OrderFields>({
|
|
89
|
+
* initialQuery: listQuery({ filter: filter.eq('state.status', 'PAID') }),
|
|
90
|
+
* execute: (query, options) =>
|
|
91
|
+
* client.streamRows(query, options),
|
|
92
|
+
* });
|
|
93
|
+
* if (error) return <p role="alert">{error.message}</p>;
|
|
94
|
+
* return (
|
|
95
|
+
* <>
|
|
96
|
+
* <ul>{items.map(order => <li key={order.aggregateId}>{order.state.status}</li>)}</ul>
|
|
97
|
+
* {loading && <button onClick={abort}>Stop</button>}
|
|
98
|
+
* {done && <p>{items.length} orders</p>}
|
|
99
|
+
* </>
|
|
100
|
+
* );
|
|
101
|
+
* }
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
export declare function useStreamQuery<R, FIELDS extends string = string>(options: UseStreamQueryOptions<R, FIELDS, FilterListQuery<FIELDS>>): UseStreamQueryReturn<R, FIELDS, FilterListQuery<FIELDS>>;
|
|
105
|
+
/**
|
|
106
|
+
* {@link useStreamQuery} typed by a query `Q` of your own, a subtype of
|
|
107
|
+
* `FilterListQuery`, such as one whose literal projection narrows the result.
|
|
108
|
+
*/
|
|
109
|
+
export declare function useStreamQuery<R, FIELDS extends string = string, Q extends FilterListQuery<FIELDS> = FilterListQuery<FIELDS>>(options: UseStreamQueryOptions<R, FIELDS, Q>): UseStreamQueryReturn<R, FIELDS, Q>;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
|
+
export * from './hooks/useAggregateQuery.js';
|
|
1
2
|
export * from './hooks/useCountQuery.js';
|
|
2
|
-
export * from './hooks/
|
|
3
|
-
export * from './hooks/useFetcherListQuery.js';
|
|
4
|
-
export * from './hooks/useFetcherListStreamQuery.js';
|
|
5
|
-
export * from './hooks/useFetcherPagedQuery.js';
|
|
6
|
-
export * from './hooks/useFetcherSingleQuery.js';
|
|
3
|
+
export * from './hooks/useCursorQuery.js';
|
|
7
4
|
export * from './hooks/useListQuery.js';
|
|
8
|
-
export * from './hooks/useListStreamQuery.js';
|
|
9
5
|
export * from './hooks/usePagedQuery.js';
|
|
10
6
|
export * from './hooks/useSingleQuery.js';
|
|
7
|
+
export * from './hooks/useStreamQuery.js';
|
|
11
8
|
export * from './types.js';
|