rollup-plugin-swagger-jsdoc
![Build Status](https://travis-ci.org/zoltraks/rollup-plugin-swagger-jsdoc.svg?branch=main)
Rollup plugin to generate a swagger.json file from JSDoc
comments using swagger-jsdoc library.
Install
npm install --save-dev rollup-plugin-swagger-jsdoc
Usage
import swagger from 'rollup-plugin-swagger-jsdoc';
export default {
plugins: [
swagger({
definition: {
openapi: '3.0.0',
info: {
title: 'My API',
version: '1.0.0',
},
},
apis: ['src/routes/**/*.js'],
pretty: true,
output: 'public/swagger.json'
})
]
}
How to document your API
For details, see swagger-jsdoc documentation and OpenAPI specification.
In a nutshell your API is documented using JSDoc
comments with @swagger
section in YAML
format.
The following example was taken from swagger-jsdoc library.
app.post('/login', (req, res) => {
});
Example application
Here is quick example of simple express application using swagger-ui-express library to provide Swagger UI.
You need to install express and swagger-ui-express via npm. Swagger UI will be served at /swagger location in this example.
const express = require('express');
const app = express();
const swaggerUi = require('swagger-ui-express');
const path = require('path');
const port = process.env.PORT || 8080;
const swaggerOptions = {
swaggerOptions: {
url: '/swagger.json'
}
}
app.use('/swagger', swaggerUi.serve, swaggerUi.setup(null, swaggerOptions));
app.use(express.static(path.join(__dirname, '../public')));
app.listen(port, function(err) {
if (!err) {
console.log(`Server listening on port: ${port}`);
}
});