forked from nuxt-modules/security
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path00-context.ts
33 lines (27 loc) · 1.15 KB
/
00-context.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 { getNameFromKey, headerStringFromObject} from "../../utils/headers"
import { createRouter} from "radix3"
import { defineNitroPlugin } from '#imports'
import { OptionKey } from "~/src/module"
export default defineNitroPlugin((nitroApp) => {
const router = createRouter()
nitroApp.hooks.hook('nuxt-security:headers', ({route, headers: headersConfig}) => {
const headers: Record<string, string |false > = {}
for (const [header, headerOptions] of Object.entries(headersConfig)) {
const headerName = getNameFromKey(header as OptionKey)
if(headerName) {
const value = headerStringFromObject(header as OptionKey, headerOptions)
if(value) {
headers[headerName] = value
} else {
delete headers[headerName]
}
}
}
router.insert(route, headers)
})
nitroApp.hooks.hook('request', (event) => {
event.context.security = event.context.security || {}
event.context.security.headers = router.lookup(event.path)
})
nitroApp.hooks.callHook('nuxt-security:ready')
})