Skip to content

Commit

Permalink
fix: types and conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
Baroshem committed Oct 15, 2022
1 parent 7c638cb commit 504cfd2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion playground/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineNuxtConfig } from 'nuxt/config'
import MyModule from '..'
import MyModule from '../src/module'

export default defineNuxtConfig({
modules: [
Expand Down
13 changes: 9 additions & 4 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { defineNuxtModule, addServerHandler } from '@nuxt/kit'
import defu from 'defu'
import { ModuleOptions } from './types'
import { defaultSecurityConfig } from './defaultConfig'
import { RuntimeConfig } from '@nuxt/schema'

export default defineNuxtModule<ModuleOptions>({
meta: {
Expand All @@ -15,13 +16,15 @@ export default defineNuxtModule<ModuleOptions>({
const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url))
nuxt.options.build.transpile.push(runtimeDir)
nuxt.options.runtimeConfig.security = defu(nuxt.options.runtimeConfig.security, {
...options
...options as RuntimeConfig["security"]
})

// Register enabled middlewares to automatically set default values for security response headers.
for (const header in nuxt.options.runtimeConfig.security.headers) {
if (nuxt.options.runtimeConfig.security.headers[header]) {
addServerHandler({ route: nuxt.options.runtimeConfig.security.headers[header].route, handler: resolve(runtimeDir, `server/middleware/headers/${header}`) })
if (nuxt.options.runtimeConfig.security.headers) {
for (const header in nuxt.options.runtimeConfig.security.headers) {
if (nuxt.options.runtimeConfig.security.headers[header]) {
addServerHandler({ route: nuxt.options.runtimeConfig.security.headers[header].route, handler: resolve(runtimeDir, `server/middleware/headers/${header}`) })
}
}
}

Expand All @@ -46,6 +49,8 @@ export default defineNuxtModule<ModuleOptions>({
addServerHandler({ route: xssValidatorConfig.route, handler: resolve(runtimeDir, 'server/middleware/xssValidator') })
}

// Register corsHandler middleware with default config that will add CORS setup
// Based on '@nozomuikuta/h3-cors' package
const corsHandlerConfig = nuxt.options.runtimeConfig.security.corsHandler
if (corsHandlerConfig) {
addServerHandler({ route: corsHandlerConfig.route, handler: resolve(runtimeDir, 'server/middleware/corsHandler') })
Expand Down

0 comments on commit 504cfd2

Please sign in to comment.