Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions apps/web/src/app/api/waitlist/__tests__/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,28 @@ describe("POST /api/waitlist", () => {
expect(res.status).toBe(400);
});

it("returns 400 when JSON is not an object", async () => {
const res = await POST(makeRequest(null));
const body = await res.json();

expect(res.status).toBe(400);
expect(body.error).toBe("JSON object is required");
});

it("returns 400 for malformed JSON", async () => {
const req = new Request("http://localhost/api/waitlist", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: "{",
}) as unknown as import("next/server").NextRequest;

const res = await POST(req);
const body = await res.json();

expect(res.status).toBe(400);
expect(body.error).toBe("Invalid JSON");
});

it("handles duplicate email gracefully — returns existing entry", async () => {
selectResult = {
data: {
Expand Down
12 changes: 11 additions & 1 deletion apps/web/src/app/api/waitlist/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,18 @@ function generateReferralCode(): string {
}

export async function POST(request: NextRequest) {
let body: { email?: unknown; payment_method?: string; referral_code?: string };
try {
const parsed: unknown = await request.json();
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
return NextResponse.json({ error: "JSON object is required" }, { status: 400 });
}
body = parsed as typeof body;
} catch {
return NextResponse.json({ error: "Invalid JSON" }, { status: 400 });
}

try {
const body = await request.json();
const { email, payment_method, referral_code } = body;

if (!email || typeof email !== "string" || !email.includes("@")) {
Expand Down
Loading