Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: Convert usage example to OpenAPI v3 spec #792

Merged
merged 2 commits into from
Mar 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 24 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ A Fastify plugin for serving [Swagger (OpenAPI v2)](https://swagger.io/specifica

If you are looking for a plugin to generate routes from an existing OpenAPI schema, check out [fastify-openapi-glue](https://github.com/seriousme/fastify-openapi-glue).

Following plugins serve swagger/openapi front-ends based on the swagger definitions generated by this plugin:
Following plugins serve Swagger/OpenAPI front-ends based on the swagger definitions generated by this plugin:

- [@fastify/swagger-ui](https://github.com/fastify/fastify-swagger-ui)
- [@scalar/fastify-api-reference](https://github.com/scalar/scalar/tree/main/packages/fastify-api-reference)
Expand All @@ -17,6 +17,7 @@ See also [the migration guide](MIGRATION.md) for migrating from `@fastify/swagge

<a name="install"></a>
## Install

```
npm i @fastify/swagger
```
Expand All @@ -39,48 +40,42 @@ See [Fastify's LTS policy](https://github.com/fastify/fastify/blob/main/docs/Ref

<a name="usage"></a>
## Usage
Add it to your project with `register`, pass it some options, call the `swagger` API, and you are done!

Add it to your project with `register`, pass it some options, call the `swagger` API, and you are done! Below an example of how to configure the OpenAPI v3 specification with Fastify Swagger:

```js
const fastify = require('fastify')()

await fastify.register(require('@fastify/swagger'), {
swagger: {
openapi: {
openapi: '3.0.0',
info: {
title: 'Test swagger',
description: 'Testing the Fastify swagger API',
version: '0.1.0'
},
externalDocs: {
url: 'https://swagger.io',
description: 'Find more info here'
},
host: 'localhost',
schemes: ['http'],
consumes: ['application/json'],
produces: ['application/json'],
servers: [
{
url: 'http://localhost:3000',
description: 'Development server'
}
],
tags: [
{ name: 'user', description: 'User related end-points' },
{ name: 'code', description: 'Code related end-points' }
],
definitions: {
User: {
type: 'object',
required: ['id', 'email'],
properties: {
id: { type: 'string', format: 'uuid' },
firstName: { type: 'string' },
lastName: { type: 'string' },
email: {type: 'string', format: 'email' }
components: {
securitySchemes: {
apiKey: {
type: 'apiKey',
name: 'apiKey',
in: 'header'
}
}
},
securityDefinitions: {
apiKey: {
type: 'apiKey',
name: 'apiKey',
in: 'header'
}
externalDocs: {
url: 'https://swagger.io',
description: 'Find more info here'
}
}
})
Expand All @@ -90,6 +85,7 @@ fastify.put('/some-route/:id', {
description: 'post some data',
tags: ['user', 'code'],
summary: 'qwerty',
security: [{ apiKey: [] }],
params: {
type: 'object',
properties: {
Expand Down Expand Up @@ -126,14 +122,9 @@ fastify.put('/some-route/:id', {
foo: { type: 'string' }
}
}
},
security: [
{
"apiKey": []
}
]
}
}
}, (req, reply) => {})
}, (req, reply) => { })

await fastify.ready()
fastify.swagger()
Expand Down