Spaces:
Runtime error
Runtime error
File size: 1,859 Bytes
cd8bd0a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | import { NextResponse } from "next/server";
import { updateSettings } from "@/lib/localDb";
import {
getPayloadRulesConfig,
normalizePayloadRulesConfig,
} from "@omniroute/open-sse/services/payloadRules.ts";
import { updatePayloadRulesSchema } from "@/shared/validation/schemas";
import { isValidationFailure, validateBody } from "@/shared/validation/helpers";
import { requireManagementAuth } from "@/lib/api/requireManagementAuth";
export async function GET(request: Request) {
const authError = await requireManagementAuth(request);
if (authError) return authError;
try {
return NextResponse.json(await getPayloadRulesConfig());
} catch (error) {
console.error("Error reading payload rules config:", error);
return NextResponse.json({ error: "Failed to read payload rules config" }, { status: 500 });
}
}
export async function PUT(request: Request) {
const authError = await requireManagementAuth(request);
if (authError) return authError;
let rawBody: unknown;
try {
rawBody = await request.json();
} catch {
return NextResponse.json(
{
error: {
message: "Invalid request",
details: [{ field: "body", message: "Invalid JSON body" }],
},
},
{ status: 400 }
);
}
try {
const validation = validateBody(updatePayloadRulesSchema, rawBody);
if (isValidationFailure(validation)) {
return NextResponse.json({ error: validation.error }, { status: 400 });
}
const normalizedConfig = normalizePayloadRulesConfig(validation.data);
await updateSettings({ payloadRules: normalizedConfig });
return NextResponse.json(normalizedConfig);
} catch (error) {
console.error("Error updating payload rules config:", error);
return NextResponse.json({ error: "Failed to update payload rules config" }, { status: 500 });
}
}
|