[]previous and next page

defineGuard

Identity helper that types a reusable guard.

Identity helper that types a reusable guard. Use it to name a check once and pass it to any number of server functions or API methods.

Import#

ts
import { defineGuard } from "@shadow-js/dusk/server";

Signature#

ts
function defineGuard(guard: Guard): Guard

Parameters#

Name Description
guard The check.

Returns#

The same guard.

Throws#

  • TypeError when guard is not a function.

Examples#

ts
export const requireAdmin = defineGuard((event) => {
  if (event.locals.user?.role !== "admin") throw new HttpError(403, "Admins only");
});