A live REST backend for the fleet-mobility
React demo. It implements the exact Vehicles contract the front end already
expects, so the app flips from mock in-memory data to a real API without any
UI changes — you only swap one file (src/api.ts).
Built with FastAPI + Pydantic + Uvicorn.
- Interactive docs out of the box. Visit
/docsfor a Swagger UI where you can fire every endpoint live from the browser — great in front of an audience. - Automatic validation. The Pydantic models reproduce the front end's
TypeScript enums, so a bad
statusorfuelTypeis rejected with a clear 422 before it ever touches your data. - The "wow" moment: start with the React app on mock data, swap
api.ts, reload, and the same dashboard is now served over HTTP — create a vehicle in the UI and watch thePOST /vehiclesland in the server log.
| React call | HTTP |
|---|---|
fleetApi.list() |
GET /vehicles |
fleetApi.create() |
POST /vehicles |
fleetApi.update() |
PATCH /vehicles/{id} |
fleetApi.remove() |
DELETE /vehicles/{id} |
Data is in-memory and seeded from the same eight vehicles the React app shipped with, so the dashboard looks identical. A server restart resets the fleet — convenient for repeated run-throughs.
pip install -r requirements.txt
uvicorn app.main:app --reload --port 8000- API root: http://localhost:8000
- Swagger UI: http://localhost:8000/docs
-
Copy
api.ts(in this repo's root) oversrc/api.tsin the fleet-mobility project. The method signatures are identical, so nothing else changes. -
In the fleet-mobility project root, add a
.envfile:VITE_API_URL=http://localhost:8000 -
Start the backend (above), then
npm run devas usual. The Vehicles module now loads from the live API; the "Simulate outage" toggle still exercises the error/retry UI.
app/
main.py # FastAPI app + the four CRUD routes
models.py # Pydantic schemas mirroring src/types.ts
seed.py # initial fleet, ported from src/data.ts
requirements.txt
api.ts # drop-in replacement for the React app's src/api.ts