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,18 +1,19 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import prisma from "@/app/prisma";
|
||||
import { z } from "zod/v4";
|
||||
|
||||
interface QueryParams {
|
||||
siteId: string;
|
||||
}
|
||||
const schema = z.object({
|
||||
siteId: z.string(),
|
||||
});
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const searchParams = request.nextUrl.searchParams;
|
||||
const siteId = searchParams.get("siteId");
|
||||
const siteId = schema.parse({ siteId: searchParams.get("siteId") });
|
||||
|
||||
try {
|
||||
const site = await prisma.site.findUnique({
|
||||
where: {
|
||||
id: Number(siteId),
|
||||
id: Number(siteId.siteId),
|
||||
},
|
||||
include: {
|
||||
networks: {
|
||||
@@ -24,8 +25,10 @@ export async function GET(request: NextRequest) {
|
||||
});
|
||||
|
||||
return NextResponse.json({ site });
|
||||
} catch (error) {
|
||||
console.error("Error fetching site:", error);
|
||||
return NextResponse.json({ error: "Failed to fetch 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