mirror of
https://github.com/crocofied/CoreControl.git
synced 2025-12-29 16:14:43 +00:00
Zod for API Routes
This commit is contained in:
@@ -1,27 +1,35 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import prisma from "@/app/prisma";
|
||||
import { Network } from "@/app/types";
|
||||
import { z } from "zod/v4";
|
||||
|
||||
interface Body {
|
||||
id: string;
|
||||
name: string;
|
||||
description: string;
|
||||
networks: Network[];
|
||||
}
|
||||
const schema = z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
description: z.string(),
|
||||
networks: z.array(z.object({
|
||||
id: z.string().optional(),
|
||||
name: z.string().optional(),
|
||||
ipv4Subnet: z.string().optional(),
|
||||
ipv6Subnet: z.string().optional(),
|
||||
gateway: z.string().optional(),
|
||||
})),
|
||||
});
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
const body: Body = await request.json();
|
||||
const { id, name, description, networks } = body;
|
||||
|
||||
try {
|
||||
const body = schema.parse(await request.json());
|
||||
const { id, name, description, networks } = body;
|
||||
|
||||
const site = await prisma.site.update({
|
||||
where: { id: Number(id) },
|
||||
data: { name, description },
|
||||
});
|
||||
|
||||
return NextResponse.json(site);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ error: "Failed to update site" }, { status: 500 });
|
||||
} catch (error: any) {
|
||||
if(error instanceof z.ZodError) {
|
||||
return NextResponse.json({ error: error.issues[0].message }, { status: 400 });
|
||||
}
|
||||
return NextResponse.json({ error: "Internal Server Error" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user