Talking to the API
All the dialogue with the API goes through a single file: web/lib/products.js. No component calls fetch itself.
The three functions
| Function | Request | Used by |
|---|---|---|
fetchProducts() | GET /api/products/ | app/page.js |
createProduct(product) | POST /api/products/ | addProduct in app/actions.js |
deleteProduct(id) | DELETE /api/products/{id}/ | removeProduct in app/actions.js |
The endpoints themselves are described on the API side in the API reference.
The API address
The address comes from the API_URL . It is read on every request, not at build time.
| Environment | API_URL | Set in |
|---|---|---|
| Your machine | http://localhost:8000 | web/.env.local |
| dev | https://api.dev.trinity.jostud.com | infra/docker-compose.dev.yml |
| prod | https://api.trinity.jostud.com | infra/docker-compose.prod.yml |
Why read the address at runtime?
A NEXT_PUBLIC_* variable would be baked into the image at build time. You would then need a different image for dev and prod. With API_URL read at runtime, the same image works everywhere, only the variable changes. That is what lets the pipeline build the image once and deploy it as is.
Errors
Every function goes through checkResponse(): if the API answers with an error code (400, 404, 500…), it throws Error("API responded with status 400").
| Where the error happens | What the user sees |
|---|---|
page.js (read) | A red banner "API indisponible : …" instead of the list |
addProduct (add) | The message "Ajout impossible : …" under the form |
removeProduct (delete) | A Next.js error: it is not caught yet, see Limits |
Missing API_URL | Error("API_URL is not defined"), shown as an unavailable API |
Pagination
Today the API returns a plain array. If Django REST Framework pagination is ever enabled, it will return { count, results: [...] }. fetchProducts() already handles both shapes.