(request, env, ctx)
| 9 | |
| 10 | export default { |
| 11 | async fetch(request, env, ctx) { |
| 12 | // Get the request URL and path |
| 13 | const url = new URL(request.url); |
| 14 | const path = url.pathname; |
| 15 | |
| 16 | // Only process requests to /api/feedback |
| 17 | if (path !== '/api/feedback') { |
| 18 | return new Response('Not found', { |
| 19 | status: 404, |
| 20 | headers: corsHeaders |
| 21 | }); |
| 22 | } |
| 23 | |
| 24 | // Handle CORS preflight requests |
| 25 | if (request.method === 'OPTIONS') { |
| 26 | return new Response(null, { |
| 27 | headers: corsHeaders, |
| 28 | }); |
| 29 | } |
| 30 | |
| 31 | // Only allow POST for actual requests |
| 32 | if (request.method !== 'POST') { |
| 33 | return new Response('Method not allowed', { |
| 34 | status: 405, |
| 35 | headers: corsHeaders, |
| 36 | }); |
| 37 | } |
| 38 | |
| 39 | try { |
| 40 | // Parse request body as JSON |
| 41 | const body = await request.json(); |
| 42 | const { message, email, context } = body; |
| 43 | |
| 44 | // Validate input |
| 45 | if (!message || typeof message !== 'string') { |
| 46 | return new Response(JSON.stringify({ error: 'Message is required' }), { |
| 47 | status: 400, |
| 48 | headers: { |
| 49 | 'Content-Type': 'application/json', |
| 50 | ...corsHeaders, |
| 51 | }, |
| 52 | }); |
| 53 | } |
| 54 | |
| 55 | // Initialize Resend with API key from environment |
| 56 | const resend = new Resend(env.RESEND_API_KEY); |
| 57 | |
| 58 | // Get user info |
| 59 | const userEmail = email && typeof email === 'string' ? email : 'Anonymous user'; |
| 60 | const contextInfo = context || 'No context provided'; |
| 61 | const timestamp = new Date().toISOString(); |
| 62 | |
| 63 | // Send email |
| 64 | const { data, error } = await resend.emails.send({ |
| 65 | from: 'DataKit Feedback <feedback@datakit.page>', |
| 66 | to: ['amin@datakit.page', 'luke@datakit.page'], |
| 67 | subject: 'New DataKit Feedback', |
| 68 | html: ` |
no outgoing calls
no test coverage detected