From 02cb6dda5ed7e24b0d29980cbefa21cb34228bf1 Mon Sep 17 00:00:00 2001 From: multipleof4 Date: Mon, 24 Nov 2025 13:26:55 -0800 Subject: [PATCH] Feat: Add newsletter subscription endpoint --- functions/api/subscribe.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 functions/api/subscribe.js diff --git a/functions/api/subscribe.js b/functions/api/subscribe.js new file mode 100644 index 0000000..d0df433 --- /dev/null +++ b/functions/api/subscribe.js @@ -0,0 +1,15 @@ +export async function onRequestPost(ctx) { + const { request, env } = ctx; + const { email } = await request.json().catch(() => ({})); + if (!email || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) { + return new Response(JSON.stringify({ error: 'Invalid email' }), { status: 400, headers: { 'Content-Type': 'application/json' } }); + } + const cf = request.cf || {}; + const geo = [cf.city, cf.region, cf.country].filter(Boolean).join(', ') || 'Unknown'; + await fetch(`https://ntfy.sh/${env.NTFY_TOPIC}`, { + method: 'POST', + headers: { Priority: '3', Title: `New sub: ${geo}` }, + body: email + }); + return new Response(JSON.stringify({ ok: true }), { headers: { 'Content-Type': 'application/json' } }); +}