fix: customFetch returns parsed body, not Response object

This commit is contained in:
root
2026-07-29 23:38:39 +02:00
parent 331bdd0957
commit deabe36727
+9 -19
View File
@@ -92,8 +92,7 @@ export default function ToolDetail() {
useEffect(() => { useEffect(() => {
if (!id) return; if (!id) return;
setSimilarLoading(true); setSimilarLoading(true);
customFetch(`/api/tools/${id}/similar`) customFetch<any>(`/api/tools/${id}/similar`)
.then((r) => r.json())
.then((data) => setSimilarData(data)) .then((data) => setSimilarData(data))
.catch(() => {}) .catch(() => {})
.finally(() => setSimilarLoading(false)); .finally(() => setSimilarLoading(false));
@@ -102,30 +101,26 @@ export default function ToolDetail() {
async function handleCreateRelation() { async function handleCreateRelation() {
if (!linkToolId) return; if (!linkToolId) return;
try { try {
const res = await customFetch(`/api/tools/${id}/relations`, { await customFetch(`/api/tools/${id}/relations`, {
method: "POST", method: "POST",
body: JSON.stringify({ relatedToolId: Number(linkToolId), relationType: linkType, notes: linkNotes || undefined }), body: JSON.stringify({ relatedToolId: Number(linkToolId), relationType: linkType, notes: linkNotes || undefined }),
}); });
if (!res.ok) { const e = await res.json(); throw new Error(e.error); }
toast({ title: "Relation created" }); toast({ title: "Relation created" });
setLinkDialogOpen(false); setLinkDialogOpen(false);
setLinkToolId(""); setLinkToolId("");
setLinkNotes(""); setLinkNotes("");
setLinkType("similar"); setLinkType("similar");
const r = await customFetch(`/api/tools/${id}/similar`).then((r2) => r2.json()); setSimilarData(await customFetch<any>(`/api/tools/${id}/similar`));
setSimilarData(r);
} catch (err: any) { } catch (err: any) {
toast({ title: "Failed to create relation", description: err.message, variant: "destructive" }); toast({ title: "Failed to create relation", description: err.data?.error ?? err.message, variant: "destructive" });
} }
} }
async function handleDeleteRelation(relationId: number) { async function handleDeleteRelation(relationId: number) {
try { try {
const res = await customFetch(`/api/tools/relations/${relationId}`, { method: "DELETE" }); await customFetch(`/api/tools/relations/${relationId}`, { method: "DELETE" });
if (!res.ok) throw new Error("Failed to delete");
toast({ title: "Relation deleted" }); toast({ title: "Relation deleted" });
const r = await customFetch(`/api/tools/${id}/similar`).then((r2) => r2.json()); setSimilarData(await customFetch<any>(`/api/tools/${id}/similar`));
setSimilarData(r);
} catch { } catch {
toast({ title: "Failed to delete relation", variant: "destructive" }); toast({ title: "Failed to delete relation", variant: "destructive" });
} }
@@ -151,17 +146,13 @@ export default function ToolDetail() {
if (costAmount) body.cost = costAmount; if (costAmount) body.cost = costAmount;
if (costRenewal) body.renewalDate = costRenewal; if (costRenewal) body.renewalDate = costRenewal;
try { try {
const res = await customFetch(url, { await customFetch(url, { method, body: JSON.stringify(body) });
method,
body: JSON.stringify(body),
});
if (!res.ok) { const e = await res.json(); throw new Error(e.error); }
toast({ title: editCost ? "Cost updated" : "Cost added" }); toast({ title: editCost ? "Cost updated" : "Cost added" });
setCostDialogOpen(false); setCostDialogOpen(false);
resetCostForm(); resetCostForm();
setCosts(await customFetch<any[]>(`/api/tools/${id}/costs`)); setCosts(await customFetch<any[]>(`/api/tools/${id}/costs`));
} catch (err: any) { } catch (err: any) {
toast({ title: "Failed to save cost", description: err.message, variant: "destructive" }); toast({ title: "Failed to save cost", description: err.data?.error ?? err.message, variant: "destructive" });
} }
} }
@@ -186,8 +177,7 @@ export default function ToolDetail() {
async function handleDeleteCost(costId: number) { async function handleDeleteCost(costId: number) {
try { try {
const res = await customFetch(`/api/costs/${costId}`, { method: "DELETE" }); await customFetch(`/api/costs/${costId}`, { method: "DELETE" });
if (!res.ok) throw new Error("Failed to delete");
toast({ title: "Cost deleted" }); toast({ title: "Cost deleted" });
setCosts(await customFetch<any[]>(`/api/tools/${id}/costs`)); setCosts(await customFetch<any[]>(`/api/tools/${id}/costs`));
} catch { } catch {