import type { APIRoute } from "astro"; import NodeCache from "node-cache"; const cache = new NodeCache({ stdTTL: 300 }); // cache for 5 minutes const actorDid = "did:plc:zl4ugdpxfgrzlr5uz2nm7kcq"; type FeedResponse = { feed: { post: { uri: string; cid: string; author: { did: string; handle: string; displayName: string; avatar: string; labels: any[]; createdAt: string; }; record: { $type: string; createdAt: string; text: string; }; replyCount: number; repostCount: number; likeCount: number; quoteCount: number; indexedAt: string; labels: any[]; }; }[]; cursor: string; }; export const GET: APIRoute = async () => { let status = cache.get("status"); if (status) { return new Response(JSON.stringify(status), { headers: { "Content-Type": "application/json", "X-Cache": "HIT" }, }); } try { const res = await fetch( `https://public.api.bsky.app/xrpc/app.bsky.feed.getAuthorFeed?actor=${actorDid}&limit=1&filter=posts_no_replies`, ); if (!res.ok) throw new Error(`HTTP ${res.status}`); const json: FeedResponse = await res.json(); const latestPost = json.feed[0].post; status = { text: latestPost.record.text, createdAt: latestPost.record.createdAt, link: `https://bsky.app/profile/${actorDid}/post/${ latestPost.uri.split("/")[4] }`, }; cache.set("status", status); return new Response(JSON.stringify(status), { headers: { "Content-Type": "application/json", "X-Cache": "MISS" }, }); } catch (err) { return new Response( JSON.stringify({ error: "Failed to fetch status" }), { status: 500, headers: { "Content-Type": "application/json" }, }, ); } };