488 lines
13 KiB
JavaScript
488 lines
13 KiB
JavaScript
const fs = require('fs');
|
|
|
|
function parseExampleRes(data) {
|
|
let html = '';
|
|
let header_html = "";
|
|
if (data.response.headers.length > 0) {
|
|
data.response.headers.forEach(elem => {
|
|
if (elem.key === "Content-Type") {
|
|
header_html += "<p style='margin-left: 27px; word-wrap: break-word;'>""+elem.key+"" : ""+elem.value+""</p>";
|
|
return;
|
|
}
|
|
});
|
|
}
|
|
if (data.response.bodyOptions && data.response.bodyOptions !== "") {
|
|
if (header_html === "") {
|
|
Object.keys(data.response.bodyOptions).forEach(function(typeName) {
|
|
if (typeName === "language") {
|
|
header_html += `
|
|
<p style='margin-left: 20px; word-wrap: break-word;'>
|
|
"Content-type" : "${data.response.bodyOptions[typeName]}"
|
|
</p>`;
|
|
return;
|
|
}
|
|
});
|
|
}
|
|
}
|
|
if (header_html !== "") {
|
|
header_html = `
|
|
<p style='margin-left: 25px; word-wrap: break-word;'>
|
|
<strong>Headers : </strong>
|
|
</p>
|
|
${header_html}
|
|
`;
|
|
html += header_html;
|
|
}
|
|
if (data.response.cookies.length > 0) {
|
|
html += `
|
|
<p style='margin-left: 25px; word-wrap: break-word;'>
|
|
<strong>Cookies : </strong>
|
|
</p>
|
|
`;
|
|
data.response.cookies.forEach(elem => {
|
|
html += "<p style='margin-left: 27px; word-wrap: break-word;'>""+elem.key+"" : ""+elem.value+""</p>";
|
|
});
|
|
}
|
|
if (data.response.body !== "") {
|
|
let body = `
|
|
<p style='margin-left: 25px; word-wrap: break-word;'>
|
|
<b>Body : </b>
|
|
</p>`;
|
|
switch (typeof data.response.body) {
|
|
case 'string':
|
|
body += `<pre style='font-size: 16px; margin-left: 27px; word-wrap: break-word;'>${data.response.body}</pre>`;
|
|
break;
|
|
|
|
case 'object':
|
|
body = "";
|
|
data.response.body.forEach(item => {
|
|
body += `<pre style='font-size: 16px; margin-left: 27px; word-wrap: break-word;'>${JSON.stringify(item, null, 2)}</pre>`;
|
|
});
|
|
break;
|
|
|
|
default:
|
|
body = "";
|
|
break;
|
|
}
|
|
html += body;
|
|
}
|
|
|
|
return html;
|
|
}
|
|
|
|
function parseExampleReq(data) {
|
|
let html = '';
|
|
let header_html = "";
|
|
if (data.request.header.length > 0) {
|
|
data.request.header.forEach(elem => {
|
|
if (elem.key === "Content-Type") {
|
|
header_html += "<p style='margin-left: 27px; word-wrap: break-word;'>""+elem.key+"" : ""+elem.value+""</p>";
|
|
return;
|
|
}
|
|
});
|
|
}
|
|
if (data.request.bodyOptions && data.request.bodyOptions !== "") {
|
|
if (header_html === "") {
|
|
Object.keys(data.request.bodyOptions).forEach(function(typeName) {
|
|
if (typeName === "language") {
|
|
header_html += `
|
|
<p style='margin-left: 27px; word-wrap: break-word;'>
|
|
"Content-type" : "${data.request.bodyOptions[typeName]}"
|
|
</p>`;
|
|
return;
|
|
}
|
|
});
|
|
}
|
|
}
|
|
if (header_html !== "") {
|
|
header_html = `
|
|
<p style='margin-left: 25px; word-wrap: break-word;'>
|
|
<strong>Headers : </strong>
|
|
</p>
|
|
${header_html}
|
|
`;
|
|
html += header_html;
|
|
}
|
|
if(data.request.auth.length > 0) {
|
|
html += "<p style='margin-left: 25px; word-wrap: break-word;'><strong>Auth : </strong></p>";
|
|
data.request.auth.forEach(elem => {
|
|
html += "<p style='margin-left: 27px; word-wrap: break-word;'>key : "+elem.key+", value : "+elem.value+", type : "+elem.type+"</p>";
|
|
});
|
|
}
|
|
if (data.request.variables.length > 0) {
|
|
html += `
|
|
<p style='margin-left: 25px; word-wrap: break-word;'>
|
|
<b>Path Variables: </b>
|
|
</p>
|
|
`;
|
|
data.request.variables.forEach(elem => {
|
|
html += "<p style='margin-left: 27px; word-wrap: break-word;'>""+elem.key+"" : ""+elem.value+""</p>";
|
|
});
|
|
}
|
|
if (data.request.queryes.length > 0) {
|
|
html += `
|
|
<p style='margin-left: 25px; word-wrap: break-word;'>
|
|
<b>Query Parameters : </b>
|
|
</p>
|
|
`;
|
|
data.request.queryes.forEach(elem => {
|
|
html += "<p style='margin-left: 27px; word-wrap: break-word;'>""+elem.key+"" : ""+elem.value+""</p>";
|
|
});
|
|
}
|
|
if (data.request.body !== "") {
|
|
let body = `
|
|
<p style='margin-left: 25px; word-wrap: break-word;'>
|
|
<b>Body : </b>
|
|
</p>`;
|
|
switch (typeof data.request.body) {
|
|
case 'string':
|
|
body += `<pre style='font-size: 16px; margin-left: 27px; word-wrap: break-word;'>${data.request.body}</pre>`;
|
|
break;
|
|
|
|
case 'object':
|
|
data.request.body.forEach(item => {
|
|
body += `<pre style='font-size: 16px; margin-left: 27px; word-wrap: break-word;'>${JSON.stringify(item, null, 2)}</pre>`;
|
|
});
|
|
break;
|
|
|
|
default:
|
|
body = "";
|
|
break;
|
|
}
|
|
html += body;
|
|
}
|
|
|
|
return html;
|
|
}
|
|
|
|
function generateExamples(data) {
|
|
let html = `
|
|
<p style='margin-left: 20px;'>
|
|
<b><u>Success : </u></b>
|
|
</p>
|
|
`;
|
|
if (data.success.length > 0) {
|
|
data.success.forEach(function(item) {
|
|
let desc = "";
|
|
if (item.desc && item.desc !== "") {
|
|
desc = `
|
|
<p style='margin-left: 22px;'>
|
|
<strong>Description : </strong>
|
|
<span style='white-space: pre;'>${item.desc.replace(/ /g, " ")}</span>
|
|
</p>
|
|
`;
|
|
}
|
|
html += `
|
|
<p style='margin-left: 22px;'>
|
|
<b>Request : </b>${item.name}
|
|
</p>
|
|
${desc}
|
|
<p style='margin-left: 25px; word-wrap: break-word;'>
|
|
<strong>URL : </strong>
|
|
${item.request.url}
|
|
</p>
|
|
<p style='margin-left: 25px; word-wrap: break-word;'>
|
|
<strong>Method : </strong>
|
|
${item.request.method}
|
|
</p>
|
|
${parseExampleReq(item)}
|
|
<p style='margin-left: 22px;'>
|
|
<b>Response : </b>
|
|
</p>
|
|
<p style='margin-left: 25px;'>
|
|
<b>Status : </b> ${item.response.status}
|
|
</p>
|
|
<p style='margin-left: 25px;'>
|
|
<b>Code : </b> ${item.response.code}
|
|
</p>
|
|
${parseExampleRes(item)}
|
|
<br>`;
|
|
});
|
|
}
|
|
else {
|
|
html += `<p style='margin-left: 25px; word-wrap: break-word;'>No examples</p>`;
|
|
}
|
|
|
|
html += `
|
|
<p style='margin-left: 20px;'>
|
|
<b><u>Exceptions : </u></b>
|
|
</p>
|
|
`;
|
|
if (data.exceptions.length > 0) {
|
|
data.exceptions.forEach(function(item){
|
|
let desc = "";
|
|
if (item.desc && item.desc !== "") {
|
|
desc = `
|
|
<p style='margin-left: 22px;'>
|
|
<strong>Description : </strong>
|
|
<span style='white-space: pre;'>${item.desc.replace(/ /g, " ")}</span>
|
|
</p>
|
|
`;
|
|
}
|
|
html += `
|
|
<p style='margin-left: 22px;'>
|
|
<b>Request : </b>${item.name}
|
|
</p>
|
|
${desc}
|
|
</p>
|
|
<p style='margin-left: 25px; word-wrap: break-word;'>
|
|
<strong>URL : </strong>
|
|
${item.request.url}
|
|
</p>
|
|
<p style='margin-left: 25px; word-wrap: break-word;'>
|
|
<strong>Method : </strong>
|
|
${item.request.method}
|
|
</p>
|
|
${parseExampleReq(item)}
|
|
<p style='margin-left: 22px;'>
|
|
<b>Response : </b>
|
|
</p>
|
|
<p style='margin-left: 25px;'>
|
|
<b>Code : </b> ${item.response.code}
|
|
</p>
|
|
<p style='margin-left: 25px;'>
|
|
<b>Status : </b> ${item.response.status}
|
|
</p>
|
|
${parseExampleRes(item)}
|
|
<br>`;
|
|
});
|
|
}
|
|
else {
|
|
html += `<p style='margin-left: 25px; word-wrap: break-word;'>No examples</p>`;
|
|
}
|
|
|
|
return html;
|
|
}
|
|
|
|
function generateApiHtml(data) {
|
|
let html = `
|
|
<h3 style='margin-left: 10px;'>
|
|
<b>${data.name}</b>
|
|
</h3>
|
|
`;
|
|
let header_html = "";
|
|
let auth_html = "";
|
|
if (data.header.length > 0) {
|
|
header_html = "";
|
|
data.header.forEach(elem => {
|
|
header_html += "<p style='margin-left: 20px; word-wrap: break-word;'>""+elem.key+"" : ""+elem.value+""</p>";
|
|
});
|
|
}
|
|
if(data.auth.length > 0) {
|
|
auth_html="<p style='margin-left: 15px; word-wrap: break-word;'><strong>Auth : </strong></p>";
|
|
data.auth.forEach(elem => {
|
|
auth_html += "<p style='margin-left: 20px; word-wrap: break-word;'>key : "+elem.key+", value : "+elem.value+", type : "+elem.type+"</p>";
|
|
});
|
|
}
|
|
let body = "<pre style='font-size: 16px; margin-left: 20px; word-wrap: break-word;'>not required, leave blank</pre>";
|
|
switch (typeof data.body) {
|
|
case 'string':
|
|
if (data.body !== "") {
|
|
body = `<pre style='font-size: 16px; margin-left: 20px; word-wrap: break-word;'>${data.body}</pre>`;
|
|
}
|
|
break;
|
|
|
|
case 'object':
|
|
body = "";
|
|
data.body.forEach(item => {
|
|
body += `<pre style='font-size: 16px; margin-left: 20px; word-wrap: break-word;'>${JSON.stringify(item, null, 2)}</pre>`;
|
|
});
|
|
break;
|
|
|
|
default:
|
|
body = "<pre style='font-size: 16px; margin-left: 20px; word-wrap: break-word;'>not required, leave blank</pre>";
|
|
break;
|
|
}
|
|
|
|
if (data.bodyOptions && data.bodyOptions !== "") {
|
|
Object.keys(data.bodyOptions).forEach(function(typeName) {
|
|
if (typeName === "language") {
|
|
header_html += `
|
|
<p style='margin-left: 20px; word-wrap: break-word;'>
|
|
"Content-type" : "${data.bodyOptions[typeName]}"
|
|
</p>`;
|
|
}
|
|
});
|
|
}
|
|
|
|
let variables = "<p style='margin-left: 20px; word-wrap: break-word;'>not required, leave blank</p>";
|
|
if (data.variables.length > 0) {
|
|
variables = "";
|
|
data.variables.forEach(elem => {
|
|
variables += "<p style='margin-left: 20px; word-wrap: break-word;'>""+elem.key+"" : ""+elem.value+""</p>";
|
|
});
|
|
}
|
|
|
|
let queryes = "<p style='margin-left: 20px; word-wrap: break-word;'>not required, leave blank</p>";
|
|
if (data.queryes.length > 0) {
|
|
queryes = "";
|
|
data.queryes.forEach(elem => {
|
|
queryes += "<p style='margin-left: 20px; word-wrap: break-word;'>""+elem.key+"" : ""+elem.value+""</p>";
|
|
});
|
|
}
|
|
|
|
if (header_html === "") {
|
|
header_html = "<p style='margin-left: 20px; word-wrap: break-word;'>not required, leave blank</p>";
|
|
}
|
|
let desc = "";
|
|
if (data.desc && data.desc !== "") {
|
|
desc = `
|
|
<p style='margin-left: 15px;'>
|
|
<strong>Description : </strong>
|
|
<span style='white-space: pre;'>${data.desc.replace(/ /g, " ")}</span>
|
|
</p>
|
|
`;
|
|
}
|
|
html +=
|
|
`${desc}
|
|
<p style='margin-left: 15px; word-wrap: break-word;'>
|
|
<strong>URL : </strong>
|
|
${data.url}
|
|
</p>
|
|
<p style='margin-left: 15px; word-wrap: break-word;'>
|
|
<strong>Method : </strong>
|
|
${data.method}
|
|
</p>
|
|
<p style='margin-left: 15px; word-wrap: break-word;'>
|
|
<strong>Headers : </strong>
|
|
</p>
|
|
${header_html+auth_html}
|
|
<p style='margin-left: 15px; word-wrap: break-word;'>
|
|
<b>Path Variables: </b>
|
|
</p>
|
|
${variables}
|
|
<p style='margin-left: 15px; word-wrap: break-word;'>
|
|
<b>Query Parameters : </b>
|
|
</p>
|
|
${queryes}
|
|
<p style='margin-left: 15px; word-wrap: break-word;'>
|
|
<b>Body : </b>
|
|
</p>
|
|
${body}
|
|
<br>
|
|
<p style='margin-left: 15px;'>
|
|
<b>Examples : </b>
|
|
</p>
|
|
${generateExamples(data.examples)}
|
|
<br><hr>`;
|
|
return html;
|
|
}
|
|
|
|
module.exports = function htmlGenerator(collection_name, data, outputPath) {
|
|
console.log("Start parse");
|
|
let html =
|
|
`<html>
|
|
<head>
|
|
<style>
|
|
* {
|
|
padding: 0;
|
|
margin: 2px 0;
|
|
}
|
|
h2, h3 {
|
|
margin: 10px 0;
|
|
}
|
|
.page {
|
|
page-break-after: always;
|
|
counter-increment: page;
|
|
}
|
|
.content {
|
|
font-family: Courier New, monospace;
|
|
word-wrap: break-word;
|
|
page-break-inside: avoid;
|
|
}
|
|
@page {
|
|
size: A4;
|
|
margin: 0.5cm;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="page">
|
|
<div class="content">
|
|
<h1 style='text-align: center;'>
|
|
<u>${collection_name} Documentation</u>
|
|
</h1>
|
|
</div>
|
|
</div>
|
|
`;
|
|
|
|
Object.keys(data).forEach(function(scope) {
|
|
if (!data[scope].apis) {
|
|
let trigger = false;
|
|
Object.keys(data[scope]).forEach(function(subScope) {
|
|
if (trigger === false) {
|
|
html += `
|
|
<div class="page">
|
|
<div class="content">
|
|
<h2 style='text-align: center;'>
|
|
<u>${scope}</u>
|
|
</h2>
|
|
<h3 style='margin-left: 5px;'>
|
|
<u>${subScope}</u>
|
|
</h3>
|
|
<hr>
|
|
`;
|
|
trigger = true;
|
|
}
|
|
else {
|
|
html += `
|
|
<div class="page">
|
|
<div class="content">
|
|
<h3 style='margin-left: 5px;'>
|
|
<u>${subScope}</u>
|
|
</h3>
|
|
<hr>
|
|
`;
|
|
}
|
|
data[scope][subScope].apis.forEach(api => {
|
|
html += `
|
|
<div class="page">
|
|
<div class="content">
|
|
`;
|
|
html += generateApiHtml(api);
|
|
html += `
|
|
</div>
|
|
</div>`;
|
|
});
|
|
html += `
|
|
</div>
|
|
</div>`;
|
|
});
|
|
}
|
|
else {
|
|
html += `
|
|
<div class="page">
|
|
<div class="content">
|
|
<h2 style='margin-left: 5px;'>
|
|
<u>${scope}</u>
|
|
</h2>
|
|
<hr>
|
|
`;
|
|
data[scope].apis.forEach(api => {
|
|
html += `
|
|
<div class="page">
|
|
<div class="content">
|
|
`;
|
|
html += generateApiHtml(api);
|
|
html += `
|
|
</div>
|
|
</div>`;
|
|
});
|
|
html += `
|
|
</div>
|
|
</div>
|
|
`;
|
|
}
|
|
});
|
|
|
|
html += "</body></html>";
|
|
fs.writeFile(`${outputPath}/${collection_name}.html`, html, 'utf8', (err) => {
|
|
if (err) {
|
|
console.log(err);
|
|
}
|
|
console.log(`${outputPath}/${collection_name}.html`);
|
|
});
|
|
console.log("End parse");
|
|
return html;
|
|
};
|