#!/bin/bash # Generate TypeScript types from OpenAPI spec # Run this script from the project root: ./scripts/generate-types.sh set -e PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" BACKEND_DIR="${PROJECT_ROOT}/chimera-zero-cost/backend" FRONTEND_DIR="${PROJECT_ROOT}/chimera-zero-cost/frontend" OUTPUT_DIR="${FRONTEND_DIR}/lib/api-generated" echo "🔄 Generating TypeScript types from OpenAPI spec..." echo "Project root: ${PROJECT_ROOT}" # Check if backend is running, if not start it temporarily if ! curl -s http://localhost:7860/api/v1/health > /dev/null 2>&1; then echo "📦 Starting backend temporarily for OpenAPI spec generation..." cd "${BACKEND_DIR}" python -m uvicorn app.main:app --host 0.0.0.0 --port 7860 & BACKEND_PID=$! echo "⏳ Waiting for backend to start..." sleep 8 # Wait for health check for i in {1..30}; do if curl -s http://localhost:7860/api/v1/health > /dev/null 2>&1; then echo "✅ Backend is ready" break fi sleep 1 done else echo "✅ Backend already running" BACKEND_PID="" fi # Generate OpenAPI spec echo "📥 Fetching OpenAPI spec..." curl -s http://localhost:7860/openapi.json > "${BACKEND_DIR}/openapi.json" # Kill temp backend if we started it if [ -n "${BACKEND_PID}" ]; then echo "🛑 Stopping temporary backend..." kill ${BACKEND_PID} 2>/dev/null || true fi # Generate TypeScript client echo "🔧 Generating TypeScript client..." cd "${FRONTEND_DIR}" # Check if openapi-typescript-codegen is available if ! npx --version >/dev/null 2>&1; then echo "❌ npx not found. Please install Node.js" exit 1 fi npx openapi-typescript-codegen \ -i "${BACKEND_DIR}/openapi.json" \ -o "${OUTPUT_DIR}" \ --client axios \ --useOptions \ --exportServices \ --exportSchemas \ --httpClient axios \ --enumType string \ --name "ChimeraApi" \ --exportCore true \ --exportModels true \ --exportServices true # Clean up rm -f "${BACKEND_DIR}/openapi.json" echo "✅ TypeScript types generated in ${OUTPUT_DIR}" echo "" echo "📝 Next steps:" echo " 1. Update frontend/lib/api.ts to import from './api-generated'" echo " 2. Run 'npm run build' to verify types compile" echo " 3. Commit the generated types"