Skip to content

ahmadnassri/node-oas-fastify

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
lib
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

OAS to Fastify Plugin

OAS 3.x to Fastify routes automation

license release super linter test semantic

Usage

const fastify = require('fastify')()
const spec = require('./petstore.json')

// your handler object properties map to the OAS "operationId"
const handler = {
  listPets: () => { ... }
  createPets: () => { ... }
  showPetById: () => { ... }
}

fastify.register(require('oas-fastify'), { spec, handler }) 

Yaml Support?

This package does not support OAS Yaml format, but you can easily convert to JSON before calling `oas-fastify`
using js-yaml
const yaml = require('js-yaml')
const fs   = require('fs')

const spec = yaml.safeLoad(fs.readFileSync('openapi.yml', 'utf8'))


fastify.register(require('oas-fastify'), { spec, handler }) 
using apidevtools/swagger-cli
npx apidevtools/swagger-cli bundle spec/openapi.yml --outfile spec.json

Options

The plugin accepts an options object with the following properties:

  • spec: a valid OpenAPI Specification JSON object
  • handler: an object with properties that map to the spec's operationId names, with the values as functions that will handle the request
Example
const spec = {
  "paths": {
    "/pets": {
      "get": {
        "operationId": "listPets",
        ...
      }
    }
  }
}

const handler = {
  listPets: function (request, reply, fastify) {
    // fastify instance passed in for convenience

    reply.send({ hello: 'world' })
  }
}

Author: Ahmad Nassri • Twitter: @AhmadNassri