{"success":true,"name":"one-api-client-for-the-whole-frontend-base-url-auth-and-erro","description":"Frontend screens each call fetch/axios directly with their own URLs and handling","category":"ai-brimind","path":"ai-brimind/one-api-client-for-the-whole-frontend-base-url-auth-and-erro/SKILL.md","file":"SKILL.md","content":"---\nname: one-api-client-for-the-whole-frontend-base-url-auth-and-erro\ndescription: \"Frontend screens each call fetch/axios directly with their own URLs and handling\"\n---\n\n# One API Client for the Whole Frontend: Base URL, Auth, and Errors Live in a Single File\n\n## When to use\nThe frontend talks to a backend, and components are writing their own raw fetch/axios calls — each repeating the base URL, attaching the token its own way, and inventing its own error handling — or you are about to add yet another such call.\n\n## Example request\nThe URL 'http://[PRIVATE_HOST]/api' is hardcoded in fifteen components → the day the port or domain changes, fifteen files change; a single API module reading the base URL from env config changes once. Each screen attaches the auth token by hand → three screens forget, producing random 401s; an interceptor adding the header in ONE place makes forgetting impossible. Every component writes its own catch with its own error text → the same backend failure looks different on every page; the shared client translates the API's error contract once, uniformly. One special call needs a file-upload config → fine, the shared client allows per-call options; centralizing is about defaults in one place, not forbidding exceptions.\n\n## Instructions\nAct as a frontend developer who treats 'how we talk to the backend' as ONE piece of knowledge that must live in ONE file — because every component that hand-rolls its own fetch is copying that knowledge, and copies drift, and drift becomes the bug that only happens on some screens. Before adding any API call, do this: (1) Check whether the project already has a shared API layer (an api.js/apiClient, an axios instance, or a data-fetching setup). If yes, route your call through it — adding another raw fetch beside it is how the second, competing convention is born. If no, create it before your feature, not after: a single module exporting a configured client. (2) Put the four defaults in it, once: the base URL read from environment config (never hardcoded — dev and prod differ by definition); the auth credential attached automatically (an interceptor adding the header, or cookie settings), so no screen can forget it; the default timeout; and the error translation — one function that turns the backend's error contract into something every screen consumes identically, including the global 401 behavior. (3) Make components speak in intentions, not plumbing: expose functions like getOrders(page) or createClub(data) so screens say WHAT they need and the client handles HOW — components with URLs and headers in them are plumbing leaks. (4) Allow deliberate exceptions through options (an upload needing special headers, one long-running call needing a bigger timeout) — passed per call, visible and justified, not by abandoning the client. (5) If the codebase already has scattered raw calls, adopt the rule for everything new and migrate old ones as you touch them — state that plan instead of pretending the mess isn't there. Only proceed once you can answer: 'if the API domain, the auth scheme, or the error format changed tomorrow, how many files change?' — the correct answer is one, and if it is currently twenty, that number is the debt you are choosing.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #211\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}