@mpfrontend/data-react 0.1.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -5
- package/dist/core.d.ts +87 -0
- package/dist/core.js +110 -0
- package/dist/core.js.map +1 -0
- package/dist/index.d.ts +1 -82
- package/dist/index.js +1 -104
- package/dist/index.js.map +1 -1
- package/dist/internal.d.ts +1 -0
- package/dist/internal.js +3 -0
- package/dist/internal.js.map +1 -0
- package/package.json +7 -3
package/README.md
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
# @mpfrontend/data-react
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
Part of [MP Frontend](https://github.com/panahister/mpfrontend), released to the public npm registry as one cohort: every `@mpfrontend`
|
|
4
|
+
package at the same exact version. Runtime: client, with a universal shared entry. Install it through a workspace that `mpfrontend init`
|
|
5
|
+
created, which pins the cohort and binds the scope to the registry in `.npmrc`. [Versioning](https://github.com/panahister/mpfrontend#versioning)
|
|
6
|
+
states the compatibility policy and [Implementation status](https://github.com/panahister/mpfrontend/blob/main/docs/IMPLEMENTATION-STATUS.md) the
|
|
7
|
+
evidence.
|
|
7
8
|
|
|
8
9
|
The conventions for server data and URL state in client components. `@tanstack/react-query` and `nuqs` are peer
|
|
9
10
|
dependencies: the app owns its `QueryClient` and its nuqs adapter.
|
|
@@ -27,5 +28,6 @@ with defaults and limits. Only the sort fields and filters that the protocol dec
|
|
|
27
28
|
falls back to its default; values equal to their defaults are left out of the URL. `read(searchParams)` gives the
|
|
28
29
|
state on the server for the first render; `useListUrlState` and `useTableUrlState` give it in the browser, where each
|
|
29
30
|
change is a new history entry and a new size, sort or filter starts at the first page. With field access, a sort or
|
|
30
|
-
a filter on a field the person may not view is dropped and removed from the URL on load
|
|
31
|
+
a filter on a field the person may not view is dropped and removed from the URL on load, when a valid snapshot says so;
|
|
32
|
+
a closed or expired snapshot says nothing and leaves the URL as it was linked. The scope never enters the
|
|
31
33
|
URL. `scopedQueryKey` and `inScope` are the key functions of the convention.
|
package/dist/core.d.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { type ReactNode } from 'react';
|
|
2
|
+
import { type UseMutationResult, type UseQueryResult } from '@tanstack/react-query';
|
|
3
|
+
import type { ListSort, ListState } from '@mpfrontend/ftg-core';
|
|
4
|
+
import { type ListUrlState, type ViewCheck } from './shared.js';
|
|
5
|
+
export type { QueryScope as QueryKeyScope, ListUrlOptions, ListUrlState, ViewCheck } from './shared.js';
|
|
6
|
+
/**
|
|
7
|
+
* Keeps the app's query cache to the access scope in use. When the context reference or revision changes, every query
|
|
8
|
+
* of another scope, and every query without a scope, is cancelled and removed, so its data never shows again. Place
|
|
9
|
+
* it inside the app's QueryClientProvider and the AccessProvider.
|
|
10
|
+
*/
|
|
11
|
+
export declare function QueryScope({ children }: Readonly<{
|
|
12
|
+
children?: ReactNode;
|
|
13
|
+
}>): ReactNode;
|
|
14
|
+
type JsonParams = string | number | boolean | null | readonly JsonParams[] | {
|
|
15
|
+
readonly [key: string]: JsonParams | undefined;
|
|
16
|
+
};
|
|
17
|
+
export type ScopedQueryOptions<Params extends JsonParams, Data> = Readonly<{
|
|
18
|
+
/** A stable name of the read, the same for every parameter value. */
|
|
19
|
+
operation: string;
|
|
20
|
+
params: Params;
|
|
21
|
+
/** Reads through the app's own route or the BFF, never a backend directly, and passes the signal on. */
|
|
22
|
+
read: (params: Params, signal: AbortSignal) => Promise<Data>;
|
|
23
|
+
/**
|
|
24
|
+
* The data the server read for the first render. It seeds the query of the scope the hook mounted in, and no other: a
|
|
25
|
+
* change of scope reads for itself. A hook that mounts without a valid snapshot is not seeded.
|
|
26
|
+
*/
|
|
27
|
+
initialData?: Data;
|
|
28
|
+
enabled?: boolean;
|
|
29
|
+
staleTime?: number;
|
|
30
|
+
}>;
|
|
31
|
+
/**
|
|
32
|
+
* A server read under the current access scope: its key is the scope, the operation and the parameters. Without a
|
|
33
|
+
* valid snapshot it does not run. A change of scope aborts it, and its late answer is dropped.
|
|
34
|
+
*/
|
|
35
|
+
export declare function useScopedQuery<Params extends JsonParams, Data>(options: ScopedQueryOptions<Params, Data>): UseQueryResult<Data>;
|
|
36
|
+
export type ScopedMutationOptions<Variables, Result> = Readonly<{
|
|
37
|
+
operation: string;
|
|
38
|
+
/** Writes through the app's own route or the BFF and passes the signal on. */
|
|
39
|
+
write: (variables: Variables, signal: AbortSignal) => Promise<Result>;
|
|
40
|
+
/** The reads whose data the write changes; they are invalidated in the current scope after it succeeds. */
|
|
41
|
+
invalidates: readonly string[];
|
|
42
|
+
}>;
|
|
43
|
+
/**
|
|
44
|
+
* A write under the current access scope. It is never retried automatically, after a refusal or any other failure.
|
|
45
|
+
* A change of scope aborts it and its result is not published.
|
|
46
|
+
*/
|
|
47
|
+
export declare function useScopedMutation<Variables, Result>(options: ScopedMutationOptions<Variables, Result>): UseMutationResult<Result, Error, Variables>;
|
|
48
|
+
export type ListStateChange = Readonly<{
|
|
49
|
+
page?: number;
|
|
50
|
+
size?: number;
|
|
51
|
+
sort?: ListSort | undefined;
|
|
52
|
+
filters?: Readonly<Record<string, string>>;
|
|
53
|
+
}>;
|
|
54
|
+
/**
|
|
55
|
+
* The list state of the URL. Each change is a new history entry, so back and forward move through pages and filters;
|
|
56
|
+
* a new size, sort or filter starts again at the first page. A sort or a filter on a field the person may not view is
|
|
57
|
+
* dropped, and removed from the URL on load.
|
|
58
|
+
*/
|
|
59
|
+
export declare function useListUrlState(urlState: ListUrlState, options?: Readonly<{
|
|
60
|
+
canView?: ViewCheck;
|
|
61
|
+
}>): readonly [ListState, (change: ListStateChange) => void];
|
|
62
|
+
/** Field access for the URL state: a reader of the access snapshot. */
|
|
63
|
+
export type FieldView = Readonly<{
|
|
64
|
+
field: (key: string) => Readonly<{
|
|
65
|
+
view: boolean;
|
|
66
|
+
}>;
|
|
67
|
+
}>;
|
|
68
|
+
/**
|
|
69
|
+
* The controlled values and change events of a ResourceTable bound to the URL: page, size, sort and filters.
|
|
70
|
+
* With `access`, a sort or a filter on a column the person may not view is dropped, and removed from the URL, when a valid
|
|
71
|
+
* snapshot says so. A closed, missing or expired snapshot says nothing: the URL keeps the state it was linked with.
|
|
72
|
+
*/
|
|
73
|
+
export declare function useTableUrlState(urlState: ListUrlState, options?: Readonly<{
|
|
74
|
+
access?: FieldView;
|
|
75
|
+
}>): {
|
|
76
|
+
page: number;
|
|
77
|
+
size: number;
|
|
78
|
+
sort: Readonly<{
|
|
79
|
+
field: string;
|
|
80
|
+
direction: "asc" | "desc";
|
|
81
|
+
}> | undefined;
|
|
82
|
+
filters: Readonly<Record<string, string>>;
|
|
83
|
+
onPageChange: (page: number) => void;
|
|
84
|
+
onSizeChange: (size: number) => void;
|
|
85
|
+
onSortChange: (sort: ListSort | undefined) => void;
|
|
86
|
+
onFiltersChange: (filters: Readonly<Record<string, string>>) => void;
|
|
87
|
+
};
|
package/dist/core.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { useEffect, useState } from 'react';
|
|
3
|
+
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
|
4
|
+
import { useQueryStates } from 'nuqs';
|
|
5
|
+
import { useAccessCapture, useAccessScope, useAccessStatus } from '@mpfrontend/access-react';
|
|
6
|
+
import { inScope, scopedQueryKey } from './shared.js';
|
|
7
|
+
/**
|
|
8
|
+
* Keeps the app's query cache to the access scope in use. When the context reference or revision changes, every query
|
|
9
|
+
* of another scope, and every query without a scope, is cancelled and removed, so its data never shows again. Place
|
|
10
|
+
* it inside the app's QueryClientProvider and the AccessProvider.
|
|
11
|
+
*/
|
|
12
|
+
export function QueryScope({ children }) {
|
|
13
|
+
const client = useQueryClient(), scope = useAccessScope();
|
|
14
|
+
const key = scope ? scope.contextRef + '\n' + scope.revision : '';
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
const stale = { predicate: (query) => !inScope(query.queryKey, scope) };
|
|
17
|
+
void client.cancelQueries(stale);
|
|
18
|
+
client.removeQueries(stale);
|
|
19
|
+
// The scope object changes with its key only.
|
|
20
|
+
}, [client, key]);
|
|
21
|
+
return children ?? null;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* A server read under the current access scope: its key is the scope, the operation and the parameters. Without a
|
|
25
|
+
* valid snapshot it does not run. A change of scope aborts it, and its late answer is dropped.
|
|
26
|
+
*/
|
|
27
|
+
export function useScopedQuery(options) {
|
|
28
|
+
const scope = useAccessScope(), capture = useAccessCapture();
|
|
29
|
+
const key = scope ? scope.contextRef + '\n' + scope.revision : undefined;
|
|
30
|
+
const [mountedIn] = useState(key);
|
|
31
|
+
// Data read for one scope must never stand in for another's: it seeds only the scope the hook mounted in.
|
|
32
|
+
const seed = options.initialData !== undefined && key !== undefined && key === mountedIn;
|
|
33
|
+
return useQuery({
|
|
34
|
+
queryKey: scope ? scopedQueryKey(scope, options.operation, options.params) : ['mpfrontend-closed', options.operation],
|
|
35
|
+
enabled: scope !== undefined && (options.enabled ?? true),
|
|
36
|
+
queryFn: async ({ signal }) => {
|
|
37
|
+
const work = capture();
|
|
38
|
+
const data = await options.read(options.params, AbortSignal.any([signal, work.signal]));
|
|
39
|
+
work.assertCurrent();
|
|
40
|
+
return data;
|
|
41
|
+
},
|
|
42
|
+
...(seed ? { initialData: options.initialData } : {}),
|
|
43
|
+
...(options.staleTime !== undefined ? { staleTime: options.staleTime } : {}),
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* A write under the current access scope. It is never retried automatically, after a refusal or any other failure.
|
|
48
|
+
* A change of scope aborts it and its result is not published.
|
|
49
|
+
*/
|
|
50
|
+
export function useScopedMutation(options) {
|
|
51
|
+
const client = useQueryClient(), scope = useAccessScope(), capture = useAccessCapture();
|
|
52
|
+
return useMutation({
|
|
53
|
+
mutationKey: ['mpfrontend-write', options.operation],
|
|
54
|
+
retry: false,
|
|
55
|
+
mutationFn: async (variables) => {
|
|
56
|
+
const work = capture();
|
|
57
|
+
const result = await options.write(variables, work.signal);
|
|
58
|
+
work.assertCurrent();
|
|
59
|
+
return result;
|
|
60
|
+
},
|
|
61
|
+
onSuccess: async () => {
|
|
62
|
+
if (!scope)
|
|
63
|
+
return;
|
|
64
|
+
await Promise.all(options.invalidates.map(operation => client.invalidateQueries({ queryKey: [...scopedQueryKey(scope, operation)] })));
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* The list state of the URL. Each change is a new history entry, so back and forward move through pages and filters;
|
|
70
|
+
* a new size, sort or filter starts again at the first page. A sort or a filter on a field the person may not view is
|
|
71
|
+
* dropped, and removed from the URL on load.
|
|
72
|
+
*/
|
|
73
|
+
export function useListUrlState(urlState, options = {}) {
|
|
74
|
+
const [values, setValues] = useQueryStates(urlState.parsers, { history: 'push' });
|
|
75
|
+
const state = urlState.fromValues(values, options);
|
|
76
|
+
const hidden = urlState.hidden(values, options.canView).join(',');
|
|
77
|
+
useEffect(() => {
|
|
78
|
+
if (hidden)
|
|
79
|
+
void setValues(Object.fromEntries(hidden.split(',').map(key => [key, null])), { history: 'replace' });
|
|
80
|
+
}, [hidden, setValues]);
|
|
81
|
+
const change = (next) => {
|
|
82
|
+
const update = {};
|
|
83
|
+
if (next.size !== undefined)
|
|
84
|
+
update.size = next.size;
|
|
85
|
+
if ('sort' in next)
|
|
86
|
+
update.sort = next.sort ?? null;
|
|
87
|
+
for (const [name, value] of Object.entries(next.filters ?? {}))
|
|
88
|
+
update[name] = value === '' ? null : value;
|
|
89
|
+
update.page = next.page !== undefined ? next.page : null;
|
|
90
|
+
void setValues(update);
|
|
91
|
+
};
|
|
92
|
+
return [state, change];
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* The controlled values and change events of a ResourceTable bound to the URL: page, size, sort and filters.
|
|
96
|
+
* With `access`, a sort or a filter on a column the person may not view is dropped, and removed from the URL, when a valid
|
|
97
|
+
* snapshot says so. A closed, missing or expired snapshot says nothing: the URL keeps the state it was linked with.
|
|
98
|
+
*/
|
|
99
|
+
export function useTableUrlState(urlState, options = {}) {
|
|
100
|
+
const access = options.access, status = useAccessStatus();
|
|
101
|
+
const [state, change] = useListUrlState(urlState, access && status === 'granted' ? { canView: key => access.field(key).view } : {});
|
|
102
|
+
return {
|
|
103
|
+
page: state.page, size: state.size, sort: state.sort, filters: state.filters ?? {},
|
|
104
|
+
onPageChange: (page) => change({ page }),
|
|
105
|
+
onSizeChange: (size) => change({ size }),
|
|
106
|
+
onSortChange: (sort) => change({ sort }),
|
|
107
|
+
onFiltersChange: (filters) => change({ filters }),
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
//# sourceMappingURL=core.js.map
|
package/dist/core.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core.js","sourceRoot":"","sources":["../src/core.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;AACb,OAAO,EAAC,SAAS,EAAC,QAAQ,EAAgB,MAAM,OAAO,CAAC;AACxD,OAAO,EAAC,WAAW,EAAC,QAAQ,EAAC,cAAc,EAA4C,MAAM,uBAAuB,CAAC;AACrH,OAAO,EAAC,cAAc,EAAC,MAAM,MAAM,CAAC;AACpC,OAAO,EAAC,gBAAgB,EAAC,cAAc,EAAC,eAAe,EAAC,MAAM,0BAA0B,CAAC;AAEzF,OAAO,EAAC,OAAO,EAAC,cAAc,EAAkC,MAAM,aAAa,CAAC;AAMpF;;;;GAIG;AACH,MAAM,UAAU,UAAU,CAAC,EAAC,QAAQ,EAAiC;IACnE,MAAM,MAAM,GAAC,cAAc,EAAE,EAAC,KAAK,GAAC,cAAc,EAAE,CAAC;IACrD,MAAM,GAAG,GAAC,KAAK,CAAA,CAAC,CAAA,KAAK,CAAC,UAAU,GAAC,IAAI,GAAC,KAAK,CAAC,QAAQ,CAAA,CAAC,CAAA,EAAE,CAAC;IACxD,SAAS,CAAC,GAA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AAC,CAAC,OAAuC,EAAC,EAAE,CAAA,MAAM,CAAC,EAAC,OAAO,EAAC,CAAC;KAC7E,CAAC;AACJ,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,82 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { type UseMutationResult, type UseQueryResult } from '@tanstack/react-query';
|
|
3
|
-
import type { ListSort, ListState } from '@mpfrontend/ftg-core';
|
|
4
|
-
import { type ListUrlState, type ViewCheck } from './shared.js';
|
|
5
|
-
export type { QueryScope as QueryKeyScope, ListUrlOptions, ListUrlState, ViewCheck } from './shared.js';
|
|
6
|
-
/**
|
|
7
|
-
* Keeps the app's query cache to the access scope in use. When the context reference or revision changes, every query
|
|
8
|
-
* of another scope, and every query without a scope, is cancelled and removed, so its data never shows again. Place
|
|
9
|
-
* it inside the app's QueryClientProvider and the AccessProvider.
|
|
10
|
-
*/
|
|
11
|
-
export declare function QueryScope({ children }: Readonly<{
|
|
12
|
-
children?: ReactNode;
|
|
13
|
-
}>): ReactNode;
|
|
14
|
-
type JsonParams = string | number | boolean | null | readonly JsonParams[] | {
|
|
15
|
-
readonly [key: string]: JsonParams | undefined;
|
|
16
|
-
};
|
|
17
|
-
export type ScopedQueryOptions<Params extends JsonParams, Data> = Readonly<{
|
|
18
|
-
/** A stable name of the read, the same for every parameter value. */
|
|
19
|
-
operation: string;
|
|
20
|
-
params: Params;
|
|
21
|
-
/** Reads through the app's own route or the BFF, never a backend directly, and passes the signal on. */
|
|
22
|
-
read: (params: Params, signal: AbortSignal) => Promise<Data>;
|
|
23
|
-
initialData?: Data;
|
|
24
|
-
enabled?: boolean;
|
|
25
|
-
staleTime?: number;
|
|
26
|
-
}>;
|
|
27
|
-
/**
|
|
28
|
-
* A server read under the current access scope: its key is the scope, the operation and the parameters. Without a
|
|
29
|
-
* valid snapshot it does not run. A change of scope aborts it, and its late answer is dropped.
|
|
30
|
-
*/
|
|
31
|
-
export declare function useScopedQuery<Params extends JsonParams, Data>(options: ScopedQueryOptions<Params, Data>): UseQueryResult<Data>;
|
|
32
|
-
export type ScopedMutationOptions<Variables, Result> = Readonly<{
|
|
33
|
-
operation: string;
|
|
34
|
-
/** Writes through the app's own route or the BFF and passes the signal on. */
|
|
35
|
-
write: (variables: Variables, signal: AbortSignal) => Promise<Result>;
|
|
36
|
-
/** The reads whose data the write changes; they are invalidated in the current scope after it succeeds. */
|
|
37
|
-
invalidates: readonly string[];
|
|
38
|
-
}>;
|
|
39
|
-
/**
|
|
40
|
-
* A write under the current access scope. It is never retried automatically, after a refusal or any other failure.
|
|
41
|
-
* A change of scope aborts it and its result is not published.
|
|
42
|
-
*/
|
|
43
|
-
export declare function useScopedMutation<Variables, Result>(options: ScopedMutationOptions<Variables, Result>): UseMutationResult<Result, Error, Variables>;
|
|
44
|
-
export type ListStateChange = Readonly<{
|
|
45
|
-
page?: number;
|
|
46
|
-
size?: number;
|
|
47
|
-
sort?: ListSort | undefined;
|
|
48
|
-
filters?: Readonly<Record<string, string>>;
|
|
49
|
-
}>;
|
|
50
|
-
/**
|
|
51
|
-
* The list state of the URL. Each change is a new history entry, so back and forward move through pages and filters;
|
|
52
|
-
* a new size, sort or filter starts again at the first page. A sort or a filter on a field the person may not view is
|
|
53
|
-
* dropped, and removed from the URL on load.
|
|
54
|
-
*/
|
|
55
|
-
export declare function useListUrlState(urlState: ListUrlState, options?: Readonly<{
|
|
56
|
-
canView?: ViewCheck;
|
|
57
|
-
}>): readonly [ListState, (change: ListStateChange) => void];
|
|
58
|
-
/** Field access for the URL state: a reader of the access snapshot. */
|
|
59
|
-
export type FieldView = Readonly<{
|
|
60
|
-
field: (key: string) => Readonly<{
|
|
61
|
-
view: boolean;
|
|
62
|
-
}>;
|
|
63
|
-
}>;
|
|
64
|
-
/**
|
|
65
|
-
* The controlled values and change events of a ResourceTable bound to the URL: page, size, sort and filters.
|
|
66
|
-
* With `access`, a sort or a filter on a column the person may not view is dropped.
|
|
67
|
-
*/
|
|
68
|
-
export declare function useTableUrlState(urlState: ListUrlState, options?: Readonly<{
|
|
69
|
-
access?: FieldView;
|
|
70
|
-
}>): {
|
|
71
|
-
page: number;
|
|
72
|
-
size: number;
|
|
73
|
-
sort: Readonly<{
|
|
74
|
-
field: string;
|
|
75
|
-
direction: "asc" | "desc";
|
|
76
|
-
}> | undefined;
|
|
77
|
-
filters: Readonly<Record<string, string>>;
|
|
78
|
-
onPageChange: (page: number) => void;
|
|
79
|
-
onSizeChange: (size: number) => void;
|
|
80
|
-
onSortChange: (sort: ListSort | undefined) => void;
|
|
81
|
-
onFiltersChange: (filters: Readonly<Record<string, string>>) => void;
|
|
82
|
-
};
|
|
1
|
+
export { type FieldView, type ListStateChange, type ListUrlOptions, type ListUrlState, QueryScope, type ScopedMutationOptions, type ScopedQueryOptions, useListUrlState, useScopedMutation, useScopedQuery, useTableUrlState, type ViewCheck, } from './core.js';
|
package/dist/index.js
CHANGED
|
@@ -1,105 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
import { useEffect } from 'react';
|
|
3
|
-
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
|
4
|
-
import { useQueryStates } from 'nuqs';
|
|
5
|
-
import { useAccessCapture, useAccessScope } from '@mpfrontend/access-react';
|
|
6
|
-
import { inScope, scopedQueryKey } from './shared.js';
|
|
7
|
-
/**
|
|
8
|
-
* Keeps the app's query cache to the access scope in use. When the context reference or revision changes, every query
|
|
9
|
-
* of another scope, and every query without a scope, is cancelled and removed, so its data never shows again. Place
|
|
10
|
-
* it inside the app's QueryClientProvider and the AccessProvider.
|
|
11
|
-
*/
|
|
12
|
-
export function QueryScope({ children }) {
|
|
13
|
-
const client = useQueryClient(), scope = useAccessScope();
|
|
14
|
-
const key = scope ? scope.contextRef + '\n' + scope.revision : '';
|
|
15
|
-
useEffect(() => {
|
|
16
|
-
const stale = { predicate: (query) => !inScope(query.queryKey, scope) };
|
|
17
|
-
void client.cancelQueries(stale);
|
|
18
|
-
client.removeQueries(stale);
|
|
19
|
-
// The scope object changes with its key only.
|
|
20
|
-
}, [client, key]);
|
|
21
|
-
return children ?? null;
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* A server read under the current access scope: its key is the scope, the operation and the parameters. Without a
|
|
25
|
-
* valid snapshot it does not run. A change of scope aborts it, and its late answer is dropped.
|
|
26
|
-
*/
|
|
27
|
-
export function useScopedQuery(options) {
|
|
28
|
-
const scope = useAccessScope(), capture = useAccessCapture();
|
|
29
|
-
return useQuery({
|
|
30
|
-
queryKey: scope ? scopedQueryKey(scope, options.operation, options.params) : ['mpfrontend-closed', options.operation],
|
|
31
|
-
enabled: scope !== undefined && (options.enabled ?? true),
|
|
32
|
-
queryFn: async ({ signal }) => {
|
|
33
|
-
const work = capture();
|
|
34
|
-
const data = await options.read(options.params, AbortSignal.any([signal, work.signal]));
|
|
35
|
-
work.assertCurrent();
|
|
36
|
-
return data;
|
|
37
|
-
},
|
|
38
|
-
...(options.initialData !== undefined ? { initialData: options.initialData } : {}),
|
|
39
|
-
...(options.staleTime !== undefined ? { staleTime: options.staleTime } : {}),
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* A write under the current access scope. It is never retried automatically, after a refusal or any other failure.
|
|
44
|
-
* A change of scope aborts it and its result is not published.
|
|
45
|
-
*/
|
|
46
|
-
export function useScopedMutation(options) {
|
|
47
|
-
const client = useQueryClient(), scope = useAccessScope(), capture = useAccessCapture();
|
|
48
|
-
return useMutation({
|
|
49
|
-
mutationKey: ['mpfrontend-write', options.operation],
|
|
50
|
-
retry: false,
|
|
51
|
-
mutationFn: async (variables) => {
|
|
52
|
-
const work = capture();
|
|
53
|
-
const result = await options.write(variables, work.signal);
|
|
54
|
-
work.assertCurrent();
|
|
55
|
-
return result;
|
|
56
|
-
},
|
|
57
|
-
onSuccess: async () => {
|
|
58
|
-
if (!scope)
|
|
59
|
-
return;
|
|
60
|
-
await Promise.all(options.invalidates.map(operation => client.invalidateQueries({ queryKey: [...scopedQueryKey(scope, operation)] })));
|
|
61
|
-
},
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* The list state of the URL. Each change is a new history entry, so back and forward move through pages and filters;
|
|
66
|
-
* a new size, sort or filter starts again at the first page. A sort or a filter on a field the person may not view is
|
|
67
|
-
* dropped, and removed from the URL on load.
|
|
68
|
-
*/
|
|
69
|
-
export function useListUrlState(urlState, options = {}) {
|
|
70
|
-
const [values, setValues] = useQueryStates(urlState.parsers, { history: 'push' });
|
|
71
|
-
const state = urlState.fromValues(values, options);
|
|
72
|
-
const hidden = urlState.hidden(values, options.canView).join(',');
|
|
73
|
-
useEffect(() => {
|
|
74
|
-
if (hidden)
|
|
75
|
-
void setValues(Object.fromEntries(hidden.split(',').map(key => [key, null])), { history: 'replace' });
|
|
76
|
-
}, [hidden, setValues]);
|
|
77
|
-
const change = (next) => {
|
|
78
|
-
const update = {};
|
|
79
|
-
if (next.size !== undefined)
|
|
80
|
-
update.size = next.size;
|
|
81
|
-
if ('sort' in next)
|
|
82
|
-
update.sort = next.sort ?? null;
|
|
83
|
-
for (const [name, value] of Object.entries(next.filters ?? {}))
|
|
84
|
-
update[name] = value === '' ? null : value;
|
|
85
|
-
update.page = next.page !== undefined ? next.page : null;
|
|
86
|
-
void setValues(update);
|
|
87
|
-
};
|
|
88
|
-
return [state, change];
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* The controlled values and change events of a ResourceTable bound to the URL: page, size, sort and filters.
|
|
92
|
-
* With `access`, a sort or a filter on a column the person may not view is dropped.
|
|
93
|
-
*/
|
|
94
|
-
export function useTableUrlState(urlState, options = {}) {
|
|
95
|
-
const access = options.access;
|
|
96
|
-
const [state, change] = useListUrlState(urlState, access ? { canView: key => access.field(key).view } : {});
|
|
97
|
-
return {
|
|
98
|
-
page: state.page, size: state.size, sort: state.sort, filters: state.filters ?? {},
|
|
99
|
-
onPageChange: (page) => change({ page }),
|
|
100
|
-
onSizeChange: (size) => change({ size }),
|
|
101
|
-
onSortChange: (sort) => change({ sort }),
|
|
102
|
-
onFiltersChange: (filters) => change({ filters }),
|
|
103
|
-
};
|
|
104
|
-
}
|
|
1
|
+
export { QueryScope, useListUrlState, useScopedMutation, useScopedQuery, useTableUrlState, } from './core.js';
|
|
105
2
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,UAAU,EAGV,eAAe,EACf,iBAAiB,EACjB,cAAc,EACd,gBAAgB,GAEjB,MAAM,WAAW,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { type QueryKeyScope } from './core.js';
|
package/dist/internal.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"internal.js","sourceRoot":"","sources":["../src/internal.ts"],"names":[],"mappings":"AAAA,4FAA4F;AAC5F,OAAO,EAAoB,MAAM,WAAW,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mpfrontend/data-react",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -18,6 +18,10 @@
|
|
|
18
18
|
"./shared": {
|
|
19
19
|
"types": "./dist/shared.d.ts",
|
|
20
20
|
"import": "./dist/shared.js"
|
|
21
|
+
},
|
|
22
|
+
"./internal": {
|
|
23
|
+
"types": "./dist/internal.d.ts",
|
|
24
|
+
"import": "./dist/internal.js"
|
|
21
25
|
}
|
|
22
26
|
},
|
|
23
27
|
"files": [
|
|
@@ -29,8 +33,8 @@
|
|
|
29
33
|
"provenance": true
|
|
30
34
|
},
|
|
31
35
|
"dependencies": {
|
|
32
|
-
"@mpfrontend/access-react": "
|
|
33
|
-
"@mpfrontend/ftg-core": "
|
|
36
|
+
"@mpfrontend/access-react": "1.0.0",
|
|
37
|
+
"@mpfrontend/ftg-core": "1.0.0"
|
|
34
38
|
},
|
|
35
39
|
"devDependencies": {
|
|
36
40
|
"@happy-dom/global-registrator": "20.14.5",
|