diff --git a/src/object-tree-gen.js b/src/object-tree-gen.js index 8966ac1..7e2409e 100644 --- a/src/object-tree-gen.js +++ b/src/object-tree-gen.js @@ -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'] }, ], }); } @@ -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], }); } @@ -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 }, ]; } @@ -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') { @@ -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 }, ]); } diff --git a/src/pdf-gen.js b/src/pdf-gen.js index 66875cf..04948f9 100644 --- a/src/pdf-gen.js +++ b/src/pdf-gen.js @@ -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 }, @@ -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) { @@ -91,7 +91,6 @@ export default async function createPdf(specUrl, options) { styles: pdfStyles, }; - pdfMake.fonts = { Roboto: { normal: 'Roboto-Regular.ttf', @@ -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(); + } } diff --git a/src/pdf-parts-gen.js b/src/pdf-parts-gen.js index 8535e01..5ab6a30 100644 --- a/src/pdf-parts-gen.js +++ b/src/pdf-parts-gen.js @@ -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); } @@ -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; @@ -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({ diff --git a/src/rapipdf.js b/src/rapipdf.js index 903c2b3..a02ecd8 100644 --- a/src/rapipdf.js +++ b/src/rapipdf.js @@ -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'); @@ -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, @@ -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); } }); diff --git a/src/spec-parser.js b/src/spec-parser.js index 4c45d7c..23cc300 100644 --- a/src/spec-parser.js +++ b/src/spec-parser.js @@ -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 }; @@ -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,