Skip to content
Source
Sign inSign up
samwillis/hackernews-realtime
Open History to see the last commit

route.ts

31 lines1.0 KB

1import { parseStoryId } from "@/lib/comments";
2import { watchThread } from "@/lib/thread-watch";
3export const dynamic = "force-dynamic";
4export async function POST(request: Request) {
5 let id: number;
6 const params = new URL(request.url).searchParams;
7 const viewer = params.get("viewer") ?? undefined;
8 if (
9 viewer &&
10 !/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(
11 viewer,
12 )
13 )
14 return Response.json({ error: "Invalid viewer ID" }, { status: 400 });
15 try {
16 id = parseStoryId(new URL(request.url).searchParams.get("id"));
17 } catch {
18 return Response.json({ error: "Invalid story ID" }, { status: 400 });
19 }
20 try {
21 const health = await watchThread(id, viewer, params.get("release") === "1");
22 if (!health)
23 return Response.json({ error: "Story not found" }, { status: 404 });
24 return Response.json(health, { headers: { "Cache-Control": "no-store" } });
25 } catch {
26 return Response.json(
27 { state: "degraded", checkedAt: null, committedAt: null },
28 { status: 503 },
29 );
30 }