chore(api): add explicit type casting for request payloads

This commit is contained in:
jessy-david-dev
2026-04-11 15:20:29 +02:00
parent 7d1b236b46
commit d5a66c2dac
13 changed files with 204 additions and 96 deletions
+18 -4
View File
@@ -76,14 +76,28 @@ function pruneOldRooms(rooms: Map<string, Room>) {
// Response: { room: Room, playerId: string }
export async function POST(request: NextRequest) {
const body = await request.json();
const { playerName, maxPlayers, totalRounds } = body as { playerName: string; maxPlayers?: number; totalRounds?: number };
const { playerName, maxPlayers, totalRounds } = body as {
playerName: string;
maxPlayers?: number;
totalRounds?: number;
};
if (!playerName || typeof playerName !== "string" || playerName.trim() === "") {
if (
!playerName ||
typeof playerName !== "string" ||
playerName.trim() === ""
) {
return Response.json({ error: "Pseudo invalide" }, { status: 400 });
}
const clampedMax = Math.min(Math.max(typeof maxPlayers === "number" ? Math.floor(maxPlayers) : 16, 2), 16);
const clampedRounds = Math.min(Math.max(typeof totalRounds === "number" ? Math.floor(totalRounds) : 3, 1), 10);
const clampedMax = Math.min(
Math.max(typeof maxPlayers === "number" ? Math.floor(maxPlayers) : 16, 2),
16,
);
const clampedRounds = Math.min(
Math.max(typeof totalRounds === "number" ? Math.floor(totalRounds) : 3, 1),
10,
);
const rooms = getRooms();
pruneOldRooms(rooms);