fix architecture and write new tests
This commit is contained in:
@@ -4,6 +4,12 @@ import type { NextRequest } from 'next/server'
|
||||
const SHARED_LANGUAGE_COOKIE = 'rentaldrivego-language'
|
||||
const MARKETPLACE_LANGUAGE_COOKIE = 'marketplace-language'
|
||||
|
||||
|
||||
function rejectInternalSubrequest(request: NextRequest): NextResponse | null {
|
||||
if (!request.headers.has('x-middleware-subrequest')) return null
|
||||
return new NextResponse('Unsupported internal request header', { status: 400 })
|
||||
}
|
||||
|
||||
function isValidLanguage(val: string | null | undefined): val is 'en' | 'fr' | 'ar' {
|
||||
return val === 'en' || val === 'fr' || val === 'ar'
|
||||
}
|
||||
@@ -18,6 +24,9 @@ function detectFromAcceptLanguage(header: string | null): 'en' | 'fr' | 'ar' {
|
||||
}
|
||||
|
||||
export function middleware(request: NextRequest) {
|
||||
const rejected = rejectInternalSubrequest(request)
|
||||
if (rejected) return rejected
|
||||
|
||||
const sharedLang = request.cookies.get(SHARED_LANGUAGE_COOKIE)?.value
|
||||
|
||||
// Already has the canonical language cookie — no action needed
|
||||
|
||||
Reference in New Issue
Block a user