feat: Implement automated OpenAPI type generation
Add automated type synchronization from backend OpenAPI schema to frontend TypeScript types to prevent type drift and ensure runtime safety. Changes: - Add openapi-typescript as dev dependency - Create npm scripts for type generation (generate:types) and validation (validate:types) - Integrate type generation into build pipeline (runs before TypeScript compilation) - Generate frontend/src/types/generated.ts from backend OpenAPI schema - Add frontend/scripts/validate-types.sh for CI/CD validation - Update Web-Development.md with type generation workflow documentation - Update Backend-Development.md with OpenAPI schema sync requirements Workflow: 1. Backend automatically exposes OpenAPI schema at /api/openapi.json (FastAPI built-in) 2. Frontend build runs 'npm run generate:types' to generate types from schema 3. Generated types are committed to version control 4. CI can run 'npm run validate:types' to fail builds if types drift Fixes critical type safety issue where frontend types were manually maintained and could become out of sync with backend Pydantic models. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
45
frontend/scripts/validate-types.sh
Normal file
45
frontend/scripts/validate-types.sh
Normal file
@@ -0,0 +1,45 @@
|
||||
#!/bin/bash
|
||||
# Validate that generated types match the committed types.
|
||||
# This script is designed to run in CI/CD pipelines to prevent type drift.
|
||||
#
|
||||
# Exit codes:
|
||||
# 0 — types are in sync
|
||||
# 1 — types are out of sync
|
||||
# 2 — backend is not accessible
|
||||
# 3 — generation failed
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
FRONTEND_DIR="$(dirname "$SCRIPT_DIR")"
|
||||
TYPES_DIR="${FRONTEND_DIR}/src/types"
|
||||
GENERATED_FILE="${TYPES_DIR}/generated.ts"
|
||||
TEMP_FILE=$(mktemp)
|
||||
trap "rm -f $TEMP_FILE" EXIT
|
||||
|
||||
# Check if backend is accessible
|
||||
BACKEND_URL="${BANGUI_BACKEND_URL:-http://localhost:8000}"
|
||||
if ! curl -sf "${BACKEND_URL}/api/openapi.json" > /dev/null 2>&1; then
|
||||
echo "❌ Backend not accessible at ${BACKEND_URL}/api/openapi.json" >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
echo "📋 Validating OpenAPI schema types..."
|
||||
|
||||
# Generate types to a temporary file
|
||||
if ! npx openapi-typescript "${BACKEND_URL}/api/openapi.json" -o "$TEMP_FILE" 2>&1; then
|
||||
echo "❌ Failed to generate types from OpenAPI schema" >&2
|
||||
exit 3
|
||||
fi
|
||||
|
||||
# Compare with committed generated types
|
||||
if ! diff -u "$GENERATED_FILE" "$TEMP_FILE" > /dev/null 2>&1; then
|
||||
echo "❌ Generated types differ from committed types" >&2
|
||||
echo ""
|
||||
echo "Run 'npm run generate:types' to update types, then commit the changes:"
|
||||
diff -u "$GENERATED_FILE" "$TEMP_FILE" || true
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "✅ Types are in sync with backend schema"
|
||||
exit 0
|
||||
Reference in New Issue
Block a user