Skip to content

Function: createGuards() ​

Call Signature ​

ts
function createGuards<T, G>(options): GuardsFor<TypesOf<T>, G, any>;

Defined in: packages/trpc/src/guards.ts:260

Builds the guards with shared options: handlers (e.g. an onForbidden that throws NOT_FOUND), and a resolve that reads the subject of a call instead of ctx.rbac.

Pass the same type argument as createRbac<AppRbac>() (or typeof rbac) to check names at compile time: const { requirePermission } = createGuards<AppRbac>(). After a guard, ctx.rbac has the same names and is never null.

With per-guard names (RbacTypeConfig['guards']), G is the guard of the subjects these guards check (the guard of createRbacContext(); default: the default guard): createGuards<AppRbac, 'api'>(). Names are checked in that guard, or in a procedure's { guard }. It is a type only; the subject's guard still decides at runtime.

Type Parameters ​

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

Parameters ​

ParameterType
optionsCreateGuardsOptions<GuardSubject<T, G>> & object

Returns ​

GuardsFor<TypesOf<T>, G, any>

Call Signature ​

ts
function createGuards<T, G>(defaults?): GuardsFor<TypesOf<T>, G>;

Defined in: packages/trpc/src/guards.ts:266

Builds the guards with shared options: handlers (e.g. an onForbidden that throws NOT_FOUND), and a resolve that reads the subject of a call instead of ctx.rbac.

Pass the same type argument as createRbac<AppRbac>() (or typeof rbac) to check names at compile time: const { requirePermission } = createGuards<AppRbac>(). After a guard, ctx.rbac has the same names and is never null.

With per-guard names (RbacTypeConfig['guards']), G is the guard of the subjects these guards check (the guard of createRbacContext(); default: the default guard): createGuards<AppRbac, 'api'>(). Names are checked in that guard, or in a procedure's { guard }. It is a type only; the subject's guard still decides at runtime.

Type Parameters ​

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

Parameters ​

ParameterType
defaults?GuardHandlers<GuardSubject<T, G>>

Returns ​

GuardsFor<TypesOf<T>, G>

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