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(() => {
if (!id) return;
setSimilarLoading(true);
customFetch(`/api/tools/${id}/similar`)
.then((r) => r.json())
customFetch<any>(`/api/tools/${id}/similar`)
.then((data) => setSimilarData(data))
.catch(() => {})
.finally(() => setSimilarLoading(false));
@@ -102,30 +101,26 @@ export default function ToolDetail() {
async function handleCreateRelation() {
if (!linkToolId) return;
try {
const res = await customFetch(`/api/tools/${id}/relations`, {
await customFetch(`/api/tools/${id}/relations`, {
method: "POST",
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" });
setLinkDialogOpen(false);
setLinkToolId("");
setLinkNotes("");
setLinkType("similar");
const r = await customFetch(`/api/tools/${id}/similar`).then((r2) => r2.json());
setSimilarData(r);
setSimilarData(await customFetch<any>(`/api/tools/${id}/similar`));
} 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) {
try {
const res = await customFetch(`/api/tools/relations/${relationId}`, { method: "DELETE" });
if (!res.ok) throw new Error("Failed to delete");
await customFetch(`/api/tools/relations/${relationId}`, { method: "DELETE" });
toast({ title: "Relation deleted" });
const r = await customFetch(`/api/tools/${id}/similar`).then((r2) => r2.json());
setSimilarData(r);
setSimilarData(await customFetch<any>(`/api/tools/${id}/similar`));
} catch {
toast({ title: "Failed to delete relation", variant: "destructive" });
}
@@ -151,17 +146,13 @@ export default function ToolDetail() {
if (costAmount) body.cost = costAmount;
if (costRenewal) body.renewalDate = costRenewal;
try {
const res = await customFetch(url, {
method,
body: JSON.stringify(body),
});
if (!res.ok) { const e = await res.json(); throw new Error(e.error); }
await customFetch(url, { method, body: JSON.stringify(body) });
toast({ title: editCost ? "Cost updated" : "Cost added" });
setCostDialogOpen(false);
resetCostForm();
setCosts(await customFetch<any[]>(`/api/tools/${id}/costs`));
} 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) {
try {
const res = await customFetch(`/api/costs/${costId}`, { method: "DELETE" });
if (!res.ok) throw new Error("Failed to delete");
await customFetch(`/api/costs/${costId}`, { method: "DELETE" });
toast({ title: "Cost deleted" });
setCosts(await customFetch<any[]>(`/api/tools/${id}/costs`));
} catch {