Template
32 lines
1.1 KiB
TypeScript
32 lines
1.1 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { z } from 'zod';
|
|
|
|
const contactSchema = z.object({
|
|
name: z.string().trim().min(2, 'Please enter your name.').max(100),
|
|
email: z.string().trim().email('Please enter a valid email address.').max(254),
|
|
company: z.string().trim().max(120).optional().default(''),
|
|
message: z.string().trim().min(10, 'Please share a little more detail.').max(5000),
|
|
website: z.string().max(0).optional().default(''),
|
|
});
|
|
|
|
export async function POST(request: Request) {
|
|
try {
|
|
const payload: unknown = await request.json();
|
|
const result = contactSchema.safeParse(payload);
|
|
|
|
if (!result.success) {
|
|
return NextResponse.json(
|
|
{ error: result.error.issues[0]?.message ?? 'Please check the form and try again.' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
// Store the validated contact data here.
|
|
// For example: await db.contactSubmission.create({ data: result.data });
|
|
|
|
return NextResponse.json({ ok: true }, { status: 202 });
|
|
} catch {
|
|
return NextResponse.json({ error: 'Invalid request.' }, { status: 400 });
|
|
}
|
|
}
|