Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/wirgen/fastify-openapi-generator

Generate API with fastify by OpenAPI specs
https://github.com/wirgen/fastify-openapi-generator

fastify generator openapi swagger

Last synced: 2 months ago
JSON representation

Generate API with fastify by OpenAPI specs

Awesome Lists containing this project

README

        

# fastify-openapi-generator

Fastify routes generator. Used [Swagger](https://swagger.io/) 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
```


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

```JS
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 `.`. For example, you can collect all controllers from folder used `fs`:
```js
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](https://swagger.io/). 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](./LICENSE).