n8n/packages/nodes-base/nodes/Code/Code.node.ts

139 lines
3.8 KiB
TypeScript
Raw Normal View History

feat(Code Node): create Code node (#3965) * Introduce node deprecation (#3930) :sparkles: Introduce node deprecation * :construction: Scaffold out Code node * :shirt: Fix lint * :blue_book: Create types file * :truck: Rename theme * :fire: Remove unneeded prop * :zap: Override keybindings * :zap: Expand lintings * :zap: Create editor content getter * :truck: Ensure all helpers use `$` * :sparkles: Add autocompletion * :zap: Filter out welcome note node * :zap: Convey error line number * :zap: Highlight error line * :zap: Restore logging from node * :sparkles: More autocompletions * :zap: Streamline completions * :pencil2: Update placeholders * :zap: Update linter to new methods * :fire: Remove `$nodeItem` completions * :zap: Re-update placeholders * :art: Fix formatting * :package: Update `package-lock.json` * :zap: Refresh with multi-line empty string * :zap: Account for syntax errors * :fire: Remove unneeded variant * :zap: Minor improvements * :zap: Add more autocompletions * :truck: Rename extension * :fire: Remove outdated comments * :truck: Rename field * :sparkles: More autocompletions * :zap: Fix up error display when empty text * :fire: Remove logging * :sparkles: More error validation * :bug: Fix `pairedItem` to `pairedItem()` * :zap: Add item to validation info * :package: Update `package-lock.json` * :zap: Leftover fixes * :zap: Set `insertNewlineAndIndent` * :package: Update `package-lock.json` * :package: Re-update `package-lock.json` * :shirt: Add lint exception * :blue_book: Add type to mixin type * Clean up comment * :zap: Refactor completion per new requirements * :zap: Adjust placeholders * :zap: Add `json` autocompletions for `$input` * :art: Set border * :zap: Restore local completion source * :zap: Implement autocompletion for imports * :zap: Add `.*` to follow user typing on autocompletion * :blue_book: Fix typings in autocompletions * :shirt: Add linting for use of `item()` * :package: Update `package-lock.json` * :bug: Fix for `$items(nodeName)[0]` * :zap: Filter down built-in modules list * :zap: Refactor error handling * :zap: Linter and validation improvements * :zap: Apply review feedback * :recycle: More general refactorings * :zap: Add dot notation utility * Customize input handler * :zap: Support `.json.` completions * :zap: Adjust placeholder * :zap: Sort imports * :fire: Remove blank rows addition * :zap: Add more error validation * :package: Update `package-lock.json` * :zap: Make date logging consistent * :wrench: Adjust linting highlight range * :zap: Add line numbers to each item mode errors * :zap: Allow for links in error descriptions * :zap: More input validation * :zap: Expand linting to loops * :zap: Deprecate Function and Function Item nodes * :bug: Fix placeholder syntax * :blue_book: Narrow down type * :truck: Rename using kebab-case * :fire: Remove `mapGetters` * :pencil2: Fix casing * :zap: Adjust import for type * :pencil2: Fix quotes * :bug: Fix `activeNode` reference * :zap: Use constant * :fire: Remove logging * :pencil2: Fix typo * :zap: Add missing `notice` * :pencil2: Add tags * :pencil2: Fix alias * :pencil2: Update copy * :fire: Remove wrong linting * :pencil2: Update copy * :zap: Add validation for `null` * :zap: Add validation for non-object and non-array * :zap: Add validation for non-array with json * :pencil2: Intentionally use wrong spelling * :zap: More validation * :pencil2: More copy updates * :pencil2: Placeholder updates * :rewind: Restore spelling * :zap: Fix var name * :pencil2: More copy updates * :zap: Add luxon autocompletions * :zap: Make scrollable * :zap: Fix comma from merge conflict resolution * :package: Update `package-lock.json` * :shirt: Fix lint detail * :art: Set font family * :zap: Bring in expressions fix * :recycle: Address feedback * :zap: Exclude codemirror packages from render chunks * :bug: Fix placeholder not showing on first load * feat(editor-ui): Replace `lezer` with `esprima` in client linter (#4192) * :fire: Remove addition from misresolved conflict * :zap: Replace `lezer` with `esprima` in client linter * :zap: Add missing key * :package: Update `package-lock.json` * :zap: Match dependencies * :package: Update `package-lock.json` * :package: Re-update `package-lock.json` * :zap: Match whitespace * :bug: Fix selection * :zap: Expand validation * :fire: Remove validation * :pencil2: Update copy * :truck: Move to constants * :zap: More `null` validation * :zap: Support `all()` with index to access item * :zap: Gloss over n8n syntax error * :art: Re-style diagnostic button * :fire: Remove `item` as `itemAlias` * :zap: Add linting for `item.json` in single item mode * :zap: Refactor to add label info descriptions * :zap: More autocompletions * :shirt: Fix lint * :zap: Simplify typings * feat(nodes-base): Multiline autocompletion for `code-node-editor` (#4220) * :zap: Simplify typings * :zap: Consolidate helpers in utils * :zap: Multiline autocompletion for standalone vars * :fire: Remove unneeded mixins * :pencil2: Update copy * :pencil2: Prep TODOs * :zap: Multiline completion for `$input.method` + `$input.item` * :fire: Remove unused method * :fire: Remove another unused method * :truck: Move luxon strings to helpers * :zap: Multiline autocompletion for methods output * :zap: Refactor to use optional chaining * :shirt: Fix lint * :pencil2: Update TODOs * :zap: Multiline autocompletion for `json` fields * :blue_book: Add typings * :zap: De-duplicate callback to forEach * :bug: Fix autocompletions not working with leading whitespace * :globe_with_meridians: Apply i18n * :shirt: Fix lint * :constructor: Second-period var usage completions * :shirt: Fix lint * :shirt: Add exception * :zap: Add completion telemetry * :blue_book: Add typing * :zap: Major refactoring to organize * :bug: Fix multiline `.all()[index]` * :bug: Do not autoclose square brackets prior to `.json` * :bug: Fix accessor for multiline `jsonField` completions * :zap: Add completions for half-assignments * :bug: Fix `jsonField` completions for `x.json` * :pencil2: Improve comments * :bug: Fix `.json[field]` for multiline matches * :zap: Cleanup * :package: Update `package-lock.json` * :shirt: Fix lint * :bug: Rely on original value for custom matcher * :zap: Create `customMatcherJsonFieldCompletions` to simplify setup * :bug: Include selector in `customMatcherJsonField` completions * :pencil2: Make naming consistent * :pencil2: Add docline * :zap: Finish self-review cleanup * :fire: Remove outdated comment * :pushpin: Pin luxon to major-minor * :pencil2: Fix typo * :package: Update `package-lock.json` * :package: Update `package-lock.json` * :package: Re-update `package-lock.json` * :heavy_plus_sign: Add `luxon` for Gmail node * :package: Update `package-lock.json` * :zap: Replace Function with Code in suggested nodes * :bug: Fix `$prevNode` completions * :pencil2: Update `$execution.mode` copy * :zap: Separate luxon getters from methods * :zap: Adjusting linter to tolerate `.binary` * :zap: Adjust top-level item keys check * :zap: Anticipate user expecting `item` to pre-exist * :zap: Add linting for legacy item access * :zap: Add hint for attempted `items` access * :zap: Add keybinding for toggling comments * :pencil2: Update copy of `all`, `first`, `last` and `itemMatching` * :bug: Make `input.all()` etc act on copies * :package: Update `package-lock.json` * :bug: Fix guard in `$input.last()` * :recycle: Address Jan's feedback * :arrow_up: Upgrade `eslint-plugin-n8n-nodes-base` * :package: Update `package-lock.json` * :fire: Remove unneeded exceptions * :zap: Restore placeholder logic * :zap: Add placeholders to client * :zap: Account for shadow item * :pencil2: More completion info labels * :shirt: Fix lint * :pencil2: Update copy * :pencil2: Update copy * :pencil2: More copy updates * :package: Update `package-lock.json` * :zap: Add more validation * :zap: Add placheolder on first load * Replace `Cmd` with `Mod` * :package: Update `package-lock.json`
2022-10-13 05:28:02 -07:00
import {
IExecuteFunctions,
INodeExecutionData,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import { getSandboxContext, Sandbox } from './Sandbox';
import { standardizeOutput } from './utils';
import type { CodeNodeMode } from './utils';
export class Code implements INodeType {
description: INodeTypeDescription = {
displayName: 'Code',
name: 'code',
icon: 'fa:code',
group: ['transform'],
version: 1,
description: 'Run custom JavaScript code',
defaults: {
name: 'Code',
color: '#FF9922',
},
inputs: ['main'],
outputs: ['main'],
parameterPane: 'wide',
properties: [
{
displayName: 'Mode',
name: 'mode',
type: 'options',
noDataExpression: true,
options: [
{
name: 'Run Once for All Items',
value: 'runOnceForAllItems',
description: 'Run this code only once, no matter how many input items there are',
},
{
name: 'Run Once for Each Item',
value: 'runOnceForEachItem',
description: 'Run this code as many times as there are input items',
},
],
default: 'runOnceForAllItems',
},
{
displayName: 'JavaScript',
name: 'jsCode',
typeOptions: {
editor: 'codeNodeEditor',
},
type: 'string',
default: '', // set by component
description:
'JavaScript code to execute.<br><br>Tip: You can use luxon vars like <code>$today</code> for dates and <code>$jmespath</code> for querying JSON structures. <a href="https://docs.n8n.io/nodes/n8n-nodes-base.function">Learn more</a>.',
noDataExpression: true,
},
{
displayName:
'Type <code>$</code> for a list of <a target="_blank" href="https://docs.n8n.io/code-examples/methods-variables-reference/">special vars/methods</a>. Debug by using <code>console.log()</code> statements and viewing their output in the browser console.',
name: 'notice',
type: 'notice',
default: '',
},
],
};
async execute(this: IExecuteFunctions) {
let items = this.getInputData();
const nodeMode = this.getNodeParameter('mode', 0) as CodeNodeMode;
const workflowMode = this.getMode();
// ----------------------------------
// runOnceForAllItems
// ----------------------------------
if (nodeMode === 'runOnceForAllItems') {
const jsCodeAllItems = this.getNodeParameter('jsCode', 0) as string;
const context = getSandboxContext.call(this);
const sandbox = new Sandbox(context, workflowMode, nodeMode);
if (workflowMode === 'manual') {
sandbox.on('console.log', this.sendMessageToUI);
}
try {
items = await sandbox.runCode(jsCodeAllItems);
} catch (error) {
if (!this.continueOnFail()) return Promise.reject(error);
items = [{ json: { error: error.message } }];
}
for (const item of items) {
standardizeOutput(item.json);
}
return this.prepareOutputData(items);
}
// ----------------------------------
// runOnceForEachItem
// ----------------------------------
const returnData: INodeExecutionData[] = [];
for (let index = 0; index < items.length; index++) {
let item = items[index];
const jsCodeEachItem = this.getNodeParameter('jsCode', index) as string;
const context = getSandboxContext.call(this, index);
const sandbox = new Sandbox(context, workflowMode, nodeMode);
if (workflowMode === 'manual') {
sandbox.on('console.log', this.sendMessageToUI);
}
try {
item = await sandbox.runCode(jsCodeEachItem, index);
} catch (error) {
if (!this.continueOnFail()) return Promise.reject(error);
returnData.push({ json: { error: error.message } });
}
if (item) {
returnData.push({
json: standardizeOutput(item.json),
pairedItem: { item: index },
...(item.binary && { binary: item.binary }),
});
}
}
return this.prepareOutputData(returnData);
}
}