Some improvements

This commit is contained in:
Jan Oberhauser 2021-03-25 23:45:16 +01:00
parent 0ba526dd67
commit d8883fbefe
4 changed files with 122 additions and 125 deletions

View file

@ -7,7 +7,7 @@ export class UProcApi implements ICredentialType {
name = 'uprocApi'; name = 'uprocApi';
displayName = 'uProc API'; displayName = 'uProc API';
properties = [ properties = [
{ {
displayName: 'Email', displayName: 'Email',
name: 'email', name: 'email',
type: 'string' as NodePropertyTypes, type: 'string' as NodePropertyTypes,

View file

@ -22,13 +22,13 @@ export async function uprocApiRequest(this: IHookFunctions | IExecuteFunctions |
const options: OptionsWithUri = { const options: OptionsWithUri = {
headers: { headers: {
Authorization: `Basic ${token}`, Authorization: `Basic ${token}`,
"User-agent": "n8n" 'User-agent': 'n8n',
}, },
method, method,
qs, qs,
body, body,
uri: uri || `https://api.uproc.io/api/v2/process`, uri: uri || `https://api.uproc.io/api/v2/process`,
json: true json: true,
}; };
try { try {

View file

@ -30,12 +30,12 @@ for (const group of (groups as IDataObject).groups as IDataObject[]) {
displayOptions: { displayOptions: {
show: { show: {
group: [ group: [
group.name group.name,
], ],
}, },
}, },
default: '', default: '',
options: [] options: [],
}; };
const options = []; const options = [];
@ -45,7 +45,7 @@ for (const group of (groups as IDataObject).groups as IDataObject[]) {
const option = { const option = {
name: tool.d as string, name: tool.d as string,
value: tool.k, value: tool.k,
description: (tool.ed as string) + ` <a href="${link}" target='_blank'>Info</a>` description: (tool.ed as string) + ` <a href="${link}" target='_blank'>Info</a>`,
}; };
options.push(option); options.push(option);
} }
@ -82,7 +82,7 @@ for (const tool of (tools as IDataObject).processors as IDataObject[]) {
tool.g, tool.g,
], ],
tool: [ tool: [
tool.k tool.k,
], ],
}, },
}, },

View file

@ -1,145 +1,142 @@
import { import {
IExecuteFunctions, IExecuteFunctions,
} from 'n8n-core'; } from 'n8n-core';
import { import {
IDataObject, IDataObject,
INodeExecutionData, INodeExecutionData,
INodeType, INodeType,
INodeTypeDescription, INodeTypeDescription,
} from 'n8n-workflow'; } from 'n8n-workflow';
import { import {
uprocApiRequest, uprocApiRequest,
} from './GenericFunctions'; } from './GenericFunctions';
import { import {
groupOptions, groupOptions,
} from './GroupDescription'; } from './GroupDescription';
import { import {
toolOperations, toolOperations,
toolParameters, toolParameters,
} from './ToolDescription'; } from './ToolDescription';
export class UProc implements INodeType { export class UProc implements INodeType {
description: INodeTypeDescription = { description: INodeTypeDescription = {
displayName: 'uProc', displayName: 'uProc',
name: 'uproc', name: 'uproc',
icon: 'file:uproc.png', icon: 'file:uproc.png',
group: ['output'], group: ['output'],
version: 1, version: 1,
subtitle: '={{$parameter["tool"]}}', subtitle: '={{$parameter["tool"]}}',
description: 'Consume uProc API', description: 'Consume uProc API',
defaults: { defaults: {
name: 'uProc', name: 'uProc',
color: '#219ef9', color: '#219ef9',
}, },
inputs: ['main'], inputs: ['main'],
outputs: ['main'], outputs: ['main'],
credentials: [ credentials: [
{ {
name: 'uprocApi', name: 'uprocApi',
required: true, required: true,
}, },
], ],
properties: [ properties: [
...groupOptions, ...groupOptions,
...toolOperations, ...toolOperations,
...toolParameters, ...toolParameters,
{ {
displayName: 'Additional Options', displayName: 'Additional Options',
name: 'additionalOptions', name: 'additionalOptions',
type: 'collection', type: 'collection',
placeholder: 'Add Option', placeholder: 'Add Option',
default: {}, default: {},
displayOptions: { displayOptions: {
show: { show: {
group: [ group: [
'audio', 'audio',
'communication', 'communication',
'company', 'company',
'finance', 'finance',
'geographic', 'geographic',
'image', 'image',
'internet', 'internet',
'personal', 'personal',
'product', 'product',
'security', 'security',
'text', 'text',
], ],
}, },
}, },
options: [ options: [
{ {
displayName: 'Data Webhook', displayName: 'Data Webhook',
name: 'dataWebhook', name: 'dataWebhook',
type: 'string', type: 'string',
description: 'URL to send tool response when tool has resolved your request. You can create your own webhook at en <a href="https://beeceptor.com" target="_blank">Beeceptor</a>, <a href="https://www.integromat.com/" target="_blank">Integromat</a>, <a href="https://zapier.com/" target="_blank">Zapier</a> or <a href="https://n8n.io/" target="_blank">n8n</a>', description: 'URL to send tool response when tool has resolved your request. You can create your own webhook at en <a href="https://beeceptor.com" target="_blank">Beeceptor</a>, <a href="https://www.integromat.com/" target="_blank">Integromat</a>, <a href="https://zapier.com/" target="_blank">Zapier</a> or <a href="https://n8n.io/" target="_blank">n8n</a>',
default: '', default: '',
} },
] ],
} },
], ],
}; };
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> { async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData(); const items = this.getInputData();
const returnData: IDataObject[] = []; const returnData: IDataObject[] = [];
const length = items.length as unknown as number; const length = items.length as unknown as number;
let responseData; let responseData;
const group = this.getNodeParameter('group', 0) as string; const group = this.getNodeParameter('group', 0) as string;
const tool = this.getNodeParameter('tool', 0) as string; const tool = this.getNodeParameter('tool', 0) as string;
const additionalOptions = this.getNodeParameter('additionalOptions', 0) as IDataObject; const additionalOptions = this.getNodeParameter('additionalOptions', 0) as IDataObject;
const dataWebhook = additionalOptions.dataWebhook as string;
const dataWebhook = additionalOptions.dataWebhook as string; interface LooseObject {
[key: string]: any; // tslint:disable-line:no-any
}
interface LooseObject { const fields = toolParameters.filter((field) => {
[key: string]: any; return field && field.displayOptions && field.displayOptions.show && field.displayOptions.show.group && field.displayOptions.show.tool &&
} field.displayOptions.show.group.indexOf(group) !== -1 && field.displayOptions.show.tool.indexOf(tool) !== -1;
}).map((field) => {
return field.name;
});
const fields = toolParameters.filter((field) => { const requestPromises = [];
return field && field.displayOptions && field.displayOptions.show && field.displayOptions.show.group && field.displayOptions.show.tool && for (let i = 0; i < length; i++) {
field.displayOptions.show.group.indexOf(group) !== -1 && field.displayOptions.show.tool.indexOf(tool) !== -1; const toolKey = tool.replace(/([A-Z]+)/g, '-$1').toLowerCase();
}).map((field) => { const body: LooseObject = {
return field.name; processor: toolKey,
}); params: {},
};
const requestPromises = []; fields.forEach((field) => {
for (let i = 0; i < length; i++) { if (field && field.length) {
const toolKey = tool.replace(/([A-Z]+)/g, "-$1").toLowerCase(); const data = this.getNodeParameter(field, i) as string;
const body: LooseObject = { body.params[field] = data + '';
processor: toolKey, }
params: {} });
};
//console.log(fields); if (dataWebhook && dataWebhook.length) {
fields.forEach((field) => { body.callback = {};
if (field && field.length) { }
const data = this.getNodeParameter(field, i) as string;
//console.log(field, data);
body.params[field] = data + "";
}
});
if (dataWebhook && dataWebhook.length) { if (dataWebhook && dataWebhook.length) {
body.callback = {}; body.callback.data = dataWebhook;
} }
if (dataWebhook && dataWebhook.length) { //Change to multiple requests
body.callback.data = dataWebhook; responseData = await uprocApiRequest.call(this, 'POST', body);
}
//Change to multiple requests if (Array.isArray(responseData)) {
responseData = await uprocApiRequest.call(this, 'POST', body); returnData.push.apply(returnData, responseData as IDataObject[]);
} else {
if (Array.isArray(responseData)) { returnData.push(responseData as IDataObject);
returnData.push.apply(returnData, responseData as IDataObject[]); }
} else { }
returnData.push(responseData as IDataObject); return [this.helpers.returnJsonArray(returnData)];
} }
}
return [this.helpers.returnJsonArray(returnData)];
}
} }