forked from scrive/openapi2slate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paths.js
112 lines (103 loc) · 3.11 KB
/
paths.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
var _ = require('lodash');
var parameters = require('./parameters.js');
var responses = require('./responses.js');
module.exports = {
addEndpointToPaths: function(basePath, paths) {
return _.map(paths, function(p, ep) {
p.endpoint = basePath + ep;
return p;
});
},
groupPathsByTag: function(paths) {
return _.groupBy(paths, function(p) {
var pathDetails;
if(p.post != undefined) {
pathDetails = p.post;
}
else if(p.get != undefined) {
pathDetails = p.get;
}
return pathDetails.tags[0];
});
},
filterInternalTag: function(paths) {
return _.filter(paths, function(p, ep) {
var pathDetails;
if(p.post != undefined) {
pathDetails = p.post;
}
else if(p.get != undefined) {
pathDetails = p.get;
}
return ! _.find(pathDetails.tags, function(t) { return t == "Internal"; });
});
},
sectionForGroupedEndpointPaths: function(groupedPaths, includeInternal) {
var sectionText = '';
_.forEach(groupedPaths, function(pathGroup, groupTag) {
sectionText += `# ${groupTag}\n`;
_.forEach(pathGroup, function(pathTop) {
sectionText += pathItemObjectWithEndpointText(operationObjectText, pathTop, pathTop.endpoint, includeInternal);
});
});
return sectionText;
},
sectionIndexOfGroupedEndpointPaths: function(groupedPaths, includeInternal) {
var sectionText = '# List of API Calls\n';
_.forEach(groupedPaths, function(pathGroup, groupTag) {
sectionText += `\n## ${groupTag}\n\n`;
_.forEach(pathGroup, function(pathTop) {
sectionText += pathItemObjectWithEndpointText(operationObjectSnippet, pathTop, pathTop.endpoint, includeInternal);
});
});
return sectionText;
}
}
function pathItemObjectWithEndpointText(operationFunc, pio, endpoint, includeInternal) {
var pioText = '';
if(pio.parameters != undefined) {
pioText += parameters.parameterSection(pio.parameters, 3, includeInternal);
}
if(pio.get != undefined) {
pioText += operationFunc(pio.get, 'GET', endpoint, includeInternal);
}
if(pio.post != undefined) {
pioText += operationFunc(pio.post, 'POST', endpoint, includeInternal);
}
// TODO put, delete, options, head, patch
return pioText;
};
function operationObjectText(opObj, method, endpoint, includeInternal) {
var opObjText =
`
<div><a id="${endpoint.replace(/\//g,'')}"></a></div>
## ${opObj.summary}
<p>
<strong>
<code class="operation-method operation-method-${method}">${method} ${endpoint}</code>
</strong>
</p>
${opObj.description}
`;
if(opObj.parameters != undefined) {
opObjText += parameters.parameterSection(opObj.parameters, 3, includeInternal);
}
if(opObj.responses != undefined) {
opObjText += responses.responsesObject(opObj.responses);
}
opObjText += '\n\n';
return opObjText;
};
function operationObjectSnippet(opObj, method, endpoint, includeInternal) {
var opObjSnippet =
`
<p>
<a href="#${endpoint.replace(/\//g,'')}">
<strong>
<code class="operation-method operation-method-${method}">${method} ${endpoint}</code>
</strong>
</a>
</p>
`;
return opObjSnippet;
};