Files
n8n-enterprise-unlocked/packages/nodes-base/nodes/RenameKeys/RenameKeys.node.ts
Jan Oberhauser bdb84130d6 feat(core): Add support for pairedItem (beta) (#3012)
*  Add pairedItem support

* 👕 Fix lint issue

* 🐛 Fix resolution in frontend

* 🐛 Fix resolution issue

* 🐛 Fix resolution in frontend

* 🐛 Fix another resolution issue in frontend

*  Try to automatically add pairedItem data if possible

*  Cleanup

*  Display expression errors in editor UI

* 🐛 Fix issue that it did not display errors in production

* 🐛 Fix auto-fix of missing pairedItem data

* 🐛 Fix frontend resolution for not executed nodes

*  Fail execution on pairedItem resolve issue and display information
about itemIndex and runIndex

*  Allow that pairedItem is only set to number if runIndex is 0

*  Improve Expression Errors

*  Remove no longer needed code

*  Make errors more helpful

*  Add additional errors

* 👕 Fix lint issue

*  Add pairedItem support to core nodes

*  Improve support in Merge-Node

*  Fix issue with not correctly converted incoming pairedItem data

* 🐛 Fix frontend resolve issue

* 🐛 Fix frontend parameter name display issue

*  Improve errors

* 👕 Fix lint issue

*  Improve errors

*  Make it possible to display parameter name in error messages

*  Improve error messages

*  Fix error message

*  Improve error messages

*  Add another error message

*  Simplify
2022-06-03 17:25:07 +02:00

122 lines
3.0 KiB
TypeScript

import { IExecuteFunctions } from 'n8n-core';
import {
INodeExecutionData,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import {
get,
set,
unset,
} from 'lodash';
interface IRenameKey {
currentKey: string;
newKey: string;
}
export class RenameKeys implements INodeType {
description: INodeTypeDescription = {
displayName: 'Rename Keys',
name: 'renameKeys',
icon: 'fa:edit',
group: ['transform'],
version: 1,
description: 'Renames keys',
defaults: {
name: 'Rename Keys',
color: '#772244',
},
inputs: ['main'],
outputs: ['main'],
properties: [
{
displayName: 'Keys',
name: 'keys',
placeholder: 'Add new key',
description: 'Adds a key which should be renamed',
type: 'fixedCollection',
typeOptions: {
multipleValues: true,
sortable: true,
},
default: {},
options: [
{
displayName: 'Key',
name: 'key',
values: [
{
displayName: 'Current Key Name',
name: 'currentKey',
type: 'string',
default: '',
placeholder: 'currentKey',
description: 'The current name of the key. It is also possible to define deep keys by using dot-notation like for example: "level1.level2.currentKey".',
},
{
displayName: 'New Key Name',
name: 'newKey',
type: 'string',
default: '',
placeholder: 'newKey',
description: 'the name the key should be renamed to. It is also possible to define deep keys by using dot-notation like for example: "level1.level2.newKey".',
},
],
},
],
},
],
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: INodeExecutionData[] = [];
let item: INodeExecutionData;
let newItem: INodeExecutionData;
let renameKeys: IRenameKey[];
let value: any; // tslint:disable-line:no-any
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
renameKeys = this.getNodeParameter('keys.key', itemIndex, []) as IRenameKey[];
item = items[itemIndex];
// Copy the whole JSON data as data on any level can be renamed
newItem = {
json: JSON.parse(JSON.stringify(item.json)),
pairedItem: {
item: itemIndex,
},
};
if (item.binary !== undefined) {
// Reference binary data if any exists. We can reference it
// as this nodes does not change it
newItem.binary = item.binary;
}
renameKeys.forEach((renameKey) => {
if (renameKey.currentKey === '' || renameKey.newKey === '' || renameKey.currentKey === renameKey.newKey) {
// Ignore all which do not have all the values set or if the new key is equal to the current key
return;
}
value = get(item.json, renameKey.currentKey as string);
if (value === undefined) {
return;
}
set(newItem.json, renameKey.newKey, value);
unset(newItem.json, renameKey.currentKey as string);
});
returnData.push(newItem);
}
return [returnData];
}
}