A lightweight, self-hosted multi-tenant web publishing platform

feat: response schemas for every endpoint, and frontend types generated from OpenAPI #16

Merged pre1ude.dev
targeting dev from refactor/response-schemas
Merged opened by pre1ude.dev
targeting dev from refactor/response-schemas

Why #

Only one of ~49 endpoints declared a response_model; the rest returned hand-built dicts. That meant:

  • /api/openapi.json described the inputs but not the outputs;
  • frontend/src/lib/types.ts was written by hand, so a backend field rename compiled fine and broke at runtime;
  • nothing stopped a handler from accidentally returning an extra field.

What changed #

Backend (c4fd01b) #

  • New response models, matching today's JSON exactly: schemas/common.py (DetailResponse), plus additions to schemas/{auth,users,keys,notifications,pages,admin,files}.py (e.g. WhoAmIResponse, PageDetailResponse, AdminUserListResponse, InstanceInfoResponse).
  • Every JSON endpoint now sets response_model=…. File downloads (/pages/{id}/files/download, /pages/{id}/download-zip) and the hidden /sites/* routes are intentionally excluded.
  • ListResponse.items is typed as FileNode instead of list[dict].
  • routes/openapi_test.py fails if any JSON endpoint is added without a response_model, and checks the OpenAPI schema builds.
  • Response and error text are unchanged; no endpoint behaviour changes.

Frontend (fd63d55) #

  • backend/scripts/export_openapi.py prints the schema; make types runs it and openapi-typescript to produce frontend/src/lib/api/schema.d.ts. make types-check fails if the committed file is stale.
  • lib/types.ts is now built from the generated schemas (User, Page, AdminUserItem, InstanceInfo, …) instead of duplicating them. NotificationItem narrows audience and meta; RuntimeConfigSnapshot keeps a hand-written value union.
  • The generated file is added to .prettierignore; openapi-typescript is a dev dependency.
  • The stricter types flagged four incomplete test fixtures (email/pending_email on users, app_port and unvouched_limits on instance info); those fixtures were completed rather than loosening the types.
  • PageSummary's nullable fields (display_title, description, thumbnail_path) are now required-but-nullable in the schema, so clients don't have to treat them as possibly missing.

Review notes #

  • Most of the line count is the generated schema.d.ts; review backend/schemas/* and frontend/src/lib/types.ts instead.
  • Datetimes remain ISO strings (str | None) for now; typing them as real datetimes belongs with the model work in refactor/models-and-migrations.
  • Because responses are now validated against the models, a handler returning data that doesn't match its schema fails with a 500 instead of passing it through. The existing suite exercises the common paths, but a manual pass over the admin pages is worthwhile.

Testing #

  • Backend: ruff format --check, ruff check, basedpyright, pytest (incl. the new guard test) — clean.
  • Frontend: bun run check (0 errors), bun run test, bun run lint for changed files — clean. make types-check is clean.
  • Not run: CI on Tangled; browser click-through.

Not in this PR #

  • A CI step for make types-check (needs both toolchains in one job).
  • Typing GET /generate-key as POST (it creates data, but changing the verb is a breaking API change).

Merge notes #

Independent of refactor/frontend-load-functions and refactor/models-and-migrations. If the load-functions branch merges first, lib/types.ts and the page imports may need a trivial rebase; the pages only import Page, User and friends by name.

Commits 2

feat(frontend): derive API types from the backend OpenAPI schema
feat(backend): response schemas for every JSON endpoint
Sign up or Sign in to add to the discussion.