0.3.1 • Published 2 years ago

fastify-openapi-generator v0.3.1

Weekly downloads
2
License
Apache-2.0
Repository
github
Last release
2 years ago

fastify-openapi-generator

Fastify routes generator. Used Swagger YAML specification. Supports schemas for describe models and validate response.

Supports Fastify versions >=2.0.0.

Install

npm i fastify-openapi-generator --save

Usage

Add it to your project with register and pass it some basic options, then call the swagger api and you are done!

const fastify = require("fastify")();

fastify.register(require("fastify-openapi-generator"), {
  controllers: require("./controllers"),
  routeDocs: "/docs/",
  yaml: "swagger.yaml",
  template: "swagger.html",
});

API

register options

controllers

Array of handlers for fastify routes. Describe handlers in operationId with pattern <controller>.<function>. For example, you can collect all controllers from folder used fs:

const fs = require("fs");
const path = require("path");
const basename = path.basename(__filename);

let controllers = {};
fs
  .readdirSync(__dirname)
  .filter(file => {
    return (file.indexOf(".") !== 0) && file !== basename && (file.slice(-3) === ".js");
  })
  .forEach(file => {
    controllers[file.slice(0, -3)] = require(path.join(__dirname, file));
  });

module.exports = controllers;

routeDocs

Prefix for swagger documentation. By default, /docs. Also, you can get JSON (/docs/json) and YAML (/docs/yaml) files.

yaml

Path for swagger specification file in YAML. By default, swagger.yaml.

template

Path for custom html template for Swagger. As start point you can copy swagger.html from plugin.

Security

Global security definitions and route level security provide documentation only. It does not implement authentication nor route security for you. Once your authentication is implemented, along with your defined security, users will be able to successfully authenticate and interact with your API using the user interfaces of the documentation.

License

Licensed under Apache 2.0.