Skip to content

Function: createGuards() ​

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

Defined in: packages/fastify/src/guards.ts:218

Builds the guards with shared defaults (e.g. a custom onForbidden that renders an HTML page).

Pass the same type argument as createRbac<AppTypes>() (or typeof rbac) to check names at compile time: const { requirePermission } = createGuards<AppTypes>(). The handlers (defaults and per-guard options) then get a request whose request.rbac has the same names.

With per-guard names (RbacTypeConfig['guards']), G is the guard of the requests these guards run on (the middleware's guard; default: the default guard): createGuards<AppRbac, 'api'>(). Names are checked in that guard, or in a route's { guard }. It is a type only; the middleware'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<GuardRequest<TypesOf<T>, G>>

Returns ​

GuardsFor<TypesOf<T>, G>

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