1- import Operators from '../constants/operators.js' ;
2-
31// assumption is that apis are giving json as output
42
3+ import { computeEvaluateNode } from './compute/evaluatenode' ;
4+ import { computeRequestNode } from './compute/requestnode' ;
5+
56class Graph {
67 constructor ( nodes , edges , onGraphComplete ) {
78 this . nodes = nodes ;
@@ -18,165 +19,6 @@ class Graph {
1819 return Date . now ( ) - this . startTime > this . timeout ;
1920 }
2021
21- #formulateRequest( node , finalUrl ) {
22- let restMethod = node . data . requestType . toLowerCase ( ) ;
23- let contentType = 'application/json' ;
24- let requestData = undefined ;
25-
26- if ( restMethod === 'get' ) {
27- if ( node . data . requestBody ) {
28- if ( node . data . requestBody . type === 'raw-json' ) {
29- contentType = 'application/json' ;
30- requestData = node . data . requestBody . body ? JSON . parse ( node . data . requestBody . body ) : JSON . parse ( '{}' ) ;
31- }
32- }
33- } else if ( restMethod === 'post' || restMethod === 'put' ) {
34- if ( node . data . requestBody ) {
35- if ( node . data . requestBody . type === 'form-data' ) {
36- contentType = 'multipart/form-data' ;
37- requestData = {
38- key : node . data . requestBody . body . key ,
39- value : node . data . requestBody . body . value ,
40- name : node . data . requestBody . body . name ,
41- } ;
42- } else if ( node . data . requestBody . type === 'raw-json' ) {
43- contentType = 'application/json' ;
44- requestData = node . data . requestBody . body ? JSON . parse ( node . data . requestBody . body ) : JSON . parse ( '{}' ) ;
45- }
46- }
47- }
48-
49- const options = {
50- method : restMethod ,
51- url : finalUrl ,
52- headers : {
53- 'Content-type' : contentType ,
54- } ,
55- data : requestData ,
56- } ;
57-
58- if ( this . auth . type === 'basic-auth' ) {
59- options . auth = { } ;
60- options . auth . username = this . auth . username ;
61- options . auth . password = this . auth . password ;
62- }
63-
64- this . logs . push ( `${ restMethod } ${ finalUrl } ` ) ;
65- return options ;
66- }
67-
68- #computeNodeVariable( variable , prevNodeOutputData ) {
69- if ( variable . type . toLowerCase ( ) === 'string' ) {
70- return variable . value ;
71- }
72-
73- if ( variable . type . toLowerCase ( ) === 'number' ) {
74- return variable . value ;
75- }
76-
77- if ( variable . type . toLowerCase ( ) === 'bool' ) {
78- return Boolean ( variable . value ) ;
79- }
80-
81- if ( variable . type . toLowerCase ( ) === 'select' ) {
82- if ( prevNodeOutputData == undefined || Object . keys ( prevNodeOutputData ) . length === 0 ) {
83- this . logs . push (
84- `Cannot evaluate variable ${ variable } as previous node output data ${ JSON . stringify ( prevNodeOutputData ) } is empty` ,
85- ) ;
86- throw 'Error evaluating node variables' ;
87- }
88- const jsonTree = variable . value . split ( '.' ) ;
89- const getVal = ( parent , pos ) => {
90- if ( pos == jsonTree . length ) {
91- return parent ;
92- }
93- const key = jsonTree [ pos ] ;
94- if ( key == '' ) {
95- return parent ;
96- }
97-
98- return getVal ( parent [ key ] , pos + 1 ) ;
99- } ;
100- const result = getVal ( prevNodeOutputData , 0 ) ;
101- if ( result == undefined ) {
102- this . logs . push (
103- `Cannot evaluate variable ${ JSON . stringify ( variable ) } as previous node output data ${ JSON . stringify ( prevNodeOutputData ) } did not contain the variable` ,
104- ) ;
105- throw 'Error evaluating node variables' ;
106- }
107- return result ;
108- }
109- }
110-
111- #computeNodeVariables( variables , prevNodeOutputData ) {
112- let evalVariables = { } ;
113- Object . entries ( variables ) . map ( ( [ vname , variable ] , index ) => {
114- evalVariables [ vname ] = this . #computeNodeVariable( variable , prevNodeOutputData ) ;
115- } ) ;
116- return evalVariables ;
117- }
118-
119- #runHttpRequest( request ) {
120- const { ipcRenderer } = window ;
121-
122- return new Promise ( ( resolve , reject ) => {
123- ipcRenderer . invoke ( 'renderer:run-http-request' , request ) . then ( resolve ) . catch ( reject ) ;
124- } ) ;
125- }
126-
127- async #computeRequestNode( node , prevNodeOutputData ) {
128- // step1 evaluate variables of this node
129- const evalVariables = this . #computeNodeVariables( node . data . variables , prevNodeOutputData ) ;
130-
131- // step2 replace variables in url with value
132- let finalUrl = node . data . url ;
133- Object . entries ( evalVariables ) . map ( ( [ vname , vvalue ] , index ) => {
134- finalUrl = finalUrl . replace ( `{${ vname } }` , vvalue ) ;
135- } ) ;
136-
137- // step 3
138- const options = this . #formulateRequest( node , finalUrl ) ;
139-
140- console . debug ( 'Evaluated variables: ' , evalVariables ) ;
141- console . debug ( 'Evaluated Url: ' , finalUrl ) ;
142-
143- const res = await this . #runHttpRequest( options ) ;
144-
145- if ( res . error ) {
146- console . debug ( 'Failure at node: ' , node ) ;
147- console . debug ( 'Error encountered: ' , JSON . stringify ( res . error ) ) ;
148- this . logs . push ( `Request failed: ${ JSON . stringify ( res . error ) } ` ) ;
149- return [ 'Failed' , node , res . error ] ;
150- } else {
151- this . logs . push ( `Request successful: ${ JSON . stringify ( res ) } ` ) ;
152- console . debug ( 'Response: ' , JSON . stringify ( res ) ) ;
153- return [ 'Success' , node , res ] ;
154- }
155- }
156-
157- #computeEvaluateNode( node , prevNodeOutputData ) {
158- const evalVariables = this . #computeNodeVariables( node . data . variables , prevNodeOutputData ) ;
159- const var1 = evalVariables . var1 ;
160- const var2 = evalVariables . var2 ;
161-
162- const operator = node . data . operator ;
163- if ( operator == undefined ) {
164- throw 'Operator undefined' ;
165- }
166- this . logs . push (
167- `Evaluate var1: ${ JSON . stringify ( var1 ) } of type: ${ typeof var1 } , var2: ${ JSON . stringify ( var2 ) } of type: ${ typeof var2 } with operator: ${ operator } ` ,
168- ) ;
169- if ( operator == Operators . isEqualTo ) {
170- return var1 === var2 ;
171- } else if ( operator == Operators . isNotEqualTo ) {
172- return var1 != var2 ;
173- } else if ( operator == Operators . isGreaterThan ) {
174- return var1 > var2 ;
175- } else if ( operator == Operators . isLessThan ) {
176- return var1 < var2 ;
177- }
178- }
179-
18022 #computeConnectingEdge( node , result ) {
18123 let connectingEdge = undefined ;
18224
@@ -225,7 +67,7 @@ class Graph {
22567 }
22668
22769 if ( node . type === 'evaluateNode' ) {
228- if ( this . # computeEvaluateNode( node , prevNodeOutputData ) ) {
70+ if ( computeEvaluateNode ( node , prevNodeOutputData , this . logs ) ) {
22971 this . logs . push ( 'Result: true' ) ;
23072 result = [ 'Success' , node , prevNodeOutput , true ] ;
23173 } else {
@@ -250,7 +92,7 @@ class Graph {
25092 }
25193
25294 if ( node . type === 'requestNode' ) {
253- result = await this . # computeRequestNode( node , prevNodeOutputData ) ;
95+ result = await computeRequestNode ( node , prevNodeOutputData , this . auth , this . logs ) ;
25496 }
25597
25698 if ( this . #checkTimeout( ) ) {
@@ -308,6 +150,7 @@ class Graph {
308150 }
309151 this . logs . push ( 'End Flowtest' ) ;
310152 this . logs . push ( `Total time: ${ Date . now ( ) - this . startTime } ms` ) ;
153+ console . log ( this . logs ) ;
311154 this . onGraphComplete ( result , this . logs ) ;
312155 } ) ;
313156 } else {
0 commit comments