Skip to content

Commit 35c1995

Browse files
committed
✨ feat: Revise API for deployment
1 parent 92c3565 commit 35c1995

4 files changed

Lines changed: 51 additions & 40 deletions

File tree

.vercelignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Ignore everything (folders and files) on root only
2+
/*
3+
!api
4+
!package.json
5+
!vercel.json

api/health.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { VercelRequest, VercelResponse } from "@vercel/node"
2+
3+
export default function handler(req: VercelRequest, res: VercelResponse) {
4+
return res.status(200).json({
5+
status: "healthy",
6+
timestamp: new Date().toISOString(),
7+
method: req.method,
8+
url: req.url,
9+
})
10+
}

api/newsletter-signup.ts

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,32 @@
1+
import { VercelRequest, VercelResponse } from "@vercel/node"
12
import { Resend } from "resend"
23

34
const 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
}

vercel.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"$schema": "https://openapi.vercel.sh/vercel.json",
3+
"functions": {
4+
"api/newsletter-signup.ts": {
5+
"maxDuration": 30
6+
},
7+
"api/health.ts": {
8+
"maxDuration": 30
9+
}
10+
}
11+
}

0 commit comments

Comments
 (0)