-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
44 lines (38 loc) · 1.6 KB
/
middleware.ts
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
import { NextResponse, NextRequest } from 'next/server';
export async function middleware(request: NextRequest) {
const { method, url } = request;
if (method === "POST" && url.includes("api/games")) {
try {
const dataSend = await request.json();
const { title, genre, price, platform } = dataSend;
console.log('THIS MESSAGE FROM MIDDLEWARE', title, genre, price, platform);
// Validation: Check required fields
if (!title || !genre || !price) {
console.log('Validation error: Missing fields');
return new NextResponse(
JSON.stringify({ error: 'Title, genre, and price are required fields.' }),
{ status: 400, headers: { 'Content-Type': 'application/json' } }
);
}
// Validation: Check platform values
const allowedPlatforms = ['xbox', 'pc', 'playstation'];
if (!platform || !Array.isArray(platform) || platform.some((p) => !allowedPlatforms.includes(p))) {
console.log('Validation error: Invalid platform');
return new NextResponse(
JSON.stringify({
error: `Platform must be an array containing only: ${allowedPlatforms.join(', ')}`,
}),
{ status: 400, headers: { 'Content-Type': 'application/json' } }
);
}
} catch (error) {
console.error('Middleware error:', error);
return new NextResponse(
JSON.stringify({ error: 'Invalid request payload' }),
{ status: 400, headers: { 'Content-Type': 'application/json' } }
);
}
}
// Allow the request to proceed if validation passes
return NextResponse.next();
}