项目作者: wirgen

项目描述 :
Generate API with fastify by OpenAPI specs
高级语言: JavaScript
项目地址: git://github.com/wirgen/fastify-openapi-generator.git
创建时间: 2019-08-31T01:06:48Z
项目社区:https://github.com/wirgen/fastify-openapi-generator

开源协议:Apache License 2.0

下载


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

```shell script
npm i fastify-openapi-generator —save

  1. <a name="usage"></a>
  2. ## Usage
  3. Add it to your project with `register` and pass it some basic options, then call the `swagger` api and you are done!
  4. ```JS
  5. const fastify = require("fastify")();
  6. fastify.register(require("fastify-openapi-generator"), {
  7. controllers: require("./controllers"),
  8. routeDocs: "/docs/",
  9. yaml: "swagger.yaml",
  10. template: "swagger.html",
  11. });

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:

  1. const fs = require("fs");
  2. const path = require("path");
  3. const basename = path.basename(__filename);
  4. let controllers = {};
  5. fs
  6. .readdirSync(__dirname)
  7. .filter(file => {
  8. return (file.indexOf(".") !== 0) && file !== basename && (file.slice(-3) === ".js");
  9. })
  10. .forEach(file => {
  11. controllers[file.slice(0, -3)] = require(path.join(__dirname, file));
  12. });
  13. 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.