API client

An API client is a front-end module that centralizes calls to the server: base URL, headers, JSON conversion, HTTP error handling. Components do not call fetch directly, which avoids duplication and makes changes and testing easier.

Example

export const api = (p) => fetch(`/api${p}`).then(r => { if (!r.ok) throw new Error(r.status); return r.json(); });

Related concepts

The whole glossary