@@ -85,7 +85,7 @@ interface CORSConfig {
85
85
* - `HTTPMethod[]` - Allow multiple HTTP methods.
86
86
* - eg: ['GET', 'PUT', 'POST']
87
87
*/
88
- methods ?: undefined | null | '' | '*' | HTTPMethod | HTTPMethod [ ]
88
+ methods ?: boolean | undefined | null | '' | '*' | HTTPMethod | HTTPMethod [ ]
89
89
/**
90
90
* @default `*`
91
91
*
@@ -117,7 +117,7 @@ interface CORSConfig {
117
117
*/
118
118
exposedHeaders ?: string | string [ ]
119
119
/**
120
- * @default `false `
120
+ * @default `true `
121
121
*
122
122
* Assign **Access-Control-Allow-Credentials** header.
123
123
*
@@ -162,7 +162,7 @@ export const cors = (
162
162
methods = '*' ,
163
163
allowedHeaders = '*' ,
164
164
exposedHeaders = '*' ,
165
- credentials = false ,
165
+ credentials = true ,
166
166
maxAge = 5 ,
167
167
preflight = true
168
168
} = config
@@ -183,12 +183,10 @@ export const cors = (
183
183
switch ( typeof origin ) {
184
184
case 'string' :
185
185
const protocolStart = from . indexOf ( '://' )
186
- if ( protocolStart !== - 1 )
187
- from = from . slice ( protocolStart + 3 )
186
+ if ( protocolStart !== - 1 ) from = from . slice ( protocolStart + 3 )
188
187
189
188
const trailingSlash = from . indexOf ( '/' , 0 )
190
- if ( trailingSlash !== - 1 )
191
- from = from . slice ( trailingSlash )
189
+ if ( trailingSlash !== - 1 ) from = from . slice ( trailingSlash )
192
190
193
191
return origin === from
194
192
@@ -204,7 +202,8 @@ export const cors = (
204
202
// origin === `true` means any origin
205
203
if ( origin === true ) {
206
204
set . headers [ 'Vary' ] = '*'
207
- set . headers [ 'Access-Control-Allow-Origin' ] = request . headers . get ( 'Origin' ) || '*'
205
+ set . headers [ 'Access-Control-Allow-Origin' ] =
206
+ request . headers . get ( 'Origin' ) || '*'
208
207
209
208
return
210
209
}
@@ -234,8 +233,11 @@ export const cors = (
234
233
set . headers [ 'Access-Control-Allow-Origin' ] = headers . join ( ', ' )
235
234
}
236
235
237
- const handleMethod = ( set : Context [ 'set' ] ) => {
238
- if ( ! methods ?. length ) return
236
+ const handleMethod = ( set : Context [ 'set' ] , method : string ) => {
237
+ if ( methods === true )
238
+ return ( set . headers [ 'Access-Control-Allow-Methods' ] = method ?? '*' )
239
+
240
+ if ( methods === false || ! methods ?. length ) return
239
241
240
242
if ( methods === '*' )
241
243
return ( set . headers [ 'Access-Control-Allow-Methods' ] = '*' )
@@ -249,7 +251,7 @@ export const cors = (
249
251
if ( preflight )
250
252
app . options ( '/' , ( { set, request } ) => {
251
253
handleOrigin ( set as any , request )
252
- handleMethod ( set )
254
+ handleMethod ( set , request . method )
253
255
254
256
if ( exposedHeaders . length )
255
257
set . headers [ 'Access-Control-Allow-Headers' ] =
@@ -265,7 +267,7 @@ export const cors = (
265
267
} )
266
268
} ) . options ( '/*' , ( { set, request } ) => {
267
269
handleOrigin ( set as any , request )
268
- handleMethod ( set )
270
+ handleMethod ( set , request . method )
269
271
270
272
if ( exposedHeaders . length )
271
273
set . headers [ 'Access-Control-Allow-Headers' ] =
@@ -281,24 +283,22 @@ export const cors = (
281
283
} )
282
284
} )
283
285
284
- return app . onRequest ( ( { set, request } ) => {
286
+ const defaultHeaders : Record < string , string > = {
287
+ 'Access-Control-Allow-Headers' :
288
+ typeof allowedHeaders === 'string'
289
+ ? allowedHeaders
290
+ : allowedHeaders . join ( ', ' ) ,
291
+ 'Access-Control-Exposed-Headers' :
292
+ typeof exposedHeaders === 'string'
293
+ ? exposedHeaders
294
+ : exposedHeaders . join ( ', ' )
295
+ }
296
+
297
+ if ( credentials ) defaultHeaders [ 'Access-Control-Allow-Credentials' ] = 'true'
298
+
299
+ return app . headers ( defaultHeaders ) . onRequest ( ( { set, request } ) => {
285
300
handleOrigin ( set , request )
286
- handleMethod ( set )
287
-
288
- if ( allowedHeaders . length )
289
- set . headers [ 'Access-Control-Allow-Headers' ] =
290
- typeof allowedHeaders === 'string'
291
- ? allowedHeaders
292
- : allowedHeaders . join ( ', ' )
293
-
294
- if ( exposedHeaders . length )
295
- set . headers [ 'Access-Control-Exposed-Headers' ] =
296
- typeof exposedHeaders === 'string'
297
- ? exposedHeaders
298
- : exposedHeaders . join ( ', ' )
299
-
300
- if ( credentials )
301
- set . headers [ 'Access-Control-Allow-Credentials' ] = 'true'
301
+ handleMethod ( set , request . method )
302
302
} )
303
303
}
304
304
0 commit comments