Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Changes to Read and Print the CallBacks information in the output pdf #84

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 74 additions & 7 deletions src/pdf-parts-gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {

function markdownToPdfmake(markdown) {
const html = marked(markdown);
return htmlToPdfmake(html);
return htmlToPdfmake(html);
}

// Info Def
Expand Down Expand Up @@ -202,17 +202,24 @@ function getExamplesDef(contentTypeObj, localizedExampleLabel) {
}

// Request Body Def
function getRequestBodyDef(requestBody, schemaStyle, localize, includeExample = false) {
function getRequestBodyDef(requestBody, schemaStyle, localize, includeExample = false, isCallBack = false) {
if (!requestBody) {
return;
}
const content = [];
let formParamDef;
for (const contentType in requestBody.content) {
const contentTypeObj = requestBody.content[contentType];
const requestBodyDef = [
const requestBodyDef = [];
if (isCallBack) {
requestBodyDef.push({
text: 'CALLBACK REQUEST', color: '#005b96', margin: [0, 10, 0, 0], style: ['medium', 'b'],
});
}
requestBodyDef.push(
{ text: `${localize.requestBody} - ${contentType}`, margin: [0, 10, 0, 0], style: ['small', 'b'] },
];
);


if ((contentType.includes('form') || contentType.includes('multipart-form')) && contentTypeObj.schema) {
formParamDef = getParameterTableDef(contentTypeObj.schema.properties, 'FORM DATA', localize);
Expand Down Expand Up @@ -270,13 +277,16 @@ function getRequestBodyDef(requestBody, schemaStyle, localize, includeExample =
// Response Def
function getResponseDef(responses, schemaStyle, localize, includeExample = false) {
const respDef = [];
const responseDef = [];

for (const statusCode in responses) {
const allResponseDefs = [];

for (const contentType in responses[statusCode].content) {
const responseDef = [
{ text: `${localize.responseModel} - ${contentType}`, margin: [10, 10, 0, 0], style: ['small', 'b'] },
];

console.log(responseDef, 'responseDefresponseDefresponseDefresponseDefresponseDef');
const contentTypeObj = responses[statusCode].content[contentType];
let origSchema = contentTypeObj.schema;
if (origSchema) {
Expand Down Expand Up @@ -345,6 +355,7 @@ function getResponseDef(responses, schemaStyle, localize, includeExample = false

// API details def
export function getApiDef(spec, filterPath, schemaStyle, localize, includeExample, includeApiList) {
console.log(spec, filterPath, schemaStyle, localize, includeExample, includeApiList);
const content = [{ text: localize.api, style: ['h2', 'b'] }];
let tagSeq = 0;

Expand Down Expand Up @@ -383,7 +394,7 @@ export function getApiDef(spec, filterPath, schemaStyle, localize, includeExampl
};
operationContent.push(pathDescrMarkDef);
}

console.log('path.requestBody', path);
// Generate Request Defs
const requestSetDef = [];
const pathParams = path.parameters ? path.parameters.filter((param) => param.in === 'path') : null;
Expand All @@ -396,8 +407,17 @@ export function getApiDef(spec, filterPath, schemaStyle, localize, includeExampl
const requestBodyTableDefs = getRequestBodyDef(path.requestBody, schemaStyle, localize, includeExample);
const headerParamTableDef = getParameterTableDef(headerParams, 'header', localize, includeExample);
const cookieParamTableDef = getParameterTableDef(cookieParams, 'cookie', localize, includeExample);
const callBackData = null;
if (path.callbacks != null) {
const myKey = Object.keys(path.callbacks)[0];
console.log(path.callbacks[myKey]);
const callBackDefKey = '{$request.body#/callbackUrl}';
callBackData = path.callbacks[myKey][callBackDefKey];
}
const callBackRequestBodyTableDefs = null;
if (callBackData != null) callBackRequestBodyTableDefs = getRequestBodyDef(callBackData.post.requestBody, schemaStyle, localize, includeExample, true);
operationContent.push({ text: localize.request, style: ['p', 'b', 'alternate'], margin: [0, 10, 0, 0] });
if (pathParamTableDef || queryParamTableDef || headerParamTableDef || cookieParamTableDef || requestBodyTableDefs) {
if (pathParamTableDef || queryParamTableDef || headerParamTableDef || cookieParamTableDef || requestBodyTableDefs || callBackRequestBodyTableDefs) {
if (pathParamTableDef) {
requestSetDef.push(pathParamTableDef);
}
Expand All @@ -409,6 +429,11 @@ export function getApiDef(spec, filterPath, schemaStyle, localize, includeExampl
requestSetDef.push(v);
});
}
/* if (callBackRequestBodyTableDefs != null) {
callBackRequestBodyTableDefs.map((v) => {
requestSetDef.push(v);
});
} */
if (headerParamTableDef) {
requestSetDef.push(headerParamTableDef);
}
Expand All @@ -428,12 +453,41 @@ export function getApiDef(spec, filterPath, schemaStyle, localize, includeExampl
// Generate Response Defs
operationContent.push({ text: localize.response, style: ['p', 'b', 'alternate'], margin: [0, 10, 0, 0] });
const respDef = getResponseDef(path.responses, schemaStyle, localize, includeExample);
const respCallBackDev = null;

if (respDef && respDef.length > 0) {
operationContent.push({
stack: respDef,
margin: [10, 5, 0, 5],
});
}
// operationContent.push({
// text: 'CallBack Request L', color: '#006400', margin: [10, 10, 0, 0], style: ['small', 'b'],
// });
if (callBackRequestBodyTableDefs != null) {
callBackRequestBodyTableDefs.map((v) => {
v = v.map((item, index) => {
if (index != 0) {
item.margin = [10, 5, 0, 5];
}
return item;
});
operationContent.push(v);
});
}
if (callBackData != null) respCallBackDev = getResponseDef(callBackData.post.responses, schemaStyle, localize, includeExample, true);
if (respCallBackDev != null && respCallBackDev.length > 0) {
operationContent.push({

text: 'CALLBACK RESPONSE', color: '#005b96', margin: [0, 10, 0, 0], style: ['medium', 'b'],
});

operationContent.push({
stack: respCallBackDev,
margin: [10, 5, 0, 5],
});
}


// End of Operation - Line (Except the last content)
if (j === tag.paths.length - 1) {
Expand All @@ -443,6 +497,18 @@ export function getApiDef(spec, filterPath, schemaStyle, localize, includeExampl
}],
});
}
// csk - moved the callback push from above to here
/* if (callBackRequestBodyTableDefs != null) {
callBackRequestBodyTableDefs.map((v) => {
requestSetDef.push(v);
});
}
if (respCallBackDev != null && respCallBackDev.length > 0) {
operationContent.push({
stack: respCallBackDev,
margin: [10, 5, 0, 5],
});
} */
}

if (pathSeq > 0) {
Expand All @@ -457,6 +523,7 @@ export function getApiDef(spec, filterPath, schemaStyle, localize, includeExampl
tagDescrMarkDef = { text: '' };
}


content.push(
{
text: `${tagSeq}. ${tag.name.toUpperCase()}`,
Expand Down
19 changes: 15 additions & 4 deletions src/spec-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,33 @@
import Swagger from 'swagger-client';
import converter from 'swagger2openapi';


export default async function ProcessSpec(specUrl, sortTags) {
let jsonParsedSpec;
let convertedSpec;
const convertOptions = { patch: true, warnOnly: true };
try {
let specObj;
console.log(specUrl);
if (typeof specUrl === 'string') {
specObj = await Swagger(specUrl);
specObj = await Swagger({
disableInterfaces: false,
url: specUrl,
});
} else {
specObj = await Swagger({ spec: specUrl });
specObj = await Swagger({
disableInterfaces: false,
spec: specUrl,
});
}

jsonParsedSpec = specObj.spec;
if (specObj.spec.swagger) {
convertedSpec = await converter.convertObj(specObj.spec, convertOptions);
jsonParsedSpec = convertedSpec.openapi;
}
console.log(convertedSpec);
console.log(jsonParsedSpec);
} catch (err) {
console.info('%c There was an issue while parsing the spec %o ', 'color:orangered', err); // eslint-disable-line no-console
}
Expand Down Expand Up @@ -128,7 +139,6 @@ export default async function ProcessSpec(specUrl, sortTags) {
} else {
finalParameters = fullPath.parameters ? fullPath.parameters.slice(0) : [];
}

const pathObj = {
summary,
method: methodName,
Expand All @@ -143,10 +153,11 @@ export default async function ProcessSpec(specUrl, sortTags) {
security: fullPath.security,
commonSummary: commonPathProp.summary,
commonDescription: commonPathProp.description,
callbacks: fullPath.callbacks ? fullPath.callbacks : null,
};

if (fullPath.tags) {
fullPath.tags.forEach((mtag) => {
console.log(mtag);
const mtagObj = tags.find((v) => v.name === mtag);
if (mtagObj) {
mtagObj.paths.push(pathObj);
Expand Down