1
1
import type { Handler } from './types'
2
2
3
- const METHODS = [ 'all' , 'get' , 'post' , 'put' , 'delete' , 'head' , 'options' , 'patch' ] as const
3
+ const METHODS = [ 'all' , 'get' , 'post' , 'put' , 'delete' , 'head' ] as const
4
4
function defineDynamicClass ( ) : {
5
5
new ( ) : {
6
6
[ K in typeof METHODS [ number ] ] : ( path : string , handler : Handler ) => Pico
@@ -18,25 +18,27 @@ class Pico extends defineDynamicClass() {
18
18
constructor ( ) {
19
19
super ( )
20
20
; [ ...METHODS ] . map ( ( method ) => {
21
- this [ method ] = ( path : string , handler : Handler ) => {
22
- const route = {
23
- pattern : new URLPattern ( {
24
- pathname : path ,
25
- } ) ,
26
- method : method . toUpperCase ( ) ,
27
- handler,
28
- }
29
- this . routes . push ( route )
30
- return this
31
- }
21
+ this [ method ] = ( path : string , handler : Handler ) => this . on ( method , path , handler )
32
22
} )
33
23
}
34
24
25
+ on ( method : string , path : string , handler : Handler ) {
26
+ const route = {
27
+ pattern : new URLPattern ( {
28
+ pathname : path ,
29
+ } ) ,
30
+ method : method . toLowerCase ( ) ,
31
+ handler,
32
+ }
33
+ this . routes . push ( route )
34
+ return this
35
+ }
36
+
35
37
match ( method : string , url : string ) : { handler : Handler ; result : URLPatternURLPatternResult } {
36
- method = method . toUpperCase ( )
38
+ method = method . toLowerCase ( )
37
39
for ( const route of this . routes ) {
38
40
const matched = route . pattern . test ( url )
39
- if ( ( matched && route . method === 'ALL ' ) || ( matched && route . method === method ) ) {
41
+ if ( ( matched && route . method === 'all ' ) || ( matched && route . method === method ) ) {
40
42
return { handler : route . handler , result : route . pattern . exec ( url ) }
41
43
}
42
44
}
0 commit comments