Fix admin DELETE requests empty JSON body error

This commit is contained in:
2026-07-24 13:40:23 -04:00
parent 8c8e7a4fb5
commit c4ecc934e9
+11 -5
View File
@@ -118,12 +118,15 @@ export interface ListMeta {
}
async function request<T>(path: string, init?: RequestInit): Promise<T> {
const headers = new Headers(init?.headers);
// Only set JSON content-type when we actually send a body (DELETE/GET have none —
// Fastify rejects empty body + application/json).
if (init?.body && !(init.body instanceof FormData) && !headers.has("Content-Type")) {
headers.set("Content-Type", "application/json");
}
const res = await fetch(path, {
...init,
headers: {
...(init?.body instanceof FormData ? {} : { "Content-Type": "application/json" }),
...init?.headers,
},
headers,
});
if (!res.ok) {
let msg = res.statusText;
@@ -136,7 +139,10 @@ async function request<T>(path: string, init?: RequestInit): Promise<T> {
throw new Error(msg || `HTTP ${res.status}`);
}
if (res.status === 204) return undefined as T;
return res.json() as Promise<T>;
// Some DELETEs return empty body
const text = await res.text();
if (!text) return undefined as T;
return JSON.parse(text) as T;
}
export const api = {