swagger-jsdoc
Advanced tools
Comparing version 1.1.2 to 1.2.0
@@ -7,2 +7,3 @@ /** @module index */ | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var doctrine = require('doctrine'); | ||
@@ -12,3 +13,2 @@ var jsYaml = require('js-yaml'); | ||
/** | ||
@@ -18,3 +18,3 @@ * Parses the provided API file for JSDoc comments. | ||
* @param {string} file - File to be parsed | ||
* @returns {array} JSDoc comments | ||
* @returns {{jsdoc: array, yaml: array}} JSDoc comments and Yaml files | ||
* @requires doctrine | ||
@@ -26,13 +26,22 @@ */ | ||
var fileContent = fs.readFileSync(file, { encoding: 'utf8' }); | ||
var regexResults = fileContent.match(jsDocRegex); | ||
var ext = path.parse(file).ext; | ||
var yaml = []; | ||
var jsDocComments = []; | ||
var jsDocComments = []; | ||
if (regexResults) { | ||
for (var i = 0; i < regexResults.length; i = i + 1) { | ||
var jsDocComment = doctrine.parse(regexResults[i], { unwrap: true }); | ||
jsDocComments.push(jsDocComment); | ||
if (ext === '.yaml' || ext === '.yml') { | ||
yaml.push(jsYaml.safeLoad(fileContent)); | ||
} else { | ||
var regexResults = fileContent.match(jsDocRegex); | ||
if (regexResults) { | ||
for (var i = 0; i < regexResults.length; i = i + 1) { | ||
var jsDocComment = doctrine.parse(regexResults[i], { unwrap: true }); | ||
jsDocComments.push(jsDocComment); | ||
} | ||
} | ||
} | ||
return jsDocComments; | ||
return { | ||
yaml: yaml, | ||
jsdoc: jsDocComments, | ||
}; | ||
} | ||
@@ -87,10 +96,11 @@ | ||
/** | ||
* Adds the data in the Swagger JSDoc comments to the swagger object. | ||
* Adds the data in to the swagger object. | ||
* @function | ||
* @param {object} swaggerObject - Swagger object which will be written to | ||
* @param {array} swaggerJsDocComments - JSDoc comments tagged with '@swagger' | ||
* @param {object[]} data - objects of parsed swagger data from yaml or jsDoc | ||
* comments | ||
*/ | ||
function addDataToSwaggerObject(swaggerObject, swaggerJsDocComments) { | ||
for (var i = 0; i < swaggerJsDocComments.length; i = i + 1) { | ||
var pathObject = swaggerJsDocComments[i]; | ||
function addDataToSwaggerObject(swaggerObject, data) { | ||
for (var i = 0; i < data.length; i = i + 1) { | ||
var pathObject = data[i]; | ||
var propertyNames = Object.getOwnPropertyNames(pathObject); | ||
@@ -100,3 +110,7 @@ for (var j = 0; j < propertyNames.length; j = j + 1) { | ||
switch (propertyName) { | ||
case 'securityDefinition': | ||
case 'response': | ||
case 'parameter': | ||
case 'definition': { | ||
var keyName = propertyName + 's'; | ||
var definitionNames = Object | ||
@@ -106,3 +120,3 @@ .getOwnPropertyNames(pathObject[propertyName]); | ||
var definitionName = definitionNames[k]; | ||
swaggerObject.definitions[definitionName] = | ||
swaggerObject[keyName][definitionName] = | ||
pathObject[propertyName][definitionName]; | ||
@@ -145,7 +159,11 @@ } | ||
swaggerObject.definitions = {}; | ||
swaggerObject.responses = {}; | ||
swaggerObject.parameters = {}; | ||
swaggerObject.securityDefinitions = {}; | ||
// Parse the documentation in the APIs array. | ||
for (var i = 0; i < options.apis.length; i = i + 1) { | ||
var jsDocComments = parseApiFile(options.apis[i]); | ||
var swaggerJsDocComments = filterJsDocComments(jsDocComments); | ||
var files = parseApiFile(options.apis[i]); | ||
var swaggerJsDocComments = filterJsDocComments(files.jsdoc); | ||
addDataToSwaggerObject(swaggerObject, files.yaml); | ||
addDataToSwaggerObject(swaggerObject, swaggerJsDocComments); | ||
@@ -152,0 +170,0 @@ } |
{ | ||
"name": "swagger-jsdoc", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"description": "Generates swagger doc based on JSDoc", | ||
@@ -30,16 +30,16 @@ "main": "index.js", | ||
"dependencies": { | ||
"doctrine": "^0.7.0", | ||
"js-yaml": "^3.4.2", | ||
"swagger-parser": "^3.0.1" | ||
"doctrine": "^1.2.0", | ||
"js-yaml": "^3.5.3", | ||
"swagger-parser": "^3.4.0" | ||
}, | ||
"devDependencies": { | ||
"body-parser": "^1.14.0", | ||
"express": "^4.13.3", | ||
"istanbul": "^0.3.21", | ||
"jscs": "^2.1.1", | ||
"mocha": "^2.3.3", | ||
"mocha-jscs": "^3.0.0", | ||
"mocha-jshint": "^2.2.3", | ||
"supertest": "^1.1.0" | ||
"body-parser": "^1.15.0", | ||
"express": "^4.13.4", | ||
"istanbul": "^0.4.2", | ||
"jscs": "^2.10.1", | ||
"mocha": "^2.4.5", | ||
"mocha-jscs": "^4.2.0", | ||
"mocha-jshint": "^2.3.1", | ||
"supertest": "^1.2.0" | ||
} | ||
} |
@@ -174,3 +174,18 @@ # swagger-jsdoc | ||
### Load external definitions | ||
You can load external definitions or paths after ``swaggerJSDoc()`` function. | ||
```javascript | ||
// Initialize swagger-jsdoc -> returns validated swagger spec in json format | ||
var swaggerSpec = swaggerJSDoc(options); | ||
// load external schema json | ||
swaggerSpec.definitions.in_login = require("config/schemajson/in.login.schema.json"); | ||
swaggerSpec.definitions.out_login = require("config/schemajson/out.login.schema.json"); | ||
// or set manual paths | ||
swaggerSpec.paths["api/v1/cool"] = {"get" : { ... } } | ||
}; | ||
``` | ||
## Example App | ||
@@ -177,0 +192,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
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
12975
154
203
1
+ Addeddoctrine@1.5.0(transitive)
+ Addedesutils@2.0.3(transitive)
+ Addedisarray@1.0.0(transitive)
- Removeddoctrine@0.7.2(transitive)
- Removedesutils@1.1.6(transitive)
- Removedisarray@0.0.1(transitive)
Updateddoctrine@^1.2.0
Updatedjs-yaml@^3.5.3
Updatedswagger-parser@^3.4.0