-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from inyourtime/chore/docs
chore: add readme
- Loading branch information
Showing
2 changed files
with
73 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,66 @@ | ||
# fastify-openapi-merge | ||
# fastify-openapi-merge | ||
|
||
`fastify-openapi-merge` is a `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-merge | ||
``` | ||
|
||
## Usage | ||
|
||
Register the plugin in your Fastify application and specify the required options: | ||
```javascript | ||
const path = require('node:path') | ||
const fastify = require('fastify')(); | ||
const fastifyOpenapiMerge = require('fastify-openapi-merge'); | ||
|
||
fastify.register(fastifyOpenapiMerge, { | ||
specDir: path.join(__dirname, 'specs'), // Directory or array of directories containing OpenAPI files | ||
routePrefix: '/openapi', // 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 merged specification. Default: `/openapi`. | ||
- `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 merged OpenAPI specification: | ||
- `GET <routePrefix>/json` Returns the merged specification in JSON format. | ||
- `GET <routePrefix>/yaml` Returns the merged specification in YAML format. | ||
|
||
## Example | ||
|
||
If you set routePrefix: `'/docs'`, the plugin will expose the following routes: | ||
- `GET /docs/json` Serves the merged specification in JSON format. | ||
- `GET /docs/yaml` Serves the merged 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-merge/blob/main/LICENSE) file for details. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters