-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
33 lines (26 loc) · 1.06 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
import { NextResponse, URLPattern} from 'next/server'
import type { NextRequest } from 'next/server'
// import mongoose from "mongoose"
// import { URLPattern } from 'url-pattern';
const entryIdPattern = new URLPattern({pathname: "/api/entries/:id"})
const allEntriesPattern = new URLPattern({pathname: "/api/entries"})
const checkMongoIDRegExp = new RegExp("^[0-9a-fA-F]{24}$");
// This function can be marked `async` if using `await` inside
export function middleware(request: NextRequest) {
const reqHref = request.nextUrl.href
if(entryIdPattern.test(reqHref)){
const { id } = entryIdPattern.exec(reqHref)?.pathname.groups!
if(!checkMongoIDRegExp.test(id!)){
const url = request.nextUrl.clone()
url.pathname = '/api/bad-request'
url.search = `?message=${id} is not a valid id`
return NextResponse.rewrite(url)
}
}
return NextResponse.next();
// return NextResponse.redirect(new URL('/home', request.url))
}
// See "Matching Paths" below to learn more
export const config = {
matcher:[ '/api/entries', '/api/entries/:path*'],
}