Blocks Policy

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.

ExportKindWhat it is
PolicyclassThe base class for a policy: one per resource, constructed with (user, resource).
CurrentUserclassWho is acting: an id, one role and the resolved flags, with hasRole, isOwner and hasFlag.
CurrentUserDatatypeWhat 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

ts
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.user and this.resource. Both are protected, so code outside the policy cannot read them through it.
  • TResource comes first, so the common case names only the resource: Policy<Post>.
  • TUser defaults to CurrentUser. It has no constraint: pass your own type, such as { user, organization, membership }, for an actor that is not a CurrentUser. An instance of a CurrentUser subclass is accepted where the default is declared.
  • Policy adds 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

ts
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.

  • id and role are the values you passed. flags is the object you passed, or an empty object when you passed none.
  • hasRole(...roles) is true when the user’s role is one of roles. With no arguments it is false.
  • isOwner(ownerId) is true when ownerId equals the user’s id. It is false for null, undefined and the empty string, even for a user whose id is the empty string.
  • hasFlag(name) is true only when the flag is exactly true. A missing flag, a missing flags, false and any other value are false. 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, in hasRole and in hasFlag.
  • The constructor does not infer TRole and TFlag from its data, which is what NoInfer does. new CurrentUser({ id, role: 'member' }) is a CurrentUser<string, string>, so hasRole('admin') still compiles.
  • A subclass keeps all three methods and can add fields. See The current user.

CurrentUserData

ts
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.