Skip to content

Function: createRbacDecorators() ​

ts
function createRbacDecorators<T, G>(): RbacDecoratorsFor<TypesOf<T>, G>;

Defined in: packages/nestjs/src/decorators.ts:210

Returns the requirement decorators with names checked against your typed names (the same T as createRbac<T>(...), or typeof rbac). Runtime behaviour is the same as the top-level decorators.

ts
type AppTypes = { permissions: 'edit articles' | 'delete articles'; roles: 'admin' | 'writer' }
export const { RequirePermissions, RequireRoles } = createRbacDecorators<AppTypes>()

@RequirePermissions('edit articles') // ok
@RequirePermissions('edit artcles')  // type error

@CurrentRbac() can't type its parameter (a Nest parameter decorator never does): annotate it with the same names, @CurrentRbac() access: SubjectRbac<AppTypes>.

With per-guard names (RbacTypeConfig['guards']), G is the guard of the resolved subject (RbacModule's guard; default: the default guard): createRbacDecorators<AppRbac, 'api'>(). Names are checked in that guard, or in a decorator's { guard }, and @CurrentRbac() is a SubjectRbac<AppRbac, 'api'>.

Type Parameters ​

Type ParameterDefault type
T extends RbacTypeConfig | Rbac<RbacTypeConfig>DefaultRbacTypes
G extends stringDefaultGuardName<TypesOf<T>> & GuardName<TypesOf<T>>

Returns ​

RbacDecoratorsFor<TypesOf<T>, G>

Released under the MIT License. Inspired by spatie/laravel-permission.