239 lines
6.4 KiB
JavaScript
239 lines
6.4 KiB
JavaScript
const fs = require('fs');
|
|
const generatePdf = require('./pdf-generator');
|
|
const generateHtml = require('./html-generator');
|
|
|
|
function getExamples(data, scopeAuth, apiRoute) {
|
|
const examples = {
|
|
success: [],
|
|
exceptions: []
|
|
};
|
|
|
|
data.forEach(function(item) {
|
|
const method = item.originalRequest.method;
|
|
const headers = [];
|
|
const auth = [];
|
|
const variables = [];
|
|
const queryes = [];
|
|
item.originalRequest.header.forEach(headeritem => {
|
|
const header_obj = {
|
|
key: headeritem.key,
|
|
value: headeritem.value
|
|
};
|
|
headers.push(header_obj);
|
|
});
|
|
|
|
if(item.originalRequest.auth) {
|
|
const auth_type = item.originalRequest.auth.type;
|
|
item.originalRequest.auth[auth_type].forEach(authitem=> {
|
|
const auth_obj = {
|
|
key: authitem.key,
|
|
value: authitem.value,
|
|
type: authitem.type
|
|
};
|
|
auth.push(auth_obj);
|
|
});
|
|
}
|
|
else {
|
|
if (scopeAuth !== null) {
|
|
const auth_type = scopeAuth.type;
|
|
scopeAuth[auth_type].forEach(authitem=> {
|
|
const auth_obj = {
|
|
key: authitem.key,
|
|
value: authitem.value,
|
|
type: authitem.type
|
|
};
|
|
req_auth.push(auth_obj);
|
|
});
|
|
}
|
|
}
|
|
|
|
let body = "";
|
|
let mode = "";
|
|
let options = "";
|
|
if(item.originalRequest.body) {
|
|
body = item.originalRequest.body[item.originalRequest.body.mode];
|
|
mode = item.originalRequest.body.mode;
|
|
if (item.originalRequest.body.options) {
|
|
options = item.originalRequest.body.options[item.originalRequest.body.mode];
|
|
}
|
|
}
|
|
|
|
const url = item.originalRequest.url.raw.replace("{{api_url}}", apiRoute);
|
|
if (item.originalRequest.url.variable) {
|
|
item.originalRequest.url.variable.forEach(item=> {
|
|
variables.push(item);
|
|
});
|
|
}
|
|
if (item.originalRequest.url.query) {
|
|
item.originalRequest.url.query.forEach(item=> {
|
|
queryes.push(item);
|
|
});
|
|
}
|
|
|
|
const result = {
|
|
name: item.name,
|
|
request: {
|
|
header: headers,
|
|
auth: auth,
|
|
method: method,
|
|
body: body,
|
|
bodyMode: mode,
|
|
bodyOptions: options,
|
|
url: url,
|
|
variables: variables,
|
|
queryes: queryes,
|
|
desc: item.originalRequest.description || ""
|
|
},
|
|
response: {
|
|
status: item.status,
|
|
code: item.code,
|
|
language: item._postman_previewlanguage,
|
|
headers: item.header,
|
|
cookies: item.cookie,
|
|
body: item.body
|
|
}
|
|
};
|
|
if (item.code === 200) {
|
|
examples.success.push(result);
|
|
}
|
|
else {
|
|
examples.exceptions.push(result);
|
|
}
|
|
});
|
|
return examples;
|
|
}
|
|
|
|
function parseData(data, scopeAuth, apiRoute) {
|
|
const req_name = data.name;
|
|
const req_method = data.request.method;
|
|
const req_headers = [];
|
|
const req_auth = [];
|
|
const variables = [];
|
|
const queryes = [];
|
|
|
|
let examples;
|
|
if (data.response && data.response.length > 0) {
|
|
examples = getExamples(data.response, scopeAuth, apiRoute);
|
|
}
|
|
|
|
data.request.header.forEach(headeritem => {
|
|
const header_obj = {
|
|
key: headeritem.key,
|
|
value: headeritem.value
|
|
};
|
|
if (header_obj.key !== 'Authorization') {
|
|
req_headers.push(header_obj);
|
|
}
|
|
});
|
|
|
|
if(data.request.auth) {
|
|
const auth_type = data.request.auth.type;
|
|
data.request.auth[auth_type].forEach(authitem=> {
|
|
const auth_obj = {
|
|
key: authitem.key,
|
|
value: authitem.value,
|
|
type: authitem.type
|
|
};
|
|
req_auth.push(auth_obj);
|
|
});
|
|
}
|
|
else {
|
|
if (scopeAuth !== null) {
|
|
const auth_type = scopeAuth.type;
|
|
scopeAuth[auth_type].forEach(authitem=> {
|
|
const auth_obj = {
|
|
key: authitem.key,
|
|
value: authitem.value,
|
|
type: authitem.type
|
|
};
|
|
req_auth.push(auth_obj);
|
|
});
|
|
}
|
|
}
|
|
|
|
let req_body = "";
|
|
let req_mode = "";
|
|
let req_options = "";
|
|
if(data.request.body) {
|
|
req_body = data.request.body[data.request.body.mode];
|
|
req_mode = data.request.body.mode;
|
|
if (data.request.body.options) {
|
|
req_options = data.request.body.options[data.request.body.mode];
|
|
}
|
|
}
|
|
|
|
const reqUrl = data.request.url.raw.replace("{{api_url}}", apiRoute);
|
|
if (reqUrl.includes('public')) {
|
|
req_headers.push({key: 'Authorization (optional)', value: 'Bearer |token|'});
|
|
}
|
|
if (reqUrl.includes('user')) {
|
|
req_headers.push({key: 'Authorization (required)', value: 'Bearer |token|'});
|
|
}
|
|
if (reqUrl.includes('admin')) {
|
|
req_headers.push({key: 'Authorization (required)', value: 'Bearer |token|'});
|
|
}
|
|
|
|
if (data.request.url.variable) {
|
|
data.request.url.variable.forEach(item=> {
|
|
variables.push(item);
|
|
});
|
|
}
|
|
if (data.request.url.query) {
|
|
data.request.url.query.forEach(item=> {
|
|
queryes.push(item);
|
|
});
|
|
}
|
|
|
|
return {
|
|
name: req_name,
|
|
header: req_headers,
|
|
auth: req_auth,
|
|
method: req_method,
|
|
body: req_body,
|
|
bodyMode: req_mode,
|
|
bodyOptions: req_options,
|
|
url: reqUrl,
|
|
variables: variables,
|
|
queryes: queryes,
|
|
desc: data.request.description || "",
|
|
examples: examples || {success: [],exceptions: []}
|
|
};
|
|
}
|
|
|
|
module.exports = function filereader(filepath, outputPath, apiRoute) {
|
|
fs.readFile(filepath, 'utf-8', (err, data) => {
|
|
if(err) {
|
|
console.log(err);
|
|
}
|
|
const jsonData = JSON.parse(data);
|
|
const collection_name = jsonData.info.name;
|
|
const resultData = {};
|
|
jsonData.item.forEach(scope => {
|
|
if(scope.item) {
|
|
let auth = null;
|
|
if (scope.auth && scope.auth.length > 0) {
|
|
auth = scope.auth;
|
|
}
|
|
resultData[scope.name] = {};
|
|
scope.item.forEach(subscope => {
|
|
if(subscope.item) {
|
|
resultData[scope.name][subscope.name] = {};
|
|
resultData[scope.name][subscope.name].apis = [];
|
|
subscope.item.forEach(reqitem => {
|
|
resultData[scope.name][subscope.name].apis.push(parseData(reqitem, auth, apiRoute));
|
|
});
|
|
}
|
|
else {
|
|
if(!resultData[scope.name].apis) {
|
|
resultData[scope.name].apis = [];
|
|
}
|
|
resultData[scope.name].apis.push(parseData(subscope, auth, apiRoute));
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
generatePdf(generateHtml(collection_name, resultData, outputPath),`${collection_name}.pdf`, outputPath);
|
|
});
|
|
};
|