|
| 1 | +# OpenAPI Generator |
| 2 | + |
| 3 | +This directory contains the tooling to generate a TypeScript Axios client from the FastAPI backend's OpenAPI specification. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The generator performs two main tasks: |
| 8 | + |
| 9 | +1. **Extract OpenAPI Schema** - Imports the FastAPI application and generates the OpenAPI JSON specification |
| 10 | +2. **Generate TypeScript Client** - Uses OpenAPI Generator CLI (via Docker) to create a typed Axios client |
| 11 | + |
| 12 | +## Prerequisites |
| 13 | + |
| 14 | +- **Python 3.x** with the backend dependencies installed |
| 15 | +- **Docker** - Required to run the OpenAPI Generator CLI |
| 16 | + |
| 17 | +## Directory Structure |
| 18 | + |
| 19 | +``` |
| 20 | +openapi/ |
| 21 | +├── README.md # This file |
| 22 | +├── generate.py # Main generator script |
| 23 | +├── openapi.json # Generated OpenAPI specification |
| 24 | +└── templates/ |
| 25 | + └── typescript-axios/ |
| 26 | + └── baseApi.mustache # Custom template for base API configuration |
| 27 | +``` |
| 28 | + |
| 29 | +## Usage |
| 30 | + |
| 31 | +Run the generator from the project root: |
| 32 | + |
| 33 | +```bash |
| 34 | +cd /path/to/inquiro |
| 35 | +python openapi/generate.py |
| 36 | +``` |
| 37 | + |
| 38 | +This will: |
| 39 | + |
| 40 | +1. Load environment variables from `backend/dev.env` |
| 41 | +2. Import the FastAPI app from `app.main` |
| 42 | +3. Generate `openapi/openapi.json` from the app's routes |
| 43 | +4. Run the OpenAPI Generator CLI via Docker |
| 44 | +5. Output the TypeScript Axios client to `frontend/src/api/` |
| 45 | + |
| 46 | +## Generated Output |
| 47 | + |
| 48 | +The generator creates the following structure in `frontend/src/api/`: |
| 49 | + |
| 50 | +``` |
| 51 | +frontend/src/api/ |
| 52 | +├── api.ts # Aggregated API exports |
| 53 | +├── base.ts # Base API class with Axios configuration |
| 54 | +├── common.ts # Common utilities |
| 55 | +├── configuration.ts # API configuration options |
| 56 | +├── index.ts # Main entry point |
| 57 | +├── apis/ # Generated API classes by tag |
| 58 | +│ ├── authentication-api.ts |
| 59 | +│ ├── users-api.ts |
| 60 | +│ ├── projects-api.ts |
| 61 | +│ ├── paper-api.ts |
| 62 | +│ └── search-api.ts |
| 63 | +└── models/ # TypeScript interfaces for request/response types |
| 64 | + ├── index.ts |
| 65 | + ├── user-create.ts |
| 66 | + ├── user-response.ts |
| 67 | + └── ... |
| 68 | +``` |
| 69 | + |
| 70 | +## Configuration |
| 71 | + |
| 72 | +The generator uses these settings in `generate.py`: |
| 73 | + |
| 74 | +| Variable | Description | |
| 75 | +|----------|-------------| |
| 76 | +| `BACKEND_APP_IMPORT` | Python module path to FastAPI app (`app.main`) | |
| 77 | +| `OUTPUT_SCHEMA` | Path for generated OpenAPI JSON | |
| 78 | +| `CLIENT_OUTPUT_DIR` | Output directory for TypeScript client | |
| 79 | + |
| 80 | +### OpenAPI Generator Options |
| 81 | + |
| 82 | +The Docker command includes these options: |
| 83 | + |
| 84 | +- `-g typescript-axios` - Generate TypeScript client using Axios |
| 85 | +- `-t /local/openapi/templates/typescript-axios` - Use custom templates |
| 86 | +- `--skip-validate-spec` - Skip OpenAPI spec validation |
| 87 | +- `supportsES6=true` - Generate ES6 compatible code |
| 88 | +- `withSeparateModelsAndApi=true` - Separate models and API files |
| 89 | +- `apiPackage=apis` - Place API files in `apis/` subdirectory |
| 90 | +- `modelPackage=models` - Place model files in `models/` subdirectory |
| 91 | +- `--global-property=apiDocs=false,modelDocs=false,apiTests=false,modelTests=false` - Disable generation of API docs, model docs, and test files |
| 92 | + |
| 93 | +## Custom Templates |
| 94 | + |
| 95 | +The `templates/typescript-axios/baseApi.mustache` template customizes the base API class to: |
| 96 | + |
| 97 | +- Use Vite's environment variable `VITE_API_BASE_URL` for the API base path |
| 98 | +- Configure the default Axios instance |
| 99 | + |
| 100 | +## Environment Variables |
| 101 | + |
| 102 | +The generated client expects the following environment variable in the frontend: |
| 103 | + |
| 104 | +```env |
| 105 | +VITE_API_BASE_URL=http://localhost:8000 |
| 106 | +``` |
| 107 | + |
| 108 | +## Troubleshooting |
| 109 | + |
| 110 | +### Docker Permission Issues |
| 111 | + |
| 112 | +If you encounter Docker permission errors, ensure your user is in the `docker` group: |
| 113 | + |
| 114 | +```bash |
| 115 | +sudo usermod -aG docker $USER |
| 116 | +``` |
| 117 | + |
| 118 | +### Module Import Errors |
| 119 | + |
| 120 | +If the FastAPI app fails to import, ensure: |
| 121 | + |
| 122 | +1. You're running from the project root directory |
| 123 | +2. Backend dependencies are installed (`pip install -r backend/requirements.txt`) |
| 124 | +3. The `backend/dev.env` file exists with required environment variables |
0 commit comments