express-jsdoc-swagger
Advanced tools
Comparing version 1.5.0 to 1.6.0
@@ -58,2 +58,3 @@ // Type definitions for express-jsdoc-swagger | ||
swaggerUiOptions?: SwaggerUiOptions; | ||
notRequiredAsNullable?: boolean; | ||
} | ||
@@ -60,0 +61,0 @@ |
31
index.js
const swaggerUi = require('swagger-ui-express'); | ||
const merge = require('merge'); | ||
const defaultOptions = require('./config/default'); | ||
const processSwagger = require('./processSwagger'); | ||
const swaggerEvents = require('./swaggerEvents'); | ||
const DEFAULT_SWAGGERUI_URL = '/api-docs'; | ||
const DEFAULT_EXPOSE_SWAGGERUI = true; | ||
const DEFAULT_APIDOCS_URL = '/v3/api-docs'; | ||
const DEFAULT_EXPOSE_APIDOCS = false; | ||
const expressJSDocSwagger = app => (options = {}, userSwagger = {}) => { | ||
const expressJSDocSwagger = app => (userOptions = {}, userSwagger = {}) => { | ||
const events = swaggerEvents(); | ||
@@ -16,2 +13,7 @@ const { instance } = events; | ||
const options = { | ||
...defaultOptions, | ||
...userOptions, | ||
}; | ||
processSwagger(options, events.processFile) | ||
@@ -21,11 +23,16 @@ .then(result => { | ||
...swaggerObject, | ||
...result, | ||
...result.swaggerObject, | ||
}; | ||
swaggerObject = merge.recursive(true, swaggerObject, userSwagger); | ||
events.finish(swaggerObject); | ||
events.finish(swaggerObject, { | ||
jsdocInfo: result.jsdocInfo, | ||
getPaths: result.getPaths, | ||
getComponents: result.getComponents, | ||
getTags: result.getTags, | ||
}); | ||
}) | ||
.catch(events.error); | ||
if (options.exposeSwaggerUI || DEFAULT_EXPOSE_SWAGGERUI) { | ||
app.use(options.swaggerUIPath || DEFAULT_SWAGGERUI_URL, (req, res, next) => { | ||
if (options.exposeSwaggerUI) { | ||
app.use(options.swaggerUIPath, (req, res, next) => { | ||
swaggerObject = { | ||
@@ -40,4 +47,4 @@ ...swaggerObject, | ||
if (options.exposeApiDocs || DEFAULT_EXPOSE_APIDOCS) { | ||
app.get(options.apiDocsPath || DEFAULT_APIDOCS_URL, (req, res) => { | ||
if (options.exposeApiDocs) { | ||
app.get(options.apiDocsPath, (req, res) => { | ||
res.json(swaggerObject); | ||
@@ -44,0 +51,0 @@ }); |
{ | ||
"name": "express-jsdoc-swagger", | ||
"version": "1.5.0", | ||
"version": "1.6.0", | ||
"description": "Swagger OpenAPI 3.x generator", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -35,7 +35,9 @@ const readFiles = require('./consumers/readFiles'); | ||
logger({ entity: 'paths', swaggerObject }); | ||
swaggerObject = getComponents(swaggerObject, data); | ||
swaggerObject = getComponents(swaggerObject, data, options); | ||
logger({ entity: 'components', swaggerObject }); | ||
swaggerObject = getTags(swaggerObject, data); | ||
logger({ entity: 'tags', swaggerObject }); | ||
return swaggerObject; | ||
return { | ||
swaggerObject, jsdocInfo, getPaths, getComponents, getTags, | ||
}; | ||
}); | ||
@@ -42,0 +44,0 @@ }; |
@@ -42,8 +42,19 @@ ![npm](https://img.shields.io/npm/v/express-jsdoc-swagger) | ||
}, | ||
filesPattern: './**/*.js', // Glob pattern to find your jsdoc files (it supports arrays too ['./**/*.controller.js', './**/*.route.js']) | ||
swaggerUIPath: '/your-url', // SwaggerUI will be render in this url. Default: '/api-docs' | ||
baseDir: __dirname, | ||
exposeSwaggerUI: true, // Expose OpenAPI UI. Default true | ||
exposeApiDocs: false, // Expose Open API JSON Docs documentation in `apiDocsPath` path. Default false. | ||
apiDocsPath: '/v3/api-docs', // Open API JSON Docs endpoint. Default value '/v3/api-docs'. | ||
// Glob pattern to find your jsdoc files (multiple patterns can be added in an array) | ||
filesPattern: './**/*.js', | ||
// URL where SwaggerUI will be rendered | ||
swaggerUIPath: '/api-docs', | ||
// Expose OpenAPI UI | ||
exposeSwaggerUI: true, | ||
// Expose Open API JSON Docs documentation in `apiDocsPath` path. | ||
exposeApiDocs: false, | ||
// Open API JSON Docs endpoint. | ||
apiDocsPath: '/v3/api-docs', | ||
// Set non-required fields as nullable by default | ||
notRequiredAsNullable: false, | ||
// You can customize your UI options. | ||
// you can extend swagger-ui-express config. You can checkout an example of this | ||
// in the `example/configuration/swaggerOptions.js` | ||
swaggerUiOptions: {}, | ||
}; | ||
@@ -66,3 +77,2 @@ | ||
app.listen(PORT, () => console.log(`Example app listening at http://localhost:${PORT}`)); | ||
``` | ||
@@ -87,4 +97,5 @@ | ||
}, | ||
filesPattern: './**/*.js', // Glob pattern to find your jsdoc files | ||
baseDir: __dirname, | ||
// Glob pattern to find your jsdoc files (multiple patterns can be added in an array) | ||
filesPattern: './**/*.js', | ||
}; | ||
@@ -91,0 +102,0 @@ ``` |
@@ -17,4 +17,4 @@ const { EventEmitter } = require('events'); | ||
const finish = eventEmitter => info => ( | ||
eventEmitter.emit(FINISH_EVENT_NAME, info) | ||
const finish = eventEmitter => (info, methods) => ( | ||
eventEmitter.emit(FINISH_EVENT_NAME, info, methods) | ||
); | ||
@@ -21,0 +21,0 @@ |
@@ -30,6 +30,7 @@ const { getTagInfo, getTagsInfo } = require('../utils/tags'); | ||
const formatProperties = properties => { | ||
const formatProperties = (properties, options = {}) => { | ||
if (!properties || !Array.isArray(properties)) return {}; | ||
return properties.reduce((acum, property) => { | ||
const name = getPropertyName(property); | ||
const isRequired = property.name.includes(REQUIRED); | ||
const { | ||
@@ -50,2 +51,7 @@ name: typeName, applications, expression, elements, | ||
...addEnumValues(enumValues), | ||
// Add nullable to non-required fields if option to do that is enabled | ||
...(options.notRequiredAsNullable && !isRequired ? { | ||
nullable: true, | ||
} : {}), | ||
}, | ||
@@ -62,3 +68,3 @@ }; | ||
const parseSchema = schema => { | ||
const parseSchema = (schema, options = {}) => { | ||
const typedef = getTagInfo(schema.tags, 'typedef'); | ||
@@ -79,3 +85,3 @@ const propertyValues = getTagsInfo(schema.tags, 'property'); | ||
type: 'object', | ||
properties: formatProperties(propertyValues), | ||
properties: formatProperties(propertyValues, options), | ||
}, | ||
@@ -85,6 +91,6 @@ }; | ||
const parseComponents = (swaggerObject = {}, components = []) => { | ||
const parseComponents = (swaggerObject = {}, components = [], options = {}) => { | ||
if (!components || !Array.isArray(components)) return { components: { schemas: {} } }; | ||
const componentSchema = components.reduce((acum, item) => ({ | ||
...acum, ...parseSchema(item), | ||
...acum, ...parseSchema(item, options), | ||
}), {}); | ||
@@ -91,0 +97,0 @@ return { |
@@ -1,2 +0,1 @@ | ||
const errorMessage = require('./errorMessage'); | ||
const { refSchema } = require('./refSchema'); | ||
@@ -8,17 +7,16 @@ | ||
* This method checks the first item of the data type list to validate if | ||
* it contains any of the keywords representing the different schema types | ||
* it contains any of the keywords representing the different union types | ||
* in Swagger: 'oneOf', 'anyOf' or 'allOf'. | ||
* | ||
* @param {object[]} elements - List of data types for the property | ||
* @returns {string|null} Name of the schema type (if any) | ||
* @returns {string|null} Name of the union type (if any) | ||
*/ | ||
const getSchemaType = elements => { | ||
const schemaType = elements[0].name; | ||
const getUnionType = elements => { | ||
const unionType = elements[0].name; | ||
if (!VALID_TYPES.includes(schemaType)) { | ||
errorMessage(`SchemaType ${schemaType} invalid, it should be one of these ${VALID_TYPES.join(', ')}`); | ||
if (!VALID_TYPES.includes(unionType)) { | ||
return null; | ||
} | ||
return schemaType; | ||
return unionType; | ||
}; | ||
@@ -49,11 +47,11 @@ | ||
const schemaType = getSchemaType(elements); | ||
const types = !schemaType ? elements : elements.slice(1); | ||
const unionType = getUnionType(elements); | ||
const types = !unionType ? elements : elements.slice(1); | ||
// If there are multiple types in the list, wrap them into a schema type | ||
// If there are multiple types in the list, wrap them into a union type | ||
// ('oneOf' will be used by default if none is specified) | ||
if (types.length > 1 || schemaType === 'allOf') { | ||
if (types.length > 1 || unionType === 'allOf') { | ||
schema = { | ||
...schema, | ||
[schemaType || 'oneOf']: types.map(type => refSchema(type)), | ||
[unionType || 'oneOf']: types.map(type => refSchema(type)), | ||
}; | ||
@@ -60,0 +58,0 @@ } else { |
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
67155
57
1294
200