@@ -42,6 +42,7 @@ export type Rewrite = {
42
42
basePath ?: false
43
43
locale ?: false
44
44
has ?: RouteHas [ ]
45
+ missing ?: RouteHas [ ]
45
46
regex : string
46
47
}
47
48
@@ -51,6 +52,7 @@ export type Header = {
51
52
locale ?: false
52
53
headers : Array < { key : string ; value : string } >
53
54
has ?: RouteHas [ ]
55
+ missing ?: RouteHas [ ]
54
56
regex : string
55
57
}
56
58
export type Redirect = {
@@ -59,6 +61,7 @@ export type Redirect = {
59
61
basePath ?: false
60
62
locale ?: false
61
63
has ?: RouteHas [ ]
64
+ missing ?: RouteHas [ ]
62
65
statusCode ?: number
63
66
permanent ?: boolean
64
67
regex : string
@@ -138,11 +141,11 @@ export function parseUrl(url: string): ParsedUrl {
138
141
139
142
// prepare-destination.ts
140
143
// Changed to use WHATWG Fetch Request instead of IncomingMessage
141
- export function matchHas ( req : Pick < Request , 'headers' | 'url' > , has : RouteHas [ ] , query : Params ) : false | Params {
144
+ export function matchHas ( req : Pick < Request , 'headers' | 'url' > , query : Params , has : RouteHas [ ] = [ ] , missing : RouteHas [ ] = [ ] ) : false | Params {
142
145
const params : Params = { }
143
146
const cookies = getCookies ( req . headers )
144
147
const url = new URL ( req . url )
145
- const allMatch = has . every ( ( hasItem ) => {
148
+ const hasMatch = ( hasItem : RouteHas ) => {
146
149
let value : undefined | string | null
147
150
let key = hasItem . key
148
151
@@ -189,7 +192,9 @@ export function matchHas(req: Pick<Request, 'headers' | 'url'>, has: RouteHas[],
189
192
}
190
193
}
191
194
return false
192
- } )
195
+ }
196
+
197
+ const allMatch = has . every ( ( item ) => hasMatch ( item ) ) && ! missing . some ( ( item ) => hasMatch ( item ) )
193
198
194
199
if ( allMatch ) {
195
200
return params
@@ -371,6 +376,7 @@ export interface MiddlewareMatcher {
371
376
regexp : string
372
377
locale ?: false
373
378
has ?: RouteHas [ ]
379
+ missing ?: RouteHas [ ]
374
380
}
375
381
376
382
export function getMiddlewareRouteMatcher ( matchers : MiddlewareMatcher [ ] ) : MiddlewareRouteMatch {
@@ -381,8 +387,8 @@ export function getMiddlewareRouteMatcher(matchers: MiddlewareMatcher[]): Middle
381
387
continue
382
388
}
383
389
384
- if ( matcher . has ) {
385
- const hasParams = matchHas ( req , matcher . has , query )
390
+ if ( matcher . has || matcher . missing ) {
391
+ const hasParams = matchHas ( req , query , matcher . has , matcher . missing )
386
392
if ( ! hasParams ) {
387
393
continue
388
394
}
0 commit comments