Facade
A facade is a thin, friendly API in front of a messy subsystem. Callers use one function or object; the facade talks to fetch, storage, and validation for them.
Detailed Explanation
Libraries and browsers expose many moving parts: fetch, headers, JSON parse, error codes, retries, tokens. A page that repeats that in every screen will drift. A facade is api.getUser(id) that does the dance once.
export async function getUser(id) {
const res = await fetch(`/api/users/${id}`, { headers: auth() });
if (!res.ok) throw new ApiError(res.status);
return res.json();
}
The facade does not add a new feature. It hides order of operations and default arguments. It is not a proxy (it does not pretend to be fetch). It is not a decorator (it does not wrap an existing getUser). It is a new, smaller surface.
Good facades stay small. When getUser grows flags for every query param, you have leaked the subsystem again. Split methods (listUsers({ q })) or accept one options object that you document.
On the server, a facade around fs, path, and crypto is how you test: the app imports ./files and tests replace that module. The same idea is a ports-and-adapters / hexagonal port.
What it costs you: an extra layer to open when debugging, and facades that lie (they hide errors you needed). Catching all exceptions and returning null is a facade that makes bugs silent.
Reach for a facade when three or more collaborators are used together in the same way. Reach for a decorator when one collaborator needs extra policy. Reach for middleware when many requests share a pipeline.
Version the facade when the subsystem changes shape. getUser that used to return { email } and now returns a paginated envelope is a breaking change even if fetch still works. Keep a thin mapper so old callers can stay on getUser while new ones call listUsersPage.
A facade is also a metrics seam: one api.latency histogram covers every screen that uses it. That is harder when each screen talks to fetch directly.
Code Examples
function createApi(base, token) {
async function request(path, options = {}) {
const res = await fetch(`${base}${path}`, {
...options,
headers: {
Accept: "application/json",
Authorization: `Bearer ${token}`,
...options.headers,
},
});
if (!res.ok) {
const text = await res.text();
throw new Error(`${res.status} ${text}`);
}
if (res.status === 204) return null;
return res.json();
}
return {
getUser: (id) => request(`/users/${id}`),
listUsers: () => request("/users"),
};
}
const api = createApi("https://example.com", "tok");
console.log(typeof api.getUser); // => functionFacade over several node APIs
// Facade over several node APIs
const fs = require("node:fs/promises");
const path = require("node:path");
async function readJson(dir, name) {
const text = await fs.readFile(path.join(dir, name), "utf8");
return JSON.parse(text);
}Common Gotchas
Swallowing errors is not simplicity
wrap, add context, rethrow. Facades that return `null` on every failure hide outages.
A facade that exposes every option of the inner API is not a facade
it is an alias.
One facade per subsystem beats a god `services` object
split when unrelated teams own the backends.
Tests should mock the facade or inject `fetch`, not both at random
pick a seam.