Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rollup-plugin-swagger-jsdoc

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rollup-plugin-swagger-jsdoc

Rollup plugin to generate a swagger.json file from JSDoc comments.

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
6
increased by500%
Maintainers
1
Weekly downloads
 
Created
Source

rollup-plugin-swagger-jsdoc

Build Status

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

// rollup.config.js
import swagger from 'rollup-plugin-swagger-jsdoc';

export default {
    // ...
    plugins: [
        swagger({
            definition: {
                // Specification (optional, defaults to swagger: '2.0')
                openapi: '3.0.0',
                info: {
                    // Title (required)
                    title: 'My API',
                    // Version (required)
                    version: '1.0.0',
                },
            },
            // Path to the API docs
            apis: ['src/routes/**/*.js'],
            // Pretty format output JSON 
            pretty: true,
            // Output swagger.json file
            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.

/**
 * @swagger
 *
 * /login:
 *   post:
 *     description: Login to the application
 *     produces:
 *       - application/json
 *     parameters:
 *       - name: username
 *         description: Username to use for login.
 *         in: formData
 *         required: true
 *         type: string
 *       - name: password
 *         description: User's password.
 *         in: formData
 *         required: true
 *         type: string
 *     responses:
 *       200:
 *         description: login
 */
app.post('/login', (req, res) => {
  // Your implementation comes here ...
});

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: {
    // Path to exposed swagger.json file
    url: '/swagger.json'
  }
}
 
// Expose Swagger UI
app.use('/swagger', swaggerUi.serve, swaggerUi.setup(null, swaggerOptions));
// Expose generated swagger.json during build
app.use(express.static(path.join(__dirname, '../public')));

app.listen(port, function(err) { 
    if (!err) {
        console.log(`Server listening on port: ${port}`);
    }
});

Keywords

FAQs

Package last updated on 29 Oct 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc