-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExactUrlPathnameMatcher.ts
More file actions
47 lines (43 loc) · 924 Bytes
/
ExactUrlPathnameMatcher.ts
File metadata and controls
47 lines (43 loc) · 924 Bytes
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
import Url from 'urlite'
import {
Matcher,
} from './Matcher'
import {
MatchResult,
} from './MatchResult'
export interface ExactUrlPathnameMatcherInput {
req: {
url: string
}
}
export type ExactUrlPathnameMatchResult<U extends [string, ...string[]]> = MatchResult<{
pathname: U[number]
}>
/**
* Match exact urls
*/
export class ExactUrlPathnameMatcher<
U extends [string, ...string[]],
P extends ExactUrlPathnameMatcherInput
>
implements Matcher<ExactUrlPathnameMatchResult<U>, P> {
constructor(private readonly urls: U) {
this.match = this.match.bind(this)
}
match({ req }: P): ExactUrlPathnameMatchResult<U> {
/* istanbul ignore else */
// original URL returns '/' if pathname is empty
const pathname = Url.parse(req.url).pathname ?? '/'
if (this.urls.indexOf(pathname) >= 0) {
return {
matched: true,
result: {
pathname,
},
}
}
return {
matched: false,
}
}
}