feat: enhance API response handling and add new components for module management
This commit is contained in:
@@ -1,27 +1,28 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import * as z from 'zod';
|
||||
|
||||
import { apiError, apiSuccess, emailSchema } from '@kit/next/route-helpers';
|
||||
import { getLogger } from '@kit/shared/logger';
|
||||
|
||||
const ContactSchema = z.object({
|
||||
recipientEmail: z.string().optional(),
|
||||
name: z.string().optional(),
|
||||
email: emailSchema,
|
||||
subject: z.string().optional(),
|
||||
message: z.string().min(1, 'Nachricht ist erforderlich'),
|
||||
});
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const logger = await getLogger();
|
||||
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { recipientEmail, name, email, subject, message } = body;
|
||||
const parsed = ContactSchema.safeParse(body);
|
||||
|
||||
if (!email || !message) {
|
||||
return NextResponse.json(
|
||||
{ error: 'E-Mail und Nachricht sind erforderlich' },
|
||||
{ status: 400 },
|
||||
);
|
||||
if (!parsed.success) {
|
||||
return apiError(parsed.error.issues[0]?.message ?? 'Ungültige Eingabe');
|
||||
}
|
||||
|
||||
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Ungültige E-Mail-Adresse' },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
const { recipientEmail, name, email, subject, message } = parsed.data;
|
||||
|
||||
// In production: use @kit/mailers to send the email
|
||||
// For now: log and return success
|
||||
@@ -45,9 +46,9 @@ export async function POST(request: Request) {
|
||||
// text: `Name: ${name}\nE-Mail: ${email}\n\n${message}`,
|
||||
// });
|
||||
|
||||
return NextResponse.json({ success: true, message: 'Nachricht gesendet' });
|
||||
return apiSuccess({ message: 'Nachricht gesendet' });
|
||||
} catch (err) {
|
||||
logger.error({ error: err, context: 'contact' }, '[contact] Error');
|
||||
return NextResponse.json({ error: 'Serverfehler' }, { status: 500 });
|
||||
return apiError('Serverfehler', 500);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user