Skip to content

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 ​

FunctionRequestUsed 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.

EnvironmentAPI_URLSet in
Your machinehttp://localhost:8000web/.env.local
devhttps://api.dev.trinity.jostud.cominfra/docker-compose.dev.yml
prodhttps://api.trinity.jostud.cominfra/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 happensWhat 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_URLError("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.