swagger-jsdoc
Advanced tools
Comparing version 3.3.0 to 3.4.0
@@ -1,38 +0,11 @@ | ||
const parser = require('swagger-parser'); | ||
const createSpecification = require('./createSpecification'); | ||
const specHelper = require('./specification'); | ||
const parseApiFile = require('./parseApiFile'); | ||
const filterJsDocComments = require('./filterJsDocComments'); | ||
const convertGlobPaths = require('./convertGlobPaths'); | ||
const hasEmptyProperty = require('./hasEmptyProperty'); | ||
const finalizeSpecificationObject = require('./finalizeSpecificationObject'); | ||
const updateSpecificationObject = require('./updateSpecificationObject'); | ||
/** | ||
* OpenAPI specification validator does not accept empty values for a few properties. | ||
* Solves validator error: "Schema error should NOT have additional properties" | ||
* @function | ||
* @param {object} inputSpec - The swagger/openapi specification | ||
* @param {object} improvedSpec - The cleaned version of the inputSpec | ||
*/ | ||
function cleanUselessProperties(inputSpec) { | ||
const improvedSpec = JSON.parse(JSON.stringify(inputSpec)); | ||
const toClean = [ | ||
'definitions', | ||
'responses', | ||
'parameters', | ||
'securityDefinitions', | ||
]; | ||
toClean.forEach(unnecessaryProp => { | ||
if (hasEmptyProperty(improvedSpec[unnecessaryProp])) { | ||
delete improvedSpec[unnecessaryProp]; | ||
} | ||
}); | ||
return improvedSpec; | ||
} | ||
function getSpecificationObject(options) { | ||
// Get input definition and prepare the specification's skeleton | ||
const definition = options.swaggerDefinition || options.definition; | ||
let specification = createSpecification(definition); | ||
const specification = createSpecification(definition); | ||
@@ -43,22 +16,9 @@ // Parse the documentation containing information about APIs. | ||
for (let i = 0; i < apiPaths.length; i += 1) { | ||
const files = parseApiFile(apiPaths[i]); | ||
const swaggerJsDocComments = filterJsDocComments(files.jsdoc); | ||
specHelper.addDataToSwaggerObject(specification, files.yaml); | ||
specHelper.addDataToSwaggerObject(specification, swaggerJsDocComments); | ||
const parsedFile = parseApiFile(apiPaths[i]); | ||
updateSpecificationObject(parsedFile, specification); | ||
} | ||
parser.parse(specification, (err, api) => { | ||
if (!err) { | ||
specification = api; | ||
} | ||
}); | ||
if (specification.openapi) { | ||
specification = cleanUselessProperties(specification); | ||
} | ||
return specification; | ||
return finalizeSpecificationObject(specification); | ||
} | ||
module.exports = getSpecificationObject; |
const fs = require('fs'); | ||
const path = require('path'); | ||
const doctrine = require('doctrine'); | ||
const jsYaml = require('js-yaml'); | ||
const parseApiFileContent = require('./parseApiFileContent'); | ||
/** | ||
@@ -11,29 +9,10 @@ * Parses the provided API file for JSDoc comments. | ||
* @returns {{jsdoc: array, yaml: array}} JSDoc comments and Yaml files | ||
* @requires doctrine | ||
*/ | ||
function parseApiFile(file) { | ||
const jsDocRegex = /\/\*\*([\s\S]*?)\*\//gm; | ||
const fileContent = fs.readFileSync(file, { encoding: 'utf8' }); | ||
const ext = path.extname(file); | ||
const yaml = []; | ||
const jsDocComments = []; | ||
if (ext === '.yaml' || ext === '.yml') { | ||
yaml.push(jsYaml.safeLoad(fileContent)); | ||
} else { | ||
const regexResults = fileContent.match(jsDocRegex); | ||
if (regexResults) { | ||
for (let i = 0; i < regexResults.length; i += 1) { | ||
const jsDocComment = doctrine.parse(regexResults[i], { unwrap: true }); | ||
jsDocComments.push(jsDocComment); | ||
} | ||
} | ||
} | ||
return { | ||
yaml, | ||
jsdoc: jsDocComments, | ||
}; | ||
return parseApiFileContent(fileContent, ext); | ||
} | ||
module.exports = parseApiFile; |
@@ -5,2 +5,7 @@ /** @module index */ | ||
const createSpecification = require('./helpers/createSpecification'); | ||
const parseApiFileContent = require('./helpers/parseApiFileContent'); | ||
const updateSpecificationObject = require('./helpers/updateSpecificationObject'); | ||
const finalizeSpecificationObject = require('./helpers/finalizeSpecificationObject'); | ||
/** | ||
@@ -36,1 +41,6 @@ * Generates the specification. | ||
}; | ||
module.exports.createSpecification = createSpecification; | ||
module.exports.parseApiFileContent = parseApiFileContent; | ||
module.exports.updateSpecificationObject = updateSpecificationObject; | ||
module.exports.finalizeSpecificationObject = finalizeSpecificationObject; |
{ | ||
"name": "swagger-jsdoc", | ||
"version": "3.3.0", | ||
"version": "3.4.0", | ||
"description": "Generates swagger doc based on JSDoc", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -25,2 +25,9 @@ # swagger-jsdoc | ||
### Webpack integration | ||
You can use this package with a webpack plugin to keep your swagger documentation up-to-date when building your app: | ||
- [swagger-jsdoc-webpack-plugin](https://github.com/patsimm/swagger-jsdoc-webpack-plugin) - Rebuild the swagger definition based on a predefined list of files on each webpack build. | ||
- [swagger-jsdoc-sync-webpack-plugin](https://github.com/gautier-lefebvre/swagger-jsdoc-sync-webpack-plugin) - Rebuild the swagger definition based on the files imported in your app on each webpack build. | ||
## Supported versions | ||
@@ -27,0 +34,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
141563
21
564
171