Fix admin DELETE requests empty JSON body error
This commit is contained in:
+11
-5
@@ -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 = {
|
||||
|
||||
Reference in New Issue
Block a user