-
Notifications
You must be signed in to change notification settings - Fork 12.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(n8n Form Node): Respond with Text (#12979)
- Loading branch information
Showing
6 changed files
with
276 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { type Response } from 'express'; | ||
import { | ||
type NodeTypeAndVersion, | ||
type IWebhookFunctions, | ||
type IWebhookResponseData, | ||
} from 'n8n-workflow'; | ||
|
||
import { sanitizeHtml } from './utils'; | ||
|
||
export const renderFormCompletion = async ( | ||
context: IWebhookFunctions, | ||
res: Response, | ||
trigger: NodeTypeAndVersion, | ||
): Promise<IWebhookResponseData> => { | ||
const completionTitle = context.getNodeParameter('completionTitle', '') as string; | ||
const completionMessage = context.getNodeParameter('completionMessage', '') as string; | ||
const redirectUrl = context.getNodeParameter('redirectUrl', '') as string; | ||
const options = context.getNodeParameter('options', {}) as { formTitle: string }; | ||
const responseText = context.getNodeParameter('responseText', '') as string; | ||
|
||
if (redirectUrl) { | ||
res.send( | ||
`<html><head><meta http-equiv="refresh" content="0; url=${redirectUrl}"></head></html>`, | ||
); | ||
return { noWebhookResponse: true }; | ||
} | ||
|
||
let title = options.formTitle; | ||
if (!title) { | ||
title = context.evaluateExpression(`{{ $('${trigger?.name}').params.formTitle }}`) as string; | ||
} | ||
const appendAttribution = context.evaluateExpression( | ||
`{{ $('${trigger?.name}').params.options?.appendAttribution === false ? false : true }}`, | ||
) as boolean; | ||
|
||
res.render('form-trigger-completion', { | ||
title: completionTitle, | ||
message: completionMessage, | ||
formTitle: title, | ||
appendAttribution, | ||
responseText: sanitizeHtml(responseText), | ||
}); | ||
|
||
return { noWebhookResponse: true }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { type Response } from 'express'; | ||
import { | ||
type NodeTypeAndVersion, | ||
type IWebhookFunctions, | ||
FORM_NODE_TYPE, | ||
WAIT_NODE_TYPE, | ||
type FormFieldsParameter, | ||
type IWebhookResponseData, | ||
} from 'n8n-workflow'; | ||
|
||
import { renderForm } from './utils'; | ||
|
||
export const renderFormNode = async ( | ||
context: IWebhookFunctions, | ||
res: Response, | ||
trigger: NodeTypeAndVersion, | ||
fields: FormFieldsParameter, | ||
mode: 'test' | 'production', | ||
): Promise<IWebhookResponseData> => { | ||
const options = context.getNodeParameter('options', {}) as { | ||
formTitle: string; | ||
formDescription: string; | ||
buttonLabel: string; | ||
}; | ||
|
||
let title = options.formTitle; | ||
if (!title) { | ||
title = context.evaluateExpression(`{{ $('${trigger?.name}').params.formTitle }}`) as string; | ||
} | ||
|
||
let description = options.formDescription; | ||
if (!description) { | ||
description = context.evaluateExpression( | ||
`{{ $('${trigger?.name}').params.formDescription }}`, | ||
) as string; | ||
} | ||
|
||
let buttonLabel = options.buttonLabel; | ||
if (!buttonLabel) { | ||
buttonLabel = | ||
(context.evaluateExpression( | ||
`{{ $('${trigger?.name}').params.options?.buttonLabel }}`, | ||
) as string) || 'Submit'; | ||
} | ||
|
||
const responseMode = 'onReceived'; | ||
|
||
let redirectUrl; | ||
|
||
const connectedNodes = context.getChildNodes(context.getNode().name); | ||
|
||
const hasNextPage = connectedNodes.some( | ||
(node) => !node.disabled && (node.type === FORM_NODE_TYPE || node.type === WAIT_NODE_TYPE), | ||
); | ||
|
||
if (hasNextPage) { | ||
redirectUrl = context.evaluateExpression('{{ $execution.resumeFormUrl }}') as string; | ||
} | ||
|
||
const appendAttribution = context.evaluateExpression( | ||
`{{ $('${trigger?.name}').params.options?.appendAttribution === false ? false : true }}`, | ||
) as boolean; | ||
|
||
renderForm({ | ||
context, | ||
res, | ||
formTitle: title, | ||
formDescription: description, | ||
formFields: fields, | ||
responseMode, | ||
mode, | ||
redirectUrl, | ||
appendAttribution, | ||
buttonLabel, | ||
}); | ||
|
||
return { | ||
noWebhookResponse: true, | ||
}; | ||
}; |
Oops, something went wrong.