mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 10:27:56 +01:00
refactor(api): two GET handlers route through readIdParam, clients check res.ok
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) <noreply@anthropic.com>
This commit is contained in:
4 files changed
+15
-16
No files matched your search
@@ -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);
|
||||
}
|
||||
@@ -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()
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user