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

fastify-swagger

Package Overview
Dependencies
Maintainers
7
Versions
100
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastify-swagger

Generate Swagger files automatically for Fastify.

  • 0.14.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
77K
increased by12.91%
Maintainers
7
Weekly downloads
 
Created
Source

fastify-swagger

Greenkeeper badge js-standard-style Build Status

Swagger documentation generator for Fastify. It uses the schemas you declare in your routes to generate a swagger compliant doc.

Install

npm i fastify-swagger --save

Usage

Add it to your project with register and pass it some basic options, then call the swagger api and you are done!

const fastify = require('fastify')()

fastify.register(require('fastify-swagger'), {
  routePrefix: '/documentation',
  swagger: {
    info: {
      title: 'Test swagger',
      description: 'testing the fastify swagger api',
      version: '0.1.0'
    },
    externalDocs: {
      url: 'https://swagger.io',
      description: 'Find more info here'
    },
    host: 'localhost',
    schemes: ['http'],
    consumes: ['application/json'],
    produces: ['application/json'],
    tags: [
      { name: 'user', description: 'User related end-points' },
      { name: 'code', description: 'Code related end-points' }
    ],
    securityDefinitions: {
      apiKey: {
        type: 'apiKey',
        name: 'apiKey',
        in: 'header'
      }
    }
  }
})

fastify.put('/some-route/:id', {
  schema: {
    description: 'post some data',
    tags: ['user', 'code'],
    summary: 'qwerty',
    params: {
      type: 'object',
      properties: {
        id: {
          type: 'string',
          description: 'user id'
        }
      }
    },
    body: {
      type: 'object',
      properties: {
        hello: { type: 'string' },
        obj: {
          type: 'object',
          properties: {
            some: { type: 'string' }
          }
        }
      }
    },
    response: {
      201: {
        description: 'Successful response',
        type: 'object',
        properties: {
          hello: { type: 'string' }
        }
      }
    },
    security: [
      {
        "api_key": []
      }
    ]
  }
}, (req, reply) => {})

fastify.ready(err => {
  if (err) throw err
  fastify.swagger()
})

API

register options

modes

fastify-swagger supports two registration modes dynamic and static:

dynamic

dynamic mode is the default one, if you use the plugin this way - swagger specification would be gathered from your routes definitions.

{
  swagger: {
    info: {
      title: String,
      description: String,
      version: String
    },
    externalDocs: Object,
    host: String,
    schemes: [ String ],
    consumes: [ String ],
    produces: [ String ],
    tags: [ Object ],
    securityDefinitions: Object
  }
}

All the above parameters are optional. You can use all the properties of the swagger specification, if you find anything missing, please open an issue or a pr!

Example of the fastify-swagger usage in the dynamic mode is available here.

static

static mode should be configured explicitly. In this mode fastify-swagger serves given specification, you should craft it yourselfe.

{
  mode: 'static',
  specification: {
    path: './examples/example-static-specification.yaml',
    postProcessor: function(swaggerObject) {
      return swaggerObject
    }
  }
}

Example of the fastify-swagger usage in the static mode is available here.

specification.postProcessor parameter is optional. It allows you to change your swagger object on the fly (for example - based on the environment). It accepts swaggerObject - basically a javascript object which was parsed from your yaml or json file and should return a swagger object.

additional

If you pass { exposeRoute: true } during the registration the plugin will expose the documentation with the following apis:

urldescription
'/documentation/json'the json object representing the api
'/documentation/yaml'the yaml object representing the api
'/documentation/'the swagger ui
Overwrite swagger url end-point

If you would like to overwrite the /documentation url you can use the routePrefix option.

fastify.register(require('fastify-swagger'), {
  swagger: {
    info: {
      title: 'Test swagger',
      description: 'testing the fastify swagger api',
      version: '0.1.0'
    },
    ...
  },
  exposeRoute: true,
  routePrefix: '/documentations'
}

swagger options

Calling fastify.swagger will return to you a JSON object representing your api, if you pass { yaml: true } to fastify.swagger, it will return you a yaml string.

Hide a route

Sometimes you may need to hide a certain route from the documentation, just pass { hide: true } to the schema object inside the route declaration.

Security

Global security definitions and route level security provide documentation only. It does not implement authentication nor route security for you. Once your authentication is implemented, along with your defined security, users will be able to successfully authenticate and interact with your API using the user interfaces of the documentation.

Development

In order to start development run:

npm i
npm run prepare

So that swagger-ui static folder will be generated for you.

See also

Sometimes you already have a Swagger definition and you need to build Fastify routes from that. In that case checkout fastify-swaggergen which helps you in doing just that.

Acknowledgements

This project is kindly sponsored by:

License

Licensed under MIT.

Keywords

FAQs

Package last updated on 03 Aug 2018

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