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

@fastify/swagger

Package Overview
Dependencies
Maintainers
20
Versions
43
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fastify/swagger

Serve Swagger/OpenAPI documentation for Fastify, supporting dynamic generation

  • 9.4.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
20
Created

What is @fastify/swagger?

@fastify/swagger is a Fastify plugin that integrates Swagger/OpenAPI documentation generation and UI into Fastify applications. It allows developers to automatically generate API documentation from their Fastify routes and provides a user-friendly interface to explore and test the API endpoints.

What are @fastify/swagger's main functionalities?

Generate OpenAPI Documentation

This feature allows you to generate OpenAPI documentation for your Fastify routes. The code sample demonstrates how to set up the @fastify/swagger plugin and define a route with a schema that will be included in the generated documentation.

const fastify = require('fastify')();
const swagger = require('@fastify/swagger');

fastify.register(swagger, {
  routePrefix: '/documentation',
  swagger: {
    info: {
      title: 'Test API',
      description: 'Testing the Fastify swagger API',
      version: '0.1.0'
    },
    host: 'localhost:3000',
    schemes: ['http'],
    consumes: ['application/json'],
    produces: ['application/json']
  },
  exposeRoute: true
});

fastify.get('/example', {
  schema: {
    description: 'Example endpoint',
    tags: ['example'],
    summary: 'An example endpoint',
    response: {
      200: {
        description: 'Successful response',
        type: 'object',
        properties: {
          hello: { type: 'string' }
        }
      }
    }
  }
}, async (request, reply) => {
  return { hello: 'world' };
});

fastify.listen(3000, err => {
  if (err) throw err;
  console.log('Server listening on http://localhost:3000');
});

Serve Swagger UI

This feature allows you to serve a Swagger UI for your API documentation. The code sample demonstrates how to set up the @fastify/swagger and @fastify/swagger-ui plugins to serve the Swagger UI at the '/docs' route.

const fastify = require('fastify')();
const swagger = require('@fastify/swagger');
const swaggerUi = require('@fastify/swagger-ui');

fastify.register(swagger, {
  swagger: {
    info: {
      title: 'Test API',
      description: 'Testing the Fastify swagger API',
      version: '0.1.0'
    }
  }
});

fastify.register(swaggerUi, {
  routePrefix: '/docs',
  swagger: {
    info: {
      title: 'Test API',
      description: 'Testing the Fastify swagger API',
      version: '0.1.0'
    }
  },
  exposeRoute: true
});

fastify.get('/example', async (request, reply) => {
  return { hello: 'world' };
});

fastify.listen(3000, err => {
  if (err) throw err;
  console.log('Server listening on http://localhost:3000');
});

Other packages similar to @fastify/swagger

Keywords

FAQs

Package last updated on 22 Nov 2024

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