My personal site hosted @ https://indexx.dev
1import type { APIRoute } from "astro"; 2import NodeCache from "node-cache"; 3 4const cache = new NodeCache({ stdTTL: 300 }); // cache for 5 minutes 5const actorDid = "did:plc:zl4ugdpxfgrzlr5uz2nm7kcq"; 6 7type FeedResponse = { 8 feed: { 9 post: { 10 uri: string; 11 cid: string; 12 author: { 13 did: string; 14 handle: string; 15 displayName: string; 16 avatar: string; 17 labels: any[]; 18 createdAt: string; 19 }; 20 record: { 21 $type: string; 22 createdAt: string; 23 text: string; 24 }; 25 replyCount: number; 26 repostCount: number; 27 likeCount: number; 28 quoteCount: number; 29 indexedAt: string; 30 labels: any[]; 31 }; 32 }[]; 33 cursor: string; 34}; 35 36export const GET: APIRoute = async () => { 37 let status = cache.get("status"); 38 39 if (status) { 40 return new Response(JSON.stringify(status), { 41 headers: { "Content-Type": "application/json", "X-Cache": "HIT" }, 42 }); 43 } 44 45 try { 46 const res = await fetch( 47 `https://public.api.bsky.app/xrpc/app.bsky.feed.getAuthorFeed?actor=${actorDid}&limit=1&filter=posts_no_replies`, 48 ); 49 if (!res.ok) throw new Error(`HTTP ${res.status}`); 50 51 const json: FeedResponse = await res.json(); 52 const latestPost = json.feed[0].post; 53 status = { 54 text: latestPost.record.text, 55 createdAt: latestPost.record.createdAt, 56 link: `https://bsky.app/profile/${actorDid}/post/${ 57 latestPost.uri.split("/")[4] 58 }`, 59 }; 60 61 cache.set("status", status); 62 63 return new Response(JSON.stringify(status), { 64 headers: { "Content-Type": "application/json", "X-Cache": "MISS" }, 65 }); 66 } catch (err) { 67 return new Response( 68 JSON.stringify({ error: "Failed to fetch status" }), 69 { 70 status: 500, 71 headers: { "Content-Type": "application/json" }, 72 }, 73 ); 74 } 75};