@l3dev/abac 0.1.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Leon Semmens
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 ADDED
@@ -0,0 +1,98 @@
1
+ # abac
2
+
3
+ ## Description
4
+
5
+ A type-safe ABAC library.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install @l3dev/abac
11
+ ```
12
+
13
+ ```bash
14
+ pnpm add @l3dev/abac
15
+ ```
16
+
17
+ ## Example
18
+
19
+ ```ts
20
+ import { ABAC } from "@l3dev/abac";
21
+
22
+ type User = {
23
+ id: string;
24
+ username: string;
25
+ admin: boolean;
26
+ };
27
+
28
+ type Organisation = {
29
+ id: string;
30
+ name: string;
31
+ public: boolean;
32
+ owner: User;
33
+ };
34
+
35
+ // Define ABAC resources
36
+
37
+ const OrganisationABAC = ABAC.createResource("organisation")
38
+ .withSubject<User>()
39
+ .withObject<Organisation>()
40
+ .setActions([
41
+ ABAC.createAction("create").withoutObject(),
42
+ ABAC.createAction("view.public"),
43
+ ABAC.createAction("join")
44
+ .setTitle("Allow Joining")
45
+ .setDescription("Allow users to join the organisation")
46
+ .setConfigurable(true),
47
+ ABAC.createAction("delete")
48
+ ]);
49
+
50
+ // Define ABAC policies
51
+
52
+ export const abac = ABAC.create([OrganisationABAC])
53
+ .addPolicy((policy) =>
54
+ policy
55
+ .setName("Admins")
56
+ .setMatch(({ subject }) => "admin" in subject && subject.admin)
57
+ .allowAll()
58
+ )
59
+ .addPolicy((policy) =>
60
+ policy
61
+ .setName("Default")
62
+ .allow("organisation.*", ABAC.Filter.ifgranted())
63
+ .allow("organisation:delete", (ctx) => ctx.subject.id === ctx.object.owner.id)
64
+ );
65
+
66
+ // Checking permissions
67
+
68
+ const adminUser: User = {
69
+ id: "1",
70
+ username: "Admin",
71
+ admin: true
72
+ };
73
+
74
+ const myUser: User = {
75
+ id: "2",
76
+ username: "User",
77
+ admin: false
78
+ };
79
+
80
+ const otherUser: User = {
81
+ id: "3",
82
+ username: "Other User",
83
+ admin: false
84
+ };
85
+
86
+ const myOrganisation: Organisation = {
87
+ id: "1",
88
+ name: "My Organisation",
89
+ public: false,
90
+ owner: myUser
91
+ };
92
+
93
+ abac.can(myUser, "organisation:delete").granted({ object: myOrganisation }); // true
94
+
95
+ abac.can(otherUser, "organisation:delete").granted({ object: myOrganisation }); // false
96
+
97
+ abac.can(adminUser, "organisation:delete").granted({ object: myOrganisation }); // true
98
+ ```
@@ -0,0 +1,72 @@
1
+ import { Action, type RuleCtx, type InferActionSubject } from "./action.js";
2
+ import { ActionGroup } from "./actionGroup.js";
3
+ import type { ResourceMap } from "./internal/collections.js";
4
+ import type { UnsetMarker } from "./internal/core.js";
5
+ import type { ActionFromPath, ActionPaths, FilterActionPaths } from "./internal/paths.js";
6
+ import { Permission, type PermissionOptions } from "./permission.js";
7
+ import { Policy } from "./policy.js";
8
+ import { PolicyFilters } from "./policyFilters.js";
9
+ import { Resource } from "./resource.js";
10
+ export type ConfigurableAction = {
11
+ path: string;
12
+ action: Action<any, any>;
13
+ own: boolean;
14
+ };
15
+ export declare class ABAC<TResources extends ResourceMap> {
16
+ private _resourceMap;
17
+ private _policies;
18
+ constructor(resourceMap: TResources);
19
+ $resources: TResources;
20
+ $actions: ActionPaths<TResources>;
21
+ $configurableActions: FilterActionPaths<TResources, any, true>;
22
+ $filterActions: <TCtx>() => FilterActionPaths<TResources, TCtx>;
23
+ getConfigurableActions(): ConfigurableAction[];
24
+ can<TActionPath extends ActionPaths<TResources>>(subject: InferActionSubject<ActionFromPath<TResources, TActionPath>>, path: TActionPath, options?: PermissionOptions<TActionPath, ActionFromPath<TResources, TActionPath>>): Omit<Permission<TActionPath, ActionFromPath<TResources, TActionPath>>, "granted" | "filter"> & (import("./internal/core.js").IsOnlyEmptyObject<Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject">> extends true ? {
25
+ granted(): boolean;
26
+ test(): {
27
+ granted: boolean;
28
+ specitivity: import("./policy.js").Specitivity;
29
+ policy: Policy<any> | null;
30
+ rule: import("./policy.js").Rule | null;
31
+ };
32
+ } : {
33
+ granted(ctx: import("./internal/core.js").Expand<Partial<Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject"> extends infer T ? T extends Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject"> ? T extends import("./internal/core.js").EmptyObject ? Omit<Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject">, "object"> : Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject"> : never : never> & Pick<Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject"> extends infer T_1 ? T_1 extends Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject"> ? T_1 extends import("./internal/core.js").EmptyObject ? Omit<Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject">, "object"> : Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject"> : never : never, ((Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject"> extends infer T_5 ? T_5 extends Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject"> ? T_5 extends import("./internal/core.js").EmptyObject ? Omit<Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject">, "object"> : Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject"> : never : never) extends infer T_2 ? { [K in keyof T_2]: (Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject"> extends infer T_3 ? T_3 extends Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject"> ? T_3 extends import("./internal/core.js").EmptyObject ? Omit<Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject">, "object"> : Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject"> : never : never)[K] extends Exclude<(Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject"> extends infer T_4 ? T_4 extends Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject"> ? T_4 extends import("./internal/core.js").EmptyObject ? Omit<Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject">, "object"> : Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject"> : never : never)[K], undefined> ? K : never; } : never)[keyof (Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject"> extends infer T_6 ? T_6 extends Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject"> ? T_6 extends import("./internal/core.js").EmptyObject ? Omit<Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject">, "object"> : Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject"> : never : never)]>>): boolean;
34
+ test(ctx: import("./internal/core.js").Expand<Partial<Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject"> extends infer T ? T extends Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject"> ? T extends import("./internal/core.js").EmptyObject ? Omit<Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject">, "object"> : Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject"> : never : never> & Pick<Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject"> extends infer T_1 ? T_1 extends Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject"> ? T_1 extends import("./internal/core.js").EmptyObject ? Omit<Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject">, "object"> : Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject"> : never : never, ((Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject"> extends infer T_5 ? T_5 extends Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject"> ? T_5 extends import("./internal/core.js").EmptyObject ? Omit<Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject">, "object"> : Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject"> : never : never) extends infer T_2 ? { [K in keyof T_2]: (Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject"> extends infer T_3 ? T_3 extends Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject"> ? T_3 extends import("./internal/core.js").EmptyObject ? Omit<Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject">, "object"> : Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject"> : never : never)[K] extends Exclude<(Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject"> extends infer T_4 ? T_4 extends Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject"> ? T_4 extends import("./internal/core.js").EmptyObject ? Omit<Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject">, "object"> : Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject"> : never : never)[K], undefined> ? K : never; } : never)[keyof (Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject"> extends infer T_6 ? T_6 extends Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject"> ? T_6 extends import("./internal/core.js").EmptyObject ? Omit<Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject">, "object"> : Omit<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "subject"> : never : never)]>>): {
35
+ granted: boolean;
36
+ specitivity: import("./policy.js").Specitivity;
37
+ policy: Policy<any> | null;
38
+ rule: import("./policy.js").Rule | null;
39
+ };
40
+ }) & (import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>> extends import("./internal/core.js").EmptyObject ? {} : import("./action.js").InferActionAdditionalContext<ActionFromPath<TResources, TActionPath>> extends {} ? {
41
+ filter(objects: Exclude<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>["object"], void | undefined>[]): Exclude<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>["object"], void | undefined>[];
42
+ } : {
43
+ filter(objects: Exclude<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>["object"], void | undefined>[], ctx: import("./internal/core.js").Expand<Partial<import("./action.js").InferActionAdditionalContext<ActionFromPath<TResources, TActionPath>>> & Pick<import("./action.js").InferActionAdditionalContext<ActionFromPath<TResources, TActionPath>>, (import("./action.js").InferActionAdditionalContext<ActionFromPath<TResources, TActionPath>> extends infer T ? { [K in keyof T]: import("./action.js").InferActionAdditionalContext<ActionFromPath<TResources, TActionPath>>[K] extends Exclude<import("./action.js").InferActionAdditionalContext<ActionFromPath<TResources, TActionPath>>[K], undefined> ? K : never; } : never)[Exclude<keyof import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>, "object" | "subject">]>>): Exclude<import("./action.js").InferActionRuleCtx<ActionFromPath<TResources, TActionPath>>["object"], void | undefined>[];
44
+ });
45
+ addPolicy(build: (policy: Policy<TResources>) => Policy<TResources>): this;
46
+ static create<TResource extends Resource<any, any>>(resources: TResource[]): ABAC<import("./internal/core.js").UnionToIntersection<TResource extends Resource<infer TName extends string, any> ? { [name in TName]: TResource; } : never, ResourceMap>>;
47
+ static createResource<TName extends string>(name: TName): Resource<TName, {
48
+ ctx: {
49
+ subject: UnsetMarker;
50
+ object: UnsetMarker;
51
+ };
52
+ actions: UnsetMarker;
53
+ subResources: UnsetMarker;
54
+ ownable: false;
55
+ }>;
56
+ static createAction<TName extends string, TCtx extends RuleCtx = {
57
+ subject: UnsetMarker;
58
+ object: UnsetMarker;
59
+ }>(name: TName): Action<TName, {
60
+ ctx: TCtx;
61
+ configurable: false;
62
+ }>;
63
+ static createActionGroup<TName extends string, TCtx extends RuleCtx = {
64
+ subject: UnsetMarker;
65
+ object: UnsetMarker;
66
+ }>(name: TName): ActionGroup<TName, {
67
+ ctx: TCtx;
68
+ actions: UnsetMarker;
69
+ }>;
70
+ static createPolicy<TResources extends ResourceMap>(): Policy<TResources>;
71
+ static Filter: typeof PolicyFilters;
72
+ }
package/build/abac.js ADDED
@@ -0,0 +1,65 @@
1
+ import { Action } from "./action.js";
2
+ import { ActionGroup } from "./actionGroup.js";
3
+ import { ActionVisitor } from "./actionVisitor.js";
4
+ import { Permission } from "./permission.js";
5
+ import { Policy } from "./policy.js";
6
+ import { PolicyFilters } from "./policyFilters.js";
7
+ import { Resource } from "./resource.js";
8
+ export class ABAC {
9
+ _resourceMap;
10
+ _policies = [];
11
+ constructor(resourceMap) {
12
+ this._resourceMap = resourceMap;
13
+ }
14
+ getConfigurableActions() {
15
+ const actions = [];
16
+ const visitor = new ActionVisitor(this._resourceMap);
17
+ visitor.traverse(({ action, resource, path }) => {
18
+ if (!action.configurable) {
19
+ return;
20
+ }
21
+ actions.push({
22
+ path,
23
+ action,
24
+ own: resource.ownable && resource.ownConfigurable && !action.noObject
25
+ });
26
+ });
27
+ return actions;
28
+ }
29
+ can(subject, path, options) {
30
+ const policies = this._policies.filter((policy) => policy.match({ subject }));
31
+ const visitor = new ActionVisitor(this._resourceMap);
32
+ const actionPath = path.endsWith(".own") ? path.slice(0, -4) : path;
33
+ const result = visitor.findByPath(actionPath);
34
+ if (!result) {
35
+ throw new Error(`No action '${actionPath}' found in ABAC resource map`);
36
+ }
37
+ return Permission.create(subject, path, result.resource, result.action, policies, options);
38
+ }
39
+ addPolicy(build) {
40
+ const policy = build(ABAC.createPolicy());
41
+ this._policies.push(policy);
42
+ return this;
43
+ }
44
+ static create(resources) {
45
+ const resourceMap = resources.reduce((map, resource) => ({
46
+ ...map,
47
+ [resource.name]: resource
48
+ }), {});
49
+ return new ABAC(resourceMap);
50
+ }
51
+ static createResource(name) {
52
+ return new Resource(name);
53
+ }
54
+ static createAction(name) {
55
+ return new Action(name);
56
+ }
57
+ static createActionGroup(name) {
58
+ return new ActionGroup(name);
59
+ }
60
+ static createPolicy() {
61
+ return new Policy();
62
+ }
63
+ static Filter = PolicyFilters;
64
+ }
65
+ //# sourceMappingURL=abac.js.map
@@ -0,0 +1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AAM,CAAC,YAAY,CAGxB,IAAW;QACZ,OAAO,IAAI,MAAM,CAA4C,IAAI,CAAC,CAAC;IACpE,CAAC;IAEM,MAAM,CAAC,iBAAiB,CAG7B,IAAW;QACZ,OAAO,IAAI,WAAW,CAMpB,IAAI,CAAC,CAAC;IACT,CAAC;IAEM,MAAM,CAAC,YAAY;QACzB,OAAO,IAAI,MAAM,EAAc,CAAC;IACjC,CAAC;IAEM,MAAM,CAAC,MAAM,GAAG,aAAa,CAAC","sourcesContent":["import { Action, type RuleCtx, type InferActionSubject } from \"./action.js\";\nimport { ActionGroup } from \"./actionGroup.js\";\nimport { ActionVisitor } from \"./actionVisitor.js\";\nimport type { ResourceMap, UnionToResourceMap } from \"./internal/collections.js\";\nimport type { UnsetMarker } from \"./internal/core.js\";\nimport type { ActionFromPath, ActionPaths, FilterActionPaths } from \"./internal/paths.js\";\nimport { Permission, type PermissionOptions } from \"./permission.js\";\nimport { Policy } from \"./policy.js\";\nimport { PolicyFilters } from \"./policyFilters.js\";\nimport { Resource } from \"./resource.js\";\n\nexport type ConfigurableAction = {\n\tpath: string;\n\taction: Action<any, any>;\n\town: boolean;\n};\n\nexport class ABAC<TResources extends ResourceMap> {\n\tprivate _resourceMap: TResources;\n\tprivate _policies: Policy<TResources>[] = [];\n\n\tconstructor(resourceMap: TResources) {\n\t\tthis._resourceMap = resourceMap;\n\t}\n\n\tdeclare public $resources: TResources;\n\tdeclare public $actions: ActionPaths<TResources>;\n\tdeclare public $configurableActions: FilterActionPaths<TResources, any, true>;\n\tdeclare public $filterActions: <TCtx>() => FilterActionPaths<TResources, TCtx>;\n\n\tpublic getConfigurableActions() {\n\t\tconst actions: ConfigurableAction[] = [];\n\n\t\tconst visitor = new ActionVisitor(this._resourceMap);\n\t\tvisitor.traverse(({ action, resource, path }) => {\n\t\t\tif (!action.configurable) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tactions.push({\n\t\t\t\tpath,\n\t\t\t\taction,\n\t\t\t\town: resource.ownable && resource.ownConfigurable && !action.noObject\n\t\t\t});\n\t\t});\n\n\t\treturn actions;\n\t}\n\n\tpublic can<TActionPath extends ActionPaths<TResources>>(\n\t\tsubject: InferActionSubject<ActionFromPath<TResources, TActionPath>>,\n\t\tpath: TActionPath,\n\t\toptions?: PermissionOptions<TActionPath, ActionFromPath<TResources, TActionPath>>\n\t) {\n\t\tconst policies = this._policies.filter((policy) => policy.match({ subject }));\n\n\t\tconst visitor = new ActionVisitor(this._resourceMap);\n\n\t\tconst actionPath = path.endsWith(\".own\") ? path.slice(0, -4) : path;\n\t\tconst result = visitor.findByPath(actionPath as TActionPath);\n\t\tif (!result) {\n\t\t\tthrow new Error(`No action '${actionPath}' found in ABAC resource map`);\n\t\t}\n\n\t\treturn Permission.create(subject, path, result.resource, result.action, policies, options);\n\t}\n\n\tpublic addPolicy(build: (policy: Policy<TResources>) => Policy<TResources>) {\n\t\tconst policy = build(ABAC.createPolicy());\n\t\tthis._policies.push(policy);\n\t\treturn this;\n\t}\n\n\tpublic static create<TResource extends Resource<any, any>>(resources: TResource[]) {\n\t\tconst resourceMap = resources.reduce(\n\t\t\t(map, resource) => ({\n\t\t\t\t...map,\n\t\t\t\t[resource.name]: resource\n\t\t\t}),\n\t\t\t{} as UnionToResourceMap<TResource>\n\t\t);\n\n\t\treturn new ABAC(resourceMap);\n\t}\n\n\tpublic static createResource<TName extends string>(name: TName) {\n\t\treturn new Resource<\n\t\t\tTName,\n\t\t\t{\n\t\t\t\tctx: {\n\t\t\t\t\tsubject: UnsetMarker;\n\t\t\t\t\tobject: UnsetMarker;\n\t\t\t\t};\n\t\t\t\tactions: UnsetMarker;\n\t\t\t\tsubResources: UnsetMarker;\n\t\t\t\townable: false;\n\t\t\t}\n\t\t>(name);\n\t}\n\n\tpublic static createAction<\n\t\tTName extends string,\n\t\tTCtx extends RuleCtx = { subject: UnsetMarker; object: UnsetMarker }\n\t>(name: TName) {\n\t\treturn new Action<TName, { ctx: TCtx; configurable: false }>(name);\n\t}\n\n\tpublic static createActionGroup<\n\t\tTName extends string,\n\t\tTCtx extends RuleCtx = { subject: UnsetMarker; object: UnsetMarker }\n\t>(name: TName) {\n\t\treturn new ActionGroup<\n\t\t\tTName,\n\t\t\t{\n\t\t\t\tctx: TCtx;\n\t\t\t\tactions: UnsetMarker;\n\t\t\t}\n\t\t>(name);\n\t}\n\n\tpublic static createPolicy<TResources extends ResourceMap>() {\n\t\treturn new Policy<TResources>();\n\t}\n\n\tpublic static Filter = PolicyFilters;\n}\n"]}
@@ -0,0 +1,61 @@
1
+ import type { MakeOptional } from "./internal/core.js";
2
+ export type RuleCtx = {
3
+ subject: any;
4
+ object: any;
5
+ };
6
+ export type ActionContext = {
7
+ ctx: RuleCtx;
8
+ configurable: boolean;
9
+ };
10
+ export type InferActionRuleCtx<TAction> = TAction extends Action<any, infer TContext> ? TContext["ctx"] : never;
11
+ export type InferActionSubject<TAction> = Pick<InferActionRuleCtx<TAction>, "subject">["subject"];
12
+ export type InferActionObject<TAction> = Exclude<Pick<InferActionRuleCtx<TAction>, "object">["object"], void | undefined>;
13
+ export type InferActionAdditionalContext<TAction> = Omit<InferActionRuleCtx<TAction>, "subject" | "object">;
14
+ export declare class Action<TName extends string, TContext extends ActionContext> {
15
+ private _name;
16
+ private _title;
17
+ private _description;
18
+ private _configurable;
19
+ private _noObject;
20
+ constructor(name: TName);
21
+ get name(): TName;
22
+ get title(): string | null;
23
+ setTitle(title: string): this;
24
+ get description(): string | null;
25
+ setDescription(description: string): this;
26
+ get configurable(): boolean;
27
+ setConfigurable<TConfigurable extends boolean>(configurable: TConfigurable): Action<TName, {
28
+ ctx: TContext["ctx"];
29
+ configurable: TConfigurable;
30
+ }>;
31
+ withSubject<TOverrideSubject>(): Action<TName, {
32
+ ctx: {
33
+ subject: TOverrideSubject;
34
+ object: TContext["ctx"]["object"];
35
+ };
36
+ configurable: TContext["configurable"];
37
+ }>;
38
+ withObject<TOverrideObject>(): Action<TName, {
39
+ ctx: {
40
+ subject: TContext["ctx"]["subject"];
41
+ object: TOverrideObject;
42
+ };
43
+ configurable: TContext["configurable"];
44
+ }>;
45
+ get noObject(): boolean;
46
+ withoutObject(): Action<TName, {
47
+ ctx: {
48
+ subject: TContext["ctx"]["subject"];
49
+ object: void;
50
+ };
51
+ configurable: TContext["configurable"];
52
+ }>;
53
+ optionalObject(): Action<TName, {
54
+ ctx: {
55
+ subject: TContext["ctx"]["subject"];
56
+ object: MakeOptional<TContext["ctx"]["object"]>;
57
+ };
58
+ configurable: TContext["configurable"];
59
+ }>;
60
+ withAdditionalContext<TAdditionalContext extends object>(): Action<TName, TContext & TAdditionalContext>;
61
+ }
@@ -0,0 +1,54 @@
1
+ export class Action {
2
+ _name;
3
+ _title = null;
4
+ _description = null;
5
+ _configurable = false;
6
+ _noObject = false;
7
+ constructor(name) {
8
+ this._name = name;
9
+ }
10
+ get name() {
11
+ return this._name;
12
+ }
13
+ get title() {
14
+ return this._title;
15
+ }
16
+ setTitle(title) {
17
+ this._title = title;
18
+ return this;
19
+ }
20
+ get description() {
21
+ return this._description;
22
+ }
23
+ setDescription(description) {
24
+ this._description = description;
25
+ return this;
26
+ }
27
+ get configurable() {
28
+ return this._configurable;
29
+ }
30
+ setConfigurable(configurable) {
31
+ this._configurable = configurable;
32
+ return this;
33
+ }
34
+ withSubject() {
35
+ return this;
36
+ }
37
+ withObject() {
38
+ return this;
39
+ }
40
+ get noObject() {
41
+ return this._noObject;
42
+ }
43
+ withoutObject() {
44
+ this._noObject = true;
45
+ return this.withObject();
46
+ }
47
+ optionalObject() {
48
+ return this.withObject();
49
+ }
50
+ withAdditionalContext() {
51
+ return this;
52
+ }
53
+ }
54
+ //# sourceMappingURL=action.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"action.js","sourceRoot":"","sources":["../src/action.ts"],"names":[],"mappings":"AA2BA,MAAM,OAAO,MAAM;IACV,KAAK,CAAQ;IACb,MAAM,GAAkB,IAAI,CAAC;IAC7B,YAAY,GAAkB,IAAI,CAAC;IACnC,aAAa,GAAY,KAAK,CAAC;IAC/B,SAAS,GAAY,KAAK,CAAC;IAEnC,YAAY,IAAW;QACtB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;IACnB,CAAC;IAED,IAAW,IAAI;QACd,OAAO,IAAI,CAAC,KAAK,CAAC;IACnB,CAAC;IAED,IAAW,KAAK;QACf,OAAO,IAAI,CAAC,MAAM,CAAC;IACpB,CAAC;IAEM,QAAQ,CAAC,KAAa;QAC5B,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,OAAO,IAAI,CAAC;IACb,CAAC;IAED,IAAW,WAAW;QACrB,OAAO,IAAI,CAAC,YAAY,CAAC;IAC1B,CAAC;IAEM,cAAc,CAAC,WAAmB;QACxC,IAAI,CAAC,YAAY,GAAG,WAAW,CAAC;QAChC,OAAO,IAAI,CAAC;IACb,CAAC;IAED,IAAW,YAAY;QACtB,OAAO,IAAI,CAAC,aAAa,CAAC;IAC3B,CAAC;IAEM,eAAe,CAAgC,YAA2B;QAChF,IAAI,CAAC,aAAa,GAAG,YAAY,CAAC;QAClC,OAAO,IAMN,CAAC;IACH,CAAC;IAEM,WAAW;QACjB,OAAO,IASN,CAAC;IACH,CAAC;IAEM,UAAU;QAChB,OAAO,IASN,CAAC;IACH,CAAC;IAED,IAAW,QAAQ;QAClB,OAAO,IAAI,CAAC,SAAS,CAAC;IACvB,CAAC;IAEM,aAAa;QACnB,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;QACtB,OAAO,IAAI,CAAC,UAAU,EAAQ,CAAC;IAChC,CAAC;IAEM,cAAc;QACpB,OAAO,IAAI,CAAC,UAAU,EAA2C,CAAC;IACnE,CAAC;IAEM,qBAAqB;QAC3B,OAAO,IAA+D,CAAC;IACxE,CAAC;CACD","sourcesContent":["import type { MakeOptional } from \"./internal/core.js\";\n\nexport type RuleCtx = {\n\tsubject: any;\n\tobject: any;\n};\n\nexport type ActionContext = {\n\tctx: RuleCtx;\n\tconfigurable: boolean;\n};\n\nexport type InferActionRuleCtx<TAction> =\n\tTAction extends Action<any, infer TContext> ? TContext[\"ctx\"] : never;\n\nexport type InferActionSubject<TAction> = Pick<InferActionRuleCtx<TAction>, \"subject\">[\"subject\"];\n\nexport type InferActionObject<TAction> = Exclude<\n\tPick<InferActionRuleCtx<TAction>, \"object\">[\"object\"],\n\tvoid | undefined\n>;\n\nexport type InferActionAdditionalContext<TAction> = Omit<\n\tInferActionRuleCtx<TAction>,\n\t\"subject\" | \"object\"\n>;\n\nexport class Action<TName extends string, TContext extends ActionContext> {\n\tprivate _name: TName;\n\tprivate _title: string | null = null;\n\tprivate _description: string | null = null;\n\tprivate _configurable: boolean = false;\n\tprivate _noObject: boolean = false;\n\n\tconstructor(name: TName) {\n\t\tthis._name = name;\n\t}\n\n\tpublic get name(): TName {\n\t\treturn this._name;\n\t}\n\n\tpublic get title(): string | null {\n\t\treturn this._title;\n\t}\n\n\tpublic setTitle(title: string) {\n\t\tthis._title = title;\n\t\treturn this;\n\t}\n\n\tpublic get description(): string | null {\n\t\treturn this._description;\n\t}\n\n\tpublic setDescription(description: string) {\n\t\tthis._description = description;\n\t\treturn this;\n\t}\n\n\tpublic get configurable(): boolean {\n\t\treturn this._configurable;\n\t}\n\n\tpublic setConfigurable<TConfigurable extends boolean>(configurable: TConfigurable) {\n\t\tthis._configurable = configurable;\n\t\treturn this as Action<\n\t\t\tTName,\n\t\t\t{\n\t\t\t\tctx: TContext[\"ctx\"];\n\t\t\t\tconfigurable: TConfigurable;\n\t\t\t}\n\t\t>;\n\t}\n\n\tpublic withSubject<TOverrideSubject>() {\n\t\treturn this as Action<\n\t\t\tTName,\n\t\t\t{\n\t\t\t\tctx: {\n\t\t\t\t\tsubject: TOverrideSubject;\n\t\t\t\t\tobject: TContext[\"ctx\"][\"object\"];\n\t\t\t\t};\n\t\t\t\tconfigurable: TContext[\"configurable\"];\n\t\t\t}\n\t\t>;\n\t}\n\n\tpublic withObject<TOverrideObject>() {\n\t\treturn this as Action<\n\t\t\tTName,\n\t\t\t{\n\t\t\t\tctx: {\n\t\t\t\t\tsubject: TContext[\"ctx\"][\"subject\"];\n\t\t\t\t\tobject: TOverrideObject;\n\t\t\t\t};\n\t\t\t\tconfigurable: TContext[\"configurable\"];\n\t\t\t}\n\t\t>;\n\t}\n\n\tpublic get noObject() {\n\t\treturn this._noObject;\n\t}\n\n\tpublic withoutObject() {\n\t\tthis._noObject = true;\n\t\treturn this.withObject<void>();\n\t}\n\n\tpublic optionalObject() {\n\t\treturn this.withObject<MakeOptional<TContext[\"ctx\"][\"object\"]>>();\n\t}\n\n\tpublic withAdditionalContext<TAdditionalContext extends object>() {\n\t\treturn this as unknown as Action<TName, TContext & TAdditionalContext>;\n\t}\n}\n"]}
@@ -0,0 +1,44 @@
1
+ import { type Action, type RuleCtx } from "./action.js";
2
+ import type { ActionMap, ToActionMap } from "./internal/collections.js";
3
+ import type { Expand, UnsetMarker } from "./internal/core.js";
4
+ export type ActionGroupContext = {
5
+ ctx: RuleCtx;
6
+ actions: ActionMap | UnsetMarker;
7
+ };
8
+ export type AnyActionGroupContext = {
9
+ ctx: RuleCtx;
10
+ actions: ActionMap;
11
+ };
12
+ export declare class ActionGroup<TName extends string, TContext extends ActionGroupContext = AnyActionGroupContext> {
13
+ private _name;
14
+ private _title;
15
+ private _description;
16
+ private _actions;
17
+ constructor(name: TName);
18
+ get name(): TName;
19
+ get title(): string | null;
20
+ setTitle(title: string): this;
21
+ get description(): string | null;
22
+ setDescription(description: string): this;
23
+ withSubject<TSubject>(): ActionGroup<TName, {
24
+ ctx: Expand<Omit<TContext["ctx"], "subject"> & {
25
+ subject: TSubject;
26
+ }>;
27
+ actions: TContext["actions"];
28
+ }>;
29
+ withObject<TObject>(): ActionGroup<TName, {
30
+ ctx: Expand<Omit<TContext["ctx"], "object"> & {
31
+ object: TObject;
32
+ }>;
33
+ actions: TContext["actions"];
34
+ }>;
35
+ withAdditionalContext<TAdditionalContext extends object>(): ActionGroup<TName, {
36
+ ctx: TContext["ctx"] & TAdditionalContext;
37
+ actions: TContext["actions"];
38
+ }>;
39
+ get actions(): TContext["actions"];
40
+ setActions<TActions extends Action<any, any> | ActionGroup<any, any>>(actions: TActions[]): ActionGroup<TName, {
41
+ ctx: TContext["ctx"];
42
+ actions: Expand<ToActionMap<TActions>>;
43
+ }>;
44
+ }
@@ -0,0 +1,47 @@
1
+ import {} from "./action.js";
2
+ export class ActionGroup {
3
+ _name;
4
+ _title = null;
5
+ _description = null;
6
+ _actions = {};
7
+ constructor(name) {
8
+ this._name = name;
9
+ }
10
+ get name() {
11
+ return this._name;
12
+ }
13
+ get title() {
14
+ return this._title;
15
+ }
16
+ setTitle(title) {
17
+ this._title = title;
18
+ return this;
19
+ }
20
+ get description() {
21
+ return this._description;
22
+ }
23
+ setDescription(description) {
24
+ this._description = description;
25
+ return this;
26
+ }
27
+ withSubject() {
28
+ return this;
29
+ }
30
+ withObject() {
31
+ return this;
32
+ }
33
+ withAdditionalContext() {
34
+ return this;
35
+ }
36
+ get actions() {
37
+ return this._actions;
38
+ }
39
+ setActions(actions) {
40
+ this._actions = actions.reduce((acc, action) => ({
41
+ ...acc,
42
+ [action.name]: action
43
+ }), {});
44
+ return this;
45
+ }
46
+ }
47
+ //# sourceMappingURL=actionGroup.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"actionGroup.js","sourceRoot":"","sources":["../src/actionGroup.ts"],"names":[],"mappings":"AAAA,OAAO,EAA6B,MAAM,aAAa,CAAC;AAcxD,MAAM,OAAO,WAAW;IAIf,KAAK,CAAQ;IACb,MAAM,GAAkB,IAAI,CAAC;IAC7B,YAAY,GAAkB,IAAI,CAAC;IACnC,QAAQ,GAAwB,EAAyB,CAAC;IAElE,YAAY,IAAW;QACtB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;IACnB,CAAC;IAED,IAAW,IAAI;QACd,OAAO,IAAI,CAAC,KAAK,CAAC;IACnB,CAAC;IAED,IAAW,KAAK;QACf,OAAO,IAAI,CAAC,MAAM,CAAC;IACpB,CAAC;IAEM,QAAQ,CAAC,KAAa;QAC5B,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,OAAO,IAAI,CAAC;IACb,CAAC;IAED,IAAW,WAAW;QACrB,OAAO,IAAI,CAAC,YAAY,CAAC;IAC1B,CAAC;IAEM,cAAc,CAAC,WAAmB;QACxC,IAAI,CAAC,YAAY,GAAG,WAAW,CAAC;QAChC,OAAO,IAAI,CAAC;IACb,CAAC;IAEM,WAAW;QACjB,OAAO,IAUN,CAAC;IACH,CAAC;IAEM,UAAU;QAChB,OAAO,IAUN,CAAC;IACH,CAAC;IAEM,qBAAqB;QAC3B,OAAO,IAMN,CAAC;IACH,CAAC;IAED,IAAI,OAAO;QACV,OAAO,IAAI,CAAC,QAAQ,CAAC;IACtB,CAAC;IAEM,UAAU,CAChB,OAAmB;QAEnB,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,MAAM,CAC7B,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE,CAAC,CAAC;YACjB,GAAG,GAAG;YACN,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,MAAM;SACrB,CAAC,EACF,EAAE,CACF,CAAC;QACF,OAAO,IAMN,CAAC;IACH,CAAC;CACD","sourcesContent":["import { type Action, type RuleCtx } from \"./action.js\";\nimport type { ActionMap, ToActionMap } from \"./internal/collections.js\";\nimport type { Expand, UnsetMarker } from \"./internal/core.js\";\n\nexport type ActionGroupContext = {\n\tctx: RuleCtx;\n\tactions: ActionMap | UnsetMarker;\n};\n\nexport type AnyActionGroupContext = {\n\tctx: RuleCtx;\n\tactions: ActionMap;\n};\n\nexport class ActionGroup<\n\tTName extends string,\n\tTContext extends ActionGroupContext = AnyActionGroupContext\n> {\n\tprivate _name: TName;\n\tprivate _title: string | null = null;\n\tprivate _description: string | null = null;\n\tprivate _actions: TContext[\"actions\"] = {} as TContext[\"actions\"];\n\n\tconstructor(name: TName) {\n\t\tthis._name = name;\n\t}\n\n\tpublic get name(): TName {\n\t\treturn this._name;\n\t}\n\n\tpublic get title(): string | null {\n\t\treturn this._title;\n\t}\n\n\tpublic setTitle(title: string) {\n\t\tthis._title = title;\n\t\treturn this;\n\t}\n\n\tpublic get description(): string | null {\n\t\treturn this._description;\n\t}\n\n\tpublic setDescription(description: string) {\n\t\tthis._description = description;\n\t\treturn this;\n\t}\n\n\tpublic withSubject<TSubject>() {\n\t\treturn this as unknown as ActionGroup<\n\t\t\tTName,\n\t\t\t{\n\t\t\t\tctx: Expand<\n\t\t\t\t\tOmit<TContext[\"ctx\"], \"subject\"> & {\n\t\t\t\t\t\tsubject: TSubject;\n\t\t\t\t\t}\n\t\t\t\t>;\n\t\t\t\tactions: TContext[\"actions\"];\n\t\t\t}\n\t\t>;\n\t}\n\n\tpublic withObject<TObject>() {\n\t\treturn this as unknown as ActionGroup<\n\t\t\tTName,\n\t\t\t{\n\t\t\t\tctx: Expand<\n\t\t\t\t\tOmit<TContext[\"ctx\"], \"object\"> & {\n\t\t\t\t\t\tobject: TObject;\n\t\t\t\t\t}\n\t\t\t\t>;\n\t\t\t\tactions: TContext[\"actions\"];\n\t\t\t}\n\t\t>;\n\t}\n\n\tpublic withAdditionalContext<TAdditionalContext extends object>() {\n\t\treturn this as unknown as ActionGroup<\n\t\t\tTName,\n\t\t\t{\n\t\t\t\tctx: TContext[\"ctx\"] & TAdditionalContext;\n\t\t\t\tactions: TContext[\"actions\"];\n\t\t\t}\n\t\t>;\n\t}\n\n\tget actions(): TContext[\"actions\"] {\n\t\treturn this._actions;\n\t}\n\n\tpublic setActions<TActions extends Action<any, any> | ActionGroup<any, any>>(\n\t\tactions: TActions[]\n\t) {\n\t\tthis._actions = actions.reduce(\n\t\t\t(acc, action) => ({\n\t\t\t\t...acc,\n\t\t\t\t[action.name]: action\n\t\t\t}),\n\t\t\t{}\n\t\t);\n\t\treturn this as unknown as ActionGroup<\n\t\t\tTName,\n\t\t\t{\n\t\t\t\tctx: TContext[\"ctx\"];\n\t\t\t\tactions: Expand<ToActionMap<TActions>>;\n\t\t\t}\n\t\t>;\n\t}\n}\n"]}
@@ -0,0 +1,29 @@
1
+ import { Action } from "./action.js";
2
+ import type { ResourceMap } from "./internal/collections.js";
3
+ import type { ActionFromPath, ActionPaths } from "./internal/paths.js";
4
+ import type { Resource } from "./resource.js";
5
+ type VisitContext = {
6
+ action: Action<any, any>;
7
+ resource: Resource<any, any>;
8
+ path: string;
9
+ };
10
+ type Predicate = (ctx: VisitContext) => boolean;
11
+ type Visit = (ctx: VisitContext) => void;
12
+ export declare class ActionVisitor<TResources extends ResourceMap> {
13
+ private resources;
14
+ private callback;
15
+ private lastVisited;
16
+ private stop;
17
+ constructor(resources: TResources);
18
+ find(predicate: Predicate): VisitContext | null;
19
+ findByPath<TActionPath extends ActionPaths<TResources>>(path: TActionPath): {
20
+ resource: Resource<any, any>;
21
+ action: ActionFromPath<TResources, TActionPath>;
22
+ } | null;
23
+ traverse(visit: Visit): void;
24
+ private start;
25
+ private traverseResources;
26
+ private traverseResource;
27
+ private traverseActions;
28
+ }
29
+ export {};
@@ -0,0 +1,64 @@
1
+ import { Action } from "./action.js";
2
+ export class ActionVisitor {
3
+ resources;
4
+ callback = () => { };
5
+ lastVisited = null;
6
+ stop = false;
7
+ constructor(resources) {
8
+ this.resources = resources;
9
+ }
10
+ find(predicate) {
11
+ this.start(predicate);
12
+ return this.stop ? this.lastVisited : null;
13
+ }
14
+ findByPath(path) {
15
+ const ctx = this.find(({ path: ctxPath }) => path === ctxPath);
16
+ if (!ctx) {
17
+ return null;
18
+ }
19
+ return {
20
+ resource: ctx.resource,
21
+ action: ctx.action
22
+ };
23
+ }
24
+ traverse(visit) {
25
+ this.start(visit);
26
+ }
27
+ start(callback) {
28
+ this.stop = false;
29
+ this.callback = callback;
30
+ this.traverseResources(this.resources);
31
+ }
32
+ traverseResources(resources, path) {
33
+ for (const [name, resource] of Object.entries(resources)) {
34
+ if (this.stop) {
35
+ break;
36
+ }
37
+ this.traverseResource(resource, path ? `${path}.${name}` : name);
38
+ }
39
+ }
40
+ traverseResource(resource, path) {
41
+ this.traverseActions(resource, resource.actions, path);
42
+ this.traverseResources(resource.subResources, path);
43
+ }
44
+ traverseActions(resource, actions, path) {
45
+ for (const [name, action] of Object.entries(actions)) {
46
+ if (this.stop) {
47
+ break;
48
+ }
49
+ if (action instanceof Action) {
50
+ const ctx = { action, resource, path: `${path}:${name}` };
51
+ this.lastVisited = ctx;
52
+ const stop = this.callback(ctx);
53
+ if (stop) {
54
+ this.stop = true;
55
+ break;
56
+ }
57
+ }
58
+ else {
59
+ this.traverseActions(resource, action.actions, `${path}.${name}`);
60
+ }
61
+ }
62
+ }
63
+ }
64
+ //# sourceMappingURL=actionVisitor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"actionVisitor.js","sourceRoot":"","sources":["../src/actionVisitor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAarC,MAAM,OAAO,aAAa;IACjB,SAAS,CAAa;IACtB,QAAQ,GAAa,GAAG,EAAE,GAAE,CAAC,CAAC;IAC9B,WAAW,GAAwB,IAAI,CAAC;IACxC,IAAI,GAAY,KAAK,CAAC;IAE9B,YAAY,SAAqB;QAChC,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC5B,CAAC;IAEM,IAAI,CAAC,SAAoB;QAC/B,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;QAEtB,OAAO,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,IAAI,CAAC;IAC5C,CAAC;IAEM,UAAU,CAA8C,IAAiB;QAC/E,MAAM,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC,IAAI,KAAK,OAAO,CAAC,CAAC;QAC/D,IAAI,CAAC,GAAG,EAAE,CAAC;YACV,OAAO,IAAI,CAAC;QACb,CA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AC9B,MAAM,GAAG,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,GAAG,IAAI,IAAI,IAAI,EAAE,EAAE,CAAC;gBAC1D,IAAI,CAAC,WAAW,GAAG,GAAG,CAAC;gBAEvB,MAAM,IAAI,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;gBAChC,IAAI,IAAI,EAAE,CAAC;oBACV,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;oBACjB,MAAM;gBACP,CAAC;YACF,CAAC;iBAAM,CAAC;gBACP,IAAI,CAAC,eAAe,CAAC,QAAQ,EAAE,MAAM,CAAC,OAAO,EAAE,GAAG,IAAI,IAAI,IAAI,EAAE,CAAC,CAAC;YACnE,CAAC;QACF,CAAC;IACF,CAAC;CACD","sourcesContent":["import { Action } from \"./action.js\";\nimport type { ActionMap, ResourceMap } from \"./internal/collections.js\";\nimport type { ActionFromPath, ActionPaths } from \"./internal/paths.js\";\nimport type { Resource } from \"./resource.js\";\n\ntype VisitContext = { action: Action<any, any>; resource: Resource<any, any>; path: string };\n\ntype Callback = (ctx: VisitContext) => any;\n\ntype Predicate = (ctx: VisitContext) => boolean;\n\ntype Visit = (ctx: VisitContext) => void;\n\nexport class ActionVisitor<TResources extends ResourceMap> {\n\tprivate resources: TResources;\n\tprivate callback: Callback = () => {};\n\tprivate lastVisited: VisitContext | null = null;\n\tprivate stop: boolean = false;\n\n\tconstructor(resources: TResources) {\n\t\tthis.resources = resources;\n\t}\n\n\tpublic find(predicate: Predicate) {\n\t\tthis.start(predicate);\n\n\t\treturn this.stop ? this.lastVisited : null;\n\t}\n\n\tpublic findByPath<TActionPath extends ActionPaths<TResources>>(path: TActionPath) {\n\t\tconst ctx = this.find(({ path: ctxPath }) => path === ctxPath);\n\t\tif (!ctx) {\n\t\t\treturn null;\n\t\t}\n\n\t\treturn {\n\t\t\tresource: ctx.resource,\n\t\t\taction: ctx.action as ActionFromPath<TResources, TActionPath>\n\t\t};\n\t}\n\n\tpublic traverse(visit: Visit) {\n\t\tthis.start(visit);\n\t}\n\n\tprivate start(callback: Callback) {\n\t\tthis.stop = false;\n\t\tthis.callback = callback;\n\n\t\tthis.traverseResources(this.resources);\n\t}\n\n\tprivate traverseResources(resources: ResourceMap, path?: string) {\n\t\tfor (const [name, resource] of Object.entries(resources)) {\n\t\t\tif (this.stop) {\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tthis.traverseResource(resource, path ? `${path}.${name}` : name);\n\t\t}\n\t}\n\n\tprivate traverseResource(resource: Resource<any, any>, path: string) {\n\t\tthis.traverseActions(resource, resource.actions, path);\n\t\tthis.traverseResources(resource.subResources, path);\n\t}\n\n\tprivate traverseActions(resource: Resource<any, any>, actions: ActionMap, path: string) {\n\t\tfor (const [name, action] of Object.entries(actions)) {\n\t\t\tif (this.stop) {\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tif (action instanceof Action) {\n\t\t\t\tconst ctx = { action, resource, path: `${path}:${name}` };\n\t\t\t\tthis.lastVisited = ctx;\n\n\t\t\t\tconst stop = this.callback(ctx);\n\t\t\t\tif (stop) {\n\t\t\t\t\tthis.stop = true;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tthis.traverseActions(resource, action.actions, `${path}.${name}`);\n\t\t\t}\n\t\t}\n\t}\n}\n"]}
@@ -0,0 +1,9 @@
1
+ export type * from "./abac.js";
2
+ export { ABAC } from "./abac.js";
3
+ export type * from "./action.js";
4
+ export type * from "./actionGroup.js";
5
+ export type * from "./resource.js";
6
+ export { Specitivity } from "./policy.js";
7
+ export type * from "./policy.js";
8
+ export type * from "./permission.js";
9
+ export type { ActionFromPath, ActionPaths } from "./internal/paths.js";
package/build/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export { ABAC } from "./abac.js";
2
+ export { Specitivity } from "./policy.js";
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAIjC,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC","sourcesContent":["/* eslint-disable import/export */\nexport type * from \"./abac.js\";\nexport { ABAC } from \"./abac.js\";\nexport type * from \"./action.js\";\nexport type * from \"./actionGroup.js\";\nexport type * from \"./resource.js\";\nexport { Specitivity } from \"./policy.js\";\nexport type * from \"./policy.js\";\nexport type * from \"./permission.js\";\nexport type { ActionFromPath, ActionPaths } from \"./internal/paths.js\";\n"]}