@@ -50,6 +50,12 @@ export const cors = (options?: CorsOptions): Middleware => {
50
50
const allowedOrigins =
51
51
typeof config . origin === 'string' ? [ config . origin ] : config . origin ;
52
52
const allowsWildcard = allowedOrigins . includes ( '*' ) ;
53
+ const allowedMethods = config . allowMethods . map ( ( method ) =>
54
+ method . toUpperCase ( )
55
+ ) ;
56
+ const allowedHeaders = config . allowHeaders . map ( ( header ) =>
57
+ header . toLowerCase ( )
58
+ ) ;
53
59
54
60
return async ( _params , reqCtx , next ) => {
55
61
const requestOrigin = reqCtx . request . headers . get ( 'Origin' ) ;
@@ -72,18 +78,11 @@ export const cors = (options?: CorsOptions): Middleware => {
72
78
?. toLowerCase ( ) ;
73
79
if (
74
80
! requestMethod ||
75
- ! config . allowMethods
76
- . map ( ( m ) => m . toUpperCase ( ) )
77
- . includes ( requestMethod ) ||
81
+ ! allowedMethods . includes ( requestMethod ) ||
78
82
! requestHeaders ||
79
83
requestHeaders
80
84
. split ( ',' )
81
- . some (
82
- ( header ) =>
83
- ! config . allowHeaders
84
- . map ( ( h ) => h . toLowerCase ( ) )
85
- . includes ( header . trim ( ) )
86
- )
85
+ . some ( ( header ) => ! allowedHeaders . includes ( header . trim ( ) ) )
87
86
) {
88
87
await next ( ) ;
89
88
return ;
@@ -106,10 +105,10 @@ export const cors = (options?: CorsOptions): Middleware => {
106
105
config . maxAge . toString ( )
107
106
) ;
108
107
}
109
- config . allowMethods . forEach ( ( method ) => {
108
+ allowedMethods . forEach ( ( method ) => {
110
109
reqCtx . res . headers . append ( 'access-control-allow-methods' , method ) ;
111
110
} ) ;
112
- config . allowHeaders . forEach ( ( header ) => {
111
+ allowedHeaders . forEach ( ( header ) => {
113
112
reqCtx . res . headers . append ( 'access-control-allow-headers' , header ) ;
114
113
} ) ;
115
114
return new Response ( null , {
0 commit comments