Fastify routes generator. Used Swagger YAML specification. Supports schemas for describe models and validate response.
Supports Fastify versions >=2.0.0
.
npm i fastify-openapi-generator --save
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",
});
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;
Prefix for swagger documentation. By default, /docs
. Also, you can get JSON (/docs/json
) and YAML (/docs/yaml
) files.
Path for swagger specification file in YAML. By default, swagger.yaml
.
Path for custom html template for Swagger. As start point you can copy swagger.html
from plugin.
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.
Licensed under Apache 2.0.