A plugin for Fastify to autogenerate a configuration based on a OpenApi(v2/v3) specification.
Switch branches/tags
Nothing to show
Clone or download
Latest commit 4ef7b56 Dec 3, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
bin
examples updated generatedProject dependencies Nov 30, 2018
lib removed warning Nov 30, 2018
test
.gitattributes Initial commit Jun 30, 2018
.gitignore Initial codedrop after rename Jun 30, 2018
.travis.yml Updated travis.yml Aug 14, 2018
LICENSE.txt
README.md Fixed typo in README Aug 5, 2018
greenkeeper.json cleanup of greenkeeper.json Jun 30, 2018
index.js
package-lock.json
package.json chore(package): update fastify-cli to version 0.26.2 Dec 3, 2018

README.md

Fastify OpenApi Glue

Build Status Greenkeeper badge

A plugin for fastify to autogenerate a configuration based on a OpenApi(v2/v3) specification.

Install:

npm i fastify-openapi-glue --save

Plugin

Usage

Add the plugin to your project with register and pass it some basic options and you are done !

const openapiGlue = require("fastify-openapi-glue");

const options = {
  specification: `${__dirname}/petstore-swagger.v2.json`,
  service: `${__dirname}/service.js`
};


fastify.register(openapiGlue, options);

All schema and routes will be taken from the OpenApi specification listed in the options. No need to specify them in your code.

Options

  • specification: this can be a JSON object, or the name of a JSON or YAML file containing a valid OpenApi(v2/v3) file
  • service: this can be a javascript object or class, or the name of a javascript file containing such an object. If the import of the file results in a function instead of an object then the function will be executed during import.

Both options are mandatory.

See the examples section for a demo.

Generator

To make life even more easy there is the openapi-glue cli. The openapi-glue cli takes a valid OpenApi (v2/v3) file (JSON or YAML) and generates a project including a fastify plugin that you can use on any fastify server, a stub of the service class and a skeleton of a test harness to test the plugin.

Usage

  openapi-glue [options] <OpenApi specification>

or if you don't have openapi-glue installed:

  npx github:seriousme/fastify-openapi-glue <OpenApi specification>

This will generate a project based on the provided OpenApi specification. Any existing files in the project folder will be overwritten! See the generator examples section for a demo.

Options:


  -p <name>                   The name of the project to generate
  --projectName=<name>        [default: generatedProject]

  -b <dir> --baseDir=<dir>    Directory to generate the project in.
                              This directory must already exist.
                              [default: "."]

The following options are only usefull for testing the openapi-glue plugin:
  -c --checksumOnly           Don't generate the project on disk but
                              return checksums only.
  -l --localPlugin            Use a local path to the plugin.

See the generator example section for a demo.

Examples

Clone this repository and run npm i

Plugin

Executing npm start will start fastify on localhost port 3000 with the routes extracted from the petstore example and the accompanying service definition

{
  "statusCode": 400,
  "error": "Bad Request",
  "message": "params.petId should be integer"
}
{
  "statusCode": 500,
  "error": "Internal Server Error",
  "message": "Operation findPetsByStatus not implemented"
}
{
  "statusCode": 500,
  "error": "Internal Server Error",
  "message": "name is required!"
}

as the pet returned by service.js does not match the response schema.

Generator

The folder examples/generatedProject contains the result of running openapi-glue -l --baseDir=examples examples/petstore/petstore-swagger.v2.yaml. The generated code can be started using npm start in examples/generatedProject (you will need to run npm i in the generated folder first)

Fastify-swaggergen

Fastify-openapi-glue is the successor to the now deprecated fastify-swaggergen project. Main difference is that it:

  • aims to support OpenApi and not just Swagger V2 (hence the name change)
  • does not include fastify-swagger support anymore. If you need to show the swagger UI you can include it yourself. Removing the swagger UI clears up a number of dependencies.

License

Licensed under MIT