@svadmin/core 0.0.3 → 0.0.6
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/auth-hooks.svelte.ts +293 -0
- package/src/context.ts +12 -0
- package/src/data-transfer.test.ts +118 -0
- package/src/hooks.svelte.ts +37 -2
- package/src/i18n.svelte.ts +6 -4
- package/src/i18n.test.ts +90 -0
- package/src/index.ts +12 -0
- package/src/inferencer.test.ts +189 -0
- package/src/inferencer.ts +247 -0
- package/src/permissions.test.ts +55 -0
- package/src/router-provider.test.ts +71 -0
- package/src/router-provider.ts +100 -0
- package/src/useParsed.ts +74 -0
package/src/useParsed.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// useParsed — parse current URL hash into structured route info
|
|
2
|
+
|
|
3
|
+
import { currentPath } from './router';
|
|
4
|
+
import { getResources } from './context';
|
|
5
|
+
|
|
6
|
+
interface ParsedRoute {
|
|
7
|
+
resource?: string;
|
|
8
|
+
action?: 'list' | 'create' | 'edit' | 'show';
|
|
9
|
+
id?: string;
|
|
10
|
+
params: Record<string, string>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Parse the current hash URL into a structured route object.
|
|
15
|
+
* Returns reactive properties that update when the URL changes.
|
|
16
|
+
*
|
|
17
|
+
* Usage:
|
|
18
|
+
* const parsed = useParsed();
|
|
19
|
+
* // parsed.resource === 'posts'
|
|
20
|
+
* // parsed.action === 'edit'
|
|
21
|
+
* // parsed.id === '123'
|
|
22
|
+
*/
|
|
23
|
+
export function useParsed(): ParsedRoute {
|
|
24
|
+
const path = $derived(currentPath());
|
|
25
|
+
const resources = $derived((() => { try { return getResources(); } catch { return []; } })());
|
|
26
|
+
|
|
27
|
+
const parsed = $derived.by(() => {
|
|
28
|
+
const p = path;
|
|
29
|
+
const result: ParsedRoute = { params: {} };
|
|
30
|
+
|
|
31
|
+
// Parse query params from hash
|
|
32
|
+
const [pathname, queryString] = p.split('?');
|
|
33
|
+
if (queryString) {
|
|
34
|
+
const sp = new URLSearchParams(queryString);
|
|
35
|
+
for (const [k, v] of sp.entries()) {
|
|
36
|
+
result.params[k] = v;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const segments = pathname.split('/').filter(Boolean);
|
|
41
|
+
if (segments.length === 0) return result;
|
|
42
|
+
|
|
43
|
+
// First segment is typically the resource name
|
|
44
|
+
const resourceNames = resources.map(r => r.name);
|
|
45
|
+
if (resourceNames.includes(segments[0])) {
|
|
46
|
+
result.resource = segments[0];
|
|
47
|
+
|
|
48
|
+
if (segments.length === 1) {
|
|
49
|
+
result.action = 'list';
|
|
50
|
+
} else if (segments[1] === 'create') {
|
|
51
|
+
result.action = 'create';
|
|
52
|
+
} else if (segments[1] === 'edit' && segments[2]) {
|
|
53
|
+
result.action = 'edit';
|
|
54
|
+
result.id = segments[2];
|
|
55
|
+
} else if (segments[1] === 'show' && segments[2]) {
|
|
56
|
+
result.action = 'show';
|
|
57
|
+
result.id = segments[2];
|
|
58
|
+
} else if (segments[1]) {
|
|
59
|
+
// Legacy: /:resource/:id
|
|
60
|
+
result.action = 'show';
|
|
61
|
+
result.id = segments[1];
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return result;
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
return {
|
|
69
|
+
get resource() { return parsed.resource; },
|
|
70
|
+
get action() { return parsed.action; },
|
|
71
|
+
get id() { return parsed.id; },
|
|
72
|
+
get params() { return parsed.params; },
|
|
73
|
+
};
|
|
74
|
+
}
|