Package Exports
- @fastify/swagger-ui
- @fastify/swagger-ui/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@fastify/swagger-ui) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@fastify/swagger-ui
A Fastify plugin for serving Swagger UI.
Supports Fastify versions 4.x
.
Install
npm i @fastify/swagger-ui
Usage
Add it with @fastify/swagger
to your project with register
, pass it some options, call the swagger
API, and you are done!
const fastify = require('fastify')()
await fastify.register(require('@fastify/swagger'))
await fastify.register(require('@fastify/swagger-ui'), {
routePrefix: '/documentation',
uiConfig: {
docExpansion: 'full',
deepLinking: false
},
uiHooks: {
onRequest: function (request, reply, next) { next() },
preHandler: function (request, reply, next) { next() }
},
staticCSP: true,
transformStaticCSP: (header) => 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' }
}
},
default: {
description: 'Default response',
type: 'object',
properties: {
foo: { type: 'string' }
}
}
},
security: [
{
"apiKey": []
}
]
}
}, (req, reply) => {})
await fastify.ready()
API
Register options
Options
Option | Default | Description |
---|---|---|
baseDir | undefined | Specify the directory where all spec files that are included in the main one using $ref will be located. By default, this is the directory where the main spec file is located. Provided value should be an absolute path without trailing slash. |
initOAuth | {} | Configuration options for Swagger UI initOAuth. |
routePrefix | '/documentation' | Overwrite the default Swagger UI route prefix. |
staticCSP | false | Enable CSP header for static resources. |
transformStaticCSP | undefined | Synchronous function to transform CSP header for static resources if the header has been previously set. |
uiConfig | {} | Configuration options for Swagger UI. Must be literal values, see #5710. |
uiHooks | {} | Additional hooks for the documentation's routes. You can provide the onRequest and preHandler hooks with the same route's options interface. |
logLevel | info | Allow to define route log level. |
The plugin will expose the documentation with the following APIs:
URL | Description |
---|---|
'/documentation/json' |
The JSON object representing the API |
'/documentation/yaml' |
The YAML object representing the API |
'/documentation/' |
The swagger UI |
'/documentation/*' |
External files that you may use in $ref |
License
Licensed under MIT.