Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/ahmadnassri/node-oas-fastify

OAS 3.0 to Fastify routes automation
https://github.com/ahmadnassri/node-oas-fastify

fastify node oas openapi

Last synced: 3 months ago
JSON representation

OAS 3.0 to Fastify routes automation

Awesome Lists containing this project

README

        

# OAS to Fastify Plugin

OAS 3.x to Fastify routes automation

[![license][license-img]][license-url]
[![release][release-img]][release-url]
[![super linter][super-linter-img]][super-linter-url]
[![test][test-img]][test-url]
[![semantic][semantic-img]][semantic-url]

## Usage

``` js
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`][]

``` js
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`][]

``` bash
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

``` js
const spec = {
"paths": {
"/pets": {
"get": {
"operationId": "listPets",
...
}
}
}
}

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

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

[`js-yaml`]: https://www.npmjs.com/package/js-yaml
[`apidevtools/swagger-cli`]: https://www.npmjs.com/package/@apidevtools/swagger-cli
[OpenAPI Specification]: https://github.com/OAI/OpenAPI-Specification/

----
> Author: [Ahmad Nassri](https://www.ahmadnassri.com/) •
> Twitter: [@AhmadNassri](https://twitter.com/AhmadNassri)

[license-url]: LICENSE
[license-img]: https://badgen.net/github/license/ahmadnassri/node-oas-fastify

[release-url]: https://github.com/ahmadnassri/node-oas-fastify/releases
[release-img]: https://badgen.net/github/release/ahmadnassri/node-oas-fastify

[super-linter-url]: https://github.com/ahmadnassri/node-oas-fastify/actions?query=workflow%3Asuper-linter
[super-linter-img]: https://github.com/ahmadnassri/node-oas-fastify/workflows/super-linter/badge.svg

[test-url]: https://github.com/ahmadnassri/node-oas-fastify/actions?query=workflow%3Atest
[test-img]: https://github.com/ahmadnassri/node-oas-fastify/workflows/test/badge.svg

[semantic-url]: https://github.com/ahmadnassri/node-oas-fastify/actions?query=workflow%3Arelease
[semantic-img]: https://badgen.net/badge/📦/semantically%20released/blue