Skip to content

Commit

Permalink
LDP-2644: Add option to disable form handler middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
vloss3 committed Nov 20, 2024
1 parent 5191f4c commit 11f5461
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ is added automatically to requests. Defaults to `false`.
- 'info': Log all server requests and errors.
- 'error': Log only errors.

- `disableFormHandler`: If set to `true`, the form handler middleware will be disabled. Defaults to `false`.

## Overriding options with environment variables

Runtime config values can be overridden with environment variables via `NUXT_PUBLIC_` prefix. Supported runtime overrides:
Expand Down
11 changes: 8 additions & 3 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface ModuleOptions {
passThroughHeaders?: string[]
exposeAPIRouteRules?: boolean
serverLogLevel?: boolean | 'info' | 'error'
disableFormHandler?: boolean
}

export default defineNuxtModule<ModuleOptions>({
Expand All @@ -43,6 +44,7 @@ export default defineNuxtModule<ModuleOptions>({
serverApiProxy: true,
passThroughHeaders: ['cache-control', 'content-language', 'set-cookie', 'x-drupal-cache', 'x-drupal-dynamic-cache'],
serverLogLevel: 'info',
disableFormHandler: false,
},
setup(options, nuxt) {
const nuxtOptions = nuxt.options as NuxtOptionsWithDrupalCe
Expand All @@ -63,15 +65,18 @@ export default defineNuxtModule<ModuleOptions>({
addServerPlugin(resolve(runtimeDir, 'server/plugins/errorLogger'))
}
addImportsDir(resolve(runtimeDir, 'composables/useDrupalCe'))
addServerHandler({
handler: resolve(runtimeDir, 'server/middleware/drupalFormHandler'),
})
if (!options.disableFormHandler) {
addServerHandler({
handler: resolve(runtimeDir, 'server/middleware/drupalFormHandler'),
})
}

const publicOptions = { ...options }
// Server options are not needed in the client bundle.
delete publicOptions.serverLogLevel
delete publicOptions.passThroughHeaders
delete publicOptions.exposeAPIRouteRules
delete publicOptions.disableFormHandler

nuxt.options.runtimeConfig.public.drupalCe = defu(nuxt.options.runtimeConfig.public.drupalCe ?? {}, publicOptions)

Expand Down
4 changes: 1 addition & 3 deletions src/runtime/server/middleware/drupalFormHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import { useRuntimeConfig } from '#imports'

export default defineEventHandler(async (event) => {
const { ceApiEndpoint } = useRuntimeConfig().public.drupalCe
const skipFormHandler = event.node.req.headers['x-drupalce-skip-form-handler'] === 'true'

if (event.node.req.method === 'POST' && !skipFormHandler) {
if (event.node.req.method === 'POST') {
const formData = await readFormData(event)

if (formData) {
Expand Down

0 comments on commit 11f5461

Please sign in to comment.