jonaskello / fastify-swagger-ui

Serve Swagger-UI for Fastify

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

@fastify/swagger-ui

NPM version CI js-standard-style

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.

About

Serve Swagger-UI for Fastify

License:MIT License


Languages

Language:JavaScript 88.0%Language:TypeScript 12.0%