2025-02-04 09:35:25 -08:00
|
|
|
import { type Response } from 'express';
|
|
|
|
import {
|
|
|
|
type NodeTypeAndVersion,
|
|
|
|
type IWebhookFunctions,
|
|
|
|
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 appendAttribution = context.evaluateExpression(
|
|
|
|
`{{ $('${trigger?.name}').params.options?.appendAttribution === false ? false : true }}`,
|
|
|
|
) as boolean;
|
|
|
|
|
|
|
|
renderForm({
|
|
|
|
context,
|
|
|
|
res,
|
|
|
|
formTitle: title,
|
|
|
|
formDescription: description,
|
|
|
|
formFields: fields,
|
2025-02-19 04:59:38 -08:00
|
|
|
responseMode: 'responseNode',
|
2025-02-04 09:35:25 -08:00
|
|
|
mode,
|
2025-02-19 04:59:38 -08:00
|
|
|
redirectUrl: undefined,
|
2025-02-04 09:35:25 -08:00
|
|
|
appendAttribution,
|
|
|
|
buttonLabel,
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
noWebhookResponse: true,
|
|
|
|
};
|
|
|
|
};
|