Skip to content

Commit

Permalink
feat: Add new expression variables and support for luxon
Browse files Browse the repository at this point in the history
* 🔨 scaffolding for  and

* 🔨 added autocomplete

* 🔨 N8N-2961-New-expression-variables

* 🔨 added luxon DateTime to expressions and Functions node, replased  with , clean up

* 🔨 added  and , fixed  return values

* 🔨 added tests for new variables

* 🔨 removed unnecessary import

* 🔨 return type fix

* 🔨 working on review, wip

* 🔨 working on review, improved errors, wip

* 🔨 fixed disappearing error message box

* 🔨 excluded variables from function node, added jmespath setup

* :hamer: added $jmsepath to function nodes

* 🔨 replacing proxy with data when using jmespath

* 🔨 renamed function

* 🔨 updated tips to function nodes

* 🔨 fixes for errors messages

* 🔨 review fixes

* 🔨 removed $input and $() from autocomplete

* ⚡ removed comments

* ⚡ Remove unused code

Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
  • Loading branch information
michael-radency and janober authored Mar 13, 2022
1 parent a957142 commit e8500e6
Show file tree
Hide file tree
Showing 11 changed files with 468 additions and 14 deletions.
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/editor-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
},
"dependencies": {
"@fontsource/open-sans": "^4.5.0",
"luxon": "^2.3.0",
"n8n-design-system": "~0.13.0",
"monaco-editor": "^0.29.1",
"timeago.js": "^4.0.2",
Expand All @@ -45,6 +46,7 @@
"@types/lodash.camelcase": "^4.3.6",
"@types/lodash.get": "^4.4.6",
"@types/lodash.set": "^4.3.6",
"@types/luxon": "^2.0.9",
"@types/node": "14.17.27",
"@types/quill": "^2.0.1",
"@types/uuid": "^8.3.2",
Expand Down
38 changes: 33 additions & 5 deletions packages/editor-ui/src/components/CodeEdit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

<script lang="ts">
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
import {DateTime} from 'luxon';
import { genericHelpers } from '@/components/mixins/genericHelpers';
import { workflowHelpers } from '@/components/mixins/workflowHelpers';
Expand Down Expand Up @@ -176,13 +177,39 @@ export default mixins(
const autoCompleteItems = [
`function $evaluateExpression(expression: string, itemIndex?: number): any {};`,
`function getNodeParameter(parameterName: string, itemIndex: number, fallbackValue?: any): any {};`,
`function getWorkflowStaticData(type: string): object {};`,
`function $item(itemIndex: number, runIndex?: number) {};`,
`function $items(nodeName?: string, outputIndex?: number, runIndex?: number) {};`,
`function getWorkflowStaticData(type: string): {};`,
`function $item(itemIndex: number, runIndex?: number): {};`,
`function $items(nodeName?: string, outputIndex?: number, runIndex?: number): {};`,
];
const baseKeys = ['$env', '$executionId', '$mode', '$parameter', '$position', '$resumeWebhookUrl', '$workflow'];
const additionalKeys = ['$json', '$binary'];
const baseKeys = [
'$env',
'$executionId',
'$mode',
'$parameter',
'$resumeWebhookUrl',
'$workflow',
'$now',
'$today',
'$thisRunIndex',
'DateTime',
'Duration',
'Interval',
];
const functionItemKeys = [
'$json',
'$binary',
'$position',
'$thisItem',
'$thisItemIndex',
];
const additionalKeys: string[] = [];
if (this.codeAutocomplete === 'functionItem') {
additionalKeys.push(...functionItemKeys);
}
if (executedWorkflow && connectionInputData && connectionInputData.length) {
baseKeys.push(...additionalKeys);
} else {
Expand Down Expand Up @@ -216,6 +243,7 @@ export default mixins(
} catch(error) {}
}
autoCompleteItems.push(`const $node = ${JSON.stringify(nodes)}`);
autoCompleteItems.push(`function $jmespath(jsonDoc: object, query: string): {};`);
if (this.codeAutocomplete === 'function') {
if (connectionInputData) {
Expand Down
1 change: 1 addition & 0 deletions packages/editor-ui/src/components/mixins/pushConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ export const pushConnection = mixins(
title: 'Problem executing workflow',
message: runDataExecutedErrorMessage,
type: 'error',
duration: 0,
});
} else {
// Workflow did execute without a problem
Expand Down
4 changes: 3 additions & 1 deletion packages/nodes-base/nodes/Function/Function.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export class Function implements INodeType {
type: 'string',
default: `// Code here will run only once, no matter how many input items there are.
// More info and help: https://docs.n8n.io/nodes/n8n-nodes-base.function
// Tip: You can use luxon for dates and $jmespath for querying JSON structures
// Loop over inputs and add a new field called 'myNewField' to the JSON of each one
for (item of items) {
Expand Down Expand Up @@ -151,7 +152,8 @@ return items;`,
// Try to find the line number which contains the error and attach to error message
const stackLines = error.stack.split('\n');
if (stackLines.length > 0) {
const lineParts = stackLines[1].split(':');
stackLines.shift();
const lineParts = stackLines.find((line: string) => line.includes('Function')).split(':');
if (lineParts.length > 2) {
const lineNumber = lineParts.splice(-2, 1);
if (!isNaN(lineNumber)) {
Expand Down
4 changes: 3 additions & 1 deletion packages/nodes-base/nodes/FunctionItem/FunctionItem.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class FunctionItem implements INodeType {
type: 'string',
default: `// Code here will run once per input item.
// More info and help: https://docs.n8n.io/nodes/n8n-nodes-base.functionItem
// Tip: You can use luxon for dates and $jmespath for querying JSON structures
// Add a new field called 'myNewField' to the JSON of the item
item.myNewField = 1;
Expand Down Expand Up @@ -138,7 +139,8 @@ return item;`,
// Try to find the line number which contains the error and attach to error message
const stackLines = error.stack.split('\n');
if (stackLines.length > 0) {
const lineParts = stackLines[1].split(':');
stackLines.shift();
const lineParts = stackLines.find((line: string) => line.includes('FunctionItem')).split(':');
if (lineParts.length > 2) {
const lineNumber = lineParts.splice(-2, 1);
if (!isNaN(lineNumber)) {
Expand Down
4 changes: 4 additions & 0 deletions packages/workflow/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
"devDependencies": {
"@types/express": "^4.17.6",
"@types/jest": "^26.0.13",
"@types/jmespath": "^0.15.0",
"@types/lodash.get": "^4.4.6",
"@types/lodash.merge": "^4.6.6",
"@types/luxon": "^2.0.9",
"@types/lodash.set": "^4.3.6",
"@types/node": "14.17.27",
"@types/xml2js": "^0.4.3",
Expand All @@ -48,10 +50,12 @@
"typescript": "~4.3.5"
},
"dependencies": {
"jmespath": "^0.16.0",
"lodash.get": "^4.4.2",
"lodash.isequal": "^4.5.0",
"lodash.merge": "^4.6.2",
"lodash.set": "^4.3.2",
"luxon": "^2.3.0",
"riot-tmpl": "^3.0.8",
"xml2js": "^0.4.23"
},
Expand Down
8 changes: 8 additions & 0 deletions packages/workflow/src/Expression.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// @ts-ignore
import * as tmpl from 'riot-tmpl';
import { DateTime, Duration, Interval } from 'luxon';

// eslint-disable-next-line import/no-cycle
import {
INode,
Expand Down Expand Up @@ -114,6 +116,12 @@ export class Expression {

// @ts-ignore
data.document = {};

// @ts-ignore
data.DateTime = DateTime;
data.Interval = Interval;
data.Duration = Duration;

// @ts-ignore
data.constructor = {};

Expand Down
7 changes: 7 additions & 0 deletions packages/workflow/src/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,13 @@ export interface IWorkflowDataProxyData {
$parameter: any;
$position: any;
$workflow: any;
$: any;
$input: any;
$thisItem: any;
$thisRunIndex: number;
$thisItemIndex: number;
$now: any;
$today: any;
}

export type IWorkflowDataProxyAdditionalKeys = IDataObject;
Expand Down
Loading

0 comments on commit e8500e6

Please sign in to comment.