@svadmin/core 0.34.2 → 0.36.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/package.json +1 -1
- package/src/audit.ts +11 -3
- package/src/auth-hooks.svelte.ts +83 -13
- package/src/context.svelte.ts +202 -29
- package/src/form-hooks.svelte.ts +70 -20
- package/src/hook-utils.svelte.ts +33 -21
- package/src/hooks.svelte.ts +180 -39
- package/src/index.ts +47 -4
- package/src/live.svelte.ts +7 -3
- package/src/mutation-hooks.svelte.ts +161 -52
- package/src/notification.svelte.ts +14 -4
- package/src/provider-bundle.ts +183 -0
- package/src/query-hooks.svelte.ts +55 -9
- package/src/query-keys.ts +124 -18
- package/src/task-hooks.svelte.ts +68 -19
- package/src/types.ts +25 -0
- package/src/useCan.ts +19 -4
- package/src/utility-hooks.svelte.ts +34 -6
package/src/useCan.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// useCan — reactive permission check hook with TanStack Query integration
|
|
2
2
|
|
|
3
3
|
import { createQuery } from '@tanstack/svelte-query';
|
|
4
|
-
import {
|
|
4
|
+
import { captureAdminContext } from './context.svelte';
|
|
5
5
|
import type { Action, CanResult } from './permissions.svelte';
|
|
6
6
|
|
|
7
7
|
export interface UseCanOptions {
|
|
@@ -32,11 +32,26 @@ export interface UseCanResult {
|
|
|
32
32
|
* ```
|
|
33
33
|
*/
|
|
34
34
|
export function useCan(options: () => UseCanOptions): UseCanResult {
|
|
35
|
+
const adminContext = captureAdminContext();
|
|
35
36
|
const query = createQuery<CanResult>(() => {
|
|
36
37
|
const opts = options();
|
|
38
|
+
const provider = adminContext.accessControlProvider;
|
|
37
39
|
return {
|
|
38
|
-
queryKey:
|
|
39
|
-
|
|
40
|
+
queryKey: adminContext.queryKeys(opts.resource).access.can(opts.resource, {
|
|
41
|
+
action: opts.action,
|
|
42
|
+
params: opts.params,
|
|
43
|
+
meta: opts.meta,
|
|
44
|
+
}),
|
|
45
|
+
queryFn: async () => {
|
|
46
|
+
if (!provider) return { can: true };
|
|
47
|
+
const result = await provider.can({
|
|
48
|
+
resource: opts.resource,
|
|
49
|
+
action: opts.action,
|
|
50
|
+
params: opts.params,
|
|
51
|
+
meta: adminContext.getProviderMeta(opts.resource, opts.meta),
|
|
52
|
+
});
|
|
53
|
+
return Array.isArray(result) ? (result[0] ?? { can: false }) : result;
|
|
54
|
+
},
|
|
40
55
|
enabled: opts.queryOptions?.enabled ?? true,
|
|
41
56
|
staleTime: opts.queryOptions?.staleTime ?? 5 * 60 * 1000,
|
|
42
57
|
};
|
|
@@ -44,7 +59,7 @@ export function useCan(options: () => UseCanOptions): UseCanResult {
|
|
|
44
59
|
|
|
45
60
|
return {
|
|
46
61
|
get allowed() {
|
|
47
|
-
const p =
|
|
62
|
+
const p = adminContext.accessControlProvider;
|
|
48
63
|
if (!p) return true;
|
|
49
64
|
return (query.data as CanResult | undefined)?.can ?? false;
|
|
50
65
|
},
|
|
@@ -2,6 +2,7 @@ import { useForm, type UseFormOptions } from './form-hooks.svelte';
|
|
|
2
2
|
import { useQueryClient } from '@tanstack/svelte-query';
|
|
3
3
|
import { useParsed } from './useParsed.svelte';
|
|
4
4
|
import { captureAdminContext } from './context.svelte';
|
|
5
|
+
import { dataQueryMatches } from './query-keys';
|
|
5
6
|
import { getAdminOptions } from './options.svelte';
|
|
6
7
|
import { t } from './i18n.svelte';
|
|
7
8
|
import type { BaseRecord, HttpError, Filter, KnownResources, ResourceDefinition } from './types';
|
|
@@ -39,6 +40,7 @@ export function useModalForm<
|
|
|
39
40
|
TError = HttpError,
|
|
40
41
|
>(options: UseModalFormOptions<TVariables, TData, TError> = {} as UseModalFormOptions<TVariables, TData, TError>) {
|
|
41
42
|
const queryClient = useQueryClient();
|
|
43
|
+
const adminContext = captureAdminContext();
|
|
42
44
|
const formState = useForm<TVariables, TData, TError>(options);
|
|
43
45
|
let visible = $state(options.defaultVisible ?? false);
|
|
44
46
|
|
|
@@ -57,11 +59,11 @@ export function useModalForm<
|
|
|
57
59
|
visible = false;
|
|
58
60
|
formState.reset();
|
|
59
61
|
if (options.autoSave?.invalidateOnClose) {
|
|
60
|
-
const
|
|
61
|
-
const
|
|
62
|
-
queryClient.invalidateQueries({
|
|
63
|
-
|
|
64
|
-
|
|
62
|
+
const resource = formState.resource;
|
|
63
|
+
const matcher = adminContext.queryKeyMatcher(resource, options.dataProviderName);
|
|
64
|
+
queryClient.invalidateQueries({
|
|
65
|
+
predicate: (q) => dataQueryMatches(q.queryKey, { ...matcher, resource }),
|
|
66
|
+
});
|
|
65
67
|
}
|
|
66
68
|
}
|
|
67
69
|
|
|
@@ -182,7 +184,33 @@ export function useRelation<TData extends BaseRecord = BaseRecord, TError = Http
|
|
|
182
184
|
|
|
183
185
|
// ─── useNotification ────────────────────────────────────────
|
|
184
186
|
export function useNotification() {
|
|
185
|
-
|
|
187
|
+
const adminContext = captureAdminContext();
|
|
188
|
+
|
|
189
|
+
function sendToast(
|
|
190
|
+
type: 'success' | 'error' | 'warning' | 'info',
|
|
191
|
+
message: string,
|
|
192
|
+
duration?: number,
|
|
193
|
+
options?: { key?: string },
|
|
194
|
+
): void {
|
|
195
|
+
const provider = adminContext.notificationProvider;
|
|
196
|
+
if (provider) {
|
|
197
|
+
provider.open({ type, message, key: options?.key });
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
toast[type](message, duration, options);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return {
|
|
204
|
+
open: (message: string, duration?: number, options?: { key?: string }) =>
|
|
205
|
+
sendToast('info', message, duration, options),
|
|
206
|
+
success: (message: string, duration?: number, options?: { key?: string }) =>
|
|
207
|
+
sendToast('success', message, duration, options),
|
|
208
|
+
error: (message: string, duration?: number, options?: { key?: string }) =>
|
|
209
|
+
sendToast('error', message, duration, options),
|
|
210
|
+
warning: (message: string, duration?: number, options?: { key?: string }) =>
|
|
211
|
+
sendToast('warning', message, duration, options),
|
|
212
|
+
close: (key: string) => adminContext.notificationProvider?.close(key),
|
|
213
|
+
};
|
|
186
214
|
}
|
|
187
215
|
|
|
188
216
|
// ─── Field-specific hooks (refine-compatible) ─────────────────────
|