@ahoo-wang/wow-react 10.0.0-alpha.5 → 10.0.0-alpha.7
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 +9 -11
- 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
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ahoo-wang/wow-react",
|
|
3
|
-
"version": "10.0.0-alpha.
|
|
3
|
+
"version": "10.0.0-alpha.7",
|
|
4
4
|
"description": "React hooks for Wow (https://github.com/Ahoo-Wang/Wow) queries: single, list, paged, count and list-stream. Requires React 19.3 or later.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"wow",
|
|
@@ -47,9 +47,8 @@
|
|
|
47
47
|
"dequal": "^2.0.3"
|
|
48
48
|
},
|
|
49
49
|
"peerDependencies": {
|
|
50
|
-
"@ahoo-wang/fetcher": "^5.1.5 || ^6.0.0",
|
|
51
50
|
"react": "^19.0.0",
|
|
52
|
-
"@ahoo-wang/wow-client": "~10.0.0-alpha.
|
|
51
|
+
"@ahoo-wang/wow-client": "~10.0.0-alpha.7"
|
|
53
52
|
},
|
|
54
53
|
"devDependencies": {
|
|
55
54
|
"@ahoo-wang/fetcher": "^6.0.1",
|
|
@@ -60,21 +59,20 @@
|
|
|
60
59
|
"@types/react": "^19.3.0",
|
|
61
60
|
"@types/react-dom": "^19.3.0",
|
|
62
61
|
"@vitejs/plugin-react": "6.1.2",
|
|
63
|
-
"@vitest/coverage-v8": "4.1.
|
|
62
|
+
"@vitest/coverage-v8": "4.1.12",
|
|
64
63
|
"babel-plugin-react-compiler": "1.0.0",
|
|
65
64
|
"eslint": "^10.13.0",
|
|
66
|
-
"eslint-plugin-react-compiler": "19.1.0-rc.2",
|
|
67
65
|
"eslint-plugin-react-hooks": "^7.1.1",
|
|
68
66
|
"globals": "^17.13.0",
|
|
69
67
|
"jsdom": "^30.1.3",
|
|
70
|
-
"react": "^19.3.
|
|
71
|
-
"react-dom": "^19.3.
|
|
68
|
+
"react": "^19.3.1",
|
|
69
|
+
"react-dom": "^19.3.1",
|
|
72
70
|
"typescript": "~6.0.3",
|
|
73
|
-
"typescript-eslint": "^8.
|
|
71
|
+
"typescript-eslint": "^8.72.0",
|
|
74
72
|
"unplugin-dts": "1.1.2",
|
|
75
|
-
"vite": "8.3.
|
|
76
|
-
"vitest": "^4.1.
|
|
77
|
-
"@ahoo-wang/wow-client": "~10.0.0-alpha.
|
|
73
|
+
"vite": "8.3.5",
|
|
74
|
+
"vitest": "^4.1.12",
|
|
75
|
+
"@ahoo-wang/wow-client": "~10.0.0-alpha.7"
|
|
78
76
|
},
|
|
79
77
|
"scripts": {
|
|
80
78
|
"build": "vite build && pnpm test:package",
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { CountQuery, CountResult } from '@ahoo-wang/wow-client';
|
|
2
|
-
import { Endpoint } from '../internal/endpoint.js';
|
|
3
|
-
import { QueryHookOptions, QueryHookReturn } from '../types.js';
|
|
4
|
-
/**
|
|
5
|
-
* Options of {@link useFetcherCountQuery}: those of every query hook, with
|
|
6
|
-
* the endpoint and the Fetcher in place of `execute`.
|
|
7
|
-
*
|
|
8
|
-
* @template FIELDS - The field names the filter may use
|
|
9
|
-
* @template E - The error type, `Error` by default
|
|
10
|
-
* @template Q - The filter type: `FilterExpression` by default
|
|
11
|
-
*/
|
|
12
|
-
export interface UseFetcherCountQueryOptions<FIELDS extends string = string, E = Error, Q extends CountQuery<FIELDS> = CountQuery<FIELDS>> extends Omit<QueryHookOptions<Q, CountResult, E>, 'execute'>, Endpoint {
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* What {@link useFetcherCountQuery} returns: the count as `result`.
|
|
16
|
-
*
|
|
17
|
-
* @template FIELDS - The field names the filter may use
|
|
18
|
-
* @template E - The error type, `Error` by default
|
|
19
|
-
* @template Q - The filter type: `FilterExpression` by default
|
|
20
|
-
*/
|
|
21
|
-
export interface UseFetcherCountQueryReturn<FIELDS extends string = string, E = Error, Q extends CountQuery<FIELDS> = CountQuery<FIELDS>> extends QueryHookReturn<Q, CountResult, E> {
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* POSTs a count to a Wow query route through a Fetcher and keeps the
|
|
25
|
-
* count as state.
|
|
26
|
-
*
|
|
27
|
-
* `url` is resolved against the Fetcher's `baseURL`; `fetcher` is a Fetcher
|
|
28
|
-
* or the name of a registered one, the default Fetcher when omitted. The
|
|
29
|
-
* count runs on mount and whenever `query` or `setQuery()` changes the
|
|
30
|
-
* filter; set `autoExecute: false` to run it only through `execute()`. A
|
|
31
|
-
* newer filter aborts the request in flight, so a late response never
|
|
32
|
-
* overwrites a newer one; an unmount aborts it too.
|
|
33
|
-
*
|
|
34
|
-
* A change of `url`, or of `fetcher`, runs the query again. A Fetcher is
|
|
35
|
-
* compared by its name, or by its `baseURL` when it has none, so one created
|
|
36
|
-
* inline in render does not run it on every render.
|
|
37
|
-
*
|
|
38
|
-
* Returns `result` (the count, or `undefined` before the first success),
|
|
39
|
-
* `loading`, `error`, `status`, `execute`, `abort`, `reset`, `getQuery` and
|
|
40
|
-
* `setQuery`. A failed request sets `error` to the
|
|
41
|
-
* `WowProblemError` the route answered with.
|
|
42
|
-
*
|
|
43
|
-
* @template FIELDS - The field names the filter may use
|
|
44
|
-
* @template E - The error type, `Error` by default
|
|
45
|
-
*
|
|
46
|
-
* @example
|
|
47
|
-
* ```tsx
|
|
48
|
-
* import { filter } from '@ahoo-wang/wow-client/dsl';
|
|
49
|
-
* import { useFetcherCountQuery } from '@ahoo-wang/wow-react';
|
|
50
|
-
*
|
|
51
|
-
* function PaidCount() {
|
|
52
|
-
* const { result, error } = useFetcherCountQuery({
|
|
53
|
-
* url: '/shop/order/snapshot/query',
|
|
54
|
-
* initialQuery: { filter: filter.eq('state.status', 'PAID') },
|
|
55
|
-
* });
|
|
56
|
-
* if (error) return <p role="alert">{error.message}</p>;
|
|
57
|
-
* return <p>{result?.count ?? '…'} paid orders</p>;
|
|
58
|
-
* }
|
|
59
|
-
* ```
|
|
60
|
-
*/
|
|
61
|
-
export declare function useFetcherCountQuery<FIELDS extends string = string, E = Error>(options: UseFetcherCountQueryOptions<FIELDS, E, CountQuery<FIELDS>>): UseFetcherCountQueryReturn<FIELDS, E, CountQuery<FIELDS>>;
|
|
62
|
-
/**
|
|
63
|
-
* {@link useFetcherCountQuery} typed by a query `Q` of your own, a subtype of
|
|
64
|
-
* `CountQuery`.
|
|
65
|
-
*/
|
|
66
|
-
export declare function useFetcherCountQuery<FIELDS extends string = string, E = Error, Q extends CountQuery<FIELDS> = CountQuery<FIELDS>>(options: UseFetcherCountQueryOptions<FIELDS, E, Q>): UseFetcherCountQueryReturn<FIELDS, E, Q>;
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import { FilterListQuery } from '@ahoo-wang/wow-client/dsl';
|
|
2
|
-
import { Endpoint } from '../internal/endpoint.js';
|
|
3
|
-
import { QueryHookOptions, QueryHookReturn } from '../types.js';
|
|
4
|
-
/**
|
|
5
|
-
* Options of {@link useFetcherListQuery}: those of every query hook, with the
|
|
6
|
-
* endpoint and the Fetcher in place of `execute`.
|
|
7
|
-
*
|
|
8
|
-
* @template R - One row of the list
|
|
9
|
-
* @template FIELDS - The field names the query may use
|
|
10
|
-
* @template E - The error type, `Error` by default
|
|
11
|
-
* @template Q - The query type: `FilterListQuery` by default
|
|
12
|
-
*/
|
|
13
|
-
export interface UseFetcherListQueryOptions<R, FIELDS extends string = string, E = Error, Q extends FilterListQuery<FIELDS> = FilterListQuery<FIELDS>> extends Omit<QueryHookOptions<Q, R[], E>, 'execute'>, Endpoint {
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* What {@link useFetcherListQuery} returns: the rows as `result`.
|
|
17
|
-
*
|
|
18
|
-
* @template R - One row of the list
|
|
19
|
-
* @template FIELDS - The field names the query may use
|
|
20
|
-
* @template E - The error type, `Error` by default
|
|
21
|
-
* @template Q - The query type: `FilterListQuery` by default
|
|
22
|
-
*/
|
|
23
|
-
export interface UseFetcherListQueryReturn<R, FIELDS extends string = string, E = Error, Q extends FilterListQuery<FIELDS> = FilterListQuery<FIELDS>> extends QueryHookReturn<Q, R[], E> {
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* POSTs a list query to a Wow query route through a Fetcher and keeps the rows
|
|
27
|
-
* as state.
|
|
28
|
-
*
|
|
29
|
-
* `url` is resolved against the Fetcher's `baseURL`; `fetcher` is a Fetcher
|
|
30
|
-
* or the name of a registered one, the default Fetcher when omitted. The
|
|
31
|
-
* query runs on mount and whenever `query` or `setQuery()` changes it; set
|
|
32
|
-
* `autoExecute: false` to run it only through `execute()`. A newer query
|
|
33
|
-
* aborts the request in flight, so a late response never overwrites a newer
|
|
34
|
-
* one; an unmount aborts it too.
|
|
35
|
-
*
|
|
36
|
-
* A change of `url`, or of `fetcher`, runs the query again. A Fetcher is
|
|
37
|
-
* compared by its name, or by its `baseURL` when it has none, so one created
|
|
38
|
-
* inline in render does not run it on every render.
|
|
39
|
-
*
|
|
40
|
-
* Returns `result` (the rows, or `undefined` before the first success),
|
|
41
|
-
* `loading`, `error`, `status`, `execute`, `abort`, `reset`, `getQuery` and
|
|
42
|
-
* `setQuery`. A failed request sets `error` to the
|
|
43
|
-
* `WowProblemError` the route answered with.
|
|
44
|
-
*
|
|
45
|
-
* @template R - One row of the list
|
|
46
|
-
* @template FIELDS - The field names the query may use
|
|
47
|
-
* @template E - The error type, `Error` by default
|
|
48
|
-
*
|
|
49
|
-
* @example
|
|
50
|
-
* ```tsx
|
|
51
|
-
* import { desc, filter, listQuery } from '@ahoo-wang/wow-client/dsl';
|
|
52
|
-
* import type { SnapshotRecord } from '@ahoo-wang/wow-client';
|
|
53
|
-
* import { useFetcherListQuery } from '@ahoo-wang/wow-react';
|
|
54
|
-
*
|
|
55
|
-
* function LatestOrders() {
|
|
56
|
-
* const { result, loading, error, execute } = useFetcherListQuery<SnapshotRecord<OrderState>>({
|
|
57
|
-
* url: '/shop/order/snapshot/query',
|
|
58
|
-
* initialQuery: listQuery({
|
|
59
|
-
* filter: filter.eq('state.status', 'PAID'),
|
|
60
|
-
* sort: [desc('createTime')],
|
|
61
|
-
* limit: 20,
|
|
62
|
-
* }),
|
|
63
|
-
* });
|
|
64
|
-
* if (error) return <p role="alert">{error.message}</p>;
|
|
65
|
-
* if (loading || !result) return <p>Loading…</p>;
|
|
66
|
-
* return (
|
|
67
|
-
* <>
|
|
68
|
-
* <ul>{result.map(order => <li key={order.aggregateId}>{order.state.status}</li>)}</ul>
|
|
69
|
-
* <button onClick={execute}>Refresh</button>
|
|
70
|
-
* </>
|
|
71
|
-
* );
|
|
72
|
-
* }
|
|
73
|
-
* ```
|
|
74
|
-
*/
|
|
75
|
-
export declare function useFetcherListQuery<R, FIELDS extends string = string, E = Error>(options: UseFetcherListQueryOptions<R, FIELDS, E, FilterListQuery<FIELDS>>): UseFetcherListQueryReturn<R, FIELDS, E, FilterListQuery<FIELDS>>;
|
|
76
|
-
/**
|
|
77
|
-
* {@link useFetcherListQuery} typed by a query `Q` of your own, a subtype of
|
|
78
|
-
* `FilterListQuery`, such as one whose literal projection narrows the result.
|
|
79
|
-
*/
|
|
80
|
-
export declare function useFetcherListQuery<R, FIELDS extends string = string, E = Error, Q extends FilterListQuery<FIELDS> = FilterListQuery<FIELDS>>(options: UseFetcherListQueryOptions<R, FIELDS, E, Q>): UseFetcherListQueryReturn<R, FIELDS, E, Q>;
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { FilterListQuery } from '@ahoo-wang/wow-client/dsl';
|
|
2
|
-
import { Endpoint } from '../internal/endpoint.js';
|
|
3
|
-
import { UseListStreamQueryOptions, UseListStreamQueryReturn } from './useListStreamQuery.js';
|
|
4
|
-
/**
|
|
5
|
-
* Options of {@link useFetcherListStreamQuery}: those of `useListStreamQuery`,
|
|
6
|
-
* with the endpoint and the Fetcher in place of `execute`.
|
|
7
|
-
*
|
|
8
|
-
* @template R - One row of the stream: the `data` of each event
|
|
9
|
-
* @template FIELDS - The field names the query may use
|
|
10
|
-
* @template E - The error type, `Error` by default: a failed request
|
|
11
|
-
* rejects with a `WowProblemError`, and so does an error event in the
|
|
12
|
-
* stream
|
|
13
|
-
* @template Q - The query type: `FilterListQuery` by default
|
|
14
|
-
*/
|
|
15
|
-
export interface UseFetcherListStreamQueryOptions<R, FIELDS extends string = string, E = Error, Q extends FilterListQuery<FIELDS> = FilterListQuery<FIELDS>> extends Omit<UseListStreamQueryOptions<R, FIELDS, E, Q>, 'execute'>, Endpoint {
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* What {@link useFetcherListStreamQuery} returns; see
|
|
19
|
-
* {@link UseListStreamQueryReturn}.
|
|
20
|
-
*/
|
|
21
|
-
export interface UseFetcherListStreamQueryReturn<R, FIELDS extends string = string, E = Error, Q extends FilterListQuery<FIELDS> = FilterListQuery<FIELDS>> extends UseListStreamQueryReturn<R, FIELDS, E, Q> {
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Streams the rows of a list query from a Wow query route and keeps them as
|
|
25
|
-
* state; see `useListStreamQuery` for the state it returns.
|
|
26
|
-
*
|
|
27
|
-
* It POSTs the query to `url` through `fetcher` (the default Fetcher when
|
|
28
|
-
* omitted) with `Accept: text/event-stream`, the header a Wow service needs
|
|
29
|
-
* to answer with an event stream rather than JSON. An error event in the
|
|
30
|
-
* stream ends it with a `WowProblemError` in `error`.
|
|
31
|
-
*
|
|
32
|
-
* A change of `url`, or of `fetcher`, runs the query again. A Fetcher is
|
|
33
|
-
* compared by its name, or by its `baseURL` when it has none, so one created
|
|
34
|
-
* inline in render does not run it on every render.
|
|
35
|
-
*
|
|
36
|
-
* @template R - One row of the stream: the `data` of each event
|
|
37
|
-
* @template FIELDS - The field names the query may use
|
|
38
|
-
* @template E - The error type
|
|
39
|
-
*
|
|
40
|
-
* @example
|
|
41
|
-
* ```tsx
|
|
42
|
-
* import { filter, listQuery } from '@ahoo-wang/wow-client/dsl';
|
|
43
|
-
* import type { SnapshotRecord } from '@ahoo-wang/wow-client';
|
|
44
|
-
* import { useFetcherListStreamQuery } from '@ahoo-wang/wow-react';
|
|
45
|
-
*
|
|
46
|
-
* function PaidOrders() {
|
|
47
|
-
* const { items, done, loading, error } = useFetcherListStreamQuery<SnapshotRecord<OrderState>>({
|
|
48
|
-
* url: '/shop/order/snapshot/query',
|
|
49
|
-
* initialQuery: listQuery({ filter: filter.eq('state.status', 'PAID') }),
|
|
50
|
-
* });
|
|
51
|
-
* if (error) return <p role="alert">{error.message}</p>;
|
|
52
|
-
* return (
|
|
53
|
-
* <>
|
|
54
|
-
* <ul>{items.map(order => <li key={order.aggregateId}>{order.state.status}</li>)}</ul>
|
|
55
|
-
* {loading ? <p>Loading…</p> : done && <p>{items.length} orders</p>}
|
|
56
|
-
* </>
|
|
57
|
-
* );
|
|
58
|
-
* }
|
|
59
|
-
* ```
|
|
60
|
-
*/
|
|
61
|
-
export declare function useFetcherListStreamQuery<R, FIELDS extends string = string, E = Error>(options: UseFetcherListStreamQueryOptions<R, FIELDS, E, FilterListQuery<FIELDS>>): UseFetcherListStreamQueryReturn<R, FIELDS, E, FilterListQuery<FIELDS>>;
|
|
62
|
-
/**
|
|
63
|
-
* {@link useFetcherListStreamQuery} typed by a query `Q` of your own, a subtype of
|
|
64
|
-
* `FilterListQuery`, such as one whose literal projection narrows the result.
|
|
65
|
-
*/
|
|
66
|
-
export declare function useFetcherListStreamQuery<R, FIELDS extends string = string, E = Error, Q extends FilterListQuery<FIELDS> = FilterListQuery<FIELDS>>(options: UseFetcherListStreamQueryOptions<R, FIELDS, E, Q>): UseFetcherListStreamQueryReturn<R, FIELDS, E, Q>;
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import { PagedQuery, PagedResult } from '@ahoo-wang/wow-client';
|
|
2
|
-
import { Endpoint } from '../internal/endpoint.js';
|
|
3
|
-
import { QueryHookOptions, QueryHookReturn } from '../types.js';
|
|
4
|
-
/**
|
|
5
|
-
* Options of {@link useFetcherPagedQuery}: those of every query hook, with
|
|
6
|
-
* the endpoint and the Fetcher in place of `execute`.
|
|
7
|
-
*
|
|
8
|
-
* @template R - One row of the page
|
|
9
|
-
* @template FIELDS - The field names the query may use
|
|
10
|
-
* @template E - The error type, `Error` by default
|
|
11
|
-
* @template Q - The query type: `FilterPagedQuery` by default
|
|
12
|
-
*/
|
|
13
|
-
export interface UseFetcherPagedQueryOptions<R, FIELDS extends string = string, E = Error, Q extends PagedQuery<FIELDS> = PagedQuery<FIELDS>> extends Omit<QueryHookOptions<Q, PagedResult<R>, E>, 'execute'>, Endpoint {
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* What {@link useFetcherPagedQuery} returns: the page (`total` and `list`)
|
|
17
|
-
* as `result`.
|
|
18
|
-
*
|
|
19
|
-
* @template R - One row of the page
|
|
20
|
-
* @template FIELDS - The field names the query may use
|
|
21
|
-
* @template E - The error type, `Error` by default
|
|
22
|
-
* @template Q - The query type: `FilterPagedQuery` by default
|
|
23
|
-
*/
|
|
24
|
-
export interface UseFetcherPagedQueryReturn<R, FIELDS extends string = string, E = Error, Q extends PagedQuery<FIELDS> = PagedQuery<FIELDS>> extends QueryHookReturn<Q, PagedResult<R>, E> {
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* POSTs a paged query to a Wow query route through a Fetcher and keeps the page
|
|
28
|
-
* as state.
|
|
29
|
-
*
|
|
30
|
-
* `url` is resolved against the Fetcher's `baseURL`; `fetcher` is a Fetcher
|
|
31
|
-
* or the name of a registered one, the default Fetcher when omitted. The
|
|
32
|
-
* query runs on mount and whenever `query` or `setQuery()` changes it — turn
|
|
33
|
-
* pages with `setQuery()`; set `autoExecute: false` to run it only through
|
|
34
|
-
* `execute()`. A newer query aborts the request in flight, so a late response
|
|
35
|
-
* never overwrites a newer one; an unmount aborts it too.
|
|
36
|
-
*
|
|
37
|
-
* A change of `url`, or of `fetcher`, runs the query again. A Fetcher is
|
|
38
|
-
* compared by its name, or by its `baseURL` when it has none, so one created
|
|
39
|
-
* inline in render does not run it on every render.
|
|
40
|
-
*
|
|
41
|
-
* Returns `result` (`{ total, list }`, or `undefined` before the first
|
|
42
|
-
* success), `loading`, `error`, `status`, `execute`, `abort`, `reset`,
|
|
43
|
-
* `getQuery` and `setQuery`. A failed request sets `error` to the
|
|
44
|
-
* `WowProblemError` the route answered with.
|
|
45
|
-
*
|
|
46
|
-
* @template R - One row of the page
|
|
47
|
-
* @template FIELDS - The field names the query may use
|
|
48
|
-
* @template E - The error type, `Error` by default
|
|
49
|
-
*
|
|
50
|
-
* @example
|
|
51
|
-
* ```tsx
|
|
52
|
-
* import { filter, pagedQuery } from '@ahoo-wang/wow-client/dsl';
|
|
53
|
-
* import type { SnapshotRecord } from '@ahoo-wang/wow-client';
|
|
54
|
-
* import { useFetcherPagedQuery } from '@ahoo-wang/wow-react';
|
|
55
|
-
*
|
|
56
|
-
* function PaidOrders({ page }: { page: number }) {
|
|
57
|
-
* const { result, loading, error } = useFetcherPagedQuery<SnapshotRecord<OrderState>>({
|
|
58
|
-
* url: '/shop/order/snapshot/query',
|
|
59
|
-
* query: pagedQuery({
|
|
60
|
-
* filter: filter.eq('state.status', 'PAID'),
|
|
61
|
-
* pagination: { index: page, size: 20 },
|
|
62
|
-
* }),
|
|
63
|
-
* });
|
|
64
|
-
* if (error) return <p role="alert">{error.message}</p>;
|
|
65
|
-
* if (loading || !result) return <p>Loading…</p>;
|
|
66
|
-
* return <p>{result.list.length} of {result.total}</p>;
|
|
67
|
-
* }
|
|
68
|
-
* ```
|
|
69
|
-
*/
|
|
70
|
-
export declare function useFetcherPagedQuery<R, FIELDS extends string = string, E = Error>(options: UseFetcherPagedQueryOptions<R, FIELDS, E, PagedQuery<FIELDS>>): UseFetcherPagedQueryReturn<R, FIELDS, E, PagedQuery<FIELDS>>;
|
|
71
|
-
/**
|
|
72
|
-
* {@link useFetcherPagedQuery} typed by a query `Q` of your own, a subtype of
|
|
73
|
-
* `PagedQuery`, such as one whose literal projection narrows the result.
|
|
74
|
-
*/
|
|
75
|
-
export declare function useFetcherPagedQuery<R, FIELDS extends string = string, E = Error, Q extends PagedQuery<FIELDS> = PagedQuery<FIELDS>>(options: UseFetcherPagedQueryOptions<R, FIELDS, E, Q>): UseFetcherPagedQueryReturn<R, FIELDS, E, Q>;
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import { FilterSingleQuery } from '@ahoo-wang/wow-client/dsl';
|
|
2
|
-
import { Endpoint } from '../internal/endpoint.js';
|
|
3
|
-
import { QueryHookOptions, QueryHookReturn } from '../types.js';
|
|
4
|
-
/**
|
|
5
|
-
* Options of {@link useFetcherSingleQuery}: those of every query hook, with
|
|
6
|
-
* the endpoint and the Fetcher in place of `execute`.
|
|
7
|
-
*
|
|
8
|
-
* @template R - The item the query returns
|
|
9
|
-
* @template FIELDS - The field names the query may use
|
|
10
|
-
* @template E - The error type, `Error` by default
|
|
11
|
-
* @template Q - The query type: `FilterSingleQuery` by default
|
|
12
|
-
*/
|
|
13
|
-
export interface UseFetcherSingleQueryOptions<R, FIELDS extends string = string, E = Error, Q extends FilterSingleQuery<FIELDS> = FilterSingleQuery<FIELDS>> extends Omit<QueryHookOptions<Q, R, E>, 'execute'>, Endpoint {
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* What {@link useFetcherSingleQuery} returns: the item as `result`.
|
|
17
|
-
*
|
|
18
|
-
* @template R - The item the query returns
|
|
19
|
-
* @template FIELDS - The field names the query may use
|
|
20
|
-
* @template E - The error type, `Error` by default
|
|
21
|
-
* @template Q - The query type: `FilterSingleQuery` by default
|
|
22
|
-
*/
|
|
23
|
-
export interface UseFetcherSingleQueryReturn<R, FIELDS extends string = string, E = Error, Q extends FilterSingleQuery<FIELDS> = FilterSingleQuery<FIELDS>> extends QueryHookReturn<Q, R, E> {
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* POSTs a single query to a Wow query route through a Fetcher and keeps the
|
|
27
|
-
* result as state.
|
|
28
|
-
*
|
|
29
|
-
* `url` is resolved against the Fetcher's `baseURL`; `fetcher` is a Fetcher
|
|
30
|
-
* or the name of a registered one, the default Fetcher when omitted. The
|
|
31
|
-
* query runs on mount and whenever `query` or `setQuery()` changes it; set
|
|
32
|
-
* `autoExecute: false` to run it only through `execute()`. A newer query
|
|
33
|
-
* aborts the request in flight, so a late response never overwrites a newer
|
|
34
|
-
* one; an unmount aborts it too.
|
|
35
|
-
*
|
|
36
|
-
* A change of `url`, or of `fetcher`, runs the query again. A Fetcher is
|
|
37
|
-
* compared by its name, or by its `baseURL` when it has none, so one created
|
|
38
|
-
* inline in render does not run it on every render.
|
|
39
|
-
*
|
|
40
|
-
* Returns `result` (the item, or `undefined` before the first success),
|
|
41
|
-
* `loading`, `error`, `status`, `execute`, `abort`, `reset`, `getQuery` and
|
|
42
|
-
* `setQuery`. A failed request sets `error` to the
|
|
43
|
-
* `WowProblemError` the route answered with.
|
|
44
|
-
*
|
|
45
|
-
* @template R - The item the query returns
|
|
46
|
-
* @template FIELDS - The field names the query may use
|
|
47
|
-
* @template E - The error type, `Error` by default
|
|
48
|
-
*
|
|
49
|
-
* @example
|
|
50
|
-
* ```tsx
|
|
51
|
-
* import { filter, singleQuery } from '@ahoo-wang/wow-client/dsl';
|
|
52
|
-
* import type { SnapshotRecord } from '@ahoo-wang/wow-client';
|
|
53
|
-
* import { useFetcherSingleQuery } from '@ahoo-wang/wow-react';
|
|
54
|
-
*
|
|
55
|
-
* function OrderStatus({ id }: { id: string }) {
|
|
56
|
-
* const { result, loading, error } = useFetcherSingleQuery<SnapshotRecord<OrderState>>({
|
|
57
|
-
* url: '/shop/order/snapshot/query',
|
|
58
|
-
* query: singleQuery({ filter: filter.id(id) }),
|
|
59
|
-
* });
|
|
60
|
-
* if (error) return <p role="alert">{error.message}</p>;
|
|
61
|
-
* if (loading || !result) return <p>Loading…</p>;
|
|
62
|
-
* return <p>{result.state.status}</p>;
|
|
63
|
-
* }
|
|
64
|
-
* ```
|
|
65
|
-
*/
|
|
66
|
-
export declare function useFetcherSingleQuery<R, FIELDS extends string = string, E = Error>(options: UseFetcherSingleQueryOptions<R, FIELDS, E, FilterSingleQuery<FIELDS>>): UseFetcherSingleQueryReturn<R, FIELDS, E, FilterSingleQuery<FIELDS>>;
|
|
67
|
-
/**
|
|
68
|
-
* {@link useFetcherSingleQuery} typed by a query `Q` of your own, a subtype of
|
|
69
|
-
* `FilterSingleQuery`, such as one whose literal projection narrows the result.
|
|
70
|
-
*/
|
|
71
|
-
export declare function useFetcherSingleQuery<R, FIELDS extends string = string, E = Error, Q extends FilterSingleQuery<FIELDS> = FilterSingleQuery<FIELDS>>(options: UseFetcherSingleQueryOptions<R, FIELDS, E, Q>): UseFetcherSingleQueryReturn<R, FIELDS, E, Q>;
|
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import { FilterListQuery } from '@ahoo-wang/wow-client/dsl';
|
|
2
|
-
import { ListStreamExecutor, QueryHookOptions, QueryHookReturn } from '../types.js';
|
|
3
|
-
/**
|
|
4
|
-
* Options of {@link useListStreamQuery}: those of every query hook, with an
|
|
5
|
-
* `execute` that opens the stream.
|
|
6
|
-
*
|
|
7
|
-
* `onSuccess` receives every row once the stream has ended.
|
|
8
|
-
*
|
|
9
|
-
* @template R - One row of the stream: the `data` of each event
|
|
10
|
-
* @template FIELDS - The field names the query may use
|
|
11
|
-
* @template E - The error type, `Error` by default: a failed request
|
|
12
|
-
* rejects with a `WowProblemError`, and so does an error event in the
|
|
13
|
-
* stream
|
|
14
|
-
* @template Q - The query type: `FilterListQuery` by default
|
|
15
|
-
*/
|
|
16
|
-
export interface UseListStreamQueryOptions<R, FIELDS extends string = string, E = Error, Q extends FilterListQuery<FIELDS> = FilterListQuery<FIELDS>> extends Omit<QueryHookOptions<Q, R[], E>, 'execute'> {
|
|
17
|
-
/** Opens the stream for a query. */
|
|
18
|
-
execute: ListStreamExecutor<R, Q>;
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* What {@link useListStreamQuery} and `useFetcherListStreamQuery` return: the
|
|
22
|
-
* rows as they arrive, and whether the stream has ended.
|
|
23
|
-
*
|
|
24
|
-
* - `items` — the rows of the current query received so far, in order,
|
|
25
|
-
* updated at most about once a frame (every 16 ms) while they arrive. A new
|
|
26
|
-
* query starts from an empty list; `reset()` empties it; `abort()` and an
|
|
27
|
-
* error keep the rows received before them.
|
|
28
|
-
* - `done` — the stream ended normally and `items` holds every row.
|
|
29
|
-
* - `loading` — from the request until the stream ends, fails or is aborted.
|
|
30
|
-
* - `error` — the request failed, or the server sent an error event in the
|
|
31
|
-
* stream: a `WowProblemError`, with the problem's `code`.
|
|
32
|
-
* - `status` — `idle`, `loading`, `success` (the same as `done`) or `error`.
|
|
33
|
-
* - `execute()` runs the current query again and aborts the stream in flight;
|
|
34
|
-
* `abort()` stops the stream; `reset()` stops it and empties `items`.
|
|
35
|
-
*/
|
|
36
|
-
export interface UseListStreamQueryReturn<R, FIELDS extends string = string, E = Error, Q extends FilterListQuery<FIELDS> = FilterListQuery<FIELDS>> extends Omit<QueryHookReturn<Q, R[], E>, 'result'> {
|
|
37
|
-
/**
|
|
38
|
-
* The rows of the current query received so far, a new array at every
|
|
39
|
-
* update.
|
|
40
|
-
*/
|
|
41
|
-
items: R[];
|
|
42
|
-
/** Whether the stream ended normally, so `items` holds every row. */
|
|
43
|
-
done: boolean;
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Streams the rows of a list query as server-sent events and keeps them as
|
|
47
|
-
* state.
|
|
48
|
-
*
|
|
49
|
-
* The hook owns the stream: it reads it, collects the rows into `items`, and
|
|
50
|
-
* cancels it when a newer query starts, on `abort()` or `reset()`, and on
|
|
51
|
-
* unmount. Components render `items` and never hold a reader, so the hook is
|
|
52
|
-
* safe under StrictMode. `autoExecute` defaults to `true`.
|
|
53
|
-
*
|
|
54
|
-
* Pass a query client's `listStream` as `execute` — it sends
|
|
55
|
-
* `Accept: text/event-stream` and turns error events into a
|
|
56
|
-
* `WowProblemError` — or
|
|
57
|
-
* use `useFetcherListStreamQuery` with a URL.
|
|
58
|
-
*
|
|
59
|
-
* @template R - One row of the stream: the `data` of each event
|
|
60
|
-
* @template FIELDS - The field names the query may use. With a client whose
|
|
61
|
-
* fields are narrower than `string`, as a generated client's are, pass
|
|
62
|
-
* them here: TypeScript does not infer them from `execute` once `R` is given
|
|
63
|
-
* @template E - The error type
|
|
64
|
-
*
|
|
65
|
-
* @example
|
|
66
|
-
* ```tsx
|
|
67
|
-
* import type { SnapshotQueryClient, SnapshotRecord } from '@ahoo-wang/wow-client';
|
|
68
|
-
* import { filter, listQuery } from '@ahoo-wang/wow-client/dsl';
|
|
69
|
-
* import { useListStreamQuery } from '@ahoo-wang/wow-react';
|
|
70
|
-
*
|
|
71
|
-
* function PaidOrders({ client }: { client: SnapshotQueryClient<OrderState, OrderFields> }) {
|
|
72
|
-
* const { items, done, loading, error, abort } = useListStreamQuery<SnapshotRecord<OrderState>, OrderFields>({
|
|
73
|
-
* initialQuery: listQuery({ filter: filter.eq('state.status', 'PAID') }),
|
|
74
|
-
* execute: (query, options) =>
|
|
75
|
-
* client.listStream(query, options),
|
|
76
|
-
* });
|
|
77
|
-
* if (error) return <p role="alert">{error.message}</p>;
|
|
78
|
-
* return (
|
|
79
|
-
* <>
|
|
80
|
-
* <ul>{items.map(order => <li key={order.aggregateId}>{order.state.status}</li>)}</ul>
|
|
81
|
-
* {loading && <button onClick={abort}>Stop</button>}
|
|
82
|
-
* {done && <p>{items.length} orders</p>}
|
|
83
|
-
* </>
|
|
84
|
-
* );
|
|
85
|
-
* }
|
|
86
|
-
* ```
|
|
87
|
-
*/
|
|
88
|
-
export declare function useListStreamQuery<R, FIELDS extends string = string, E = Error>(options: UseListStreamQueryOptions<R, FIELDS, E, FilterListQuery<FIELDS>>): UseListStreamQueryReturn<R, FIELDS, E, FilterListQuery<FIELDS>>;
|
|
89
|
-
/**
|
|
90
|
-
* {@link useListStreamQuery} typed by a query `Q` of your own, a subtype of
|
|
91
|
-
* `FilterListQuery`, such as one whose literal projection narrows the result.
|
|
92
|
-
*/
|
|
93
|
-
export declare function useListStreamQuery<R, FIELDS extends string = string, E = Error, Q extends FilterListQuery<FIELDS> = FilterListQuery<FIELDS>>(options: UseListStreamQueryOptions<R, FIELDS, E, Q>): UseListStreamQueryReturn<R, FIELDS, E, Q>;
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { Fetcher } from '@ahoo-wang/fetcher';
|
|
2
|
-
import { ListStreamExecutor, QueryExecutor } from '../types.js';
|
|
3
|
-
/**
|
|
4
|
-
* Where a `useFetcher…` hook sends its query. Every `useFetcher…Options`
|
|
5
|
-
* extends it, so the two members are declared, and documented, once.
|
|
6
|
-
*/
|
|
7
|
-
export interface Endpoint {
|
|
8
|
-
/**
|
|
9
|
-
* The query route, resolved against the Fetcher's `baseURL`: for example
|
|
10
|
-
* `/shop/order/snapshot/query` for the snapshots of the `order` aggregate
|
|
11
|
-
* of the `shop` context.
|
|
12
|
-
*/
|
|
13
|
-
url: string;
|
|
14
|
-
/**
|
|
15
|
-
* The Fetcher that sends the request, or the name of a registered one; the
|
|
16
|
-
* default Fetcher when omitted.
|
|
17
|
-
*/
|
|
18
|
-
fetcher?: string | Fetcher;
|
|
19
|
-
}
|
|
20
|
-
/** The operations of a query route that answer JSON. */
|
|
21
|
-
export type QueryKind = 'single' | 'list' | 'paged' | 'count';
|
|
22
|
-
/** Runs a query of `kind` on the route and reads the JSON answer. */
|
|
23
|
-
export declare function postQuery<Q extends object, R>(endpoint: Endpoint, kind: QueryKind): QueryExecutor<Q, R>;
|
|
24
|
-
/**
|
|
25
|
-
* Opens the stream of a list query on the route: server-sent events, read
|
|
26
|
-
* one record at a time; the stream errors with the problem the server sends
|
|
27
|
-
* midway.
|
|
28
|
-
*/
|
|
29
|
-
export declare function postQueryStream<R, Q extends object>(endpoint: Endpoint): ListStreamExecutor<R, Q>;
|
|
30
|
-
/**
|
|
31
|
-
* What identifies an endpoint across renders: the url, and the Fetcher by
|
|
32
|
-
* its name, or by its `baseURL` when it has none. A hook runs its query
|
|
33
|
-
* again when this changes; a `new Fetcher({ baseURL })` written inline in
|
|
34
|
-
* render keeps the same identity, so it does not run the query on every
|
|
35
|
-
* render. Two unnamed Fetchers with one `baseURL` but different
|
|
36
|
-
* interceptors count as the same; name them to tell them apart.
|
|
37
|
-
*/
|
|
38
|
-
export declare function endpointIdentity({ url, fetcher }: Endpoint): string;
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { QueryHookOptions, QueryHookReturn } from '../types.js';
|
|
2
|
-
import { Endpoint, QueryKind } from './endpoint.js';
|
|
3
|
-
/**
|
|
4
|
-
* The request `useFetcher…` hooks: the runner with the endpoint's executor as
|
|
5
|
-
* `execute`, and the endpoint as part of the request's identity, so a change
|
|
6
|
-
* of `url` or `fetcher` runs the query again.
|
|
7
|
-
*/
|
|
8
|
-
export declare function useEndpointRunner<Q extends object, R, E>(options: Omit<QueryHookOptions<Q, R, E>, 'execute'> & Endpoint, kind: QueryKind): QueryHookReturn<Q, R, E>;
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { FilterListQuery } from '@ahoo-wang/wow-client/dsl';
|
|
2
|
-
import { UseListStreamQueryOptions, UseListStreamQueryReturn } from '../hooks/useListStreamQuery.js';
|
|
3
|
-
/**
|
|
4
|
-
* The list-stream hooks on the request state machine: a run opens the
|
|
5
|
-
* stream and reads it into `items`. Each run starts from no rows; `abort()`
|
|
6
|
-
* and an error keep the rows received; `reset()` empties them. The runner
|
|
7
|
-
* aborts the run's signal whenever the run stops counting, and
|
|
8
|
-
* readStreamRows stops publishing then, so rows of a stale stream never
|
|
9
|
-
* reach `items`.
|
|
10
|
-
*/
|
|
11
|
-
export declare function useListStream<R, FIELDS extends string, E, Q extends FilterListQuery<FIELDS>>(options: UseListStreamQueryOptions<R, FIELDS, E, Q>, identity?: string): UseListStreamQueryReturn<R, FIELDS, E, Q>;
|