Why #
Only one of ~49 endpoints declared a response_model; the rest returned hand-built dicts. That meant:
/api/openapi.jsondescribed the inputs but not the outputs;frontend/src/lib/types.tswas 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 toschemas/{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.itemsis typed asFileNodeinstead oflist[dict].routes/openapi_test.pyfails if any JSON endpoint is added without aresponse_model, and checks the OpenAPI schema builds.- Response and error text are unchanged; no endpoint behaviour changes.
Frontend (fd63d55) #
backend/scripts/export_openapi.pyprints the schema;make typesruns it andopenapi-typescriptto producefrontend/src/lib/api/schema.d.ts.make types-checkfails if the committed file is stale.lib/types.tsis now built from the generated schemas (User,Page,AdminUserItem,InstanceInfo, …) instead of duplicating them.NotificationItemnarrowsaudienceandmeta;RuntimeConfigSnapshotkeeps a hand-written value union.- The generated file is added to
.prettierignore;openapi-typescriptis a dev dependency. - The stricter types flagged four incomplete test fixtures (
email/pending_emailon users,app_portandunvouched_limitson 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; reviewbackend/schemas/*andfrontend/src/lib/types.tsinstead. - Datetimes remain ISO strings (
str | None) for now; typing them as real datetimes belongs with the model work inrefactor/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 lintfor changed files — clean.make types-checkis 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-keyasPOST(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.