-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathcorsHandler.ts
33 lines (27 loc) · 960 Bytes
/
corsHandler.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
import { defineEventHandler, handleCors } from '#imports'
import type { H3CorsOptions } from 'h3'
import { resolveSecurityRules } from '../../nitro/context'
export default defineEventHandler((event) => {
const rules = resolveSecurityRules(event)
if (rules.enabled && rules.corsHandler) {
const { corsHandler } = rules
let origin: H3CorsOptions['origin']
if (typeof corsHandler.origin === 'string' && corsHandler.origin !== '*') {
origin = [corsHandler.origin]
} else {
origin = corsHandler.origin
}
if (origin && origin !== '*' && corsHandler.useRegExp) {
origin = origin.map((o) => new RegExp(o, 'i'))
}
handleCors(event, {
origin,
methods: corsHandler.methods,
allowHeaders: corsHandler.allowHeaders,
exposeHeaders: corsHandler.exposeHeaders,
credentials: corsHandler.credentials,
maxAge: corsHandler.maxAge,
preflight: corsHandler.preflight
})
}
})