@typed-policy/eval 0.2.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) 2024 Togle Labs <m.ihsan.vp@gmail.com>
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.
@@ -0,0 +1,48 @@
1
+ import { Expr } from '@typed-policy/core';
2
+
3
+ /**
4
+ * Policy action type - matches the type in policy.ts
5
+ */
6
+ type PolicyAction<T, A> = Expr<T, A> | boolean | ((ctx: {
7
+ actor: A;
8
+ }) => boolean | Expr<T, A>);
9
+ /**
10
+ * Resource mapping for nested resource structure
11
+ * All fields are required as per v0.2 design
12
+ */
13
+ type ResourceMapping<T> = {
14
+ [K in keyof T]: {
15
+ [P in keyof T[K]]: T[K][P];
16
+ };
17
+ };
18
+ /**
19
+ * Options for evaluate function
20
+ */
21
+ type EvaluateOptions<T, A> = {
22
+ actor: A;
23
+ resources: ResourceMapping<T>;
24
+ };
25
+ /**
26
+ * Evaluate a policy expression
27
+ *
28
+ * @param expr - The expression to evaluate (Expr, boolean, or function)
29
+ * @param options - Evaluation options with actor and resources
30
+ * @returns boolean result
31
+ *
32
+ * @example
33
+ * ```typescript
34
+ * const canRead = evaluate(postPolicy.actions.read, {
35
+ * actor: { user: { id: "1", role: "admin" } },
36
+ * resources: {
37
+ * post: {
38
+ * id: "post-1",
39
+ * ownerId: "user-1",
40
+ * published: true
41
+ * }
42
+ * }
43
+ * });
44
+ * ```
45
+ */
46
+ declare function evaluate<T, A = unknown>(expr: PolicyAction<T, A>, options: EvaluateOptions<T, A>): boolean;
47
+
48
+ export { type EvaluateOptions, type ResourceMapping, evaluate };
package/dist/index.js ADDED
@@ -0,0 +1,51 @@
1
+ // src/evaluate.ts
2
+ function resolveValue(path, resources) {
3
+ const keys = path.split(".");
4
+ let current = resources;
5
+ for (const key of keys) {
6
+ if (current === null || current === void 0) {
7
+ return void 0;
8
+ }
9
+ if (typeof current !== "object") {
10
+ return void 0;
11
+ }
12
+ current = current[key];
13
+ }
14
+ return current;
15
+ }
16
+ function evaluate(expr, options) {
17
+ const { actor, resources } = options;
18
+ if (typeof expr === "boolean") {
19
+ return expr;
20
+ }
21
+ if (typeof expr === "function") {
22
+ const result = expr({ actor });
23
+ return evaluate(result, { actor, resources });
24
+ }
25
+ switch (expr.kind) {
26
+ case "literal": {
27
+ return expr.value;
28
+ }
29
+ case "function": {
30
+ const result = expr.fn({ actor });
31
+ return evaluate(result, { actor, resources });
32
+ }
33
+ case "eq": {
34
+ const leftValue = resolveValue(expr.left, resources);
35
+ const rightValue = typeof expr.right === "string" && expr.right.includes(".") ? resolveValue(expr.right, resources) : expr.right;
36
+ return leftValue === rightValue;
37
+ }
38
+ case "and": {
39
+ return expr.rules.every((rule) => evaluate(rule, { actor, resources }));
40
+ }
41
+ case "or": {
42
+ return expr.rules.some((rule) => evaluate(rule, { actor, resources }));
43
+ }
44
+ default: {
45
+ throw new Error(`Unknown expression kind: ${JSON.stringify(expr)}`);
46
+ }
47
+ }
48
+ }
49
+ export {
50
+ evaluate
51
+ };
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@typed-policy/eval",
3
+ "version": "0.2.0",
4
+ "description": "Frontend policy evaluator",
5
+ "author": "Ihsan VP <m.ihsan.vp@gmail.com>",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/toglelabs/typed-policy.git",
10
+ "directory": "packages/eval"
11
+ },
12
+ "homepage": "https://github.com/toglelabs/typed-policy/tree/main/packages/eval#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/toglelabs/typed-policy/issues"
15
+ },
16
+ "keywords": [
17
+ "typescript",
18
+ "policy",
19
+ "authorization",
20
+ "evaluator",
21
+ "frontend",
22
+ "boolean"
23
+ ],
24
+ "type": "module",
25
+ "exports": {
26
+ ".": {
27
+ "import": "./dist/index.js",
28
+ "types": "./dist/index.d.ts"
29
+ }
30
+ },
31
+ "files": [
32
+ "dist"
33
+ ],
34
+ "dependencies": {
35
+ "@typed-policy/core": "0.2.0"
36
+ },
37
+ "devDependencies": {
38
+ "@biomejs/biome": "^1.9.4",
39
+ "typescript": "^5.7.0",
40
+ "tsup": "^8.3.0",
41
+ "vitest": "^2.0.0"
42
+ },
43
+ "scripts": {
44
+ "build": "tsup src/index.ts --format esm --dts",
45
+ "typecheck": "tsc --noEmit",
46
+ "test": "vitest",
47
+ "clean": "rm -rf dist",
48
+ "lint": "biome check src",
49
+ "format": "biome format --write src"
50
+ }
51
+ }