import type { $Fetch } from "nitropack"; /** * Base API client composable. * Uses server-side apiBase for SSR requests, public apiBase for client-side. */ export function useApi() { const config = useRuntimeConfig(); // On server: config.apiBase (http://backend:8000) // On client: config.public.apiBase (/api) const apiBase = import.meta.server ? config.apiBase : config.public.apiBase; const client: $Fetch = $fetch.create({ baseURL: apiBase, headers: { "Content-Type": "application/json", }, }); async function get(url: string, params?: Record): Promise { return client(url, { method: "GET", params: params as Record | undefined, }); } async function post(url: string, body?: unknown): Promise { return client(url, { method: "POST", body, }); } async function put(url: string, body?: unknown): Promise { return client(url, { method: "PUT", body, }); } async function del(url: string): Promise { return client(url, { method: "DELETE", }); } return { apiBase, client, get, post, put, del, }; }