Skip to content

Commit cc35f3f

Browse files
committed
updated logic for generating folders
1 parent 9321351 commit cc35f3f

File tree

1 file changed

+47
-21
lines changed

1 file changed

+47
-21
lines changed

libV2/helpers/collection/generateSkeletionTreeFromOpenAPI.js

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ let _ = require('lodash'),
4141
_.forEach(paths, function (completePath) {
4242
let pathSplit = completePath === '/' ? [completePath] : _.compact(completePath.split('/'));
4343

44+
/**
45+
* /user
46+
* /team
47+
* /hi
48+
* /bye
49+
*
50+
* In this scenario, always create a base folder for the path
51+
* and then add and link the request inside the created folder.
52+
*/
4453
if (pathSplit.length === 1) {
4554
let methods = openapi.paths[completePath];
4655

@@ -57,6 +66,20 @@ let _ = require('lodash'),
5766
return;
5867
}
5968

69+
if (!tree.hasNode(`path:folder:${pathSplit[0]}`)) {
70+
tree.setNode(`path:folder:${pathSplit[0]}`, {
71+
type: 'folder',
72+
meta: {
73+
name: pathSplit[0],
74+
path: pathSplit[0],
75+
pathIdentifier: pathSplit[0]
76+
},
77+
data: {}
78+
});
79+
80+
tree.setEdge('root:collection', `path:folder:${pathSplit[0]}`);
81+
}
82+
6083
tree.setNode(`path:request:${pathSplit[0]}:${method}`, {
6184
type: 'request',
6285
data: {},
@@ -67,13 +90,7 @@ let _ = require('lodash'),
6790
}
6891
});
6992

70-
if (tree.hasNode(`path:folder:${pathSplit[0]}`)) {
71-
tree.setEdge(`path:folder:${pathSplit[0]}`, `path:request:${pathSplit[0]}:${method}`);
72-
}
73-
74-
else {
75-
tree.setEdge('root:collection', `path:request:${pathSplit[0]}:${method}`);
76-
}
93+
tree.setEdge(`path:folder:${pathSplit[0]}`, `path:request:${pathSplit[0]}:${method}`);
7794
});
7895
}
7996

@@ -98,6 +115,28 @@ let _ = require('lodash'),
98115
return;
99116
}
100117

118+
/**
119+
* If it is the last node,
120+
* it might happen that this exists as a folder.
121+
*
122+
* If yes add a request inside that folder else
123+
* add as a request on the previous path idendified which will be a folder.
124+
*/
125+
if (!tree.hasNode(`path:folder:${pathIdentifier}`)) {
126+
tree.setNode(`path:folder:${pathIdentifier}`, {
127+
type: 'folder',
128+
meta: {
129+
name: path,
130+
path: path,
131+
pathIdentifier: pathIdentifier
132+
},
133+
data: {}
134+
});
135+
136+
tree.setEdge(index === 0 ? 'root:collection' : `path:folder:${previousPathIdentified}`,
137+
`path:folder:${pathIdentifier}`);
138+
}
139+
101140
tree.setNode(`path:request:${pathIdentifier}:${method}`, {
102141
type: 'request',
103142
data: {},
@@ -108,20 +147,7 @@ let _ = require('lodash'),
108147
}
109148
});
110149

111-
/**
112-
* If it is the last node,
113-
* it might happen that this exists as a folder.
114-
*
115-
* If yes add a request inside that folder else
116-
* add as a request on the previous path idendified which will be a folder.
117-
*/
118-
if (tree.hasNode(`path:folder:${pathIdentifier}`)) {
119-
tree.setEdge(`path:folder:${pathIdentifier}`, `path:request:${pathIdentifier}:${method}`);
120-
}
121-
122-
else {
123-
tree.setEdge(`path:folder:${previousPathIdentified}`, `path:request:${pathIdentifier}:${method}`);
124-
}
150+
tree.setEdge(`path:folder:${pathIdentifier}`, `path:request:${pathIdentifier}:${method}`);
125151
});
126152
}
127153

0 commit comments

Comments
 (0)