
express-jsdoc-swagger
With this library, you can document your express endpoints using swagger OpenAPI 3 Specification without writing YAML or JSON. You can write comments similar to jsdoc
on each endpoint, and the dependecy is going to create the swagger UI.
Table of Contents
Prerequisites
This library assumes you are using:
Installation
npm i express-jsdoc-swagger
Basic Usage
const express = require('express');
const expressJSDocSwagger = require('express-jsdoc-swagger');
const options = {
info: {
version: '1.0.0',
title: 'Albums store',
license: {
name: 'MIT',
},
},
security: {
BasicAuth: {
type: 'http',
scheme: 'basic',
},
},
baseDir: __dirname,
filesPattern: './**/*.js',
swaggerUIPath: '/api-docs',
exposeSwaggerUI: true,
exposeApiDocs: false,
apiDocsPath: '/v3/api-docs',
notRequiredAsNullable: false,
swaggerUiOptions: {},
multiple: true,
};
const app = express();
const PORT = 3000;
expressJSDocSwagger(app)(options);
app.get('/api/v1', (req, res) => res.json({
success: true,
}));
app.listen(PORT, () => console.log(`Example app listening at http://localhost:${PORT}`));
Basic Examples
- Basic configuration options.
const options = {
info: {
version: '1.0.0',
title: 'Albums store',
license: {
name: 'MIT',
},
},
security: {
BasicAuth: {
type: 'http',
scheme: 'basic',
},
},
baseDir: __dirname,
filesPattern: './**/*.js',
};
- Endpoint which returns a
Songs
model array in the response.
app.get('/api/v1/albums', (req, res) => (
res.json([{
title: 'abum 1',
}])
));
- Endpoint PUT with body and path params which returns a
Songs
model array in the response.
app.put('/api/v1/albums/:id', (req, res) => (
res.json([{
title: 'abum 1',
}])
));
- Basic endpoint definition with tags, params and basic authentication
app.get('/api/v1/album', (req, res) => (
res.json({
title: 'abum 1',
})
));
- Basic endpoint definition with code example for response body
app.get('/api/v1/albums', (req, res) => (
res.json([{
title: 'track 1',
}])
));
You can find more examples here, or visit our documentation.
Validator
We developed a new package works as a validator of your API endpoints and the documentation you create with this package. This package is express-oas-validator.
Example
Install using the node package registry:
npm install --save express-oas-validator
We have to wait until we have the full swagger schema to initiate the validator.
const { init } = require('express-oas-validator');
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;
You can check out this also in our example folder.
const express = require('express');
const expressJSDocSwagger = require('express-jsdoc-swagger');
const validator = require('./validator');
const options = {
info: {
version: '1.0.0',
title: 'Albums store',
license: {
name: 'MIT',
},
},
filesPattern: './**.js',
baseDir: __dirname,
};
const app = express();
const instance = expressJSDocSwagger(app)(options);
const serverApp = async () => {
const { validateRequest, validateResponse } = await validator(instance);
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.post('/api/v1/songs', validateRequest(), (req, res) => res.send('You save a song!'));
app.post('/api/v1/name', (req, res, next) => {
try {
validateResponse('Error string', req);
return res.send('Hello World!');
} catch (error) {
return next(error);
}
});
app.get('/api/v1/authors', validateRequest({ headers: false }), (req, res) => (
res.json([{
title: 'album 1',
}])
));
app.use((err, req, res, next) => {
res.status(err.status).json(err);
});
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);
});
You can visit our documentation.
Contributors ✨
This project follows the all-contributors specification. Contributions of any kind welcome!