at://Press
at main 61 lines 1.8 kB view raw
1import type { APIRoute } from "astro"; 2import { checkOrigin, checkAuth, parseJsonBody, createPdsSession } from "../../lib/api"; 3import { isValidRkey, invalidateEntry } from "../../lib/pds"; 4import { getDraft, deleteDraft } from "../../lib/drafts"; 5import { PDS_URL, DID, BLOG_COLLECTION } from "../../lib/constants"; 6 7export const POST: APIRoute = async ({ request, cookies }) => { 8 const originErr = checkOrigin(request); 9 if (originErr) return originErr; 10 11 const authErr = checkAuth(cookies); 12 if (authErr) return authErr; 13 14 const [body, parseErr] = await parseJsonBody(request); 15 if (parseErr) return parseErr; 16 17 const rkey = typeof body.rkey === "string" ? body.rkey : ""; 18 19 if (!rkey || !isValidRkey(rkey)) { 20 return new Response(JSON.stringify({ error: "Invalid rkey" }), { status: 400 }); 21 } 22 23 // Check if this is a local draft 24 if (getDraft(rkey)) { 25 deleteDraft(rkey); 26 return new Response(JSON.stringify({ success: true })); 27 } 28 29 // Otherwise delete from PDS 30 const [accessJwt, sessionErr] = await createPdsSession(); 31 if (sessionErr) return sessionErr; 32 33 const deleteRes = await fetch( 34 `${PDS_URL}/xrpc/com.atproto.repo.deleteRecord`, 35 { 36 method: "POST", 37 headers: { 38 "Content-Type": "application/json", 39 Authorization: `Bearer ${accessJwt}`, 40 }, 41 body: JSON.stringify({ 42 repo: DID, 43 collection: BLOG_COLLECTION, 44 rkey, 45 }), 46 } 47 ); 48 49 if (!deleteRes.ok) { 50 const err = await deleteRes.text(); 51 console.error("PDS deleteRecord failed:", err); 52 return new Response( 53 JSON.stringify({ error: "Failed to delete entry" }), 54 { status: 500 } 55 ); 56 } 57 58 invalidateEntry(rkey); 59 60 return new Response(JSON.stringify({ success: true })); 61};