export const prerender = false; import type { APIRoute } from 'astro'; import * as fs from 'fs'; import * as path from 'path'; const SUBS_FILE = path.resolve('/Users/ace/palacering/palaces/manglasabang/secretariat/keychain/push-subscriptions.json'); export const POST: APIRoute = async ({ request }) => { try { const body = await request.json(); if (!body.endpoint) return new Response(JSON.stringify({ error: 'Missing endpoint' }), { status: 400, headers: { 'Content-Type': 'application/json' } }); let subs: object[] = []; if (fs.existsSync(SUBS_FILE)) { try { subs = JSON.parse(fs.readFileSync(SUBS_FILE, 'utf8')); } catch {} } const idx = subs.findIndex((s: any) => s.endpoint === body.endpoint); if (idx >= 0) subs[idx] = body; else subs.push(body); fs.writeFileSync(SUBS_FILE, JSON.stringify(subs, null, 2)); return new Response(JSON.stringify({ ok: true }), { headers: { 'Content-Type': 'application/json' } }); } catch (e: any) { return new Response(JSON.stringify({ error: e.message }), { status: 500, headers: { 'Content-Type': 'application/json' } }); } }; export const DELETE: APIRoute = async ({ request }) => { try { const body = await request.json(); if (!fs.existsSync(SUBS_FILE)) return new Response(JSON.stringify({ ok: true }), { headers: { 'Content-Type': 'application/json' } }); let subs: object[] = JSON.parse(fs.readFileSync(SUBS_FILE, 'utf8')); subs = subs.filter((s: any) => s.endpoint !== body.endpoint); fs.writeFileSync(SUBS_FILE, JSON.stringify(subs, null, 2)); return new Response(JSON.stringify({ ok: true }), { headers: { 'Content-Type': 'application/json' } }); } catch (e: any) { return new Response(JSON.stringify({ error: e.message }), { status: 500, headers: { 'Content-Type': 'application/json' } }); } };