@zap-studio/permit 0.3.2 → 0.3.4
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/CHANGELOG.md +16 -4
- package/LICENSE +1 -1
- package/README.md +18 -6
- package/dist/conditions.d.ts +177 -0
- package/dist/conditions.d.ts.map +1 -0
- package/dist/conditions.js +180 -0
- package/dist/conditions.js.map +1 -0
- package/dist/errors.d.ts +21 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/{errors.mjs → errors.js} +11 -1
- package/dist/errors.js.map +1 -0
- package/dist/helpers.d.ts +32 -0
- package/dist/helpers.d.ts.map +1 -0
- package/dist/{helpers.mjs → helpers.js} +8 -3
- package/dist/helpers.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +5 -0
- package/dist/policy.d.ts +89 -0
- package/dist/policy.d.ts.map +1 -0
- package/dist/policy.js +153 -0
- package/dist/policy.js.map +1 -0
- package/dist/types.d.ts +142 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +0 -0
- package/package.json +14 -16
- package/dist/errors.d.mts +0 -11
- package/dist/errors.d.mts.map +0 -1
- package/dist/errors.mjs.map +0 -1
- package/dist/helpers.d.mts +0 -27
- package/dist/helpers.d.mts.map +0 -1
- package/dist/helpers.mjs.map +0 -1
- package/dist/index.d.mts +0 -256
- package/dist/index.d.mts.map +0 -1
- package/dist/index.mjs +0 -319
- package/dist/index.mjs.map +0 -1
- package/dist/types.d.mts +0 -143
- package/dist/types.d.mts.map +0 -1
- package/dist/types.mjs +0 -1
package/dist/types.d.mts
DELETED
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
import { StandardSchemaV1 } from "@zap-studio/validation";
|
|
2
|
-
|
|
3
|
-
//#region src/types.d.ts
|
|
4
|
-
/**
|
|
5
|
-
* Represents the possible outcomes of a policy decision.
|
|
6
|
-
* - "allow": The action is permitted.
|
|
7
|
-
* - "deny": The action is not permitted.
|
|
8
|
-
*/
|
|
9
|
-
type Decision = "allow" | "deny";
|
|
10
|
-
/**
|
|
11
|
-
* Represents the context in which a policy decision is made.
|
|
12
|
-
* Can include user information, environment, or any relevant data.
|
|
13
|
-
*/
|
|
14
|
-
type Context<TContext = unknown> = TContext;
|
|
15
|
-
/**
|
|
16
|
-
* Represents a role within the system.
|
|
17
|
-
*/
|
|
18
|
-
type Role<TRole extends string = string> = TRole;
|
|
19
|
-
/**
|
|
20
|
-
* Represents a role hierarchy within the system.
|
|
21
|
-
* Maps each role to an array of roles it inherits from.
|
|
22
|
-
*
|
|
23
|
-
* @example
|
|
24
|
-
* ```ts
|
|
25
|
-
* type Roles = "guest" | "user" | "admin";
|
|
26
|
-
*
|
|
27
|
-
* const hierarchy: RoleHierarchy<Roles> = {
|
|
28
|
-
* guest: [],
|
|
29
|
-
* user: ["guest"],
|
|
30
|
-
* admin: ["user"],
|
|
31
|
-
* };
|
|
32
|
-
* ```
|
|
33
|
-
*/
|
|
34
|
-
type RoleHierarchy<TRole extends Role = Role> = Record<TRole, TRole[]>;
|
|
35
|
-
/**
|
|
36
|
-
* Type helper for defining resource schemas using Standard Schema.
|
|
37
|
-
* Use with `satisfies` to ensure type safety when defining resources.
|
|
38
|
-
*
|
|
39
|
-
* @example
|
|
40
|
-
* ```ts
|
|
41
|
-
* import { z } from "zod";
|
|
42
|
-
* import type { Resources } from "@zap-studio/permit/types";
|
|
43
|
-
*
|
|
44
|
-
* const resources = {
|
|
45
|
-
* post: z.object({ id: z.string(), authorId: z.string() }),
|
|
46
|
-
* comment: z.object({ id: z.string(), postId: z.string() }),
|
|
47
|
-
* } satisfies Resources;
|
|
48
|
-
* ```
|
|
49
|
-
*/
|
|
50
|
-
type Resources<TResourceKey extends string = string> = Record<TResourceKey, StandardSchemaV1>;
|
|
51
|
-
/**
|
|
52
|
-
* Type helper for defining actions per resource.
|
|
53
|
-
* Use with `satisfies` to ensure keys match the resource definitions.
|
|
54
|
-
*
|
|
55
|
-
* @example
|
|
56
|
-
* ```ts
|
|
57
|
-
* import type { Actions } from "@zap-studio/permit/types";
|
|
58
|
-
*
|
|
59
|
-
* const actions = {
|
|
60
|
-
* post: ["read", "write", "delete"],
|
|
61
|
-
* comment: ["read", "write"],
|
|
62
|
-
* } as const satisfies Actions<typeof resources>;
|
|
63
|
-
* ```
|
|
64
|
-
*/
|
|
65
|
-
type Actions<TResources extends Resources> = { [K in keyof TResources]: readonly string[] };
|
|
66
|
-
/**
|
|
67
|
-
* Infers the output type from a Standard Schema.
|
|
68
|
-
*/
|
|
69
|
-
type InferResource<TResources extends Resources, TResourceKey extends keyof TResources> = StandardSchemaV1.InferOutput<TResources[TResourceKey]>;
|
|
70
|
-
/**
|
|
71
|
-
* Infers the action union type for a specific resource.
|
|
72
|
-
*/
|
|
73
|
-
type InferAction<TActions extends Record<string, readonly string[]>, K extends keyof TActions> = TActions[K][number];
|
|
74
|
-
/**
|
|
75
|
-
* Infers the permission-string union for all resource/action combinations.
|
|
76
|
-
*
|
|
77
|
-
* @example
|
|
78
|
-
* ```ts
|
|
79
|
-
* type Permission = InferPermission<typeof resources, typeof actions>;
|
|
80
|
-
* // "post:read" | "post:write" | "comment:read"
|
|
81
|
-
* ```
|
|
82
|
-
*/
|
|
83
|
-
type InferPermission<TResources extends Resources, TActions extends Actions<TResources>> = { [K in keyof TResources & keyof TActions]: `${K & string}:${InferAction<TActions, K> & string}` }[keyof TResources & keyof TActions];
|
|
84
|
-
/**
|
|
85
|
-
* A function that determines whether a given action on a resource is allowed in a specific context.
|
|
86
|
-
*/
|
|
87
|
-
type PolicyFn<TContext extends Context, TAction extends string = string, TResource = unknown> = (context: TContext, action: TAction, resource: TResource) => Decision;
|
|
88
|
-
/**
|
|
89
|
-
* A function that evaluates a condition for a given action and resource in a specific context.
|
|
90
|
-
*/
|
|
91
|
-
type ConditionFn<TContext extends Context, TAction extends string = string, TResource = unknown> = (context: TContext, action: TAction, resource: TResource) => boolean;
|
|
92
|
-
/**
|
|
93
|
-
* Maps actions to their corresponding policy functions for a specific resource.
|
|
94
|
-
*/
|
|
95
|
-
type ActionPolicyMap<TContext extends Context, TAction extends string = string, TResource = unknown> = { [A in TAction]?: PolicyFn<TContext, A, TResource> };
|
|
96
|
-
/**
|
|
97
|
-
* Defines the rules for each resource and action combination.
|
|
98
|
-
* Each resource key maps to an object where each action key maps to a policy function.
|
|
99
|
-
*/
|
|
100
|
-
type Rules<TContext extends Context, TResources extends Resources = Resources, TActions extends Actions<TResources> = Actions<TResources>> = { [K in keyof TResources & keyof TActions]: ActionPolicyMap<TContext, InferAction<TActions, K>, InferResource<TResources, K>> };
|
|
101
|
-
/**
|
|
102
|
-
* Configuration object for creating a permit policy.
|
|
103
|
-
*
|
|
104
|
-
* @example
|
|
105
|
-
* ```ts
|
|
106
|
-
* const config: PermitConfig<MyContext> = {
|
|
107
|
-
* resources,
|
|
108
|
-
* actions,
|
|
109
|
-
* rules: {
|
|
110
|
-
* post: { read: allow(), write: deny() },
|
|
111
|
-
* },
|
|
112
|
-
* };
|
|
113
|
-
* ```
|
|
114
|
-
*/
|
|
115
|
-
interface PermitConfig<TContext extends Context, TResources extends Resources = Resources, TActions extends Actions<TResources> = Actions<TResources>> {
|
|
116
|
-
actions: TActions;
|
|
117
|
-
resources: TResources;
|
|
118
|
-
rules: Rules<TContext, TResources, TActions>;
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* Represents a policy object that can evaluate permissions.
|
|
122
|
-
* The `can` method checks if a given action is permitted on a resource in a specific context.
|
|
123
|
-
*
|
|
124
|
-
* @example
|
|
125
|
-
* ```ts
|
|
126
|
-
* const policy: Policy<MyContext> = createPolicy({
|
|
127
|
-
* resources,
|
|
128
|
-
* actions,
|
|
129
|
-
* rules: { ... },
|
|
130
|
-
* });
|
|
131
|
-
*
|
|
132
|
-
* await policy.can(ctx, "post:read", postData); // true or false
|
|
133
|
-
* ```
|
|
134
|
-
*/
|
|
135
|
-
interface Policy<TContext extends Context, TResources extends Resources = Resources, TActions extends Actions<TResources> = Actions<TResources>> {
|
|
136
|
-
/**
|
|
137
|
-
* Determines if the specified action is permitted on the resource in the given context.
|
|
138
|
-
*/
|
|
139
|
-
can<K extends keyof TResources & keyof TActions>(context: TContext, permission: `${K & string}:${InferAction<TActions, K> & string}`, resource: InferResource<TResources, K>): Promise<boolean>;
|
|
140
|
-
}
|
|
141
|
-
//#endregion
|
|
142
|
-
export { ActionPolicyMap, Actions, ConditionFn, Context, Decision, InferAction, InferPermission, InferResource, PermitConfig, Policy, PolicyFn, Resources, Role, RoleHierarchy, Rules };
|
|
143
|
-
//# sourceMappingURL=types.d.mts.map
|
package/dist/types.d.mts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.mts","names":[],"sources":["../src/types.ts"],"mappings":";;;;;AAOA;;;KAAY,QAAA;;AAMZ;;;KAAY,OAAA,uBAA8B,QAAA;;AAK1C;;KAAY,IAAA,kCAAsC,KAAA;;;AAiBlD;;;;;;;;;;;;;KAAY,aAAA,eAA4B,IAAA,GAAO,IAAA,IAAQ,MAAA,CAAO,KAAA,EAAO,KAAA;;;;;AAiBrE;;;;;;;;;;;KAAY,SAAA,yCAAkD,MAAA,CAC5D,YAAA,EACA,gBAAA;;;AAiBF;;;;;;;;;;AAOA;;KAPY,OAAA,oBAA2B,SAAA,kBACzB,UAAA;;;;KAMF,aAAA,oBACS,SAAA,6BACQ,UAAA,IACzB,gBAAA,CAAiB,WAAA,CAAY,UAAA,CAAW,YAAA;;;;KAKhC,WAAA,kBACO,MAAA,6CACD,QAAA,IACd,QAAA,CAAS,CAAA;;;;AAHb;;;;;;KAcY,eAAA,oBAAmC,SAAA,mBAA4B,OAAA,CAAQ,UAAA,mBACrE,UAAA,SAAmB,QAAA,MAAc,CAAA,aAAc,WAAA,CAAY,QAAA,EAAU,CAAA,qBAC3E,UAAA,SAAmB,QAAA;;;;KAKf,QAAA,kBACO,OAAA,2DAGd,OAAA,EAAS,QAAA,EAAU,MAAA,EAAQ,OAAA,EA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