43 lines
1.4 KiB
TypeScript
43 lines
1.4 KiB
TypeScript
import { authenticate, fromAllowed } from "@/lib/api-key";
|
|
import { parseMailInput } from "@/lib/mail-input";
|
|
import { MailValidationError } from "@/lib/schema";
|
|
import { serverError, unauthorized, validationError } from "@/lib/http";
|
|
import { enqueue, QuotaExceededError, SuppressedError } from "@/lib/queue";
|
|
import { apiError } from "@/lib/http";
|
|
|
|
export const runtime = "nodejs";
|
|
export const dynamic = "force-dynamic";
|
|
|
|
export async function POST(request: Request) {
|
|
const key = await authenticate(request);
|
|
if (!key) return unauthorized();
|
|
|
|
let body: unknown;
|
|
try {
|
|
body = await request.json();
|
|
} catch {
|
|
return validationError("Ungültiges JSON");
|
|
}
|
|
|
|
try {
|
|
const mail = parseMailInput(body, key.fromDomain);
|
|
if (!fromAllowed(key, mail.from.address)) {
|
|
return validationError(`Absender ${mail.from.address} ist für diesen Key nicht erlaubt`);
|
|
}
|
|
const result = await enqueue(key, mail, request.headers.get("idempotency-key"));
|
|
return Response.json({ id: result.id });
|
|
} catch (error) {
|
|
if (error instanceof QuotaExceededError) {
|
|
return apiError(429, "rate_limit_exceeded", error.message);
|
|
}
|
|
if (error instanceof SuppressedError) {
|
|
return apiError(422, "recipient_suppressed", error.message);
|
|
}
|
|
if (error instanceof MailValidationError) {
|
|
return validationError(error.message);
|
|
}
|
|
console.error("[courier] send:", error);
|
|
return serverError();
|
|
}
|
|
}
|