a tool for shared writing and social publishing
1import { lexToJson } from "@atproto/api";
2import { NextRequest } from "next/server";
3import { getAgent } from "../agent";
4
5export const runtime = "nodejs";
6
7export async function GET(req: NextRequest) {
8 try {
9 const searchParams = req.nextUrl.searchParams;
10 const uri = searchParams.get("uri");
11 const depth = searchParams.get("depth");
12 const parentHeight = searchParams.get("parentHeight");
13
14 if (!uri) {
15 return Response.json(
16 { error: "uri parameter is required" },
17 { status: 400 },
18 );
19 }
20
21 const agent = await getAgent();
22
23 const response = await agent.getPostThread({
24 uri,
25 depth: depth ? parseInt(depth, 10) : 6,
26 parentHeight: parentHeight ? parseInt(parentHeight, 10) : 80,
27 });
28
29 const thread = lexToJson(response.data.thread);
30
31 return Response.json(thread, {
32 headers: {
33 // Cache for 5 minutes on CDN, allow stale content for 1 hour while revalidating
34 "Cache-Control": "public, s-maxage=300, stale-while-revalidate=3600",
35 },
36 });
37 } catch (error) {
38 console.error("Error fetching Bluesky thread:", error);
39 return Response.json({ error: "Failed to fetch thread" }, { status: 500 });
40 }
41}