Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/inyourtime/fastify-openapi-serve
Fastify plugin that serve OpenAPI specifications from multiple files as JSON or YAML
https://github.com/inyourtime/fastify-openapi-serve
fastify fastify-plugin openapi
Last synced: about 1 month ago
JSON representation
Fastify plugin that serve OpenAPI specifications from multiple files as JSON or YAML
- Host: GitHub
- URL: https://github.com/inyourtime/fastify-openapi-serve
- Owner: inyourtime
- License: mit
- Created: 2024-12-07T08:23:31.000Z (about 1 month ago)
- Default Branch: main
- Last Pushed: 2024-12-10T03:01:44.000Z (about 1 month ago)
- Last Synced: 2024-12-10T03:26:16.172Z (about 1 month ago)
- Topics: fastify, fastify-plugin, openapi
- Language: JavaScript
- Homepage:
- Size: 39.1 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fastify-openapi-serve
[![CI](https://github.com/inyourtime/fastify-openapi-serve/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/inyourtime/fastify-openapi-serve/actions/workflows/ci.yml)
[![NPM version](https://img.shields.io/npm/v/fastify-openapi-serve.svg?style=flat)](https://www.npmjs.com/package/fastify-openapi-serve)Fastify plugin that merges OpenAPI specifications from multiple files and serves the merged output as JSON or YAML. This plugin simplifies managing modular OpenAPI specifications, enabling dynamic merging and serving of your API definitions.
## Installation
Install the plugin using npm
```bash
npm install fastify-openapi-serve
```## Usage
Register the plugin in your Fastify application and specify the required options:
```javascript
const path = require('node:path')
const fastify = require('fastify')();
const fastifyOpenapiServe = require('fastify-openapi-serve');fastify.register(fastifyOpenapiServe, {
specDir: path.join(__dirname, 'specs'), // Directory or array of directories containing OpenAPI files
routePrefix: '/docs', // Base route for serving the OpenAPI specification
specDefinition: {
openapi: '3.0.0',
info: {
title: 'My API',
version: '1.0.0',
},
},
});fastify.listen({ port: 3000 }, (err) => {
if (err) throw err;
console.log('Server listening on http://localhost:3000');
});```
## Options
- `specDir` (required): directory path or an array of directory paths containing OpenAPI specification files (`.yaml`, `.yml`, `.json`).
- `routePrefix` (optional): The base route prefix for serving the specification. Default: `/docs`.
- `merge` (optional): A custom function to define how multiple specifications are merged. The function receives an array of parsed specifications.
- `specDefinition` (optional): The base OpenAPI definition that will be included in the merged result.## Routes
The plugin exposes two routes for serving the OpenAPI specification:
- `GET /openapi.json` Returns the specification in JSON format.
- `GET /openapi.yaml` Returns the specification in YAML format.## Example
If you set routePrefix: `'/docs'`, the plugin will expose the following routes:
- `GET /docs/openapi.json` Serves the specification in JSON format.
- `GET /docs/openapi.yaml` Serves the specification in YAML format.## Features
- **Glob Support**: Automatically finds OpenAPI files (`.yaml`, `.yml`, `.json`) in the specified directories.
- **Custom Merging Logic**: Supports user-defined merging strategies.
- **Multiple Formats**: Serves the merged specification in both JSON and YAML formats.## License
This project is licensed under the MIT License. See the [LICENSE](https://github.com/inyourtime/fastify-openapi-serve/blob/main/LICENSE) file for details.