#!/usr/bin/env bash # gen-api-types.sh — regenerate static/types/api.d.ts from FastAPI's OpenAPI # schema, or verify it is in sync (--check). Deterministic counterpart of # check-js-sync.sh. # # Why the Biome step: openapi-typescript emits 4-space indent with inline # unions; the committed file is Biome-formatted (tabs, lineWidth 80) — the # same formatter the repo enforces on static/js. Without it, every fresh # generation would differ from the tree and the freshness gate would never # pass. biome.json excludes static/types/**, so this script formats through a # minimal throwaway config in the same temp dir (nothing in the tree is read # or written for formatting). # # Exit 0: generated file matches the committed one (or, in write mode, the # tree file was regenerated). Exit 1: stale or hand-edited (check mode). set -eu repo_root=$(git rev-parse --show-toplevel) || exit 1 mode="${1:-write}" tmp=$(mktemp -d) trap 'rm -rf "$tmp"' EXIT # 1. Dump the FastAPI OpenAPI schema (large enough to be left in /tmp). # DuckDB lock warnings under a concurrent dev server are non-fatal. uv run python -c "import json; from app.main import app; json.dump(app.openapi(), open('$tmp/openapi.json', 'w'))" >/dev/null 2>&1 || { echo "api-types: FAIL — could not build the OpenAPI schema (app.main import)." >&2 exit 1 } # 2. Generate the TypeScript declarations. # openapi-typescript drives the TypeScript *JavaScript* compiler API, which # the repo-wide typescript@7 (native, bin-only) no longer ships — it fails # with `ts.SyntaxKind` undefined. It therefore runs from the pinned sidecar # toolchain in scripts/api-types/, which carries its own typescript@5.9 and # is installed by `make init` (self-heals here if it is missing). api_types_dir="$repo_root/scripts/api-types" if [ ! -x "$api_types_dir/node_modules/.bin/openapi-typescript" ]; then npm ci --prefix "$api_types_dir" --no-audit --no-fund >/dev/null 2>&1 || { echo "api-types: FAIL — could not install the sidecar toolchain (scripts/api-types)." >&2 echo " run: npm ci --prefix scripts/api-types" >&2 exit 1 } fi if ! node "$api_types_dir/node_modules/openapi-typescript/bin/cli.js" "$tmp/openapi.json" -o "$tmp/api.d.ts" >/dev/null 2>&1; then echo "api-types: FAIL — openapi-typescript errored." >&2 exit 1 fi # 3. Apply the repo's canonical format through a throwaway config. cat > "$tmp/biome.json" <<'EOF' { "$schema": "https://biomejs.dev/schemas/2.4.15/schema.json", "files": { "includes": ["**/*.ts"] }, "formatter": { "indentStyle": "tab", "lineWidth": 80 }, "linter": { "enabled": false } } EOF if ! npx @biomejs/biome format "$tmp/api.d.ts" --write --config-path "$tmp" >/dev/null 2>&1; then echo "api-types: FAIL — Biome format errored." >&2 exit 1 fi if [ "$mode" = "--check" ]; then if diff -q static/types/api.d.ts "$tmp/api.d.ts" >/dev/null 2>&1; then echo "api-types: static/types/api.d.ts is in sync with the backend OpenAPI schema" exit 0 fi cat >&2 <<'EOF' api-types: FAIL — static/types/api.d.ts is stale or hand-edited. The backend OpenAPI schema changed but the committed api.d.ts does not match a fresh `make api-types`. Fix deterministically: make api-types # review the regenerated static/types/api.d.ts, then commit both together. CI runs `make check-types-js` in `make ci` — a stale api.d.ts fails the pipeline. EOF exit 1 fi cp "$tmp/api.d.ts" "$repo_root/static/types/api.d.ts" echo "api-types: regenerated static/types/api.d.ts"