File tree Expand file tree Collapse file tree 1 file changed +11
-6
lines changed Expand file tree Collapse file tree 1 file changed +11
-6
lines changed Original file line number Diff line number Diff line change @@ -83,18 +83,23 @@ export function route<C>(
8383}
8484
8585export function methods < C > ( methods : Record < string , Handler < C > > ) : Handler < C > {
86- const methodMap = new Map < string , Handler < C > > ( ) ;
87- for ( const method in methods ) {
88- methodMap . set ( method , methods [ method ] ) ;
89- }
86+ const map = new Map ( Object . entries ( methods ) ) ;
87+ map . delete ( "HEAD" ) ;
88+ const allow = ( ( ) => {
89+ const list = Array . from ( map . keys ( ) ) ;
90+ if ( map . has ( "GET" ) ) {
91+ list . push ( "HEAD" ) ;
92+ }
93+ return list . sort ( ) . join ( ", " ) ;
94+ } ) ( ) ;
9095 return ( req , ctx ) => {
91- const handler = methodMap . get ( req . method ) ?? fail (
96+ const handler = map . get ( req . method ) ?? fail (
9297 new HttpError (
9398 `Method ${ req . method } is not allowed for the URL` ,
9499 "MethodNotAllowed" ,
95100 {
96101 headers : [
97- [ "allow" , Array . from ( methodMap . keys ( ) ) . join ( ", " ) ] ,
102+ [ "allow" , allow ] ,
98103 ] ,
99104 } ,
100105 ) ,
You can’t perform that action at this time.
0 commit comments