-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Issue #3336148: Add support for forms #247
Changes from 6 commits
e1e9a12
19d5b8f
c28d26c
784797f
631bab2
36af30d
83ccb91
9c27504
7ab5436
6f1075f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<template> | ||
<form :formId="formId" :method="method" v-bind="attributes" :action="useRoute().fullPath" class="drupal-form"> | ||
<slot><div v-html="content" /></slot> | ||
</form> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
const props = defineProps<{ | ||
formId: String, | ||
attributes: Object, | ||
method: String, | ||
content?: String, | ||
}>() | ||
</script> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { readFormData, createError } from 'h3' | ||
import { useRuntimeConfig, useRequestEvent } from '#imports' | ||
|
||
export default defineNuxtRouteMiddleware(async (from) => { | ||
if (import.meta.server) { | ||
const runtimeConfig = useRuntimeConfig() | ||
const event = useRequestEvent() | ||
const { ceApiEndpoint } = runtimeConfig.public.drupalCe | ||
const drupalBaseUrl = getDrupalBaseUrl() | ||
|
||
if (event?.node?.req?.method === 'POST') { | ||
const formData = await readFormData(event) | ||
|
||
if (formData) { | ||
const targetUrl = from.fullPath | ||
const response = await $fetch.raw(drupalBaseUrl + ceApiEndpoint + targetUrl, { | ||
method: 'POST', | ||
body: formData, | ||
}) | ||
|
||
event.context.drupalCeCustomPageResponse = { | ||
_data: response._data, | ||
headers: Object.fromEntries(response.headers.entries()), | ||
} | ||
} else { | ||
throw createError({ | ||
statusCode: 400, | ||
statusMessage: 'Bad Request', | ||
message: 'The request contains invalid form data or no form data at all.', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's say "No or invalid form data given. See drupalFormHandler." |
||
}) | ||
} | ||
} | ||
} | ||
}) |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ import { appendResponseHeader } from 'h3' | |
import type { UseFetchOptions } from '#app' | ||
import type { $Fetch, NitroFetchRequest } from 'nitropack' | ||
import { getDrupalBaseUrl, getMenuBaseUrl } from './server' | ||
import { useRuntimeConfig, useState, useFetch, navigateTo, createError, h, resolveComponent, setResponseStatus, useNuxtApp, useRequestHeaders, ref, watch } from '#imports' | ||
import { useRuntimeConfig, useState, useFetch, navigateTo, createError, h, resolveComponent, setResponseStatus, useNuxtApp, useRequestHeaders, ref, watch, useRequestEvent } from '#imports' | ||
|
||
export const useDrupalCe = () => { | ||
const config = useRuntimeConfig().public.drupalCe | ||
|
@@ -116,9 +116,32 @@ export const useDrupalCe = () => { | |
page_layout: 'default', | ||
title: '', | ||
})) | ||
const serverResponse = useState('server-response', () => null) | ||
useFetchOptions.key = `page-${path}` | ||
const page = ref(null) | ||
const pageError = ref(null) | ||
|
||
const { data: page, error } = await useCeApi(path, useFetchOptions, true) | ||
if (import.meta.server) { | ||
serverResponse.value = useRequestEvent(nuxtApp).context.drupalCeCustomPageResponse | ||
} | ||
|
||
// Check if the page data is already provided, e.g. by a form response. | ||
if (serverResponse.value && serverResponse.value._data) { | ||
page.value = serverResponse.value._data | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this never sets pageErrror like the other if part. does it actually handle error codes correctly? |
||
passThroughHeaders(nuxtApp, serverResponse.value.headers) | ||
// Clear the server response state after it was sent to the client. | ||
if (import.meta.client) { | ||
serverResponse.value = null | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm still not 100% convinced we need serverResponse for hydrating things, since page should be already hydrated. But let's merge and re-check things once we have it merged and working. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tested and ensured it's not double-hydrated. maybe code could be nicer, but that seems all good! |
||
} | ||
} else { | ||
const { data, error } = await useCeApi(path, useFetchOptions, true) | ||
page.value = data.value | ||
pageError.value = error.value | ||
} | ||
|
||
if (page.value?.messages) { | ||
pushMessagesToState(page.value.messages) | ||
} | ||
|
||
if (page?.value?.redirect) { | ||
await callWithNuxt(nuxtApp, navigateTo, [ | ||
|
@@ -128,13 +151,11 @@ export const useDrupalCe = () => { | |
return pageState | ||
} | ||
|
||
if (error.value) { | ||
overrideErrorHandler ? overrideErrorHandler(error) : pageErrorHandler(error, { config, nuxtApp }) | ||
page.value = error.value?.data | ||
if (pageError.value) { | ||
overrideErrorHandler ? overrideErrorHandler(pageError) : pageErrorHandler(pageError, { config, nuxtApp }) | ||
page.value = pageError.value?.data | ||
} | ||
|
||
page.value?.messages && pushMessagesToState(page.value.messages) | ||
|
||
pageState.value = page | ||
return page | ||
} | ||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's rename this file to "drupalFormHandler" to clarify it's only about the drupal-form, not every form.
Also I'm wondering whether we can declare the middleware in a way it is NOT shipped in the client bundle? This codes never ever needs to run on the client, so it seems to be silly to have it there. Would using server-middleware instead of route middleware be an option that helps us there?
https://nuxt.com/docs/guide/directory-structure/server#server-middleware
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've moved it to server middleware which is global(cannot be imported in page, which should be fine)
but also as a page middleware, when using import.meta.server the code doesn't leak into client bundle