n8n-nodes-starter/nodes/ExampleNode/ExampleNode.node.ts

73 lines
2.1 KiB
TypeScript
Raw Normal View History

2019-10-03 08:53:03 +02:00
import { IExecuteFunctions } from 'n8n-core';
2022-06-29 14:54:53 +02:00
import { INodeExecutionData, INodeType, INodeTypeDescription, NodeOperationError } from 'n8n-workflow';
2019-10-03 08:53:03 +02:00
export class ExampleNode implements INodeType {
description: INodeTypeDescription = {
displayName: 'Example Node',
name: 'exampleNode',
group: ['transform'],
version: 1,
description: 'Basic Example Node',
defaults: {
name: 'Example Node',
color: '#772244',
},
inputs: ['main'],
outputs: ['main'],
properties: [
// Node properties which the user gets displayed and
// can change on the node.
{
displayName: 'My String',
name: 'myString',
type: 'string',
default: '',
placeholder: 'Placeholder value',
description: 'The description text',
2022-01-17 18:29:15 +01:00
},
],
2019-10-03 08:53:03 +02:00
};
// The function below is responsible for actually doing whatever this node
// is supposed to do. In this case, we're just appending the `myString` property
// with whatever the user has entered.
// You can make async calls and use `await`.
2019-10-03 08:53:03 +02:00
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
let item: INodeExecutionData;
let myString: string;
2022-06-27 15:09:59 +02:00
// Iterates over all input items and add the key "myString" with the
2019-10-03 08:53:03 +02:00
// value the parameter "myString" resolves to.
// (This could be a different value for each item in case it contains an expression)
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
try {
myString = this.getNodeParameter('myString', itemIndex, '') as string;
item = items[itemIndex];
item.json['myString'] = myString;
} catch (error) {
// This node should never fail but we want to showcase how
// to handle errors.
if (this.continueOnFail()) {
items.push({json: this.getInputData(itemIndex)[0].json, error});
} else {
// Adding `itemIndex` allows other workflows to handle this error
2022-06-29 14:54:53 +02:00
if (error.context) {
error.context.itemIndex = itemIndex;
throw error;
}
throw new NodeOperationError(this.getNode(), error.message, {
itemIndex,
});
}
}
2019-10-03 08:53:03 +02:00
}
return this.prepareOutputData(items);
}
}