import { IConnection , INode , INodeNameIndex , INodesGraph , INodeGraphItem , INodesGraphResult , IWorkflowBase , INodeTypes , INodeType , } from '.' ; export function getNodeTypeForName ( workflow , nodeName ) { return workflow . nodes . find ( ( node ) => node . name === nodeName ) ; } export function generateNodesGraph ( workflow , nodeTypes ) { const nodesGraph = { node_types : [ ] , node_connections : [ ] , nodes : { } , } ; const nodeNameAndIndex = { } ; workflow . nodes . forEach ( ( node , index ) => { nodesGraph . node_types . push ( node . type ) ; const nodeItem = { type : node . type , position : node . position , } ; 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 { 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 ; } } ) ; nodeItem . operation = node . parameters . operation ?. toString ( ) ?? nodeItem . operation ; nodeItem . resource = node . parameters . resource ?. toString ( ) ?? nodeItem . resource ; nodeItem . mode = node . parameters . mode ?. toString ( ) ?? nodeItem . mode ; } nodesGraph . nodes [ `${ index }` ] = nodeItem ; nodeNameAndIndex [ node . name ] = index . toString ( ) ; } ) ; const getGraphConnectionItem = ( startNode , connectionItem ) => { 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 } ; }