Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/ahmadnassri/node-oas-fastify
- Owner: ahmadnassri
- License: mit
- Created: 2020-07-22T03:01:20.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-06-17T20:01:29.000Z (7 months ago)
- Last Synced: 2024-10-01T00:06:03.125Z (4 months ago)
- Topics: fastify, node, oas, openapi
- Language: JavaScript
- Homepage:
- Size: 494 KB
- Stars: 8
- Watchers: 2
- Forks: 4
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
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 conveniencereply.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