mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
⚡ Fix JotForm-Trigger Node
This commit is contained in:
parent
9d428c0ae2
commit
73010e1ae4
|
@ -1,3 +1,5 @@
|
||||||
|
import * as formidable from 'formidable';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
IHookFunctions,
|
IHookFunctions,
|
||||||
IWebhookFunctions,
|
IWebhookFunctions,
|
||||||
|
@ -16,6 +18,12 @@ import {
|
||||||
jotformApiRequest,
|
jotformApiRequest,
|
||||||
} from './GenericFunctions';
|
} from './GenericFunctions';
|
||||||
|
|
||||||
|
|
||||||
|
interface IQuestionData {
|
||||||
|
name: string;
|
||||||
|
text: string;
|
||||||
|
}
|
||||||
|
|
||||||
export class JotFormTrigger implements INodeType {
|
export class JotFormTrigger implements INodeType {
|
||||||
description: INodeTypeDescription = {
|
description: INodeTypeDescription = {
|
||||||
displayName: 'JotForm Trigger',
|
displayName: 'JotForm Trigger',
|
||||||
|
@ -56,6 +64,20 @@ export class JotFormTrigger implements INodeType {
|
||||||
default: '',
|
default: '',
|
||||||
description: '',
|
description: '',
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
displayName: 'Resolve Data',
|
||||||
|
name: 'resolveData',
|
||||||
|
type: 'boolean',
|
||||||
|
default: true,
|
||||||
|
description: 'By default does the webhook-data use internal keys instead of the names.<br />If this option gets activated it will resolve the keys automatically to the actual names.',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
displayName: 'Only Answers',
|
||||||
|
name: 'onlyAnswers',
|
||||||
|
type: 'boolean',
|
||||||
|
default: true,
|
||||||
|
description: 'Returns only the answers of the form and not any of the other data.',
|
||||||
|
},
|
||||||
],
|
],
|
||||||
|
|
||||||
};
|
};
|
||||||
|
@ -141,10 +163,75 @@ export class JotFormTrigger implements INodeType {
|
||||||
//@ts-ignore
|
//@ts-ignore
|
||||||
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
|
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
|
||||||
const req = this.getRequestObject();
|
const req = this.getRequestObject();
|
||||||
return {
|
const formId = this.getNodeParameter('form') as string;
|
||||||
workflowData: [
|
const resolveData = this.getNodeParameter('resolveData', false) as boolean;
|
||||||
this.helpers.returnJsonArray(req.body)
|
const onlyAnswers = this.getNodeParameter('onlyAnswers', false) as boolean;
|
||||||
],
|
|
||||||
};
|
const form = new formidable.IncomingForm();
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
|
||||||
|
form.parse(req, async (err, data, files) => {
|
||||||
|
|
||||||
|
const rawRequest = JSON.parse(data.rawRequest as string);
|
||||||
|
data.rawRequest = rawRequest;
|
||||||
|
|
||||||
|
let returnData: IDataObject;
|
||||||
|
if (resolveData === false) {
|
||||||
|
if (onlyAnswers === true) {
|
||||||
|
returnData = data.rawRequest as unknown as IDataObject;
|
||||||
|
} else {
|
||||||
|
returnData = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
resolve({
|
||||||
|
workflowData: [
|
||||||
|
this.helpers.returnJsonArray(returnData),
|
||||||
|
],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resolve the data by requesting the information via API
|
||||||
|
const endpoint = `/form/${formId}/questions`;
|
||||||
|
const responseData = await jotformApiRequest.call(this, 'GET', endpoint, {});
|
||||||
|
|
||||||
|
// Create a dictionary to resolve the keys
|
||||||
|
const questionNames: IDataObject = {};
|
||||||
|
for (const question of Object.values(responseData.content) as IQuestionData[]) {
|
||||||
|
questionNames[question.name] = question.text;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resolve the keys
|
||||||
|
let questionKey: string;
|
||||||
|
const questionsData: IDataObject = {};
|
||||||
|
for (const key of Object.keys(rawRequest)) {
|
||||||
|
if (!key.includes('_')) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
questionKey = key.split('_').slice(1).join('_');
|
||||||
|
if (questionNames[questionKey] === undefined) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
questionsData[questionNames[questionKey] as string] = rawRequest[key];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (onlyAnswers === true) {
|
||||||
|
returnData = questionsData as unknown as IDataObject;
|
||||||
|
} else {
|
||||||
|
// @ts-ignore
|
||||||
|
data.rawRequest = questionsData;
|
||||||
|
returnData = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
resolve({
|
||||||
|
workflowData: [
|
||||||
|
this.helpers.returnJsonArray(returnData),
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -207,6 +207,7 @@
|
||||||
"@types/cron": "^1.6.1",
|
"@types/cron": "^1.6.1",
|
||||||
"@types/eventsource": "^1.1.2",
|
"@types/eventsource": "^1.1.2",
|
||||||
"@types/express": "^4.16.1",
|
"@types/express": "^4.16.1",
|
||||||
|
"@types/formidable": "^1.0.31",
|
||||||
"@types/gm": "^1.18.2",
|
"@types/gm": "^1.18.2",
|
||||||
"@types/imap-simple": "^4.2.0",
|
"@types/imap-simple": "^4.2.0",
|
||||||
"@types/jest": "^24.0.18",
|
"@types/jest": "^24.0.18",
|
||||||
|
@ -232,6 +233,7 @@
|
||||||
"cheerio": "^1.0.0-rc.3",
|
"cheerio": "^1.0.0-rc.3",
|
||||||
"cron": "^1.7.2",
|
"cron": "^1.7.2",
|
||||||
"eventsource": "^1.0.7",
|
"eventsource": "^1.0.7",
|
||||||
|
"formidable": "^1.2.1",
|
||||||
"glob-promise": "^3.4.0",
|
"glob-promise": "^3.4.0",
|
||||||
"gm": "^1.23.1",
|
"gm": "^1.23.1",
|
||||||
"googleapis": "^46.0.0",
|
"googleapis": "^46.0.0",
|
||||||
|
|
Loading…
Reference in a new issue