-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMethodMatcher.ts
48 lines (41 loc) · 897 Bytes
/
MethodMatcher.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
import {
Matcher,
} from './Matcher'
import {
MatchResult,
} from './MatchResult'
const validMethods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'] as const
export interface MethodMatcherInput {
req: {
method: string
}
}
export type Method = typeof validMethods[number]
export type MethodMatchResult<M extends Method[]> = MatchResult<{
method: M[number]
}>
/**
* Match methods
*/
export class MethodMatcher<
M extends Method[],
P extends MethodMatcherInput
> implements Matcher<MethodMatchResult<M>, P> {
constructor(private readonly methods: M) {
this.match = this.match.bind(this)
}
match({ req }: MethodMatcherInput): MethodMatchResult<M> {
const { method } = req
if (method && this.methods.indexOf(method as Method) >= 0) {
return {
matched: true,
result: {
method: method as M[number],
},
}
}
return {
matched: false,
}
}
}