From 70286b469e294166dc2a4b611ce5f9e0cd186f22 Mon Sep 17 00:00:00 2001 From: Jan Oberhauser Date: Sat, 15 Feb 2020 17:07:01 -0800 Subject: [PATCH] :sparkles: Make workflow metadata available in expresions and node-functions --- packages/cli/src/ActiveWorkflowRunner.ts | 6 +- packages/cli/src/Server.ts | 4 +- packages/cli/src/TestWebhooks.ts | 4 +- .../cli/src/WorkflowExecuteAdditionalData.ts | 3 +- packages/cli/src/WorkflowHelpers.ts | 3 +- packages/cli/src/WorkflowRunner.ts | 2 +- packages/cli/src/WorkflowRunnerProcess.ts | 2 +- packages/core/src/LoadNodeParameterOptions.ts | 2 +- packages/core/src/NodeExecuteFunctions.ts | 70 +++++++++++++++++++ .../src/components/mixins/workflowHelpers.ts | 8 ++- packages/workflow/src/Interfaces.ts | 19 +++++ packages/workflow/src/Workflow.ts | 21 +++--- packages/workflow/src/WorkflowDataProxy.ts | 29 ++++++++ 13 files changed, 148 insertions(+), 25 deletions(-) diff --git a/packages/cli/src/ActiveWorkflowRunner.ts b/packages/cli/src/ActiveWorkflowRunner.ts index 25255988dc..e8c55d5807 100644 --- a/packages/cli/src/ActiveWorkflowRunner.ts +++ b/packages/cli/src/ActiveWorkflowRunner.ts @@ -123,7 +123,7 @@ export class ActiveWorkflowRunner { } const nodeTypes = NodeTypes(); - const workflow = new Workflow(webhookData.workflowId, workflowData.nodes, workflowData.connections, workflowData.active, nodeTypes, workflowData.staticData, workflowData.settings); + const workflow = new Workflow({ id: webhookData.workflowId, name: workflowData.name, nodes: workflowData.nodes, connections: workflowData.connections, active: workflowData.active, nodeTypes, staticData: workflowData.staticData, settings: workflowData.settings}); // Get the node which has the webhook defined to know where to start from and to // get additional data @@ -225,7 +225,7 @@ export class ActiveWorkflowRunner { } const nodeTypes = NodeTypes(); - const workflow = new Workflow(workflowId, workflowData.nodes, workflowData.connections, workflowData.active, nodeTypes, workflowData.staticData, workflowData.settings); + const workflow = new Workflow({ id: workflowId, name: workflowData.name, nodes: workflowData.nodes, connections: workflowData.connections, active: workflowData.active, nodeTypes, staticData: workflowData.staticData, settings: workflowData.settings }); await this.activeWebhooks!.removeWorkflow(workflow); @@ -345,7 +345,7 @@ export class ActiveWorkflowRunner { throw new Error(`Could not find workflow with id "${workflowId}".`); } const nodeTypes = NodeTypes(); - workflowInstance = new Workflow(workflowId, workflowData.nodes, workflowData.connections, workflowData.active, nodeTypes, workflowData.staticData, workflowData.settings); + workflowInstance = new Workflow({ id: workflowId, name: workflowData.name, nodes: workflowData.nodes, connections: workflowData.connections, active: workflowData.active, nodeTypes, staticData: workflowData.staticData, settings: workflowData.settings }); const canBeActivated = workflowInstance.checkIfWorkflowCanBeActivated(['n8n-nodes-base.start']); if (canBeActivated === false) { diff --git a/packages/cli/src/Server.ts b/packages/cli/src/Server.ts index 971aeb23f0..504d8d9dae 100644 --- a/packages/cli/src/Server.ts +++ b/packages/cli/src/Server.ts @@ -508,7 +508,7 @@ class App { const credentials = await WorkflowCredentials(workflowData.nodes); const additionalData = await WorkflowExecuteAdditionalData.getBase(credentials); const nodeTypes = NodeTypes(); - const workflowInstance = new Workflow(workflowData.id, workflowData.nodes, workflowData.connections, false, nodeTypes, undefined, workflowData.settings); + const workflowInstance = new Workflow({ id: workflowData.id, name: workflowData.name, nodes: workflowData.nodes, connections: workflowData.connections, active: false, nodeTypes, staticData: undefined, settings: workflowData.settings }); const needsWebhook = await this.testWebhooks.needsWebhookData(workflowData, workflowInstance, additionalData, executionMode, sessionId, destinationNode); if (needsWebhook === true) { return { @@ -1088,7 +1088,7 @@ class App { } const nodeTypes = NodeTypes(); - const workflow = new Workflow(workflowId.toString(), workflowData.nodes, workflowData.connections, workflowData.active, nodeTypes, workflowData.staticData, workflowData.settings); + const workflow = new Workflow({ id: workflowId.toString(), name: workflowData.name, nodes: workflowData.nodes, connections: workflowData.connections, active: workflowData.active, nodeTypes, staticData: workflowData.staticData, settings: workflowData.settings }); return this.testWebhooks.cancelTestWebhook(workflowId, workflow); })); diff --git a/packages/cli/src/TestWebhooks.ts b/packages/cli/src/TestWebhooks.ts index 0623979892..8aac38c9c6 100644 --- a/packages/cli/src/TestWebhooks.ts +++ b/packages/cli/src/TestWebhooks.ts @@ -67,7 +67,7 @@ export class TestWebhooks { const workflowData = this.testWebhookData[webhookKey].workflowData; const nodeTypes = NodeTypes(); - const workflow = new Workflow(webhookData.workflowId, workflowData.nodes, workflowData.connections, workflowData.active, nodeTypes, workflowData.staticData, workflowData.settings); + const workflow = new Workflow({ id: webhookData.workflowId, name: workflowData.name, nodes: workflowData.nodes, connections: workflowData.connections, active: workflowData.active, nodeTypes, staticData: workflowData.staticData, settings: workflowData.settings}); // Get the node which has the webhook defined to know where to start from and to // get additional data @@ -206,7 +206,7 @@ export class TestWebhooks { const workflows: Workflow[] = []; for (const webhookKey of Object.keys(this.testWebhookData)) { workflowData = this.testWebhookData[webhookKey].workflowData; - workflow = new Workflow(workflowData.id.toString(), workflowData.nodes, workflowData.connections, workflowData.active, nodeTypes, workflowData.staticData, workflowData.settings); + workflow = new Workflow({ id: workflowData.id.toString(), name: workflowData.name, nodes: workflowData.nodes, connections: workflowData.connections, active: workflowData.active, nodeTypes, staticData: workflowData.staticData, settings: workflowData.settings }); workflows.push(); } diff --git a/packages/cli/src/WorkflowExecuteAdditionalData.ts b/packages/cli/src/WorkflowExecuteAdditionalData.ts index fa7f0abc0a..9d0fe2a038 100644 --- a/packages/cli/src/WorkflowExecuteAdditionalData.ts +++ b/packages/cli/src/WorkflowExecuteAdditionalData.ts @@ -297,7 +297,8 @@ export async function executeWorkflow(workflowInfo: IExecuteWorkflowInfo, additi const nodeTypes = NodeTypes(); - const workflow = new Workflow(workflowInfo.id, workflowData!.nodes, workflowData!.connections, workflowData!.active, nodeTypes, workflowData!.staticData); + const workflowName = workflowData ? workflowData.name : undefined; + const workflow = new Workflow({ id: workflowInfo.id, name: workflowName, nodes: workflowData!.nodes, connections: workflowData!.connections, active: workflowData!.active, nodeTypes, staticData: workflowData!.staticData }); // Does not get used so set it simply to empty string const executionId = ''; diff --git a/packages/cli/src/WorkflowHelpers.ts b/packages/cli/src/WorkflowHelpers.ts index ab723a95e5..90a41952ca 100644 --- a/packages/cli/src/WorkflowHelpers.ts +++ b/packages/cli/src/WorkflowHelpers.ts @@ -90,8 +90,7 @@ export async function executeErrorWorkflow(workflowId: string, workflowErrorData const executionMode = 'error'; const nodeTypes = NodeTypes(); - const workflowInstance = new Workflow(workflowId, workflowData.nodes, workflowData.connections, workflowData.active, nodeTypes, workflowData.staticData, workflowData.settings); - + const workflowInstance = new Workflow({ id: workflowId, name: workflowData.name, nodeTypes, nodes: workflowData.nodes, connections: workflowData.connections, active: workflowData.active, staticData: workflowData.staticData, settings: workflowData.settings}); let node: INode; let workflowStartNode: INode | undefined; diff --git a/packages/cli/src/WorkflowRunner.ts b/packages/cli/src/WorkflowRunner.ts index ec3571a5c2..739a655740 100644 --- a/packages/cli/src/WorkflowRunner.ts +++ b/packages/cli/src/WorkflowRunner.ts @@ -119,7 +119,7 @@ export class WorkflowRunner { const nodeTypes = NodeTypes(); - const workflow = new Workflow(data.workflowData.id as string | undefined, data.workflowData!.nodes, data.workflowData!.connections, data.workflowData!.active, nodeTypes, data.workflowData!.staticData); + const workflow = new Workflow({ id: data.workflowData.id as string | undefined, name: data.workflowData.name, nodes: data.workflowData!.nodes, connections: data.workflowData!.connections, active: data.workflowData!.active, nodeTypes, staticData: data.workflowData!.staticData }); const additionalData = await WorkflowExecuteAdditionalData.getBase(data.credentials); // Register the active execution diff --git a/packages/cli/src/WorkflowRunnerProcess.ts b/packages/cli/src/WorkflowRunnerProcess.ts index 4d8ef4d000..b9472fc66e 100644 --- a/packages/cli/src/WorkflowRunnerProcess.ts +++ b/packages/cli/src/WorkflowRunnerProcess.ts @@ -58,7 +58,7 @@ export class WorkflowRunnerProcess { const nodeTypes = NodeTypes(); await nodeTypes.init(nodeTypesData); - this.workflow = new Workflow(this.data.workflowData.id as string | undefined, this.data.workflowData!.nodes, this.data.workflowData!.connections, this.data.workflowData!.active, nodeTypes, this.data.workflowData!.staticData); + this.workflow = new Workflow({ id: this.data.workflowData.id as string | undefined, name: this.data.workflowData.name, nodes: this.data.workflowData!.nodes, connections: this.data.workflowData!.connections, active: this.data.workflowData!.active, nodeTypes, staticData: this.data.workflowData!.staticData}); const additionalData = await WorkflowExecuteAdditionalData.getBase(this.data.credentials); additionalData.hooks = this.getProcessForwardHooks(); diff --git a/packages/core/src/LoadNodeParameterOptions.ts b/packages/core/src/LoadNodeParameterOptions.ts index 8ff78e41cd..0da8669254 100644 --- a/packages/core/src/LoadNodeParameterOptions.ts +++ b/packages/core/src/LoadNodeParameterOptions.ts @@ -50,7 +50,7 @@ export class LoadNodeParameterOptions { connections: {}, }; - this.workflow = new Workflow(undefined, workflowData.nodes, workflowData.connections, false, nodeTypes, undefined); + this.workflow = new Workflow({ nodes: workflowData.nodes, connections: workflowData.connections, active: false, nodeTypes }); } diff --git a/packages/core/src/NodeExecuteFunctions.ts b/packages/core/src/NodeExecuteFunctions.ts index 2f90d0118f..731f466f66 100644 --- a/packages/core/src/NodeExecuteFunctions.ts +++ b/packages/core/src/NodeExecuteFunctions.ts @@ -27,6 +27,7 @@ import { IWebhookFunctions, IWorkflowDataProxyData, IWorkflowExecuteAdditionalData, + IWorkflowMetadata, NodeHelpers, NodeParameterValue, Workflow, @@ -196,6 +197,19 @@ export function getCredentials(workflow: Workflow, node: INode, type: string, ad +/** + * Returns a copy of the node + * + * @export + * @param {INode} node + * @returns {INode} + */ +export function getNode(node: INode): INode { + return JSON.parse(JSON.stringify(node)); +} + + + /** * Returns the requested resolved (all expressions replaced) node parameters. * @@ -312,6 +326,23 @@ export function getWebhookDescription(name: string, workflow: Workflow, node: IN +/** + * Returns the workflow metadata + * + * @export + * @param {Workflow} workflow + * @returns {IWorkflowMetadata} + */ +export function getWorkflowMetadata(workflow: Workflow): IWorkflowMetadata { + return { + id: workflow.id, + name: workflow.name, + active: workflow.active, + }; +} + + + /** * Returns the execute functions the poll nodes have access to. * @@ -335,6 +366,9 @@ export function getExecutePollFunctions(workflow: Workflow, node: INode, additio getMode: (): WorkflowExecuteMode => { return mode; }, + getNode: () => { + return getNode(node); + }, getNodeParameter: (parameterName: string, fallbackValue?: any): NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[] | object => { //tslint:disable-line:no-any const runExecutionData: IRunExecutionData | null = null; const itemIndex = 0; @@ -349,6 +383,9 @@ export function getExecutePollFunctions(workflow: Workflow, node: INode, additio getTimezone: (): string => { return getTimezone(workflow, additionalData); }, + getWorkflow: () => { + return getWorkflowMetadata(workflow); + }, getWorkflowStaticData(type: string): IDataObject { return workflow.getStaticData(type, node); }, @@ -383,6 +420,9 @@ export function getExecuteTriggerFunctions(workflow: Workflow, node: INode, addi getCredentials(type: string): ICredentialDataDecryptedObject | undefined { return getCredentials(workflow, node, type, additionalData); }, + getNode: () => { + return getNode(node); + }, getMode: (): WorkflowExecuteMode => { return mode; }, @@ -400,6 +440,9 @@ export function getExecuteTriggerFunctions(workflow: Workflow, node: INode, addi getTimezone: (): string => { return getTimezone(workflow, additionalData); }, + getWorkflow: () => { + return getWorkflowMetadata(workflow); + }, getWorkflowStaticData(type: string): IDataObject { return workflow.getStaticData(type, node); }, @@ -467,12 +510,18 @@ export function getExecuteFunctions(workflow: Workflow, runExecutionData: IRunEx getMode: (): WorkflowExecuteMode => { return mode; }, + getNode: () => { + return getNode(node); + }, getRestApiUrl: (): string => { return additionalData.restApiUrl; }, getTimezone: (): string => { return getTimezone(workflow, additionalData); }, + getWorkflow: () => { + return getWorkflowMetadata(workflow); + }, getWorkflowDataProxy: (itemIndex: number): IWorkflowDataProxyData => { const dataProxy = new WorkflowDataProxy(workflow, runExecutionData, runIndex, itemIndex, node.name, connectionInputData); return dataProxy.getDataProxy(); @@ -544,6 +593,9 @@ export function getExecuteSingleFunctions(workflow: Workflow, runExecutionData: getMode: (): WorkflowExecuteMode => { return mode; }, + getNode: () => { + return getNode(node); + }, getRestApiUrl: (): string => { return additionalData.restApiUrl; }, @@ -553,6 +605,9 @@ export function getExecuteSingleFunctions(workflow: Workflow, runExecutionData: getNodeParameter: (parameterName: string, fallbackValue?: any): NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[] | object => { //tslint:disable-line:no-any return getNodeParameter(workflow, runExecutionData, runIndex, connectionInputData, node, parameterName, itemIndex, fallbackValue); }, + getWorkflow: () => { + return getWorkflowMetadata(workflow); + }, getWorkflowDataProxy: (): IWorkflowDataProxyData => { const dataProxy = new WorkflowDataProxy(workflow, runExecutionData, runIndex, itemIndex, node.name, connectionInputData); return dataProxy.getDataProxy(); @@ -594,6 +649,9 @@ export function getLoadOptionsFunctions(workflow: Workflow, node: INode, additio getCurrentNodeParameters: (): INodeParameters | undefined => { return JSON.parse('' + additionalData.currentNodeParameters); }, + getNode: () => { + return getNode(node); + }, getNodeParameter: (parameterName: string, fallbackValue?: any): NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[] | object => { //tslint:disable-line:no-any const runExecutionData: IRunExecutionData | null = null; const itemIndex = 0; @@ -637,6 +695,9 @@ export function getExecuteHookFunctions(workflow: Workflow, node: INode, additio getMode: (): WorkflowExecuteMode => { return mode; }, + getNode: () => { + return getNode(node); + }, getNodeParameter: (parameterName: string, fallbackValue?: any): NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[] | object => { //tslint:disable-line:no-any const runExecutionData: IRunExecutionData | null = null; const itemIndex = 0; @@ -660,6 +721,9 @@ export function getExecuteHookFunctions(workflow: Workflow, node: INode, additio getWebhookDescription(name: string): IWebhookDescription | undefined { return getWebhookDescription(name, workflow, node); }, + getWorkflow: () => { + return getWorkflowMetadata(workflow); + }, getWorkflowStaticData(type: string): IDataObject { return workflow.getStaticData(type, node); }, @@ -705,6 +769,9 @@ export function getExecuteWebhookFunctions(workflow: Workflow, node: INode, addi getMode: (): WorkflowExecuteMode => { return mode; }, + getNode: () => { + return getNode(node); + }, getNodeParameter: (parameterName: string, fallbackValue?: any): NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[] | object => { //tslint:disable-line:no-any const runExecutionData: IRunExecutionData | null = null; const itemIndex = 0; @@ -737,6 +804,9 @@ export function getExecuteWebhookFunctions(workflow: Workflow, node: INode, addi getTimezone: (): string => { return getTimezone(workflow, additionalData); }, + getWorkflow: () => { + return getWorkflowMetadata(workflow); + }, getWorkflowStaticData(type: string): IDataObject { return workflow.getStaticData(type, node); }, diff --git a/packages/editor-ui/src/components/mixins/workflowHelpers.ts b/packages/editor-ui/src/components/mixins/workflowHelpers.ts index e12eeb21a0..507d6d31e4 100644 --- a/packages/editor-ui/src/components/mixins/workflowHelpers.ts +++ b/packages/editor-ui/src/components/mixins/workflowHelpers.ts @@ -192,14 +192,16 @@ export const workflowHelpers = mixins( }; let workflowId = this.$store.getters.workflowId; - if (workflowId !== PLACEHOLDER_EMPTY_WORKFLOW_ID) { + if (workflowId === PLACEHOLDER_EMPTY_WORKFLOW_ID) { workflowId = undefined; } + const workflowName = this.$store.getters.workflowName; + if (copyData === true) { - return new Workflow(workflowId, JSON.parse(JSON.stringify(nodes)), JSON.parse(JSON.stringify(connections)), false, nodeTypes); + return new Workflow({ id: workflowId, name: workflowName, nodes: JSON.parse(JSON.stringify(nodes)), connections: JSON.parse(JSON.stringify(connections)), active: false, nodeTypes}); } else { - return new Workflow(workflowId, nodes, connections, false, nodeTypes); + return new Workflow({ id: workflowId, name: workflowName, nodes, connections, active: false, nodeTypes}); } }, diff --git a/packages/workflow/src/Interfaces.ts b/packages/workflow/src/Interfaces.ts index 2070215911..8942225571 100644 --- a/packages/workflow/src/Interfaces.ts +++ b/packages/workflow/src/Interfaces.ts @@ -159,11 +159,13 @@ export interface IExecuteFunctions { getCredentials(type: string): ICredentialDataDecryptedObject | undefined; getInputData(inputIndex?: number, inputName?: string): INodeExecutionData[]; getMode(): WorkflowExecuteMode; + getNode(): INode; getNodeParameter(parameterName: string, itemIndex: number, fallbackValue?: any): NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[] | object; //tslint:disable-line:no-any getWorkflowDataProxy(itemIndex: number): IWorkflowDataProxyData; getWorkflowStaticData(type: string): IDataObject; getRestApiUrl(): string; getTimezone(): string; + getWorkflow(workflow: Workflow): IWorkflowMetadata; prepareOutputData(outputData: INodeExecutionData[], outputIndex?: number): Promise; helpers: { [key: string]: (...args: any[]) => any //tslint:disable-line:no-any @@ -176,9 +178,11 @@ export interface IExecuteSingleFunctions { getCredentials(type: string): ICredentialDataDecryptedObject | undefined; getInputData(inputIndex?: number, inputName?: string): INodeExecutionData; getMode(): WorkflowExecuteMode; + getNode(): INode; getNodeParameter(parameterName: string, fallbackValue?: any): NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[] | object; //tslint:disable-line:no-any getRestApiUrl(): string; getTimezone(): string; + getWorkflow(workflow: Workflow): IWorkflowMetadata; getWorkflowDataProxy(): IWorkflowDataProxyData; getWorkflowStaticData(type: string): IDataObject; helpers: { @@ -193,6 +197,7 @@ export interface IExecuteWorkflowInfo { export interface ILoadOptionsFunctions { getCredentials(type: string): ICredentialDataDecryptedObject | undefined; + getNode(): INode; getNodeParameter(parameterName: string, fallbackValue?: any): NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[] | object; //tslint:disable-line:no-any getCurrentNodeParameter(parameterName: string): NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[] | object | undefined; getCurrentNodeParameters(): INodeParameters | undefined; @@ -206,11 +211,13 @@ export interface ILoadOptionsFunctions { export interface IHookFunctions { getCredentials(type: string): ICredentialDataDecryptedObject | undefined; getMode(): WorkflowExecuteMode; + getNode(): INode; getNodeWebhookUrl: (name: string) => string | undefined; getNodeParameter(parameterName: string, fallbackValue?: any): NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[] | object; //tslint:disable-line:no-any getTimezone(): string; getWebhookDescription(name: string): IWebhookDescription | undefined; getWebhookName(): string; + getWorkflow(workflow: Workflow): IWorkflowMetadata; getWorkflowStaticData(type: string): IDataObject; helpers: { [key: string]: (...args: any[]) => any //tslint:disable-line:no-any @@ -221,9 +228,11 @@ export interface IPollFunctions { __emit(data: INodeExecutionData[][]): void; getCredentials(type: string): ICredentialDataDecryptedObject | undefined; getMode(): WorkflowExecuteMode; + getNode(): INode; getNodeParameter(parameterName: string, fallbackValue?: any): NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[] | object; //tslint:disable-line:no-any getRestApiUrl(): string; getTimezone(): string; + getWorkflow(workflow: Workflow): IWorkflowMetadata; getWorkflowStaticData(type: string): IDataObject; helpers: { [key: string]: (...args: any[]) => any //tslint:disable-line:no-any @@ -234,9 +243,11 @@ export interface ITriggerFunctions { emit(data: INodeExecutionData[][]): void; getCredentials(type: string): ICredentialDataDecryptedObject | undefined; getMode(): WorkflowExecuteMode; + getNode(): INode; getNodeParameter(parameterName: string, fallbackValue?: any): NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[] | object; //tslint:disable-line:no-any getRestApiUrl(): string; getTimezone(): string; + getWorkflow(workflow: Workflow): IWorkflowMetadata; getWorkflowStaticData(type: string): IDataObject; helpers: { [key: string]: (...args: any[]) => any //tslint:disable-line:no-any @@ -248,6 +259,7 @@ export interface IWebhookFunctions { getCredentials(type: string): ICredentialDataDecryptedObject | undefined; getHeaderData(): object; getMode(): WorkflowExecuteMode; + getNode(): INode; getNodeParameter(parameterName: string, fallbackValue?: any): NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[] | object; //tslint:disable-line:no-any getNodeWebhookUrl: (name: string) => string | undefined; getQueryData(): object; @@ -256,6 +268,7 @@ export interface IWebhookFunctions { getTimezone(): string; getWebhookName(): string; getWorkflowStaticData(type: string): IDataObject; + getWorkflow(workflow: Workflow): IWorkflowMetadata; prepareOutputData(outputData: INodeExecutionData[], outputIndex?: number): Promise; helpers: { [key: string]: (...args: any[]) => any //tslint:disable-line:no-any @@ -519,6 +532,12 @@ export interface IWorkflowDataProxyData { $parameter: any; // tslint:disable-line:no-any } +export interface IWorkflowMetadata { + id?: number | string; + name?: string; + active: boolean; +} + export type WebhookHttpMethod = 'GET' | 'POST'; export interface IWebhookResponseData { diff --git a/packages/workflow/src/Workflow.ts b/packages/workflow/src/Workflow.ts index 45854ce976..52acf8391d 100644 --- a/packages/workflow/src/Workflow.ts +++ b/packages/workflow/src/Workflow.ts @@ -41,6 +41,7 @@ tmpl.tmpl.errorHandler = () => { }; export class Workflow { id: string | undefined; + name: string | undefined; nodes: INodes = {}; connectionsBySourceNode: IConnections; connectionsByDestinationNode: IConnections; @@ -52,15 +53,17 @@ export class Workflow { // ids of registred webhooks of nodes staticData: IDataObject; - constructor(id: string | undefined, nodes: INode[], connections: IConnections, active: boolean, nodeTypes: INodeTypes, staticData?: IDataObject, settings?: IWorkflowSettings) { - this.id = id; - this.nodeTypes = nodeTypes; + // constructor(id: string | undefined, nodes: INode[], connections: IConnections, active: boolean, nodeTypes: INodeTypes, staticData?: IDataObject, settings?: IWorkflowSettings) { + constructor(parameters: {id?: string, name?: string, nodes: INode[], connections: IConnections, active: boolean, nodeTypes: INodeTypes, staticData?: IDataObject, settings?: IWorkflowSettings}) { + this.id = parameters.id; + this.name = parameters.name; + this.nodeTypes = parameters.nodeTypes; // Save nodes in workflow as object to be able to get the // nodes easily by its name. // Also directly add the default values of the node type. let nodeType: INodeType | undefined; - for (const node of nodes) { + for (const node of parameters.nodes) { this.nodes[node.name] = node; nodeType = this.nodeTypes.getByName(node.type); @@ -77,16 +80,16 @@ export class Workflow { const nodeParameters = NodeHelpers.getNodeParameters(nodeType.description.properties, node.parameters, true, false); node.parameters = nodeParameters !== null ? nodeParameters : {}; } - this.connectionsBySourceNode = connections; + this.connectionsBySourceNode = parameters.connections; // Save also the connections by the destionation nodes - this.connectionsByDestinationNode = this.__getConnectionsByDestination(connections); + this.connectionsByDestinationNode = this.__getConnectionsByDestination(parameters.connections); - this.active = active || false; + this.active = parameters.active || false; - this.staticData = ObservableObject.create(staticData || {}, undefined, { ignoreEmptyOnFirstChild: true }); + this.staticData = ObservableObject.create(parameters.staticData || {}, undefined, { ignoreEmptyOnFirstChild: true }); - this.settings = settings || {}; + this.settings = parameters.settings || {}; } diff --git a/packages/workflow/src/WorkflowDataProxy.ts b/packages/workflow/src/WorkflowDataProxy.ts index d713758e23..6a0ac34787 100644 --- a/packages/workflow/src/WorkflowDataProxy.ts +++ b/packages/workflow/src/WorkflowDataProxy.ts @@ -240,6 +240,35 @@ export class WorkflowDataProxy { + /** + * Returns a proxt to query data from the workflow + * + * @private + * @returns + * @memberof WorkflowDataProxy + */ + private workflowGetter() { + const allowedValues = [ + 'active', + 'id', + 'name', + ]; + const that = this; + + return new Proxy({}, { + get(target, name, receiver) { + if (!allowedValues.includes(name.toString())) { + throw new Error(`The key "${name.toString()}" is not supported!`); + } + + // @ts-ignore + return that.workflow[name.toString()]; + } + }); + } + + + /** * Returns a proxy to query data of all nodes *