1+ import { VercelRequest , VercelResponse } from "@vercel/node"
12import { Resend } from "resend"
23
34const resend = new Resend ( process . env . RESEND_API_KEY )
45
5- export default async function handler ( req : Request ) {
6+ export default async function handler ( req : VercelRequest , res : VercelResponse ) {
67 if ( req . method !== "POST" ) {
7- return new Response ( JSON . stringify ( { error : "Method not allowed" } ) , {
8- status : 405 ,
9- headers : { "Content-Type" : "application/json" } ,
10- } )
8+ return res . status ( 405 ) . json ( { error : "Method not allowed" } )
119 }
1210
13- try {
14- const { email, first_name, last_name } = await req . json ( )
11+ const { email, first_name, last_name } = req . body
1512
16- if ( ! email ) {
17- return new Response ( JSON . stringify ( { error : "Email is required" } ) , {
18- status : 400 ,
19- headers : { "Content-Type" : "application/json" } ,
20- } )
21- }
13+ if ( ! email ) {
14+ return res . status ( 400 ) . json ( { error : "Email is required" } )
15+ }
16+ if ( ! first_name ) {
17+ return res . status ( 400 ) . json ( { error : "First name is required" } )
18+ }
2219
23- // Validate email format
24- const emailRegex = / ^ [ ^ \s @ ] + @ [ ^ \s @ ] + \. [ ^ \s @ ] + $ /
25- if ( ! emailRegex . test ( email ) ) {
26- return new Response ( JSON . stringify ( { error : "Invalid email format" } ) , {
27- status : 400 ,
28- headers : { "Content-Type" : "application/json" } ,
29- } )
30- }
20+ // Validate email format
21+ const emailRegex = / ^ [ ^ \s @ ] + @ [ ^ \s @ ] + \. [ ^ \s @ ] + $ /
22+ if ( ! emailRegex . test ( email ) ) {
23+ return res . status ( 400 ) . json ( { error : "Invalid email format" } )
24+ }
3125
26+ try {
3227 const audienceId = process . env . RESEND_AUDIENCE_ID
3328 if ( ! audienceId ) {
34- return new Response (
35- JSON . stringify ( { error : "Server configuration error" } ) ,
36- { status : 500 , headers : { "Content-Type" : "application/json" } }
37- )
29+ return res . status ( 500 ) . json ( { error : "Server configuration error" } )
3830 }
3931
4032 // Create contact in Resend
@@ -47,24 +39,17 @@ export default async function handler(req: Request) {
4739
4840 if ( contact . error ) {
4941 console . error ( "Resend API error:" , contact . error )
50- return new Response (
51- JSON . stringify ( { error : "Failed to subscribe to newsletter" } ) ,
52- { status : 500 , headers : { "Content-Type" : "application/json" } }
53- )
42+ return res
43+ . status ( 500 )
44+ . json ( { error : "Failed to subscribe to newsletter" } )
5445 }
5546
56- return new Response (
57- JSON . stringify ( {
58- message : "Successfully subscribed to newsletter" ,
59- id : contact . data ?. id ,
60- } ) ,
61- { status : 201 , headers : { "Content-Type" : "application/json" } }
62- )
47+ return res . status ( 201 ) . json ( {
48+ message : "Successfully subscribed to newsletter" ,
49+ id : contact . data ?. id ,
50+ } )
6351 } catch ( error ) {
6452 console . error ( "Newsletter signup error:" , error )
65- return new Response ( JSON . stringify ( { error : "Internal server error" } ) , {
66- status : 500 ,
67- headers : { "Content-Type" : "application/json" } ,
68- } )
53+ return res . status ( 500 ) . json ( { error : "Internal server error" } )
6954 }
7055}
0 commit comments