Skip to content

Hook Code Examples

Scaffold a TypeScript hook

bash
zeptoz hook add Task before_insert --lang ts --new normalize_title

This creates a hook source file and compiles an artifact for runtime.

TypeScript: mutate record before insert

ts
/// <reference path="../../_sdk/types.d.ts" />

export default function hook(input, ctx) {
  const record = input.record || {};
  if (typeof record.title === "string") {
    record.title = record.title.trim();
  }

  // Example policy check
  if (!ctx.auth.hasRole("admin") && !ctx.auth.hasRole("owner")) {
    return {
      ok: false,
      errors: [{ code: "RBAC_FORBIDDEN", message: "Write requires owner/admin" }]
    };
  }

  return { ok: true, record };
}

JavaScript: after hook warning

js
export default function hook(input, ctx) {
  ctx.log.warn("downstream queue unavailable");
  return {
    ok: false,
    errors: [{ code: "DOWNSTREAM", message: "Queue unavailable" }]
  };
}

When this runs in an after_* event, Zeptoz keeps the primary operation and surfaces warning metadata in meta.hook_warnings.

Rust hook scaffold shape

rust
use zeptoz_hooks::{HookInputV1, HookResultV1};

pub fn normalize_title(input: HookInputV1) -> HookResultV1 {
    HookResultV1::ok(input.record)
}

Use generated TS types

Zeptoz writes hook typings to:

  • hooks/_sdk/types.d.ts

Include it in TypeScript hooks using the reference line shown above.