API
@blocks/policy exports two classes and one type, and nothing else. Importing it has no side effects: it adds no globals and keeps no registry.
| Export | Kind | What it is |
|---|---|---|
Policy | class | The base class for a policy: one per resource, constructed with (user, resource). |
CurrentUser | class | Who is acting: an id, one role and the resolved flags, with hasRole, isOwner and hasFlag. |
CurrentUserData | type | What CurrentUser’s constructor takes. |
The signature under each heading is the package’s published type declaration, without its comments. The type declarations need TypeScript 5.4 or newer.
Policy
abstract class Policy<TResource, TUser = CurrentUser> {
protected readonly user: TUser
protected readonly resource: TResource
constructor(user: TUser, resource: TResource)
}The base class for a policy. Extend it once per resource and add one method per action.
- The constructor takes the user first and the resource second, and keeps them as
this.userandthis.resource. Both are protected, so code outside the policy cannot read them through it. TResourcecomes first, so the common case names only the resource:Policy<Post>.TUserdefaults toCurrentUser. It has no constraint: pass your own type, such as{ user, organization, membership }, for an actor that is not aCurrentUser. An instance of aCurrentUsersubclass is accepted where the default is declared.Policyadds no methods of its own. Every public method on a policy is one you wrote.
An action is a public method you add that returns a boolean, and you ask a policy by calling it: policy.update(). Keep actions synchronous and decide from data the caller already loaded; an async action returns a Promise, which is always truthy. See Writing policies and Enforcing.
CurrentUser
class CurrentUser<TRole extends string = string, TFlag extends string = string> {
readonly id: string
readonly role: TRole
readonly flags: Readonly<Partial<Record<TFlag, boolean>>>
constructor(data: CurrentUserData<NoInfer<TRole>, NoInfer<TFlag>>)
hasRole(...roles: TRole[]): boolean
isOwner(ownerId: string | null | undefined): boolean
hasFlag(name: TFlag): boolean
}Who is acting: an id, one role, and the feature flags already resolved for them. It is Policy’s default user type.
idandroleare the values you passed.flagsis the object you passed, or an empty object when you passed none.hasRole(...roles)istruewhen the user’s role is one ofroles. With no arguments it isfalse.isOwner(ownerId)istruewhenownerIdequals the user’s id. It isfalsefornull,undefinedand the empty string, even for a user whose id is the empty string.hasFlag(name)istrueonly when the flag is exactlytrue. A missing flag, a missingflags,falseand any other value arefalse. Only the flags object’s own properties count, so a flag it inherits from a prototype is off.- With the default type parameters any role and flag name is accepted. With
CurrentUser<Role, Flag>, a role or flag that is not in the union is a compile error, in the constructor’s data, inhasRoleand inhasFlag. - The constructor does not infer
TRoleandTFlagfrom its data, which is whatNoInferdoes.new CurrentUser({ id, role: 'member' })is aCurrentUser<string, string>, sohasRole('admin')still compiles. - A subclass keeps all three methods and can add fields. See The current user.
CurrentUserData
interface CurrentUserData<TRole extends string = string, TFlag extends string = string> {
id: string
role: TRole
flags?: Readonly<Partial<Record<TFlag, boolean>>>
}What CurrentUser’s constructor takes. flags holds the feature flags the caller already resolved; a flag that is missing is off. A subclass of CurrentUser usually extends this interface with its own fields.