fix: endpoints

This commit is contained in:
charles-gauthereau
2026-05-27 22:02:22 +02:00
parent 56517226b6
commit c1b71e7772
18 changed files with 941 additions and 457 deletions
+50
View File
@@ -0,0 +1,50 @@
import { NextResponse } from "next/server";
import { z } from "zod";
type ParseJsonBodyResult<T> =
| {
ok: true;
data: T;
}
| {
ok: false;
response: NextResponse;
};
export async function parseJsonBody<TSchema extends z.ZodTypeAny>(
req: Request,
schema: TSchema
): Promise<ParseJsonBodyResult<z.infer<TSchema>>> {
let body: unknown;
try {
body = await req.json();
} catch {
return {
ok: false,
response: NextResponse.json(
{ error: "Invalid JSON body" },
{ status: 422 }
),
};
}
const parsed = schema.safeParse(body);
if (!parsed.success) {
return {
ok: false,
response: NextResponse.json(
{
error: parsed.error.issues[0]?.message ?? "Invalid body",
},
{ status: 422 }
),
};
}
return {
ok: true,
data: parsed.data,
};
}