2021-10-18 20:57:49 -07:00
|
|
|
/* eslint-disable import/no-cycle */
|
|
|
|
import {
|
|
|
|
IConnection,
|
|
|
|
INode,
|
|
|
|
INodeNameIndex,
|
|
|
|
INodesGraph,
|
|
|
|
INodeGraphItem,
|
|
|
|
INodesGraphResult,
|
|
|
|
IWorkflowBase,
|
2022-01-07 08:14:59 -08:00
|
|
|
INodeTypes,
|
|
|
|
INodeType,
|
2021-10-18 20:57:49 -07:00
|
|
|
} from '.';
|
|
|
|
|
|
|
|
export function getNodeTypeForName(workflow: IWorkflowBase, nodeName: string): INode | undefined {
|
|
|
|
return workflow.nodes.find((node) => node.name === nodeName);
|
|
|
|
}
|
|
|
|
|
2022-01-07 08:14:59 -08:00
|
|
|
export function generateNodesGraph(
|
|
|
|
workflow: IWorkflowBase,
|
|
|
|
nodeTypes: INodeTypes,
|
|
|
|
): INodesGraphResult {
|
2021-10-18 20:57:49 -07:00
|
|
|
const nodesGraph: INodesGraph = {
|
|
|
|
node_types: [],
|
|
|
|
node_connections: [],
|
|
|
|
nodes: {},
|
|
|
|
};
|
|
|
|
const nodeNameAndIndex: INodeNameIndex = {};
|
|
|
|
|
|
|
|
workflow.nodes.forEach((node: INode, index: number) => {
|
|
|
|
nodesGraph.node_types.push(node.type);
|
|
|
|
const nodeItem: INodeGraphItem = {
|
|
|
|
type: node.type,
|
2022-01-07 08:14:59 -08:00
|
|
|
position: node.position,
|
2021-10-18 20:57:49 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
if (node.type === 'n8n-nodes-base.httpRequest') {
|
|
|
|
try {
|
|
|
|
nodeItem.domain = new URL(node.parameters.url as string).hostname;
|
|
|
|
} catch (e) {
|
|
|
|
nodeItem.domain = node.parameters.url as string;
|
|
|
|
}
|
|
|
|
} else {
|
2022-01-07 08:14:59 -08:00
|
|
|
const nodeType = nodeTypes.getByName(node.type) as INodeType;
|
|
|
|
nodeType.description.properties.forEach((property) => {
|
|
|
|
if (
|
|
|
|
property.name === 'operation' ||
|
|
|
|
property.name === 'resource' ||
|
|
|
|
property.name === 'mode'
|
|
|
|
) {
|
|
|
|
nodeItem[property.name] = property.default ? property.default.toString() : undefined;
|
2021-10-18 20:57:49 -07:00
|
|
|
}
|
|
|
|
});
|
2022-01-07 08:14:59 -08:00
|
|
|
|
|
|
|
nodeItem.operation = node.parameters.operation?.toString() ?? nodeItem.operation;
|
|
|
|
nodeItem.resource = node.parameters.resource?.toString() ?? nodeItem.resource;
|
|
|
|
nodeItem.mode = node.parameters.mode?.toString() ?? nodeItem.mode;
|
2021-10-18 20:57:49 -07:00
|
|
|
}
|
|
|
|
nodesGraph.nodes[`${index}`] = nodeItem;
|
|
|
|
nodeNameAndIndex[node.name] = index.toString();
|
|
|
|
});
|
|
|
|
|
|
|
|
const getGraphConnectionItem = (startNode: string, connectionItem: IConnection) => {
|
|
|
|
return { start: nodeNameAndIndex[startNode], end: nodeNameAndIndex[connectionItem.node] };
|
|
|
|
};
|
|
|
|
|
|
|
|
Object.keys(workflow.connections).forEach((nodeName) => {
|
|
|
|
const connections = workflow.connections[nodeName];
|
|
|
|
connections.main.forEach((element) => {
|
|
|
|
element.forEach((element2) => {
|
|
|
|
nodesGraph.node_connections.push(getGraphConnectionItem(nodeName, element2));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return { nodeGraph: nodesGraph, nameIndices: nodeNameAndIndex };
|
|
|
|
}
|