From 3381326dec32a02a01ebbcc01ce8564d0c2995c3 Mon Sep 17 00:00:00 2001 From: g-but <41178744+g-but@users.noreply.github.com> Date: Sun, 26 Apr 2026 21:34:49 +0200 Subject: [PATCH] refactor(api): two GET handlers route through readIdParam, clients check res.ok MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The GET handlers in /api/people/[id] and /api/projects/[id] were the last two [id] routes still running their own inline isValidUuid check instead of going through readIdParam (which every PATCH/DELETE in the same files already used). Routed both through the helper. Side effect: bad UUIDs now return `{error: "Invalid id"}` instead of a null body. The two clients that consume these routes (PersonDetail, ProjectDetail) used to rely on null-throws (`d.interactions` blowing up on a null response) to fall through to .catch(). With a non-null error body, that throw doesn't happen and the UI crashes at render time. Hardened both clients to check `res.ok` before parsing — same pattern useFetch already uses, and now resilient to whatever shape the server returns on non-2xx. 404s still return null body (the only client paths consuming these handlers expect "no result" → null on the wire). Co-Authored-By: Claude Opus 4.7 (1M context) --- src/app/api/people/[id]/route.ts | 15 ++++----------- src/app/api/projects/[id]/route.ts | 6 +++--- src/components/people/PersonDetail.tsx | 5 ++++- src/components/projects/ProjectDetail.tsx | 5 ++++- 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/app/api/people/[id]/route.ts b/src/app/api/people/[id]/route.ts index 769ce316d..2e884c3c4 100644 --- a/src/app/api/people/[id]/route.ts +++ b/src/app/api/people/[id]/route.ts @@ -1,6 +1,5 @@ import { NextRequest, NextResponse } from "next/server"; import { getPersonDetail } from "@/db/queries/people"; -import { isValidUuid } from "@/lib/utils"; import { readIdParam, readJsonBody, z } from "@/lib/api/route-helpers"; import { db } from "@/db"; import { entities } from "@/db/schema"; @@ -67,17 +66,11 @@ export async function GET( _request: Request, { params }: { params: Promise<{ id: string }> }, ) { - const { id } = await params; + const idOrResp = await readIdParam(params); + if (idOrResp instanceof NextResponse) return idOrResp; - if (!isValidUuid(id)) { - return NextResponse.json(null, { status: 400 }); - } - - const person = await getPersonDetail(id); - - if (!person) { - return NextResponse.json(null, { status: 404 }); - } + const person = await getPersonDetail(idOrResp); + if (!person) return NextResponse.json(null, { status: 404 }); return NextResponse.json(person); } diff --git a/src/app/api/projects/[id]/route.ts b/src/app/api/projects/[id]/route.ts index 009999de3..d89cb55fb 100644 --- a/src/app/api/projects/[id]/route.ts +++ b/src/app/api/projects/[id]/route.ts @@ -4,7 +4,6 @@ import { db } from "@/db"; import { entities, entityRelations, interactions, goals } from "@/db/schema"; import { eq, and, desc, inArray } from "drizzle-orm"; import { fetchAttributesByEntityIds } from "@/db/queries/utils"; -import { isValidUuid } from "@/lib/utils"; import { readIdParam, readJsonBody, z } from "@/lib/api/route-helpers"; import { readCronJobs } from "@/lib/crons"; @@ -91,8 +90,9 @@ export async function GET( _req: Request, { params }: { params: Promise<{ id: string }> }, ) { - const { id } = await params; - if (!isValidUuid(id)) return NextResponse.json(null, { status: 400 }); + const idOrResp = await readIdParam(params); + if (idOrResp instanceof NextResponse) return idOrResp; + const id = idOrResp; const [project] = await db .select() diff --git a/src/components/people/PersonDetail.tsx b/src/components/people/PersonDetail.tsx index 28868b876..404429d35 100644 --- a/src/components/people/PersonDetail.tsx +++ b/src/components/people/PersonDetail.tsx @@ -35,7 +35,10 @@ export function PersonDetail({ useEffect(() => { let cancelled = false; fetch(`/api/people/${personId}`) - .then((res) => res.json()) + .then((res) => { + if (!res.ok) throw new Error(`HTTP ${res.status}`); + return res.json(); + }) .then((d: PersonDetailData) => { if (cancelled) return; setData(d); setInteractions(d.interactions); setDescription(d.description); setAttrs(d.attrs); setName(d.name); diff --git a/src/components/projects/ProjectDetail.tsx b/src/components/projects/ProjectDetail.tsx index aced94341..3f5a9457c 100644 --- a/src/components/projects/ProjectDetail.tsx +++ b/src/components/projects/ProjectDetail.tsx @@ -32,7 +32,10 @@ export function ProjectDetail({ // react-hooks/set-state-in-effect cascading-render warning. Promise.resolve().then(() => { if (!cancelled) setRefetching(true); }); fetch(`/api/projects/${projectId}`) - .then((r) => r.json()) + .then((r) => { + if (!r.ok) throw new Error(`HTTP ${r.status}`); + return r.json(); + }) .then((d: ProjectData) => { if (cancelled) return; setData(d);