2022-07-04 02:12:08 -07:00
|
|
|
/* eslint-disable n8n-nodes-base/node-filename-against-convention */
|
2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IExecuteFunctions,
|
2023-01-27 03:22:44 -08:00
|
|
|
IDataObject,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
2020-12-19 09:22:41 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { uprocApiRequest } from './GenericFunctions';
|
2020-12-19 09:22:41 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { groupOptions } from './GroupDescription';
|
2020-12-19 09:22:41 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { toolOperations, toolParameters } from './ToolDescription';
|
2020-12-19 09:22:41 -08:00
|
|
|
|
|
|
|
export class UProc implements INodeType {
|
2021-03-25 15:45:16 -07:00
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'uProc',
|
|
|
|
name: 'uproc',
|
2022-06-20 07:54:01 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
|
2021-03-25 15:45:16 -07:00
|
|
|
icon: 'file:uproc.png',
|
|
|
|
group: ['output'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["tool"]}}',
|
|
|
|
description: 'Consume uProc API',
|
|
|
|
defaults: {
|
|
|
|
name: 'uProc',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'uprocApi',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
...groupOptions,
|
|
|
|
...toolOperations,
|
|
|
|
...toolParameters,
|
|
|
|
{
|
|
|
|
displayName: 'Additional Options',
|
|
|
|
name: 'additionalOptions',
|
|
|
|
type: 'collection',
|
|
|
|
placeholder: 'Add Option',
|
|
|
|
default: {},
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
group: [
|
|
|
|
'audio',
|
|
|
|
'communication',
|
|
|
|
'company',
|
|
|
|
'finance',
|
|
|
|
'geographic',
|
|
|
|
'image',
|
|
|
|
'internet',
|
|
|
|
'personal',
|
|
|
|
'product',
|
|
|
|
'security',
|
|
|
|
'text',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
displayName: 'Data Webhook',
|
|
|
|
name: 'dataWebhook',
|
|
|
|
type: 'string',
|
2022-02-12 03:06:35 -08:00
|
|
|
description: 'URL to send tool response when tool has resolved your request',
|
2021-03-25 15:45:16 -07:00
|
|
|
default: '',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
|
|
|
const returnData: IDataObject[] = [];
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2021-03-25 15:45:16 -07:00
|
|
|
let responseData;
|
|
|
|
const group = this.getNodeParameter('group', 0) as string;
|
|
|
|
const tool = this.getNodeParameter('tool', 0) as string;
|
|
|
|
const additionalOptions = this.getNodeParameter('additionalOptions', 0) as IDataObject;
|
|
|
|
|
|
|
|
const dataWebhook = additionalOptions.dataWebhook as string;
|
|
|
|
|
|
|
|
interface LooseObject {
|
2022-12-02 06:25:21 -08:00
|
|
|
[key: string]: any;
|
2021-03-25 15:45:16 -07:00
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const fields = toolParameters
|
|
|
|
.filter((field) => {
|
|
|
|
return (
|
2022-12-02 12:54:28 -08:00
|
|
|
field?.displayOptions?.show?.group &&
|
2022-08-17 08:50:24 -07:00
|
|
|
field.displayOptions.show.tool &&
|
|
|
|
field.displayOptions.show.group.indexOf(group) !== -1 &&
|
|
|
|
field.displayOptions.show.tool.indexOf(tool) !== -1
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.map((field) => {
|
|
|
|
return field.name;
|
|
|
|
});
|
2021-03-25 15:45:16 -07:00
|
|
|
|
|
|
|
for (let i = 0; i < length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
const toolKey = tool.replace(/([A-Z]+)/g, '-$1').toLowerCase();
|
|
|
|
const body: LooseObject = {
|
|
|
|
processor: toolKey,
|
|
|
|
params: {},
|
|
|
|
};
|
2021-03-25 15:45:16 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
fields.forEach((field) => {
|
2022-12-02 12:54:28 -08:00
|
|
|
if (field?.length) {
|
2021-07-19 23:58:54 -07:00
|
|
|
const data = this.getNodeParameter(field, i) as string;
|
|
|
|
body.params[field] = data + '';
|
|
|
|
}
|
|
|
|
});
|
2021-03-25 15:45:16 -07:00
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
if (dataWebhook?.length) {
|
2021-07-19 23:58:54 -07:00
|
|
|
body.callback = {};
|
|
|
|
}
|
2021-03-25 15:45:16 -07:00
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
if (dataWebhook?.length) {
|
2021-07-19 23:58:54 -07:00
|
|
|
body.callback.data = dataWebhook;
|
|
|
|
}
|
2021-03-25 15:45:16 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
//Change to multiple requests
|
|
|
|
responseData = await uprocApiRequest.call(this, 'POST', body);
|
2021-03-25 15:45:16 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (Array.isArray(responseData)) {
|
|
|
|
returnData.push.apply(returnData, responseData as IDataObject[]);
|
|
|
|
} else {
|
|
|
|
returnData.push(responseData as IDataObject);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
|
|
|
returnData.push({ error: error.message });
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2021-03-25 15:45:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
|
|
}
|
2020-12-19 09:22:41 -08:00
|
|
|
}
|