types
What it does
CRUD over public.types — the type-definition registry whose design_schema/validation_schema columns drive record validation, permissions, and the generic UI (see Design Schema). Also resolves single types by slug and exposes a schema-lookup RPC pass-through.
Route and methods
/.netlify/functions/types, reachable as /api/types (/api/* → /.netlify/functions/:splat, .framework/public/_redirects). Dispatch is by ?action= plus the HTTP method (?method= can override the method):
| Call | Handler |
|---|---|
GET /api/types?action=schema&kind=<k>&slug=<s>[&app_id=<uuid>] | getSchema → RPC get_type_schema |
GET /api/types?action=list (or bare GET with no id/slug/kind) | list |
GET /api/types?slug=<s> | getBySlug (active rows only, joins apps) |
GET /api/types?id=<uuid> | get |
POST /api/types | create |
PATCH /api/types?id=<uuid> | update |
DELETE /api/types?id=<uuid> | remove |
Quirk: a bare GET carrying kind (without action=list or action=schema) routes to get, which then fails with Type ID is required — kind-filtered listing requires action=list.
Authentication
Standard createHandler auth (resolvePrincipal in .framework/functions/_shared/principal.ts): x-api-key → machine principal, x-internal-secret → internal principal, Authorization: Bearer <jwt> → human. Anonymous → 401.
create,update,removerequire asystem_adminrole (PermissionEngine.isSystemAdmin(ctx)) — no fallback permission path.list/get/getBySlug/getSchemaonly require authentication. System admins read viaadminDb(all accounts); other principals usectx.db— JWT-scoped (RLS) for humans, service role for machines.
Request contract
list (query): kind, app_id ('null' filters to global types), ownership, active ('false' → inactive only, 'all' → everything, default → active only), limit (default 50), offset (default 0), include_schema ('true' also returns validation_schema).
getBySlug (query): slug — active rows only.
getSchema (query): kind, slug required; app_id optional (passed to the RPC as null when absent).
create (body): name, slug, kind required. Optional: ownership (default 'system'), app_id (default null), design_schema (default {}), validation_schema (default null → generated), description, icon, color.
update (query id + body): name, slug, description, icon, color, is_active, kind, app_id, ownership, design_schema — only keys present in the body are written. An empty update returns the existing row unchanged.
remove (query): id.
Response contract
Standard { data, error, meta } envelope.
list→ array of sanitized type rows.get/getBySlug/create/update→ single row.remove→{ success: true }.getSchema→{ design_schema }.- Sanitization (
sanitizeRecordData): system admins get the full row. Other principals go through first-surface logic — the row's owndesign_schema.record_permissionsdecides visibility; rows without them collapse to{ id }.listre-attachesdesign_schemato the sanitized row when it was stripped, and attachesvalidation_schemawheninclude_schema=true.
Side effects
createinserts intopublic.typeswithis_active: true; when the body omitsvalidation_schemait is generated fromdesign_schemabygenerateValidationSchema(.framework/functions/_shared/schema-utils.ts).updateregeneratesvalidation_schemaevery timedesign_schemais present in the body — the derived schema is never hand-editable.removeis a soft delete (is_active = false);ownership: 'system'rows cannot be deleted, and theirownershipcannot be changed away from'system'.emitLogwritestype.created/type.updated/type.deletedaudit rows topublic.logs.- Updating a type does not restamp
design_schemaonto existing records — records keep the schema stamped at their own creation.
Errors
| Condition | HTTP | Message |
|---|---|---|
| Not system admin (create/update/remove) | 500 | System admin required |
| Missing create fields | 500 | name, slug, and kind are required |
| Duplicate active slug/kind/app_id | 500 | Type with slug '<s>' already exists for kind '<k>' |
Missing id / slug / kind+slug | 500 | Type ID is required / Type slug is required / kind and slug are required |
| Update/delete of missing row | 500 | Type not found |
| Ownership change on system type | 500 | Cannot change ownership of system types |
| Delete system type | 500 | Cannot delete system types |
| Bad action/method | 500 | Invalid action or method |
Validation failures surface as HTTP 500 — the shared error path maps thrown Errors without a statusCode to 500.
Agent notes
- Slug uniqueness is global per
kind: thetypes_kind_slug_uniqueindex ontypes(kind, slug)(.framework/migrations/015_types_kind_slug_unique.sql) is stricter thancreate's pre-check (which scopes toapp_id+is_active), so a same-kind/same-slug insert under a differentapp_idpasses the pre-check and then fails at the database. Pick globally unique slugs. kindmust match the runtime table the type governs (item,thread,message, …);admin-dataresolves types by kind+slug.getSchemadelegates to the Postgres RPCget_type_schema(kind, slug, app_id), provided by migration070_app_type_schema_rpcs.sql(along withget_app_schema,is_app_available,update_app_version). Installs missing that migration will seefunction does not existerrors.updateacceptskindandapp_idchanges — there is no guard keepingkindaligned with already-stamped records.- The unauthenticated "minimal row" branch in
listis unreachable via HTTP:createHandlerrejects anonymous callers with 401 first.