Files
ThothII/frontend/src/api/client.ts
T

19 lines
876 B
TypeScript

import { backendBaseUrl as BASE, joinBackendPath } from "./runtime-config";
export async function apiFetch<T>(path: string, init?: RequestInit): Promise<T> {
// Only declare a JSON content-type when we actually send a body. Body-less
// POSTs (resume, close) would otherwise make Fastify reject the empty body
// with FST_ERR_CTP_EMPTY_JSON_BODY (400).
const headers: Record<string, string> = {
...(init?.headers as Record<string, string> | undefined),
};
if (init?.body != null && !("content-type" in headers) && !("Content-Type" in headers)) {
headers["content-type"] = "application/json";
}
const res = await fetch(joinBackendPath(BASE, path), { ...init, headers });
if (!res.ok) throw new Error(`${res.status} ${await res.text().catch(() => "")}`);
return res.status === 204 ? (undefined as T) : ((await res.json()) as T);
}
export { BASE };