Skip to content

API Integration & Data Fetching

Independent WebbiOS Themes consume public data strictly over versioned HTTPS endpoints exposed by The Kernel API (Single API Entry Point).

  1. Use an Adapter Layer: Isolate API URLs, response parsing, and error handling inside a dedicated client module (e.g., src/lib/api-adapter.ts).
  2. Never Fake Success: When an upstream API is unreachable or returns an error, surface an explicit, honest degraded/error state (HTTP 503 or clear UI notice) rather than silently fabricating success responses.
  3. Sanitize Rendered HTML: Any rich HTML content fetched from an API (such as documentation or CMS articles) must be sanitized with a strict allowlist before injecting into the DOM to prevent XSS.
  4. Separate Dev Fixtures from Production: Local/preview fixtures are useful before backend endpoints are ready, but must be opt-in, visibly labeled with a preview banner, and blocked in production builds.
const API_BASE = import.meta.env.PUBLIC_WEBBIOS_API_BASE;
export async function fetchPublicResource<T>(path: string): Promise<{ ok: true; data: T } | { ok: false; status: number }> {
if (!API_BASE) {
return { ok: false, status: 503 };
}
const res = await fetch(`${API_BASE.replace(/\/$/, '')}${path}`, {
headers: { Accept: 'application/json' },
});
if (!res.ok) {
return { ok: false, status: res.status };
}
const payload = (await res.json()) as { success?: boolean; data?: T };
if (!payload.success || payload.data === undefined) {
return { ok: false, status: 502 };
}
return { ok: true, data: payload.data };
}