Chimera / scripts /generate-types.sh
ag235772's picture
fixing everything all again
4d55d9e
Raw History Blame Contribute Delete
2.3 kB
#!/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"