-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegExpUrlMatcher.ts
52 lines (46 loc) · 960 Bytes
/
RegExpUrlMatcher.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
import {
Matcher,
} from './Matcher'
import {
MatchResult,
} from './MatchResult'
export interface RegExpUrlMatcherInput {
req: {
url: string
}
}
export interface RegExpExecGroupArray<
T extends object
> extends Array<string> {
index: number
input: string
groups: T
}
export type RegExpUrlMatchResult<R extends object> = MatchResult<{
match: RegExpExecGroupArray<R>
}>
export class RegExpUrlMatcher<
R extends object,
P extends RegExpUrlMatcherInput = RegExpUrlMatcherInput
>
implements Matcher<RegExpUrlMatchResult<R>, P> {
constructor(private readonly urls: RegExp[]) {
this.match = this.match.bind(this)
}
match({ req }: RegExpUrlMatcherInput): RegExpUrlMatchResult<R> {
for (const url of this.urls) {
const result = url.exec(req.url) as never as RegExpExecGroupArray<R>
if (result !== null) {
return {
matched: true,
result: {
match: result,
},
}
}
}
return {
matched: false,
}
}
}