Fastify Swagger UI Plugin
Acknowledgements
This project use and override the fastify-swagger plugin implementation for JSON documentation generation. My thanks for the Fastify team for the plugin.
Installation
npm install fastify-swagger-ui --save
Usage
const fastify = require('fastify')()
fastify.register(require('fastify-swagger-ui'), {
swagger: {
info: {
title: 'Test swagger',
description: 'testing the fastify swagger api',
version: '0.1.0'
},
host: 'localhost',
schemes: ['http'],
consumes: ['application/json'],
produces: ['application/json']
}
})
fastify.post('/some-route', {
schema: {
description: 'post some data',
tags: ['user', 'code'],
summary: 'qwerty',
payload: {
type: 'object',
properties: {
hello: { type: 'string' },
obj: {
type: 'object',
properties: {
some: { type: 'string' }
}
}
}
},
out: {
description: 'Succesful response',
code: 201,
type: 'object',
properties: {
hello: { type: 'string' }
}
}
}
}, (req, reply) => {})
fastify.ready(err => {
if (err) throw err
fastify.swagger()
})
For view the documentation need to go to: http://localhost[:port]/docs
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.
By default this plugin exposes a /documentation
route, you can change the path by passing the option { route: String }
or { exposeRoute: false }
if you do not need it.
#####example
{
swagger: {
url: '/docs', // By default use /docs, but you can change ir for other
info: {
title: 'Project Documentation',
description: 'Project Backend Documentation',
version: '1.0.0'
},
host: 'ip:port',
schemes: ['http'],
consumes: ['application/json'],
produces: ['application/json'],
excludes: ['/some-route'] // this param configuration is optional
}
}
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.
Exclude routes
By default the route exclusions defined are: /documentation
/
and /*
. If you want add other routes you can especify in the fastify.swagger
options by passing for example: { excludes: ['/some-route'] }
Version
v1.0.0
Author
Nicolás Balduzzi
License
Licensed under MIT.