Skip to content

Commit

Permalink
Docs: Convert usage example to OpenAPI v3 spec (#792)
Browse files Browse the repository at this point in the history
* Docs: Update usage example to OpenAPI v3 spec

* docs: Fix indent
  • Loading branch information
melroy89 authored Mar 20, 2024
1 parent 0ac8d85 commit 9bcaed4
Showing 1 changed file with 24 additions and 33 deletions.
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

0 comments on commit 9bcaed4

Please sign in to comment.