import { NextResponse } from 'next/server'; import { Resend } from 'resend'; export const runtime = 'edge'; type ContactPayload = { name?: string; company?: string; service?: string; budget?: string; message?: string; locale?: 'fa' | 'en'; }; const required = ['name', 'service', 'budget', 'message'] as const; function escape(str: string) { return str .replace(/&/g, '&') .replace(//g, '>') .replace(/"/g, '"'); } export async function POST(req: Request) { let body: ContactPayload; try { body = (await req.json()) as ContactPayload; } catch { return NextResponse.json({ error: 'Invalid JSON' }, { status: 400 }); } for (const k of required) { if (!body[k] || String(body[k]).trim().length < 2) { return NextResponse.json( { error: `Missing field: ${k}` }, { status: 422 }, ); } } const apiKey = process.env.RESEND_API_KEY; const inbox = process.env.CONTACT_INBOX; const from = process.env.CONTACT_FROM; // Graceful no-op for local dev so the form UX can be validated // without forcing a Resend key. Production should set these. if (!apiKey || !inbox || !from) { if (process.env.NODE_ENV === 'production') { return NextResponse.json( { error: 'Email service is not configured' }, { status: 500 }, ); } console.info('[contact] received (no Resend key — logging only):', body); return NextResponse.json({ ok: true, dev: true }); } const resend = new Resend(apiKey); const subject = `New consultation request — ${body.name}`; const html = `
| Name | ${escape(body.name!)} |
| Company | ${escape(body.company ?? '')} |
| Service | ${escape(body.service!)} |
| Budget | ${escape(body.budget!)} |
| Locale | ${escape(body.locale ?? '')} |
${escape(body.message!)}