@anievo-id/rbac-evolution-solid 0.0.0 → 1.6.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/LICENSE +21 -0
- package/README.md +34 -1
- package/dist/index.cjs +162 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +129 -0
- package/dist/index.d.ts +129 -0
- package/dist/index.js +147 -0
- package/dist/index.js.map +1 -0
- package/package.json +64 -2
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 AniEvo-ID
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,36 @@
|
|
|
1
1
|
# @anievo-id/rbac-evolution-solid
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[Solid](https://www.solidjs.com) bindings for [RBAC Evolution](https://github.com/AniEvo-ID/rbac-evolution):
|
|
4
|
+
`<Can>`, `<HasRole>` and `useCan()` accessors over a snapshot of the user's roles and permissions. Solid 1.8+ and 2.x,
|
|
5
|
+
in the browser and in server rendering.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm i @anievo-id/rbac-evolution @anievo-id/rbac-evolution-solid
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```tsx
|
|
16
|
+
import { Can, RbacProvider, useCan } from '@anievo-id/rbac-evolution-solid'
|
|
17
|
+
|
|
18
|
+
// snapshot: `await rbac.for(user).snapshot()` from the server, null for guests
|
|
19
|
+
<RbacProvider snapshot={snapshot()}>
|
|
20
|
+
<Can permission="articles.edit" fallback={<p>Read only</p>}>
|
|
21
|
+
<EditButton />
|
|
22
|
+
</Can>
|
|
23
|
+
</RbacProvider>
|
|
24
|
+
|
|
25
|
+
const canPublish = useCan('articles.publish') // Accessor<boolean>
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
UI only: it decides what the page shows. Always check permissions on the server too.
|
|
29
|
+
|
|
30
|
+
## Documentation
|
|
31
|
+
|
|
32
|
+
https://creation.anievo.id/rbac-evolution/frameworks/solid
|
|
33
|
+
|
|
34
|
+
## License
|
|
35
|
+
|
|
36
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var rbacEvolution = require('@anievo-id/rbac-evolution');
|
|
4
|
+
var solidJs = require('solid-js');
|
|
5
|
+
|
|
6
|
+
// src/checker.ts
|
|
7
|
+
var EMPTY = { roles: [], permissions: [] };
|
|
8
|
+
function flatten(items) {
|
|
9
|
+
const out = [];
|
|
10
|
+
for (const item of items) {
|
|
11
|
+
if (typeof item === "string") out.push(item);
|
|
12
|
+
else out.push(...item);
|
|
13
|
+
}
|
|
14
|
+
return out;
|
|
15
|
+
}
|
|
16
|
+
function safeImplies(granted, required) {
|
|
17
|
+
try {
|
|
18
|
+
return rbacEvolution.wildcardImplies(granted, required);
|
|
19
|
+
} catch {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function createRbacChecker(snapshot, options = {}) {
|
|
24
|
+
const { roles, permissions } = snapshot ?? EMPTY;
|
|
25
|
+
const roleSet = new Set(roles);
|
|
26
|
+
const permissionSet = new Set(permissions);
|
|
27
|
+
const deniedSet = new Set((snapshot?.denied ?? []).map((name) => name.trim()));
|
|
28
|
+
const wildcard = options.wildcard ?? false;
|
|
29
|
+
const loading = options.loading ?? false;
|
|
30
|
+
const isSuperAdmin = !loading && (options.superAdminRoles ?? []).some((r) => roleSet.has(r));
|
|
31
|
+
const can = (permission) => {
|
|
32
|
+
if (loading) return false;
|
|
33
|
+
if (deniedSet.has(permission.trim())) return false;
|
|
34
|
+
if (isSuperAdmin) return true;
|
|
35
|
+
const name = permission.trim();
|
|
36
|
+
if (name === "") return false;
|
|
37
|
+
if (permissionSet.has(name)) return true;
|
|
38
|
+
return wildcard && permissions.some((granted) => safeImplies(granted, name));
|
|
39
|
+
};
|
|
40
|
+
const hasOne = (role) => !loading && roleSet.has(role.trim());
|
|
41
|
+
return {
|
|
42
|
+
roles,
|
|
43
|
+
permissions,
|
|
44
|
+
isSuperAdmin,
|
|
45
|
+
loading,
|
|
46
|
+
can,
|
|
47
|
+
cannot: (permission) => !loading && !can(permission),
|
|
48
|
+
canAny: (...list) => flatten(list).some(can),
|
|
49
|
+
canAll: (...list) => {
|
|
50
|
+
const wanted = flatten(list);
|
|
51
|
+
return wanted.length > 0 && wanted.every(can);
|
|
52
|
+
},
|
|
53
|
+
hasRole: (list) => flatten([list]).some(hasOne),
|
|
54
|
+
hasAnyRole: (...list) => flatten(list).some(hasOne),
|
|
55
|
+
hasAllRoles: (...list) => {
|
|
56
|
+
const wanted = flatten(list);
|
|
57
|
+
return wanted.length > 0 && wanted.every(hasOne);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
var RbacContext = solidJs.createContext();
|
|
62
|
+
function RbacProvider(props) {
|
|
63
|
+
const checker = solidJs.createMemo(
|
|
64
|
+
() => createRbacChecker(props.snapshot, {
|
|
65
|
+
wildcard: props.wildcard ?? false,
|
|
66
|
+
superAdminRoles: props.superAdminRoles,
|
|
67
|
+
loading: props.loading ?? false
|
|
68
|
+
})
|
|
69
|
+
);
|
|
70
|
+
const Provider = RbacContext.Provider ?? RbacContext;
|
|
71
|
+
return solidJs.createComponent(Provider, {
|
|
72
|
+
value: checker,
|
|
73
|
+
get children() {
|
|
74
|
+
return props.children;
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
function useRbac() {
|
|
79
|
+
let checker;
|
|
80
|
+
try {
|
|
81
|
+
checker = solidJs.useContext(RbacContext);
|
|
82
|
+
} catch {
|
|
83
|
+
}
|
|
84
|
+
if (!checker) throw new Error("useRbac() must be used inside <RbacProvider>.");
|
|
85
|
+
return checker;
|
|
86
|
+
}
|
|
87
|
+
function useCan(permission) {
|
|
88
|
+
const rbac = useRbac();
|
|
89
|
+
return () => rbac().can(permission);
|
|
90
|
+
}
|
|
91
|
+
function useCannot(permission) {
|
|
92
|
+
const rbac = useRbac();
|
|
93
|
+
return () => rbac().cannot(permission);
|
|
94
|
+
}
|
|
95
|
+
function useCanAny(...permissions) {
|
|
96
|
+
const rbac = useRbac();
|
|
97
|
+
return () => rbac().canAny(...permissions);
|
|
98
|
+
}
|
|
99
|
+
function useCanAll(...permissions) {
|
|
100
|
+
const rbac = useRbac();
|
|
101
|
+
return () => rbac().canAll(...permissions);
|
|
102
|
+
}
|
|
103
|
+
function useHasRole(role) {
|
|
104
|
+
const rbac = useRbac();
|
|
105
|
+
return () => rbac().hasRole(role);
|
|
106
|
+
}
|
|
107
|
+
function useHasAnyRole(...roles) {
|
|
108
|
+
const rbac = useRbac();
|
|
109
|
+
return () => rbac().hasAnyRole(...roles);
|
|
110
|
+
}
|
|
111
|
+
function useHasAllRoles(...roles) {
|
|
112
|
+
const rbac = useRbac();
|
|
113
|
+
return () => rbac().hasAllRoles(...roles);
|
|
114
|
+
}
|
|
115
|
+
var hasCondition = (p) => p.permission !== void 0 || p.role !== void 0 || p.anyOf !== void 0 || p.allOf !== void 0;
|
|
116
|
+
function checkPermissions(rbac, p) {
|
|
117
|
+
if (!hasCondition(p)) return false;
|
|
118
|
+
return (p.permission === void 0 || rbac.can(p.permission)) && (p.anyOf === void 0 || rbac.canAny(p.anyOf)) && (p.allOf === void 0 || rbac.canAll(p.allOf));
|
|
119
|
+
}
|
|
120
|
+
function checkRoles(rbac, p) {
|
|
121
|
+
if (!hasCondition(p)) return false;
|
|
122
|
+
return (p.role === void 0 || rbac.hasRole(p.role)) && (p.anyOf === void 0 || rbac.hasAnyRole(p.anyOf)) && (p.allOf === void 0 || rbac.hasAllRoles(p.allOf));
|
|
123
|
+
}
|
|
124
|
+
function gate(props, pass) {
|
|
125
|
+
const rbac = useRbac();
|
|
126
|
+
const state = solidJs.createMemo(() => rbac().loading ? "loading" : pass(rbac()) ? "pass" : "fail");
|
|
127
|
+
const view = () => {
|
|
128
|
+
const s = state();
|
|
129
|
+
if (s === "loading") return null;
|
|
130
|
+
return solidJs.untrack(() => s === "pass" ? props.children : props.fallback);
|
|
131
|
+
};
|
|
132
|
+
return view;
|
|
133
|
+
}
|
|
134
|
+
function Can(props) {
|
|
135
|
+
return gate(props, (rbac) => checkPermissions(rbac, props));
|
|
136
|
+
}
|
|
137
|
+
function Cannot(props) {
|
|
138
|
+
return gate(props, (rbac) => hasCondition(props) && !checkPermissions(rbac, props));
|
|
139
|
+
}
|
|
140
|
+
function HasRole(props) {
|
|
141
|
+
return gate(props, (rbac) => checkRoles(rbac, props));
|
|
142
|
+
}
|
|
143
|
+
function UnlessRole(props) {
|
|
144
|
+
return gate(props, (rbac) => hasCondition(props) && !checkRoles(rbac, props));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
exports.Can = Can;
|
|
148
|
+
exports.Cannot = Cannot;
|
|
149
|
+
exports.HasRole = HasRole;
|
|
150
|
+
exports.RbacProvider = RbacProvider;
|
|
151
|
+
exports.UnlessRole = UnlessRole;
|
|
152
|
+
exports.createRbacChecker = createRbacChecker;
|
|
153
|
+
exports.useCan = useCan;
|
|
154
|
+
exports.useCanAll = useCanAll;
|
|
155
|
+
exports.useCanAny = useCanAny;
|
|
156
|
+
exports.useCannot = useCannot;
|
|
157
|
+
exports.useHasAllRoles = useHasAllRoles;
|
|
158
|
+
exports.useHasAnyRole = useHasAnyRole;
|
|
159
|
+
exports.useHasRole = useHasRole;
|
|
160
|
+
exports.useRbac = useRbac;
|
|
161
|
+
//# sourceMappingURL=index.cjs.map
|
|
162
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/checker.ts","../src/context.ts","../src/components.ts"],"names":["wildcardImplies","createContext","createMemo","createComponent","useContext","untrack"],"mappings":";;;;;;AAgCA,IAAM,QAAsB,EAAE,KAAA,EAAO,EAAC,EAAG,WAAA,EAAa,EAAC,EAAE;AAEzD,SAAS,QAAQ,KAAA,EAA0C;AACzD,EAAA,MAAM,MAAgB,EAAC;AACvB,EAAA,KAAA,MAAW,QAAQ,KAAA,EAAO;AACxB,IAAA,IAAI,OAAO,IAAA,KAAS,QAAA,EAAU,GAAA,CAAI,KAAK,IAAI,CAAA;AAAA,SACtC,GAAA,CAAI,IAAA,CAAK,GAAG,IAAI,CAAA;AAAA,EACvB;AACA,EAAA,OAAO,GAAA;AACT;AAEA,SAAS,WAAA,CAAY,SAAiB,QAAA,EAA2B;AAC/D,EAAA,IAAI;AACF,IAAA,OAAOA,6BAAA,CAAgB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":"index.cjs","sourcesContent":["import { wildcardImplies } from '@anievo-id/rbac-evolution'\nimport type { Many, RbacSnapshot } from '@anievo-id/rbac-evolution'\n\nexport interface RbacCheckerOptions {\n /** Treat granted permissions as wildcard patterns (`articles.*`). Must match the server's `wildcard` setting. */\n wildcard?: boolean | undefined\n /** Roles that pass every permission check (like spatie's `Gate::before` super-admin pattern). */\n superAdminRoles?: readonly string[] | undefined\n /** The snapshot is still being fetched: `<Can>`, `<Cannot>` and `<HasRole>` render nothing. Default: `false`. */\n loading?: boolean | undefined\n}\n\n/** Synchronous checks against a {@link RbacSnapshot}. */\nexport interface RbacChecker {\n readonly roles: readonly string[]\n readonly permissions: readonly string[]\n /** `true` if the subject has one of the configured `superAdminRoles`. */\n readonly isSuperAdmin: boolean\n /** `true` while the snapshot is still being fetched (the `loading` option). Every check, `cannot` included, returns `false` meanwhile. */\n readonly loading: boolean\n can(permission: string): boolean\n cannot(permission: string): boolean\n /** `true` if ANY of the permissions is granted. `false` for an empty list. */\n canAny(...permissions: Many<string>[]): boolean\n /** `true` if ALL of the permissions are granted. `false` for an empty list (deny by default, like the Vue package). */\n canAll(...permissions: Many<string>[]): boolean\n /** `true` if the subject has the role — or ANY of the roles when given an array. */\n hasRole(roles: Many<string>): boolean\n hasAnyRole(...roles: Many<string>[]): boolean\n hasAllRoles(...roles: Many<string>[]): boolean\n}\n\nconst EMPTY: RbacSnapshot = { roles: [], permissions: [] }\n\nfunction flatten(items: readonly Many<string>[]): string[] {\n const out: string[] = []\n for (const item of items) {\n if (typeof item === 'string') out.push(item)\n else out.push(...item)\n }\n return out\n}\n\nfunction safeImplies(granted: string, required: string): boolean {\n try {\n return wildcardImplies(granted, required)\n } catch {\n return false\n }\n}\n\n/**\n * Builds a pure, synchronous checker for a snapshot (as returned by `rbac.for(user).snapshot()`).\n * Used by `RbacProvider`, but usable on its own too (no Solid needed).\n */\nexport function createRbacChecker(\n snapshot: RbacSnapshot | null | undefined,\n options: RbacCheckerOptions = {},\n): RbacChecker {\n const { roles, permissions } = snapshot ?? EMPTY\n const roleSet = new Set(roles)\n const permissionSet = new Set(permissions)\n // Names the server's `before` hook denies (`snapshot({ checkBefore })`). Absent in older snapshots: nothing denied.\n const deniedSet = new Set((snapshot?.denied ?? []).map((name) => name.trim()))\n const wildcard = options.wildcard ?? false\n // While loading, every check (`cannot` too) is false: neither a stale grant nor \"no access\" UI shows.\n const loading = options.loading ?? false\n const isSuperAdmin = !loading && (options.superAdminRoles ?? []).some((r) => roleSet.has(r))\n\n const can = (permission: string): boolean => {\n if (loading) return false\n // Like the server: `before` runs first, so its denial wins over super-admin and stored grants.\n if (deniedSet.has(permission.trim())) return false\n if (isSuperAdmin) return true\n const name = permission.trim()\n if (name === '') return false\n if (permissionSet.has(name)) return true\n return wildcard && permissions.some((granted) => safeImplies(granted, name))\n }\n const hasOne = (role: string): boolean => !loading && roleSet.has(role.trim())\n\n return {\n roles,\n permissions,\n isSuperAdmin,\n loading,\n can,\n cannot: (permission) => !loading && !can(permission),\n canAny: (...list) => flatten(list).some(can),\n canAll: (...list) => {\n const wanted = flatten(list)\n return wanted.length > 0 && wanted.every(can)\n },\n hasRole: (list) => flatten([list]).some(hasOne),\n hasAnyRole: (...list) => flatten(list).some(hasOne),\n hasAllRoles: (...list) => {\n const wanted = flatten(list)\n return wanted.length > 0 && wanted.every(hasOne)\n },\n }\n}\n","import type { Many, RbacSnapshot } from '@anievo-id/rbac-evolution'\nimport { type Accessor, createComponent, createContext, createMemo, useContext } from 'solid-js'\nimport { type RbacChecker, createRbacChecker } from './checker'\n\n/**\n * What a Solid component renders: `JSX.Element` in Solid 1.x, `Element` in 2.x (where `solid-js` no longer exports the\n * `JSX` namespace). Taken from `createComponent`, which both versions export, so one build types against either.\n */\nexport type SolidElement = ReturnType<typeof createComponent>\n\nconst RbacContext = createContext<Accessor<RbacChecker>>()\n\nexport interface RbacProviderProps {\n /** Roles & permissions of the current user, e.g. from `rbac.for(user).snapshot()` on the server. `null` = guest. */\n snapshot: RbacSnapshot | null | undefined\n /** Enable wildcard matching (`articles.*`). Must match the server's `wildcard` setting. Default: `false`. */\n wildcard?: boolean | undefined\n /** Roles that pass every `can` check (super admin). */\n superAdminRoles?: readonly string[] | undefined\n /**\n * The snapshot is still being fetched. `<Can>`, `<Cannot>`, `<HasRole>` and `<UnlessRole>` then render nothing\n * (not even `fallback`), so nothing flashes before the real permissions arrive. Default: `false`.\n */\n loading?: boolean | undefined\n children?: SolidElement\n}\n\ntype ProviderComponent = (props: { value: Accessor<RbacChecker>; children?: SolidElement }) => SolidElement\n\n/**\n * Provides a user's roles & permissions to `useCan()`, `<Can>`, ... Every prop is reactive: pass a signal or a\n * resource (`snapshot={data()}`) and everything below updates.\n *\n * UI only: this hides and shows elements. Always enforce authorization on the server.\n */\nexport function RbacProvider(props: RbacProviderProps): SolidElement {\n const checker = createMemo(() =>\n createRbacChecker(props.snapshot, {\n wildcard: props.wildcard ?? false,\n superAdminRoles: props.superAdminRoles,\n loading: props.loading ?? false,\n }),\n )\n // Solid 1.x: `Context.Provider`. Solid 2.x: the context itself is the provider component.\n const Provider = ((RbacContext as unknown as { Provider?: ProviderComponent }).Provider ??\n RbacContext) as unknown as ProviderComponent\n return createComponent(Provider, {\n value: checker,\n get children() {\n return props.children\n },\n })\n}\n\n/** The checker of the nearest `RbacProvider`, as an accessor. Throws when there is none. */\nexport function useRbac(): Accessor<RbacChecker> {\n let checker: Accessor<RbacChecker> | undefined\n try {\n checker = useContext(RbacContext)\n } catch {\n // Solid 2 throws itself for a context without a provider and default value.\n }\n if (!checker) throw new Error('useRbac() must be used inside <RbacProvider>.')\n return checker\n}\n\n/** `true` if the user has the permission. */\nexport function useCan(permission: string): Accessor<boolean> {\n const rbac = useRbac()\n return () => rbac().can(permission)\n}\n\n/** `true` if the user does NOT have the permission (`false` while loading). */\nexport function useCannot(permission: string): Accessor<boolean> {\n const rbac = useRbac()\n return () => rbac().cannot(permission)\n}\n\n/** `true` if the user has ANY of the permissions. */\nexport function useCanAny(...permissions: Many<string>[]): Accessor<boolean> {\n const rbac = useRbac()\n return () => rbac().canAny(...permissions)\n}\n\n/** `true` if the user has ALL of the permissions. */\nexport function useCanAll(...permissions: Many<string>[]): Accessor<boolean> {\n const rbac = useRbac()\n return () => rbac().canAll(...permissions)\n}\n\n/** `true` if the user has the role, or ANY of the roles when given an array. */\nexport function useHasRole(role: Many<string>): Accessor<boolean> {\n const rbac = useRbac()\n return () => rbac().hasRole(role)\n}\n\n/** `true` if the user has ANY of the roles. */\nexport function useHasAnyRole(...roles: Many<string>[]): Accessor<boolean> {\n const rbac = useRbac()\n return () => rbac().hasAnyRole(...roles)\n}\n\n/** `true` if the user has ALL of the roles. */\nexport function useHasAllRoles(...roles: Many<string>[]): Accessor<boolean> {\n const rbac = useRbac()\n return () => rbac().hasAllRoles(...roles)\n}\n","import { createMemo, untrack } from 'solid-js'\nimport type { RbacChecker } from './checker'\nimport { useRbac } from './context'\nimport type { SolidElement } from './context'\n\n/** Exactly one of `permission`, `anyOf` or `allOf`. */\nexport type PermissionCondition =\n | { permission: string; anyOf?: never; allOf?: never }\n | { permission?: never; anyOf: readonly string[]; allOf?: never }\n | { permission?: never; anyOf?: never; allOf: readonly string[] }\n\n/** Exactly one of `role`, `anyOf` or `allOf`. */\nexport type RoleCondition =\n | { role: string; anyOf?: never; allOf?: never }\n | { role?: never; anyOf: readonly string[]; allOf?: never }\n | { role?: never; anyOf?: never; allOf: readonly string[] }\n\ninterface GateProps {\n children?: SolidElement\n /** Rendered when the check fails. Default: nothing. */\n fallback?: SolidElement\n}\n\nexport type CanProps = PermissionCondition & GateProps\nexport type CannotProps = PermissionCondition & GateProps\nexport type HasRoleProps = RoleCondition & GateProps\nexport type UnlessRoleProps = RoleCondition & GateProps\n\ninterface LooseCondition {\n permission?: string | undefined\n role?: string | undefined\n anyOf?: readonly string[] | undefined\n allOf?: readonly string[] | undefined\n}\n\nconst hasCondition = (p: LooseCondition): boolean =>\n p.permission !== undefined || p.role !== undefined || p.anyOf !== undefined || p.allOf !== undefined\n\n// Conditions are ANDed; no condition at all is denied (deny by default, also for untyped JS callers).\nfunction checkPermissions(rbac: RbacChecker, p: LooseCondition): boolean {\n if (!hasCondition(p)) return false\n return (\n (p.permission === undefined || rbac.can(p.permission)) &&\n (p.anyOf === undefined || rbac.canAny(p.anyOf)) &&\n (p.allOf === undefined || rbac.canAll(p.allOf))\n )\n}\n\nfunction checkRoles(rbac: RbacChecker, p: LooseCondition): boolean {\n if (!hasCondition(p)) return false\n return (\n (p.role === undefined || rbac.hasRole(p.role)) &&\n (p.anyOf === undefined || rbac.hasAnyRole(p.anyOf)) &&\n (p.allOf === undefined || rbac.hasAllRoles(p.allOf))\n )\n}\n\n/**\n * Renders `children` when `pass` is true, else `fallback`, and nothing while loading. Like `<Show>`: the branch is\n * only rebuilt when the result changes, and the children are created untracked.\n */\nfunction gate(props: GateProps, pass: (rbac: RbacChecker) => boolean): SolidElement {\n const rbac = useRbac()\n const state = createMemo(() => (rbac().loading ? 'loading' : pass(rbac()) ? 'pass' : 'fail'))\n const view = (): SolidElement => {\n const s = state()\n if (s === 'loading') return null\n return untrack(() => (s === 'pass' ? props.children : props.fallback))\n }\n return view as unknown as SolidElement\n}\n\n/** Renders `children` if the user has the permission(s), like spatie's `@can` / `@canany`. */\nexport function Can(props: CanProps): SolidElement {\n return gate(props, (rbac) => checkPermissions(rbac, props))\n}\n\n/** Renders `children` if the user does NOT have the permission(s), like spatie's `@cannot`. */\nexport function Cannot(props: CannotProps): SolidElement {\n return gate(props, (rbac) => hasCondition(props) && !checkPermissions(rbac, props))\n}\n\n/** Renders `children` if the user has the role(s), like spatie's `@role` / `@hasanyrole` / `@hasallroles`. */\nexport function HasRole(props: HasRoleProps): SolidElement {\n return gate(props, (rbac) => checkRoles(rbac, props))\n}\n\n/**\n * Renders `children` if the user does NOT have the role(s), like spatie's `@unlessrole`.\n * `anyOf`: none of them. `allOf`: not all of them. Without a condition it renders `fallback` (deny by default).\n */\nexport function UnlessRole(props: UnlessRoleProps): SolidElement {\n return gate(props, (rbac) => hasCondition(props) && !checkRoles(rbac, props))\n}\n"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { Many, RbacSnapshot } from '@anievo-id/rbac-evolution';
|
|
2
|
+
import { createComponent, Accessor } from 'solid-js';
|
|
3
|
+
|
|
4
|
+
interface RbacCheckerOptions {
|
|
5
|
+
/** Treat granted permissions as wildcard patterns (`articles.*`). Must match the server's `wildcard` setting. */
|
|
6
|
+
wildcard?: boolean | undefined;
|
|
7
|
+
/** Roles that pass every permission check (like spatie's `Gate::before` super-admin pattern). */
|
|
8
|
+
superAdminRoles?: readonly string[] | undefined;
|
|
9
|
+
/** The snapshot is still being fetched: `<Can>`, `<Cannot>` and `<HasRole>` render nothing. Default: `false`. */
|
|
10
|
+
loading?: boolean | undefined;
|
|
11
|
+
}
|
|
12
|
+
/** Synchronous checks against a {@link RbacSnapshot}. */
|
|
13
|
+
interface RbacChecker {
|
|
14
|
+
readonly roles: readonly string[];
|
|
15
|
+
readonly permissions: readonly string[];
|
|
16
|
+
/** `true` if the subject has one of the configured `superAdminRoles`. */
|
|
17
|
+
readonly isSuperAdmin: boolean;
|
|
18
|
+
/** `true` while the snapshot is still being fetched (the `loading` option). Every check, `cannot` included, returns `false` meanwhile. */
|
|
19
|
+
readonly loading: boolean;
|
|
20
|
+
can(permission: string): boolean;
|
|
21
|
+
cannot(permission: string): boolean;
|
|
22
|
+
/** `true` if ANY of the permissions is granted. `false` for an empty list. */
|
|
23
|
+
canAny(...permissions: Many<string>[]): boolean;
|
|
24
|
+
/** `true` if ALL of the permissions are granted. `false` for an empty list (deny by default, like the Vue package). */
|
|
25
|
+
canAll(...permissions: Many<string>[]): boolean;
|
|
26
|
+
/** `true` if the subject has the role — or ANY of the roles when given an array. */
|
|
27
|
+
hasRole(roles: Many<string>): boolean;
|
|
28
|
+
hasAnyRole(...roles: Many<string>[]): boolean;
|
|
29
|
+
hasAllRoles(...roles: Many<string>[]): boolean;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Builds a pure, synchronous checker for a snapshot (as returned by `rbac.for(user).snapshot()`).
|
|
33
|
+
* Used by `RbacProvider`, but usable on its own too (no Solid needed).
|
|
34
|
+
*/
|
|
35
|
+
declare function createRbacChecker(snapshot: RbacSnapshot | null | undefined, options?: RbacCheckerOptions): RbacChecker;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* What a Solid component renders: `JSX.Element` in Solid 1.x, `Element` in 2.x (where `solid-js` no longer exports the
|
|
39
|
+
* `JSX` namespace). Taken from `createComponent`, which both versions export, so one build types against either.
|
|
40
|
+
*/
|
|
41
|
+
type SolidElement = ReturnType<typeof createComponent>;
|
|
42
|
+
interface RbacProviderProps {
|
|
43
|
+
/** Roles & permissions of the current user, e.g. from `rbac.for(user).snapshot()` on the server. `null` = guest. */
|
|
44
|
+
snapshot: RbacSnapshot | null | undefined;
|
|
45
|
+
/** Enable wildcard matching (`articles.*`). Must match the server's `wildcard` setting. Default: `false`. */
|
|
46
|
+
wildcard?: boolean | undefined;
|
|
47
|
+
/** Roles that pass every `can` check (super admin). */
|
|
48
|
+
superAdminRoles?: readonly string[] | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* The snapshot is still being fetched. `<Can>`, `<Cannot>`, `<HasRole>` and `<UnlessRole>` then render nothing
|
|
51
|
+
* (not even `fallback`), so nothing flashes before the real permissions arrive. Default: `false`.
|
|
52
|
+
*/
|
|
53
|
+
loading?: boolean | undefined;
|
|
54
|
+
children?: SolidElement;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Provides a user's roles & permissions to `useCan()`, `<Can>`, ... Every prop is reactive: pass a signal or a
|
|
58
|
+
* resource (`snapshot={data()}`) and everything below updates.
|
|
59
|
+
*
|
|
60
|
+
* UI only: this hides and shows elements. Always enforce authorization on the server.
|
|
61
|
+
*/
|
|
62
|
+
declare function RbacProvider(props: RbacProviderProps): SolidElement;
|
|
63
|
+
/** The checker of the nearest `RbacProvider`, as an accessor. Throws when there is none. */
|
|
64
|
+
declare function useRbac(): Accessor<RbacChecker>;
|
|
65
|
+
/** `true` if the user has the permission. */
|
|
66
|
+
declare function useCan(permission: string): Accessor<boolean>;
|
|
67
|
+
/** `true` if the user does NOT have the permission (`false` while loading). */
|
|
68
|
+
declare function useCannot(permission: string): Accessor<boolean>;
|
|
69
|
+
/** `true` if the user has ANY of the permissions. */
|
|
70
|
+
declare function useCanAny(...permissions: Many<string>[]): Accessor<boolean>;
|
|
71
|
+
/** `true` if the user has ALL of the permissions. */
|
|
72
|
+
declare function useCanAll(...permissions: Many<string>[]): Accessor<boolean>;
|
|
73
|
+
/** `true` if the user has the role, or ANY of the roles when given an array. */
|
|
74
|
+
declare function useHasRole(role: Many<string>): Accessor<boolean>;
|
|
75
|
+
/** `true` if the user has ANY of the roles. */
|
|
76
|
+
declare function useHasAnyRole(...roles: Many<string>[]): Accessor<boolean>;
|
|
77
|
+
/** `true` if the user has ALL of the roles. */
|
|
78
|
+
declare function useHasAllRoles(...roles: Many<string>[]): Accessor<boolean>;
|
|
79
|
+
|
|
80
|
+
/** Exactly one of `permission`, `anyOf` or `allOf`. */
|
|
81
|
+
type PermissionCondition = {
|
|
82
|
+
permission: string;
|
|
83
|
+
anyOf?: never;
|
|
84
|
+
allOf?: never;
|
|
85
|
+
} | {
|
|
86
|
+
permission?: never;
|
|
87
|
+
anyOf: readonly string[];
|
|
88
|
+
allOf?: never;
|
|
89
|
+
} | {
|
|
90
|
+
permission?: never;
|
|
91
|
+
anyOf?: never;
|
|
92
|
+
allOf: readonly string[];
|
|
93
|
+
};
|
|
94
|
+
/** Exactly one of `role`, `anyOf` or `allOf`. */
|
|
95
|
+
type RoleCondition = {
|
|
96
|
+
role: string;
|
|
97
|
+
anyOf?: never;
|
|
98
|
+
allOf?: never;
|
|
99
|
+
} | {
|
|
100
|
+
role?: never;
|
|
101
|
+
anyOf: readonly string[];
|
|
102
|
+
allOf?: never;
|
|
103
|
+
} | {
|
|
104
|
+
role?: never;
|
|
105
|
+
anyOf?: never;
|
|
106
|
+
allOf: readonly string[];
|
|
107
|
+
};
|
|
108
|
+
interface GateProps {
|
|
109
|
+
children?: SolidElement;
|
|
110
|
+
/** Rendered when the check fails. Default: nothing. */
|
|
111
|
+
fallback?: SolidElement;
|
|
112
|
+
}
|
|
113
|
+
type CanProps = PermissionCondition & GateProps;
|
|
114
|
+
type CannotProps = PermissionCondition & GateProps;
|
|
115
|
+
type HasRoleProps = RoleCondition & GateProps;
|
|
116
|
+
type UnlessRoleProps = RoleCondition & GateProps;
|
|
117
|
+
/** Renders `children` if the user has the permission(s), like spatie's `@can` / `@canany`. */
|
|
118
|
+
declare function Can(props: CanProps): SolidElement;
|
|
119
|
+
/** Renders `children` if the user does NOT have the permission(s), like spatie's `@cannot`. */
|
|
120
|
+
declare function Cannot(props: CannotProps): SolidElement;
|
|
121
|
+
/** Renders `children` if the user has the role(s), like spatie's `@role` / `@hasanyrole` / `@hasallroles`. */
|
|
122
|
+
declare function HasRole(props: HasRoleProps): SolidElement;
|
|
123
|
+
/**
|
|
124
|
+
* Renders `children` if the user does NOT have the role(s), like spatie's `@unlessrole`.
|
|
125
|
+
* `anyOf`: none of them. `allOf`: not all of them. Without a condition it renders `fallback` (deny by default).
|
|
126
|
+
*/
|
|
127
|
+
declare function UnlessRole(props: UnlessRoleProps): SolidElement;
|
|
128
|
+
|
|
129
|
+
export { Can, type CanProps, Cannot, type CannotProps, HasRole, type HasRoleProps, type PermissionCondition, type RbacChecker, type RbacCheckerOptions, RbacProvider, type RbacProviderProps, type RoleCondition, type SolidElement, UnlessRole, type UnlessRoleProps, createRbacChecker, useCan, useCanAll, useCanAny, useCannot, useHasAllRoles, useHasAnyRole, useHasRole, useRbac };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { Many, RbacSnapshot } from '@anievo-id/rbac-evolution';
|
|
2
|
+
import { createComponent, Accessor } from 'solid-js';
|
|
3
|
+
|
|
4
|
+
interface RbacCheckerOptions {
|
|
5
|
+
/** Treat granted permissions as wildcard patterns (`articles.*`). Must match the server's `wildcard` setting. */
|
|
6
|
+
wildcard?: boolean | undefined;
|
|
7
|
+
/** Roles that pass every permission check (like spatie's `Gate::before` super-admin pattern). */
|
|
8
|
+
superAdminRoles?: readonly string[] | undefined;
|
|
9
|
+
/** The snapshot is still being fetched: `<Can>`, `<Cannot>` and `<HasRole>` render nothing. Default: `false`. */
|
|
10
|
+
loading?: boolean | undefined;
|
|
11
|
+
}
|
|
12
|
+
/** Synchronous checks against a {@link RbacSnapshot}. */
|
|
13
|
+
interface RbacChecker {
|
|
14
|
+
readonly roles: readonly string[];
|
|
15
|
+
readonly permissions: readonly string[];
|
|
16
|
+
/** `true` if the subject has one of the configured `superAdminRoles`. */
|
|
17
|
+
readonly isSuperAdmin: boolean;
|
|
18
|
+
/** `true` while the snapshot is still being fetched (the `loading` option). Every check, `cannot` included, returns `false` meanwhile. */
|
|
19
|
+
readonly loading: boolean;
|
|
20
|
+
can(permission: string): boolean;
|
|
21
|
+
cannot(permission: string): boolean;
|
|
22
|
+
/** `true` if ANY of the permissions is granted. `false` for an empty list. */
|
|
23
|
+
canAny(...permissions: Many<string>[]): boolean;
|
|
24
|
+
/** `true` if ALL of the permissions are granted. `false` for an empty list (deny by default, like the Vue package). */
|
|
25
|
+
canAll(...permissions: Many<string>[]): boolean;
|
|
26
|
+
/** `true` if the subject has the role — or ANY of the roles when given an array. */
|
|
27
|
+
hasRole(roles: Many<string>): boolean;
|
|
28
|
+
hasAnyRole(...roles: Many<string>[]): boolean;
|
|
29
|
+
hasAllRoles(...roles: Many<string>[]): boolean;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Builds a pure, synchronous checker for a snapshot (as returned by `rbac.for(user).snapshot()`).
|
|
33
|
+
* Used by `RbacProvider`, but usable on its own too (no Solid needed).
|
|
34
|
+
*/
|
|
35
|
+
declare function createRbacChecker(snapshot: RbacSnapshot | null | undefined, options?: RbacCheckerOptions): RbacChecker;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* What a Solid component renders: `JSX.Element` in Solid 1.x, `Element` in 2.x (where `solid-js` no longer exports the
|
|
39
|
+
* `JSX` namespace). Taken from `createComponent`, which both versions export, so one build types against either.
|
|
40
|
+
*/
|
|
41
|
+
type SolidElement = ReturnType<typeof createComponent>;
|
|
42
|
+
interface RbacProviderProps {
|
|
43
|
+
/** Roles & permissions of the current user, e.g. from `rbac.for(user).snapshot()` on the server. `null` = guest. */
|
|
44
|
+
snapshot: RbacSnapshot | null | undefined;
|
|
45
|
+
/** Enable wildcard matching (`articles.*`). Must match the server's `wildcard` setting. Default: `false`. */
|
|
46
|
+
wildcard?: boolean | undefined;
|
|
47
|
+
/** Roles that pass every `can` check (super admin). */
|
|
48
|
+
superAdminRoles?: readonly string[] | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* The snapshot is still being fetched. `<Can>`, `<Cannot>`, `<HasRole>` and `<UnlessRole>` then render nothing
|
|
51
|
+
* (not even `fallback`), so nothing flashes before the real permissions arrive. Default: `false`.
|
|
52
|
+
*/
|
|
53
|
+
loading?: boolean | undefined;
|
|
54
|
+
children?: SolidElement;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Provides a user's roles & permissions to `useCan()`, `<Can>`, ... Every prop is reactive: pass a signal or a
|
|
58
|
+
* resource (`snapshot={data()}`) and everything below updates.
|
|
59
|
+
*
|
|
60
|
+
* UI only: this hides and shows elements. Always enforce authorization on the server.
|
|
61
|
+
*/
|
|
62
|
+
declare function RbacProvider(props: RbacProviderProps): SolidElement;
|
|
63
|
+
/** The checker of the nearest `RbacProvider`, as an accessor. Throws when there is none. */
|
|
64
|
+
declare function useRbac(): Accessor<RbacChecker>;
|
|
65
|
+
/** `true` if the user has the permission. */
|
|
66
|
+
declare function useCan(permission: string): Accessor<boolean>;
|
|
67
|
+
/** `true` if the user does NOT have the permission (`false` while loading). */
|
|
68
|
+
declare function useCannot(permission: string): Accessor<boolean>;
|
|
69
|
+
/** `true` if the user has ANY of the permissions. */
|
|
70
|
+
declare function useCanAny(...permissions: Many<string>[]): Accessor<boolean>;
|
|
71
|
+
/** `true` if the user has ALL of the permissions. */
|
|
72
|
+
declare function useCanAll(...permissions: Many<string>[]): Accessor<boolean>;
|
|
73
|
+
/** `true` if the user has the role, or ANY of the roles when given an array. */
|
|
74
|
+
declare function useHasRole(role: Many<string>): Accessor<boolean>;
|
|
75
|
+
/** `true` if the user has ANY of the roles. */
|
|
76
|
+
declare function useHasAnyRole(...roles: Many<string>[]): Accessor<boolean>;
|
|
77
|
+
/** `true` if the user has ALL of the roles. */
|
|
78
|
+
declare function useHasAllRoles(...roles: Many<string>[]): Accessor<boolean>;
|
|
79
|
+
|
|
80
|
+
/** Exactly one of `permission`, `anyOf` or `allOf`. */
|
|
81
|
+
type PermissionCondition = {
|
|
82
|
+
permission: string;
|
|
83
|
+
anyOf?: never;
|
|
84
|
+
allOf?: never;
|
|
85
|
+
} | {
|
|
86
|
+
permission?: never;
|
|
87
|
+
anyOf: readonly string[];
|
|
88
|
+
allOf?: never;
|
|
89
|
+
} | {
|
|
90
|
+
permission?: never;
|
|
91
|
+
anyOf?: never;
|
|
92
|
+
allOf: readonly string[];
|
|
93
|
+
};
|
|
94
|
+
/** Exactly one of `role`, `anyOf` or `allOf`. */
|
|
95
|
+
type RoleCondition = {
|
|
96
|
+
role: string;
|
|
97
|
+
anyOf?: never;
|
|
98
|
+
allOf?: never;
|
|
99
|
+
} | {
|
|
100
|
+
role?: never;
|
|
101
|
+
anyOf: readonly string[];
|
|
102
|
+
allOf?: never;
|
|
103
|
+
} | {
|
|
104
|
+
role?: never;
|
|
105
|
+
anyOf?: never;
|
|
106
|
+
allOf: readonly string[];
|
|
107
|
+
};
|
|
108
|
+
interface GateProps {
|
|
109
|
+
children?: SolidElement;
|
|
110
|
+
/** Rendered when the check fails. Default: nothing. */
|
|
111
|
+
fallback?: SolidElement;
|
|
112
|
+
}
|
|
113
|
+
type CanProps = PermissionCondition & GateProps;
|
|
114
|
+
type CannotProps = PermissionCondition & GateProps;
|
|
115
|
+
type HasRoleProps = RoleCondition & GateProps;
|
|
116
|
+
type UnlessRoleProps = RoleCondition & GateProps;
|
|
117
|
+
/** Renders `children` if the user has the permission(s), like spatie's `@can` / `@canany`. */
|
|
118
|
+
declare function Can(props: CanProps): SolidElement;
|
|
119
|
+
/** Renders `children` if the user does NOT have the permission(s), like spatie's `@cannot`. */
|
|
120
|
+
declare function Cannot(props: CannotProps): SolidElement;
|
|
121
|
+
/** Renders `children` if the user has the role(s), like spatie's `@role` / `@hasanyrole` / `@hasallroles`. */
|
|
122
|
+
declare function HasRole(props: HasRoleProps): SolidElement;
|
|
123
|
+
/**
|
|
124
|
+
* Renders `children` if the user does NOT have the role(s), like spatie's `@unlessrole`.
|
|
125
|
+
* `anyOf`: none of them. `allOf`: not all of them. Without a condition it renders `fallback` (deny by default).
|
|
126
|
+
*/
|
|
127
|
+
declare function UnlessRole(props: UnlessRoleProps): SolidElement;
|
|
128
|
+
|
|
129
|
+
export { Can, type CanProps, Cannot, type CannotProps, HasRole, type HasRoleProps, type PermissionCondition, type RbacChecker, type RbacCheckerOptions, RbacProvider, type RbacProviderProps, type RoleCondition, type SolidElement, UnlessRole, type UnlessRoleProps, createRbacChecker, useCan, useCanAll, useCanAny, useCannot, useHasAllRoles, useHasAnyRole, useHasRole, useRbac };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { wildcardImplies } from '@anievo-id/rbac-evolution';
|
|
2
|
+
import { createContext, createMemo, createComponent, useContext, untrack } from 'solid-js';
|
|
3
|
+
|
|
4
|
+
// src/checker.ts
|
|
5
|
+
var EMPTY = { roles: [], permissions: [] };
|
|
6
|
+
function flatten(items) {
|
|
7
|
+
const out = [];
|
|
8
|
+
for (const item of items) {
|
|
9
|
+
if (typeof item === "string") out.push(item);
|
|
10
|
+
else out.push(...item);
|
|
11
|
+
}
|
|
12
|
+
return out;
|
|
13
|
+
}
|
|
14
|
+
function safeImplies(granted, required) {
|
|
15
|
+
try {
|
|
16
|
+
return wildcardImplies(granted, required);
|
|
17
|
+
} catch {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function createRbacChecker(snapshot, options = {}) {
|
|
22
|
+
const { roles, permissions } = snapshot ?? EMPTY;
|
|
23
|
+
const roleSet = new Set(roles);
|
|
24
|
+
const permissionSet = new Set(permissions);
|
|
25
|
+
const deniedSet = new Set((snapshot?.denied ?? []).map((name) => name.trim()));
|
|
26
|
+
const wildcard = options.wildcard ?? false;
|
|
27
|
+
const loading = options.loading ?? false;
|
|
28
|
+
const isSuperAdmin = !loading && (options.superAdminRoles ?? []).some((r) => roleSet.has(r));
|
|
29
|
+
const can = (permission) => {
|
|
30
|
+
if (loading) return false;
|
|
31
|
+
if (deniedSet.has(permission.trim())) return false;
|
|
32
|
+
if (isSuperAdmin) return true;
|
|
33
|
+
const name = permission.trim();
|
|
34
|
+
if (name === "") return false;
|
|
35
|
+
if (permissionSet.has(name)) return true;
|
|
36
|
+
return wildcard && permissions.some((granted) => safeImplies(granted, name));
|
|
37
|
+
};
|
|
38
|
+
const hasOne = (role) => !loading && roleSet.has(role.trim());
|
|
39
|
+
return {
|
|
40
|
+
roles,
|
|
41
|
+
permissions,
|
|
42
|
+
isSuperAdmin,
|
|
43
|
+
loading,
|
|
44
|
+
can,
|
|
45
|
+
cannot: (permission) => !loading && !can(permission),
|
|
46
|
+
canAny: (...list) => flatten(list).some(can),
|
|
47
|
+
canAll: (...list) => {
|
|
48
|
+
const wanted = flatten(list);
|
|
49
|
+
return wanted.length > 0 && wanted.every(can);
|
|
50
|
+
},
|
|
51
|
+
hasRole: (list) => flatten([list]).some(hasOne),
|
|
52
|
+
hasAnyRole: (...list) => flatten(list).some(hasOne),
|
|
53
|
+
hasAllRoles: (...list) => {
|
|
54
|
+
const wanted = flatten(list);
|
|
55
|
+
return wanted.length > 0 && wanted.every(hasOne);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
var RbacContext = createContext();
|
|
60
|
+
function RbacProvider(props) {
|
|
61
|
+
const checker = createMemo(
|
|
62
|
+
() => createRbacChecker(props.snapshot, {
|
|
63
|
+
wildcard: props.wildcard ?? false,
|
|
64
|
+
superAdminRoles: props.superAdminRoles,
|
|
65
|
+
loading: props.loading ?? false
|
|
66
|
+
})
|
|
67
|
+
);
|
|
68
|
+
const Provider = RbacContext.Provider ?? RbacContext;
|
|
69
|
+
return createComponent(Provider, {
|
|
70
|
+
value: checker,
|
|
71
|
+
get children() {
|
|
72
|
+
return props.children;
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
function useRbac() {
|
|
77
|
+
let checker;
|
|
78
|
+
try {
|
|
79
|
+
checker = useContext(RbacContext);
|
|
80
|
+
} catch {
|
|
81
|
+
}
|
|
82
|
+
if (!checker) throw new Error("useRbac() must be used inside <RbacProvider>.");
|
|
83
|
+
return checker;
|
|
84
|
+
}
|
|
85
|
+
function useCan(permission) {
|
|
86
|
+
const rbac = useRbac();
|
|
87
|
+
return () => rbac().can(permission);
|
|
88
|
+
}
|
|
89
|
+
function useCannot(permission) {
|
|
90
|
+
const rbac = useRbac();
|
|
91
|
+
return () => rbac().cannot(permission);
|
|
92
|
+
}
|
|
93
|
+
function useCanAny(...permissions) {
|
|
94
|
+
const rbac = useRbac();
|
|
95
|
+
return () => rbac().canAny(...permissions);
|
|
96
|
+
}
|
|
97
|
+
function useCanAll(...permissions) {
|
|
98
|
+
const rbac = useRbac();
|
|
99
|
+
return () => rbac().canAll(...permissions);
|
|
100
|
+
}
|
|
101
|
+
function useHasRole(role) {
|
|
102
|
+
const rbac = useRbac();
|
|
103
|
+
return () => rbac().hasRole(role);
|
|
104
|
+
}
|
|
105
|
+
function useHasAnyRole(...roles) {
|
|
106
|
+
const rbac = useRbac();
|
|
107
|
+
return () => rbac().hasAnyRole(...roles);
|
|
108
|
+
}
|
|
109
|
+
function useHasAllRoles(...roles) {
|
|
110
|
+
const rbac = useRbac();
|
|
111
|
+
return () => rbac().hasAllRoles(...roles);
|
|
112
|
+
}
|
|
113
|
+
var hasCondition = (p) => p.permission !== void 0 || p.role !== void 0 || p.anyOf !== void 0 || p.allOf !== void 0;
|
|
114
|
+
function checkPermissions(rbac, p) {
|
|
115
|
+
if (!hasCondition(p)) return false;
|
|
116
|
+
return (p.permission === void 0 || rbac.can(p.permission)) && (p.anyOf === void 0 || rbac.canAny(p.anyOf)) && (p.allOf === void 0 || rbac.canAll(p.allOf));
|
|
117
|
+
}
|
|
118
|
+
function checkRoles(rbac, p) {
|
|
119
|
+
if (!hasCondition(p)) return false;
|
|
120
|
+
return (p.role === void 0 || rbac.hasRole(p.role)) && (p.anyOf === void 0 || rbac.hasAnyRole(p.anyOf)) && (p.allOf === void 0 || rbac.hasAllRoles(p.allOf));
|
|
121
|
+
}
|
|
122
|
+
function gate(props, pass) {
|
|
123
|
+
const rbac = useRbac();
|
|
124
|
+
const state = createMemo(() => rbac().loading ? "loading" : pass(rbac()) ? "pass" : "fail");
|
|
125
|
+
const view = () => {
|
|
126
|
+
const s = state();
|
|
127
|
+
if (s === "loading") return null;
|
|
128
|
+
return untrack(() => s === "pass" ? props.children : props.fallback);
|
|
129
|
+
};
|
|
130
|
+
return view;
|
|
131
|
+
}
|
|
132
|
+
function Can(props) {
|
|
133
|
+
return gate(props, (rbac) => checkPermissions(rbac, props));
|
|
134
|
+
}
|
|
135
|
+
function Cannot(props) {
|
|
136
|
+
return gate(props, (rbac) => hasCondition(props) && !checkPermissions(rbac, props));
|
|
137
|
+
}
|
|
138
|
+
function HasRole(props) {
|
|
139
|
+
return gate(props, (rbac) => checkRoles(rbac, props));
|
|
140
|
+
}
|
|
141
|
+
function UnlessRole(props) {
|
|
142
|
+
return gate(props, (rbac) => hasCondition(props) && !checkRoles(rbac, props));
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export { Can, Cannot, HasRole, RbacProvider, UnlessRole, createRbacChecker, useCan, useCanAll, useCanAny, useCannot, useHasAllRoles, useHasAnyRole, useHasRole, useRbac };
|
|
146
|
+
//# sourceMappingURL=index.js.map
|
|
147
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/checker.ts","../src/context.ts","../src/components.ts"],"names":["createMemo"],"mappings":";;;;AAgCA,IAAM,QAAsB,EAAE,KAAA,EAAO,EAAC,EAAG,WAAA,EAAa,EAAC,EAAE;AAEzD,SAAS,QAAQ,KAAA,EAA0C;AACzD,EAAA,MAAM,MAAgB,EAAC;AACvB,EAAA,KAAA,MAAW,QAAQ,KAAA,EAAO;AACxB,IAAA,IAAI,OAAO,IAAA,KAAS,QAAA,EAAU,GAAA,CAAI,KAAK,IAAI,CAAA;AAAA,SACtC,GAAA,CAAI,IAAA,CAAK,GAAG,IAAI,CAAA;AAAA,EACvB;AACA,EAAA,OAAO,GAAA;AACT;AAEA,SAAS,WAAA,CAAY,SAAiB,QAAA,EAA2B;AAC/D,EAAA,IAAI;AACF,IAAA,OAAO,eAAA,CAAgB,SAAS,QAAQ,CAAA;AAAA,EAC1C,CAAA,CAAA,MAAQ;AACN,IAAA,OAAO,KAAA;AAAA,EACT;AACF;AAMO,SAAS,iBAAA,CACd,QAAA,EACA,OAAA,GAA8B,EAAC,EAClB;AACb,EAAA,MAAM,EAAE,KAAA,EAAO,WAAA,EAAY,GAAI,QAAA,IAAY,KAAA;AAC3C,EAAA,MAAM,OAAA,GAAU,IAAI,GAAA,CAAI,KAAK,CAAA;AAC7B,EAAA,MAAM,aAAA,GAAgB,IAAI,GAAA,CAAI,WAAW,CAAA;AAEzC,EAAA,MAAM,SAAA,GAAY,IAAI,GAAA,CAAA,CAAK,QAAA,EAAU,MAAA,IAAU,EAAC,EAAG,GAAA,CAAI,CAAC,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O,IAAA;AAC5B,IAAA,OAAO,QAAQ,MAAO,CAAA,KAAM,SAAS,KAAA,CAAM,QAAA,GAAW,MAAM,QAAS,CAAA;AAAA,EACvE,CAAA;AACA,EAAA,OAAO,IAAA;AACT;AAGO,SAAS,IAAI,KAAA,EAA+B;AACjD,EAAA,OAAO,KAAK,KAAA,EAAO,CAAC,SAAS,gBAAA,CAAiB,IAAA,EAAM,KAAK,CAAC,CAAA;AAC5D;AAGO,SAAS,OAAO,KAAA,EAAkC;AACvD,EAAA,OAAO,IAAA,CAAK,KAAA,EAAO,CAAC,IAAA,KAAS,YAAA,CAAa,KAAK,CAAA,IAAK,CAAC,gBAAA,CAAiB,IAAA,EAAM,KAAK,CAAC,CAAA;AACpF;AAGO,SAAS,QAAQ,KAAA,EAAmC;AACzD,EAAA,OAAO,KAAK,KAAA,EAAO,CAAC,SAAS,UAAA,CAAW,IAAA,EAAM,KAAK,CAAC,CAAA;AACtD;AAMO,SAAS,WAAW,KAAA,EAAsC;AAC/D,EAAA,OAAO,IAAA,CAAK,KAAA,EAAO,CAAC,IAAA,KAAS,YAAA,CAAa,KAAK,CAAA,IAAK,CAAC,UAAA,CAAW,IAAA,EAAM,KAAK,CAAC,CAAA;AAC9E","file":"index.js","sourcesContent":["import { wildcardImplies } from '@anievo-id/rbac-evolution'\nimport type { Many, RbacSnapshot } from '@anievo-id/rbac-evolution'\n\nexport interface RbacCheckerOptions {\n /** Treat granted permissions as wildcard patterns (`articles.*`). Must match the server's `wildcard` setting. */\n wildcard?: boolean | undefined\n /** Roles that pass every permission check (like spatie's `Gate::before` super-admin pattern). */\n superAdminRoles?: readonly string[] | undefined\n /** The snapshot is still being fetched: `<Can>`, `<Cannot>` and `<HasRole>` render nothing. Default: `false`. */\n loading?: boolean | undefined\n}\n\n/** Synchronous checks against a {@link RbacSnapshot}. */\nexport interface RbacChecker {\n readonly roles: readonly string[]\n readonly permissions: readonly string[]\n /** `true` if the subject has one of the configured `superAdminRoles`. */\n readonly isSuperAdmin: boolean\n /** `true` while the snapshot is still being fetched (the `loading` option). Every check, `cannot` included, returns `false` meanwhile. */\n readonly loading: boolean\n can(permission: string): boolean\n cannot(permission: string): boolean\n /** `true` if ANY of the permissions is granted. `false` for an empty list. */\n canAny(...permissions: Many<string>[]): boolean\n /** `true` if ALL of the permissions are granted. `false` for an empty list (deny by default, like the Vue package). */\n canAll(...permissions: Many<string>[]): boolean\n /** `true` if the subject has the role — or ANY of the roles when given an array. */\n hasRole(roles: Many<string>): boolean\n hasAnyRole(...roles: Many<string>[]): boolean\n hasAllRoles(...roles: Many<string>[]): boolean\n}\n\nconst EMPTY: RbacSnapshot = { roles: [], permissions: [] }\n\nfunction flatten(items: readonly Many<string>[]): string[] {\n const out: string[] = []\n for (const item of items) {\n if (typeof item === 'string') out.push(item)\n else out.push(...item)\n }\n return out\n}\n\nfunction safeImplies(granted: string, required: string): boolean {\n try {\n return wildcardImplies(granted, required)\n } catch {\n return false\n }\n}\n\n/**\n * Builds a pure, synchronous checker for a snapshot (as returned by `rbac.for(user).snapshot()`).\n * Used by `RbacProvider`, but usable on its own too (no Solid needed).\n */\nexport function createRbacChecker(\n snapshot: RbacSnapshot | null | undefined,\n options: RbacCheckerOptions = {},\n): RbacChecker {\n const { roles, permissions } = snapshot ?? EMPTY\n const roleSet = new Set(roles)\n const permissionSet = new Set(permissions)\n // Names the server's `before` hook denies (`snapshot({ checkBefore })`). Absent in older snapshots: nothing denied.\n const deniedSet = new Set((snapshot?.denied ?? []).map((name) => name.trim()))\n const wildcard = options.wildcard ?? false\n // While loading, every check (`cannot` too) is false: neither a stale grant nor \"no access\" UI shows.\n const loading = options.loading ?? false\n const isSuperAdmin = !loading && (options.superAdminRoles ?? []).some((r) => roleSet.has(r))\n\n const can = (permission: string): boolean => {\n if (loading) return false\n // Like the server: `before` runs first, so its denial wins over super-admin and stored grants.\n if (deniedSet.has(permission.trim())) return false\n if (isSuperAdmin) return true\n const name = permission.trim()\n if (name === '') return false\n if (permissionSet.has(name)) return true\n return wildcard && permissions.some((granted) => safeImplies(granted, name))\n }\n const hasOne = (role: string): boolean => !loading && roleSet.has(role.trim())\n\n return {\n roles,\n permissions,\n isSuperAdmin,\n loading,\n can,\n cannot: (permission) => !loading && !can(permission),\n canAny: (...list) => flatten(list).some(can),\n canAll: (...list) => {\n const wanted = flatten(list)\n return wanted.length > 0 && wanted.every(can)\n },\n hasRole: (list) => flatten([list]).some(hasOne),\n hasAnyRole: (...list) => flatten(list).some(hasOne),\n hasAllRoles: (...list) => {\n const wanted = flatten(list)\n return wanted.length > 0 && wanted.every(hasOne)\n },\n }\n}\n","import type { Many, RbacSnapshot } from '@anievo-id/rbac-evolution'\nimport { type Accessor, createComponent, createContext, createMemo, useContext } from 'solid-js'\nimport { type RbacChecker, createRbacChecker } from './checker'\n\n/**\n * What a Solid component renders: `JSX.Element` in Solid 1.x, `Element` in 2.x (where `solid-js` no longer exports the\n * `JSX` namespace). Taken from `createComponent`, which both versions export, so one build types against either.\n */\nexport type SolidElement = ReturnType<typeof createComponent>\n\nconst RbacContext = createContext<Accessor<RbacChecker>>()\n\nexport interface RbacProviderProps {\n /** Roles & permissions of the current user, e.g. from `rbac.for(user).snapshot()` on the server. `null` = guest. */\n snapshot: RbacSnapshot | null | undefined\n /** Enable wildcard matching (`articles.*`). Must match the server's `wildcard` setting. Default: `false`. */\n wildcard?: boolean | undefined\n /** Roles that pass every `can` check (super admin). */\n superAdminRoles?: readonly string[] | undefined\n /**\n * The snapshot is still being fetched. `<Can>`, `<Cannot>`, `<HasRole>` and `<UnlessRole>` then render nothing\n * (not even `fallback`), so nothing flashes before the real permissions arrive. Default: `false`.\n */\n loading?: boolean | undefined\n children?: SolidElement\n}\n\ntype ProviderComponent = (props: { value: Accessor<RbacChecker>; children?: SolidElement }) => SolidElement\n\n/**\n * Provides a user's roles & permissions to `useCan()`, `<Can>`, ... Every prop is reactive: pass a signal or a\n * resource (`snapshot={data()}`) and everything below updates.\n *\n * UI only: this hides and shows elements. Always enforce authorization on the server.\n */\nexport function RbacProvider(props: RbacProviderProps): SolidElement {\n const checker = createMemo(() =>\n createRbacChecker(props.snapshot, {\n wildcard: props.wildcard ?? false,\n superAdminRoles: props.superAdminRoles,\n loading: props.loading ?? false,\n }),\n )\n // Solid 1.x: `Context.Provider`. Solid 2.x: the context itself is the provider component.\n const Provider = ((RbacContext as unknown as { Provider?: ProviderComponent }).Provider ??\n RbacContext) as unknown as ProviderComponent\n return createComponent(Provider, {\n value: checker,\n get children() {\n return props.children\n },\n })\n}\n\n/** The checker of the nearest `RbacProvider`, as an accessor. Throws when there is none. */\nexport function useRbac(): Accessor<RbacChecker> {\n let checker: Accessor<RbacChecker> | undefined\n try {\n checker = useContext(RbacContext)\n } catch {\n // Solid 2 throws itself for a context without a provider and default value.\n }\n if (!checker) throw new Error('useRbac() must be used inside <RbacProvider>.')\n return checker\n}\n\n/** `true` if the user has the permission. */\nexport function useCan(permission: string): Accessor<boolean> {\n const rbac = useRbac()\n return () => rbac().can(permission)\n}\n\n/** `true` if the user does NOT have the permission (`false` while loading). */\nexport function useCannot(permission: string): Accessor<boolean> {\n const rbac = useRbac()\n return () => rbac().cannot(permission)\n}\n\n/** `true` if the user has ANY of the permissions. */\nexport function useCanAny(...permissions: Many<string>[]): Accessor<boolean> {\n const rbac = useRbac()\n return () => rbac().canAny(...permissions)\n}\n\n/** `true` if the user has ALL of the permissions. */\nexport function useCanAll(...permissions: Many<string>[]): Accessor<boolean> {\n const rbac = useRbac()\n return () => rbac().canAll(...permissions)\n}\n\n/** `true` if the user has the role, or ANY of the roles when given an array. */\nexport function useHasRole(role: Many<string>): Accessor<boolean> {\n const rbac = useRbac()\n return () => rbac().hasRole(role)\n}\n\n/** `true` if the user has ANY of the roles. */\nexport function useHasAnyRole(...roles: Many<string>[]): Accessor<boolean> {\n const rbac = useRbac()\n return () => rbac().hasAnyRole(...roles)\n}\n\n/** `true` if the user has ALL of the roles. */\nexport function useHasAllRoles(...roles: Many<string>[]): Accessor<boolean> {\n const rbac = useRbac()\n return () => rbac().hasAllRoles(...roles)\n}\n","import { createMemo, untrack } from 'solid-js'\nimport type { RbacChecker } from './checker'\nimport { useRbac } from './context'\nimport type { SolidElement } from './context'\n\n/** Exactly one of `permission`, `anyOf` or `allOf`. */\nexport type PermissionCondition =\n | { permission: string; anyOf?: never; allOf?: never }\n | { permission?: never; anyOf: readonly string[]; allOf?: never }\n | { permission?: never; anyOf?: never; allOf: readonly string[] }\n\n/** Exactly one of `role`, `anyOf` or `allOf`. */\nexport type RoleCondition =\n | { role: string; anyOf?: never; allOf?: never }\n | { role?: never; anyOf: readonly string[]; allOf?: never }\n | { role?: never; anyOf?: never; allOf: readonly string[] }\n\ninterface GateProps {\n children?: SolidElement\n /** Rendered when the check fails. Default: nothing. */\n fallback?: SolidElement\n}\n\nexport type CanProps = PermissionCondition & GateProps\nexport type CannotProps = PermissionCondition & GateProps\nexport type HasRoleProps = RoleCondition & GateProps\nexport type UnlessRoleProps = RoleCondition & GateProps\n\ninterface LooseCondition {\n permission?: string | undefined\n role?: string | undefined\n anyOf?: readonly string[] | undefined\n allOf?: readonly string[] | undefined\n}\n\nconst hasCondition = (p: LooseCondition): boolean =>\n p.permission !== undefined || p.role !== undefined || p.anyOf !== undefined || p.allOf !== undefined\n\n// Conditions are ANDed; no condition at all is denied (deny by default, also for untyped JS callers).\nfunction checkPermissions(rbac: RbacChecker, p: LooseCondition): boolean {\n if (!hasCondition(p)) return false\n return (\n (p.permission === undefined || rbac.can(p.permission)) &&\n (p.anyOf === undefined || rbac.canAny(p.anyOf)) &&\n (p.allOf === undefined || rbac.canAll(p.allOf))\n )\n}\n\nfunction checkRoles(rbac: RbacChecker, p: LooseCondition): boolean {\n if (!hasCondition(p)) return false\n return (\n (p.role === undefined || rbac.hasRole(p.role)) &&\n (p.anyOf === undefined || rbac.hasAnyRole(p.anyOf)) &&\n (p.allOf === undefined || rbac.hasAllRoles(p.allOf))\n )\n}\n\n/**\n * Renders `children` when `pass` is true, else `fallback`, and nothing while loading. Like `<Show>`: the branch is\n * only rebuilt when the result changes, and the children are created untracked.\n */\nfunction gate(props: GateProps, pass: (rbac: RbacChecker) => boolean): SolidElement {\n const rbac = useRbac()\n const state = createMemo(() => (rbac().loading ? 'loading' : pass(rbac()) ? 'pass' : 'fail'))\n const view = (): SolidElement => {\n const s = state()\n if (s === 'loading') return null\n return untrack(() => (s === 'pass' ? props.children : props.fallback))\n }\n return view as unknown as SolidElement\n}\n\n/** Renders `children` if the user has the permission(s), like spatie's `@can` / `@canany`. */\nexport function Can(props: CanProps): SolidElement {\n return gate(props, (rbac) => checkPermissions(rbac, props))\n}\n\n/** Renders `children` if the user does NOT have the permission(s), like spatie's `@cannot`. */\nexport function Cannot(props: CannotProps): SolidElement {\n return gate(props, (rbac) => hasCondition(props) && !checkPermissions(rbac, props))\n}\n\n/** Renders `children` if the user has the role(s), like spatie's `@role` / `@hasanyrole` / `@hasallroles`. */\nexport function HasRole(props: HasRoleProps): SolidElement {\n return gate(props, (rbac) => checkRoles(rbac, props))\n}\n\n/**\n * Renders `children` if the user does NOT have the role(s), like spatie's `@unlessrole`.\n * `anyOf`: none of them. `allOf`: not all of them. Without a condition it renders `fallback` (deny by default).\n */\nexport function UnlessRole(props: UnlessRoleProps): SolidElement {\n return gate(props, (rbac) => hasCondition(props) && !checkRoles(rbac, props))\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,11 +1,73 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@anievo-id/rbac-evolution-solid",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.6.0",
|
|
4
|
+
"description": "Solid bindings for rbac-evolution: <Can>, <HasRole>, useCan() accessors. Solid 1.8+ and 2.x, SSR included.",
|
|
5
5
|
"license": "MIT",
|
|
6
|
+
"author": "AniEvo-ID (https://github.com/AniEvo-ID)",
|
|
7
|
+
"homepage": "https://creation.anievo.id/rbac-evolution/frameworks/solid",
|
|
8
|
+
"bugs": {
|
|
9
|
+
"url": "https://github.com/AniEvo-ID/rbac-evolution/issues"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
6
12
|
"repository": {
|
|
7
13
|
"type": "git",
|
|
8
14
|
"url": "git+https://github.com/AniEvo-ID/rbac-evolution.git",
|
|
9
15
|
"directory": "packages/solid"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"rbac",
|
|
19
|
+
"permissions",
|
|
20
|
+
"roles",
|
|
21
|
+
"authorization",
|
|
22
|
+
"solid",
|
|
23
|
+
"solidjs",
|
|
24
|
+
"solid-start",
|
|
25
|
+
"tanstack-start"
|
|
26
|
+
],
|
|
27
|
+
"sideEffects": false,
|
|
28
|
+
"main": "./dist/index.cjs",
|
|
29
|
+
"module": "./dist/index.js",
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"exports": {
|
|
32
|
+
".": {
|
|
33
|
+
"import": {
|
|
34
|
+
"types": "./dist/index.d.ts",
|
|
35
|
+
"default": "./dist/index.js"
|
|
36
|
+
},
|
|
37
|
+
"require": {
|
|
38
|
+
"types": "./dist/index.d.cts",
|
|
39
|
+
"default": "./dist/index.cjs"
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"./package.json": "./package.json"
|
|
43
|
+
},
|
|
44
|
+
"files": [
|
|
45
|
+
"dist"
|
|
46
|
+
],
|
|
47
|
+
"publishConfig": {
|
|
48
|
+
"access": "public"
|
|
49
|
+
},
|
|
50
|
+
"dependencies": {
|
|
51
|
+
"@anievo-id/rbac-evolution": "^1.6.0"
|
|
52
|
+
},
|
|
53
|
+
"peerDependencies": {
|
|
54
|
+
"solid-js": "^1.8.0 || ^2.0.0"
|
|
55
|
+
},
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"jsdom": "^25.0.0",
|
|
58
|
+
"solid-js": "^1.9.10",
|
|
59
|
+
"tsup": "^8.3.0",
|
|
60
|
+
"typescript": "^5.7.0",
|
|
61
|
+
"vitest": "^2.1.0"
|
|
62
|
+
},
|
|
63
|
+
"engines": {
|
|
64
|
+
"node": ">=20"
|
|
65
|
+
},
|
|
66
|
+
"scripts": {
|
|
67
|
+
"build": "tsup",
|
|
68
|
+
"dev": "tsup --watch",
|
|
69
|
+
"typecheck": "tsc --noEmit",
|
|
70
|
+
"test": "vitest run --passWithNoTests",
|
|
71
|
+
"clean": "rm -rf dist .turbo"
|
|
10
72
|
}
|
|
11
73
|
}
|