Skip to content

Commit 9bcaed4

Browse files
authored
Docs: Convert usage example to OpenAPI v3 spec (#792)
* Docs: Update usage example to OpenAPI v3 spec * docs: Fix indent
1 parent 0ac8d85 commit 9bcaed4

File tree

1 file changed

+24
-33
lines changed

1 file changed

+24
-33
lines changed

README.md

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ A Fastify plugin for serving [Swagger (OpenAPI v2)](https://swagger.io/specifica
88

99
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).
1010

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

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

1818
<a name="install"></a>
1919
## Install
20+
2021
```
2122
npm i @fastify/swagger
2223
```
@@ -39,48 +40,42 @@ See [Fastify's LTS policy](https://github.com/fastify/fastify/blob/main/docs/Ref
3940

4041
<a name="usage"></a>
4142
## Usage
42-
Add it to your project with `register`, pass it some options, call the `swagger` API, and you are done!
43+
44+
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:
4345

4446
```js
4547
const fastify = require('fastify')()
4648

4749
await fastify.register(require('@fastify/swagger'), {
48-
swagger: {
50+
openapi: {
51+
openapi: '3.0.0',
4952
info: {
5053
title: 'Test swagger',
5154
description: 'Testing the Fastify swagger API',
5255
version: '0.1.0'
5356
},
54-
externalDocs: {
55-
url: 'https://swagger.io',
56-
description: 'Find more info here'
57-
},
58-
host: 'localhost',
59-
schemes: ['http'],
60-
consumes: ['application/json'],
61-
produces: ['application/json'],
57+
servers: [
58+
{
59+
url: 'http://localhost:3000',
60+
description: 'Development server'
61+
}
62+
],
6263
tags: [
6364
{ name: 'user', description: 'User related end-points' },
6465
{ name: 'code', description: 'Code related end-points' }
6566
],
66-
definitions: {
67-
User: {
68-
type: 'object',
69-
required: ['id', 'email'],
70-
properties: {
71-
id: { type: 'string', format: 'uuid' },
72-
firstName: { type: 'string' },
73-
lastName: { type: 'string' },
74-
email: {type: 'string', format: 'email' }
67+
components: {
68+
securitySchemes: {
69+
apiKey: {
70+
type: 'apiKey',
71+
name: 'apiKey',
72+
in: 'header'
7573
}
7674
}
7775
},
78-
securityDefinitions: {
79-
apiKey: {
80-
type: 'apiKey',
81-
name: 'apiKey',
82-
in: 'header'
83-
}
76+
externalDocs: {
77+
url: 'https://swagger.io',
78+
description: 'Find more info here'
8479
}
8580
}
8681
})
@@ -90,6 +85,7 @@ fastify.put('/some-route/:id', {
9085
description: 'post some data',
9186
tags: ['user', 'code'],
9287
summary: 'qwerty',
88+
security: [{ apiKey: [] }],
9389
params: {
9490
type: 'object',
9591
properties: {
@@ -126,14 +122,9 @@ fastify.put('/some-route/:id', {
126122
foo: { type: 'string' }
127123
}
128124
}
129-
},
130-
security: [
131-
{
132-
"apiKey": []
133-
}
134-
]
125+
}
135126
}
136-
}, (req, reply) => {})
127+
}, (req, reply) => { })
137128

138129
await fastify.ready()
139130
fastify.swagger()

0 commit comments

Comments
 (0)