Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
inyourtime committed Dec 10, 2024
1 parent 0be151d commit bd1015e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 14 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,21 @@ fastify.listen({ port: 3000 }, (err) => {
## 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`.
- `routePrefix` (optional): The base route prefix for serving the 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.
The plugin exposes two routes for serving the OpenAPI specification:
- `GET <routePrefix>/json` Returns the specification in JSON format.
- `GET <routePrefix>/yaml` Returns the 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.
- `GET /docs/json` Serves the specification in JSON format.
- `GET /docs/yaml` Serves the specification in YAML format.

## Features

Expand All @@ -63,4 +63,4 @@ If you set routePrefix: `'/docs'`, the plugin will expose the following routes:

## 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 project is licensed under the MIT License. See the [LICENSE](https://github.com/inyourtime/fastify-openapi-serve/blob/main/LICENSE) file for details.
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ async function fastifyOpenapiServe (fastify, opts) {

checkSpecDir(fastify, specDir)

if (opts.merge && typeof opts.merge !== 'function') {
throw new Error('"merge" option must be a function')
}

const rootsSpec = Array.isArray(specDir) ? specDir : [specDir]
const specFiles = []

Expand All @@ -32,7 +36,7 @@ async function fastifyOpenapiServe (fastify, opts) {
url: `${openapiPath}/json`,
handler: async () => {
const mergedSpec = await mergeSpec(specFiles, {
customMerge: opts.merge,
merge: opts.merge,
specDefinition: opts.specDefinition,
})

Expand All @@ -45,7 +49,7 @@ async function fastifyOpenapiServe (fastify, opts) {
url: `${openapiPath}/yaml`,
handler: async (_, reply) => {
const mergedSpec = await mergeSpec(specFiles, {
customMerge: opts.merge,
merge: opts.merge,
specDefinition: opts.specDefinition,
yaml: true
})
Expand Down
6 changes: 3 additions & 3 deletions lib/merge-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ async function mergeSpec (specPaths, opts = {}) {
return
}

const _merge = opts.customMerge ?? defaultMerge
const _merge = opts.merge ?? defaultMerge
if (typeof _merge !== 'function') {
throw new Error('"customMerge" must be a function')
throw new Error('"merge" option must be a function')
}

opts.specDefinition = opts.specDefinition || defaultSpecDefinition
Expand All @@ -42,7 +42,7 @@ async function mergeSpec (specPaths, opts = {}) {
const content = fs.readFileSync(specPath, 'utf-8')
const parse = YAML.parse(content) ?? {}

if (!opts.customMerge) {
if (!opts.merge) {
specs.push({ oas: parse })
} else {
specs.push(parse)
Expand Down
15 changes: 15 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,18 @@ test('specDefinition custom', async (t) => {
'/bar': { get: { summary: 'Bar', responses: { 200: { description: 'OK' } } } },
})
})

test('custom merge not a function', async (t) => {
t.plan(2)
const fastify = Fastify()

try {
await fastify.register(fastifyOpenapiServe, {
specDir: path.join(__dirname, 'specs'),
merge: 1
})
} catch (e) {
t.assert.ok(e)
t.assert.strictEqual(e.message, '"merge" option must be a function')
}
})
4 changes: 2 additions & 2 deletions test/merge-spec.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ test('custom merge not a function', async (t) => {
await mergeSpec([
path.join(__dirname, 'openapi/spec.json'),
path.join(__dirname, 'openapi/spec2.json'),
], { customMerge: 1 })
], { merge: 1 })
} catch (e) {
t.assert.ok(e)
t.assert.strictEqual(e.message, '"customMerge" must be a function')
t.assert.strictEqual(e.message, '"merge" option must be a function')
}
})

Expand Down

0 comments on commit bd1015e

Please sign in to comment.