fastify / fastify-swagger

Swagger documentation generator for Fastify
MIT License
910 stars 200 forks source link

Add Redoc UI plugin #710

Closed MeowningMaster closed 1 year ago

MeowningMaster commented 1 year ago

Prerequisites

🚀 Feature Proposal

Separating the spec generation from the UI was a great idea! Are there plans to add a plugin with Redoc UI?

Motivation

It's open source and has basic support for OpenAPI 3.1 which fits very well in fastify/typebox/Ajv stack.

Example

No response

mcollina commented 1 year ago

Why don't you work on one?

MeowningMaster commented 1 year ago

It's difficult for me to find free time now. But maybe later I do

Fdawgs commented 1 year ago

FYI you don't need a plugin for this.

  1. Register the @fastify/swagger plugin
  2. Have a route that sends the swagger function; example
  3. Point to the route in the Redoc config; example
weiwuprojects commented 1 year ago

Here's my code, adapted from @Fdawgs comment

docs/index.ts


// const { docsGetSchema } = require('./schema');
import { getDocsSchema } from './schemas';

export const DocsController = (context, _opts, done) => {
  context.register(require('@fastify/static'), {
    root: __dirname,
    serve: false,
  });

  context.get('/', getDocsSchema, (req, res) => {
    res.header('cache-control', 'public, max-age=300');
    res.removeHeader('pragma');
    res.removeHeader('expires');
    res.removeHeader('surrogate-control');
    res.type('text/html; charset=utf-8');
    res.sendFile('index.html');
  });

  done();
};

- Create a file in /docs/index.html and copy the html code from https://redocly.com/docs/redoc/quickstart/
- Change spec-url to to /docs/json