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

Adding callback function to customize API page, and for sorting of AP… #66

Open
wants to merge 5 commits 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
22 changes: 14 additions & 8 deletions src/object-tree-gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function generatePropDescription(propDescrArray, localize) {
descrStack.push({
text: [
{ text: `${localize.allowed}:`, style: ['sub', 'b', 'darkGray'] },
{ text: propDescrArray[4], style: ['small', 'lightGray', 'mono'] },
{ text: propDescrArray[4], style: ['small', 'darkGray', 'mono'] },
],
});
}
Expand All @@ -118,14 +118,14 @@ function generatePropDescription(propDescrArray, localize) {
descrStack.push({
text: [
{ text: `${localize.pattern}:`, style: ['sub', 'b', 'darkGray'] },
{ text: propDescrArray[5], style: ['small', 'lightGray', 'mono'] },
{ text: propDescrArray[5], style: ['small', 'darkGray', 'mono'] },
],
});
}
if (propDescrArray[6]) {
descrStack.push({
text: `${propDescrArray[6]}`,
style: ['sub', 'lightGray'],
style: ['sub', 'darkGray'],
margin: [0, 3, 0, 0],
});
}
Expand Down Expand Up @@ -226,7 +226,7 @@ export function objectToTree(obj, localize, prevKeyDataType = 'object', prevKey

return [
{ text: prevKey, style: ['small', 'mono'], margin: 0 },
{ text: (propDescrArray[0] ? propDescrArray[0] : ''), style: ['small', 'mono', 'lightGray'], margin: 0 },
{ text: (propDescrArray[0] ? propDescrArray[0] : ''), style: ['small', 'mono', 'darkGray'], margin: 0 },
{ stack: descrStack, margin: 0 },
];
}
Expand Down Expand Up @@ -340,14 +340,20 @@ export function objectToTableTree(obj, localize, allRows = [], level = 0) {
if (typeof obj[key] === 'object') {
let objType;
if (obj[key]['::type'] === 'array') {
objType = 'array';
if (obj[key]['::props']['::type']) {
objType = `Array of ${obj[key]['::props']['::type']}`;
} else if (typeof obj[key]['::props'] !== 'object') {
const getInfo = obj[key]['::props'].split('~|~');
getInfo[0] = `Array of ${getInfo[0]}`;
objType = getInfo[0];
}
} else {
objType = 'object';
}
const objRow = [
{ text: key, style: ['small', 'b'], margin: [leftMargin, 0, 0, 0] },
{ text: objType, style: ['small', 'mono', 'lightGray'], margin: 0 },
{ text: '', margin: 0 },
{ text: objType, style: ['small', 'mono', 'darkGray'], margin: 0 },
{ text: obj[key]['::description'], style: ['sub', 'darkGray'], margin: 0 },
];
allRows.push(objRow);
if (obj[key]['::type'] === 'array') {
Expand All @@ -361,7 +367,7 @@ export function objectToTableTree(obj, localize, allRows = [], level = 0) {

allRows.push([
{ text: key, style: ['small'], margin: [leftMargin, 0, 0, 0] },
{ text: (typeAndDescr[0] ? typeAndDescr[0] : ''), style: ['small', 'mono', 'lightGray'], margin: 0 },
{ text: (typeAndDescr[0] ? typeAndDescr[0] : ''), style: ['small', 'mono', 'darkGray'], margin: 0 },
{ stack: ((descrStack && descrStack.length) > 0 ? descrStack : [{ text: '' }]), margin: 0 },
]);
}
Expand Down
13 changes: 8 additions & 5 deletions src/pdf-gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import {
getInfoDef, getSecurityDef, getApiDef, getApiListDef,
} from '@/pdf-parts-gen';

export default async function createPdf(specUrl, options) {
const parsedSpec = await ProcessSpec(specUrl, options.pdfSortTags);
export default async function createPdf(specUrl, options, customizeApiPageCallback, apiSortFn) {
const parsedSpec = await ProcessSpec(specUrl, options.pdfSortTags, apiSortFn);

const pdfStyles = {
title: { fontSize: 32 },
Expand Down Expand Up @@ -69,7 +69,7 @@ export default async function createPdf(specUrl, options) {
allContent.push(securityDef);
}
if (options.includeApiDetails) {
apiDef = getApiDef(parsedSpec, '', options.pdfSchemaStyle, options.localize, options.includeExample, options.includeApiList);
apiDef = getApiDef(parsedSpec, '', options.pdfSchemaStyle, options.localize, options.includeExample, options.includeApiList, customizeApiPageCallback);
allContent.push(apiDef);
}
if (options.includeApiList) {
Expand All @@ -91,7 +91,6 @@ export default async function createPdf(specUrl, options) {
styles: pdfStyles,
};


pdfMake.fonts = {
Roboto: {
normal: 'Roboto-Regular.ttf',
Expand All @@ -109,5 +108,9 @@ export default async function createPdf(specUrl, options) {
};
// pdfMake.vfs = pdfFonts.pdfMake.vfs;
pdfMake.vfs = pdfFonts;
pdfMake.createPdf(finalDocDef).open();
if (options.pdfName !== '') {
pdfMake.createPdf(finalDocDef).download(options.pdfName);
} else {
pdfMake.createPdf(finalDocDef).open();
}
}
12 changes: 10 additions & 2 deletions src/pdf-parts-gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ function getRequestBodyDef(requestBody, schemaStyle, localize) {
// if Schema Style is Tree
let schemaTableTreeDef;
if (schemaInObjectNotaion['::type'] && schemaInObjectNotaion['::type'] === 'array') {
schemaTableTreeDef = objectToTableTree(schemaInObjectNotaion['::prop'], localize, 'array');
schemaTableTreeDef = objectToTableTree(schemaInObjectNotaion['::props'], localize);
} else {
schemaTableTreeDef = objectToTableTree(schemaInObjectNotaion, localize);
}
Expand Down Expand Up @@ -316,7 +316,7 @@ function getResponseDef(responses, schemaStyle, localize) {
}

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

Expand Down Expand Up @@ -407,6 +407,14 @@ export function getApiDef(spec, filterPath, schemaStyle, localize, includeExampl
});
}

if (customizeApiPageCallback != null) {
const customMessageDef = customizeApiPageCallback(path.summary);

operationContent.push({
stack: customMessageDef
});
}

// End of Operation - Line (Except the last content)
if (j === tag.paths.length - 1) {
operationContent.push({
Expand Down
8 changes: 5 additions & 3 deletions src/rapipdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export default customElements.define('rapi-pdf', class RapiPdf extends HTMLEleme
}
}

generatePdf(jsonObj) {
generatePdf(jsonObj, customizeApiPageCallback, apiSortFn) {
const pdfSortTags = this.getAttribute('pdf-sort-tags') !== 'false';
const pdfPrimaryColor = this.getAttribute('pdf-primary-color');
const pdfAlternateColor = this.getAttribute('pdf-alternate-color');
Expand All @@ -220,9 +220,10 @@ export default customElements.define('rapi-pdf', class RapiPdf extends HTMLEleme
const includeExample = this.getAttribute('include-example') === 'true';
const includeApiDetails = this.getAttribute('include-api-details') !== 'false';
const includeApiList = this.getAttribute('include-api-list') === 'true';

const pdfName = this.getAttribute('pdf-name') ? this.getAttribute('pdf-name') : '';
const localize = this.localize;
const options = {
pdfName,
pdfSortTags,
pdfPrimaryColor,
pdfAlternateColor,
Expand All @@ -241,6 +242,7 @@ export default customElements.define('rapi-pdf', class RapiPdf extends HTMLEleme
localize,
};
const spec = this.specUrl || jsonObj;
createPdf(spec, options);

createPdf(spec, options, customizeApiPageCallback, apiSortFn);
}
});
5 changes: 4 additions & 1 deletion src/spec-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import Swagger from 'swagger-client';
import converter from 'swagger2openapi';

export default async function ProcessSpec(specUrl, sortTags) {
export default async function ProcessSpec(specUrl, sortTags, apiSortFn) {
let jsonParsedSpec;
let convertedSpec;
const convertOptions = { patch: true, warnOnly: true };
Expand Down Expand Up @@ -165,6 +165,9 @@ export default async function ProcessSpec(specUrl, sortTags) {
securitySchemes = (openApiSpec.components ? openApiSpec.components.securitySchemes : {});
if (sortTags) {
tags.sort((a, b) => (a.name < b.name ? -1 : (a.name > b.name ? 1 : 0)));
if(apiSortFn != null){
apiSortFn(tags);
}
}
const parsedSpec = {
info: openApiSpec.info,
Expand Down