From 222a2e9d5b354c6433ea939f09a87bea0670cbc1 Mon Sep 17 00:00:00 2001 From: multipleof4 Date: Fri, 5 Dec 2025 15:02:02 -0800 Subject: [PATCH] Refactor: Replace ntfy.sh with Resend API for contact creation --- functions/api/subscribe.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/functions/api/subscribe.js b/functions/api/subscribe.js index 71e553e..1e1cfcd 100644 --- a/functions/api/subscribe.js +++ b/functions/api/subscribe.js @@ -3,14 +3,19 @@ export async function onRequestPost({ request, env }) { if (!email || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) return new Response('{"error":"Invalid email"}', { status: 400, headers: { 'Content-Type': 'application/json' } }); - const c = request.cf || {}; - const geo = [c.city, c.region, c.country].filter(Boolean).join(', ') || 'Unknown'; - - await fetch(`https://ntfy.sh/${env.NTFY_TOPIC}`, { + const res = await fetch('https://api.resend.com/contacts', { method: 'POST', - headers: { Priority: '3', Title: `📬 ${geo}` }, - body: email + headers: { + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${env.RESEND_KEY}` + }, + body: JSON.stringify({ email }) }); - + + if (!res.ok) { + const text = await res.text(); + return new Response(`{"error":"Resend error: ${text}"}`, { status: 500, headers: { 'Content-Type': 'application/json' } }); + } + return new Response('{"ok":true}', { headers: { 'Content-Type': 'application/json' } }); }