express-jsdoc-swagger
Advanced tools
Comparing version 1.7.0 to 1.8.0
@@ -5,3 +5,3 @@ const doctrine = require('doctrine'); | ||
if (!comments || !Array.isArray(comments)) return []; | ||
const parsed = comments.map(comment => { | ||
return comments.map(comment => { | ||
const parsedValue = doctrine.parse(comment, options); | ||
@@ -19,5 +19,4 @@ const tags = parsedValue.tags.map(tag => ({ | ||
}); | ||
return parsed; | ||
}; | ||
module.exports = jsdocInfo; |
{ | ||
"name": "express-jsdoc-swagger", | ||
"version": "1.7.0", | ||
"version": "1.8.0", | ||
"description": "Swagger OpenAPI 3.x generator", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -40,2 +40,3 @@  | ||
```javascript | ||
// index.js file | ||
const express = require('express'); | ||
@@ -58,2 +59,3 @@ const expressJSDocSwagger = require('express-jsdoc-swagger'); | ||
}, | ||
// Base directory which we use to locate your JSDOC files | ||
baseDir: __dirname, | ||
@@ -99,3 +101,3 @@ // Glob pattern to find your jsdoc files (multiple patterns can be added in an array) | ||
1. Basic configuration | ||
1. Basic configuration options. | ||
@@ -135,3 +137,3 @@ ```javascript | ||
3. Endpoint that returns a `Songs` model array | ||
3. Endpoint which returns a `Songs` model array in the response. | ||
@@ -152,2 +154,20 @@ ```javascript | ||
3. Endpoint PUT with body and path params which returns a `Songs` model array in the response. | ||
```javascript | ||
/** | ||
* PUT /api/v1/albums/{id} | ||
* @summary Update album | ||
* @tags album | ||
* @param {string} name.path - name param description | ||
* @param {Song} request.body.required - songs info | ||
* @return {array<Song>} 200 - success response - application/json | ||
*/ | ||
app.put('/api/v1/albums/:id', (req, res) => ( | ||
res.json([{ | ||
title: 'abum 1', | ||
}]) | ||
)); | ||
``` | ||
4. Basic endpoint definition with tags, params and basic authentication | ||
@@ -211,19 +231,30 @@ | ||
After this you have to initialize using the `finish` event. More info in this [sections](eventEmitter.md). | ||
We have to wait until we have the full swagger schema to initiate the validator. | ||
```js | ||
const instance = expressJSDocSwagger(app)(options); | ||
// validator.js | ||
const { init } = require('express-oas-validator'); | ||
instance.on('finish', data => { | ||
init(data); | ||
resolve(app); | ||
const validators = instance => new Promise((resolve, reject) => { | ||
instance.on('finish', (swaggerDef) => { | ||
const { validateRequest, validateResponse } = init(swaggerDef); | ||
resolve({ validateRequest, validateResponse }); | ||
}); | ||
instance.on('error', (error) => { | ||
reject(error); | ||
}); | ||
}); | ||
module.exports = validators; | ||
``` | ||
This is a full example of how it works. | ||
You can check out this also in our [example folder](https://github.com/BRIKEV/express-jsdoc-swagger/tree/master/examples/validator). | ||
```js | ||
// index.js | ||
const express = require('express'); | ||
const expressJSDocSwagger = require('express-jsdoc-swagger'); | ||
const { init, validateRequest, validateResponse } = require('express-oas-validator'); | ||
const validator = require('./validator'); | ||
@@ -245,11 +276,6 @@ const options = { | ||
const serverApp = () => new Promise(resolve => { | ||
instance.on('finish', data => { | ||
init(data); | ||
resolve(app); | ||
}); | ||
const serverApp = async () => { | ||
const { validateRequest, validateResponse } = await validator(instance); | ||
app.use(express.urlencoded({ extended: true })); | ||
app.use(express.json()); | ||
/** | ||
@@ -302,5 +328,17 @@ * A song | ||
}); | ||
}); | ||
module.exports = serverApp; | ||
return app; | ||
}; | ||
const PORT = process.env.PORT || 4000; | ||
serverApp() | ||
.then(app => | ||
app.listen(PORT, () => | ||
console.log(`Listening PORT: ${PORT}`) | ||
)) | ||
.catch((err) => { | ||
console.error(err); | ||
process.exit(1); | ||
}); | ||
``` | ||
@@ -307,0 +345,0 @@ |
const info = require('./info'); | ||
const servers = require('./servers'); | ||
const getBasicInfo = (swaggerObjet = {}) => { | ||
const updatedOptions = { | ||
...swaggerObjet, | ||
info: { ...info(swaggerObjet.info) }, | ||
servers: servers(swaggerObjet.servers), | ||
}; | ||
return updatedOptions; | ||
}; | ||
const getBasicInfo = (swaggerObjet = {}) => ({ | ||
...swaggerObjet, | ||
info: { ...info(swaggerObjet.info) }, | ||
servers: servers(swaggerObjet.servers), | ||
}); | ||
module.exports = getBasicInfo; |
@@ -7,2 +7,3 @@ const { getTagInfo, getTagsInfo } = require('../utils/tags'); | ||
const combineSchema = require('../utils/combineSchema'); | ||
const validateTypes = require('../utils/validateTypes'); | ||
@@ -69,11 +70,17 @@ const REQUIRED = 'required'; | ||
const addDictionaryAdditionalProperties = typedef => { | ||
if (!typedef.type.expression || typedef.type.expression.name !== 'Dictionary') { | ||
return {}; | ||
if ( | ||
typedef.type.expression | ||
&& typedef.type.expression.name === 'Dictionary' | ||
) { | ||
const typeName = typedef.type.applications[0].name; | ||
const isPrimitive = validateTypes(typeName); | ||
return { | ||
additionalProperties: { | ||
...(isPrimitive ? { type: typeName } : { $ref: `#/components/schemas/${typeName}` }), | ||
}, | ||
}; | ||
} | ||
return { | ||
additionalProperties: { | ||
$ref: `#/components/schemas/${typedef.type.applications[0].name}`, | ||
}, | ||
}; | ||
return {}; | ||
}; | ||
@@ -80,0 +87,0 @@ |
@@ -71,4 +71,3 @@ const STATUS_CODES = require('./validStatusCodes'); | ||
const example = getParsedExample({ type, metadata, content }); | ||
return example; | ||
return getParsedExample({ type, metadata, content }); | ||
}; | ||
@@ -75,0 +74,0 @@ |
@@ -23,3 +23,3 @@ const examplesGenerator = require('./examples'); | ||
const hasBodyValues = bodyValues.length > 0; | ||
const requestBody = requestBodyGenerator(bodyValues, requestExamples); | ||
const requestBody = requestBodyGenerator(requestExamples, bodyValues); | ||
return bodyMethods[lowerCaseMethod] && hasBodyValues ? { requestBody } : {}; | ||
@@ -26,0 +26,0 @@ }; |
@@ -57,3 +57,3 @@ const setProperty = require('../utils/setProperty')('parameter'); | ||
const requestBodyGenerator = (params = [], examples) => { | ||
const requestBodyGenerator = (examples, params = []) => { | ||
if (!params || !Array.isArray(params)) return {}; | ||
@@ -60,0 +60,0 @@ |
@@ -8,3 +8,3 @@ const formatSecurity = securitySchemes => { | ||
const { security } = swaggerObject; | ||
const updatedSwaggerObject = { | ||
return { | ||
...swaggerObject, | ||
@@ -17,5 +17,4 @@ ...(security ? { security: formatSecurity(security) } : {}), | ||
}; | ||
return updatedSwaggerObject; | ||
}; | ||
module.exports = parseSecuritySchemas; |
@@ -30,3 +30,3 @@ const { getTagsInfo, formatDescriptionTag } = require('../utils/tags'); | ||
const parseTags = (swaggerObject = {}, data) => { | ||
const parseTags = (swaggerObject, data) => { | ||
if (!data || !Array.isArray(data)) return { tags: [] }; | ||
@@ -33,0 +33,0 @@ const tags = flatArray(data.map(formatTags)); |
const generator = (parseParameter, filterKey) => paramValues => { | ||
if (!paramValues || !Array.isArray(paramValues)) return []; | ||
const params = paramValues.map(parseParameter).filter(param => param[filterKey]); | ||
return params; | ||
return paramValues.map(parseParameter).filter(param => param[filterKey]); | ||
}; | ||
module.exports = generator; |
@@ -10,4 +10,4 @@ const validateTypes = require('./validateTypes'); | ||
value.elements.forEach(el => { | ||
const isPrimitive = validateTypes(el.name); | ||
items.push(isPrimitive ? { type: el.name } : { $ref: `${REF_ROUTE}${el.name}` }); | ||
const isPrimitiveType = validateTypes(el.name); | ||
items.push(isPrimitiveType ? { type: el.name } : { $ref: `${REF_ROUTE}${el.name}` }); | ||
}); | ||
@@ -21,4 +21,4 @@ return { | ||
if (value && value.expression && value.expression.name.toLowerCase() === 'array' && value.type === 'TypeApplication') { | ||
const isPrimitive = validateTypes(value.applications[0].name); | ||
return isPrimitive ? { | ||
const isPrimitiveType = validateTypes(value.applications[0].name); | ||
return isPrimitiveType ? { | ||
type: 'array', | ||
@@ -25,0 +25,0 @@ items: { type: value.applications[0].name }, |
@@ -14,4 +14,3 @@ const errorMessage = require('./errorMessage'); | ||
if (!item || !key || !options) { | ||
const requiredParamsError = new Error('item, key and options para are required'); | ||
throw requiredParamsError; | ||
throw (new Error('item, key and options para are required')); | ||
} | ||
@@ -18,0 +17,0 @@ const value = item[key]; |
377
74216
53
1399