-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
58 lines (49 loc) · 1.46 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
switch (pathname) {
case "/api/elections":
return _checkElectionParameter(request);
case "/api/elections/page":
return _checkElectionPageParameter(request);
default:
NextResponse.next();
}
}
function _checkElectionParameter(
request: NextRequest,
): NextResponse | undefined {
if (request.method !== "GET") {
return methodNotAllowed();
}
const { searchParams } = request.nextUrl;
const databaseId: string | null = searchParams.get("databaseId");
if (!databaseId || databaseId?.length !== 32) {
return _invalidParameterResponse("Invalid databaseId parameter.");
}
}
function _checkElectionPageParameter(
request: NextRequest,
): NextResponse | undefined {
if (request.method !== "GET") {
return methodNotAllowed();
}
const { searchParams } = request.nextUrl;
const pageId: string | null = searchParams.get("pageId");
if (!pageId || pageId?.length !== 36) {
return _invalidParameterResponse("Invalid pageId parameter.");
}
}
function _invalidParameterResponse(message: string): NextResponse {
return new NextResponse(null, {
status: 400,
statusText: message,
});
}
function methodNotAllowed(): NextResponse | undefined {
return new NextResponse(null, {
status: 405,
statusText: "Method Not Allowed.",
});
}