2023-09-19 03:16:35 -07:00
|
|
|
import type {
|
|
|
|
IDataObject,
|
|
|
|
IExecuteFunctions,
|
|
|
|
INode,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodeProperties,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
import { NodeOperationError } from 'n8n-workflow';
|
|
|
|
|
2023-10-27 05:15:02 -07:00
|
|
|
import { updateDisplayOptions } from '../../../utils/utilities';
|
2023-09-19 03:16:35 -07:00
|
|
|
import {
|
|
|
|
parseJsonParameter,
|
|
|
|
validateEntry,
|
|
|
|
composeReturnItem,
|
|
|
|
resolveRawData,
|
|
|
|
} from './helpers/utils';
|
|
|
|
import type { SetField, SetNodeOptions } from './helpers/interfaces';
|
|
|
|
|
|
|
|
const properties: INodeProperties[] = [
|
|
|
|
{
|
|
|
|
displayName: 'Fields to Set',
|
|
|
|
name: 'fields',
|
|
|
|
placeholder: 'Add Field',
|
|
|
|
type: 'fixedCollection',
|
|
|
|
description: 'Edit existing fields or add new ones to modify the output data',
|
|
|
|
typeOptions: {
|
|
|
|
multipleValues: true,
|
|
|
|
sortable: true,
|
|
|
|
},
|
|
|
|
default: {},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'values',
|
|
|
|
displayName: 'Values',
|
|
|
|
values: [
|
|
|
|
{
|
|
|
|
displayName: 'Name',
|
|
|
|
name: 'name',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
placeholder: 'e.g. fieldName',
|
|
|
|
description:
|
|
|
|
'Name of the field to set the value of. Supports dot-notation. Example: data.person[0].name.',
|
|
|
|
requiresDataPath: 'single',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Type',
|
|
|
|
name: 'type',
|
|
|
|
type: 'options',
|
|
|
|
description: 'The field value type',
|
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-param-options-type-unsorted-items
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'String',
|
|
|
|
value: 'stringValue',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Number',
|
|
|
|
value: 'numberValue',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Boolean',
|
|
|
|
value: 'booleanValue',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Array',
|
|
|
|
value: 'arrayValue',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Object',
|
|
|
|
value: 'objectValue',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'stringValue',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Value',
|
|
|
|
name: 'stringValue',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
type: ['stringValue'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
validateType: 'string',
|
|
|
|
ignoreValidationDuringExecution: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Value',
|
|
|
|
name: 'numberValue',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
type: ['numberValue'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
validateType: 'number',
|
|
|
|
ignoreValidationDuringExecution: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Value',
|
|
|
|
name: 'booleanValue',
|
|
|
|
type: 'options',
|
|
|
|
default: 'true',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'True',
|
|
|
|
value: 'true',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'False',
|
|
|
|
value: 'false',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
type: ['booleanValue'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
validateType: 'boolean',
|
|
|
|
ignoreValidationDuringExecution: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Value',
|
|
|
|
name: 'arrayValue',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
placeholder: 'e.g. [ arrayItem1, arrayItem2, arrayItem3 ]',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
type: ['arrayValue'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
validateType: 'array',
|
|
|
|
ignoreValidationDuringExecution: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Value',
|
|
|
|
name: 'objectValue',
|
|
|
|
type: 'string',
|
|
|
|
default: '={}',
|
|
|
|
typeOptions: {
|
|
|
|
editor: 'json',
|
|
|
|
editorLanguage: 'json',
|
|
|
|
rows: 2,
|
|
|
|
},
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
type: ['objectValue'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
validateType: 'object',
|
|
|
|
ignoreValidationDuringExecution: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const displayOptions = {
|
|
|
|
show: {
|
|
|
|
mode: ['manual'],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export const description = updateDisplayOptions(displayOptions, properties);
|
|
|
|
|
|
|
|
export async function execute(
|
|
|
|
this: IExecuteFunctions,
|
|
|
|
item: INodeExecutionData,
|
|
|
|
i: number,
|
|
|
|
options: SetNodeOptions,
|
|
|
|
rawFieldsData: IDataObject,
|
|
|
|
node: INode,
|
|
|
|
) {
|
|
|
|
try {
|
|
|
|
const fields = this.getNodeParameter('fields.values', i, []) as SetField[];
|
|
|
|
|
|
|
|
const newData: IDataObject = {};
|
|
|
|
|
|
|
|
for (const entry of fields) {
|
2023-10-12 08:07:12 -07:00
|
|
|
if (
|
|
|
|
entry.type === 'objectValue' &&
|
|
|
|
rawFieldsData[entry.name] !== undefined &&
|
|
|
|
entry.objectValue !== undefined &&
|
|
|
|
entry.objectValue !== null
|
|
|
|
) {
|
2023-09-19 03:16:35 -07:00
|
|
|
entry.objectValue = parseJsonParameter(
|
|
|
|
resolveRawData.call(this, rawFieldsData[entry.name] as string, i),
|
|
|
|
node,
|
|
|
|
i,
|
|
|
|
entry.name,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-10-03 01:18:33 -07:00
|
|
|
const { name, value } = validateEntry(
|
|
|
|
entry,
|
|
|
|
node,
|
|
|
|
i,
|
|
|
|
options.ignoreConversionErrors,
|
|
|
|
node.typeVersion,
|
|
|
|
);
|
2023-09-19 03:16:35 -07:00
|
|
|
newData[name] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return composeReturnItem.call(this, i, item, newData, options);
|
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2023-10-10 08:36:20 -07:00
|
|
|
return { json: { error: (error as Error).message, pairedItem: { item: i } } };
|
2023-09-19 03:16:35 -07:00
|
|
|
}
|
|
|
|
throw new NodeOperationError(this.getNode(), error as Error, {
|
|
|
|
itemIndex: i,
|
|
|
|
description: error.description,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|