-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswagger.js
56 lines (52 loc) · 1.64 KB
/
swagger.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { readFileSync } from 'fs'
import { join } from 'desm'
import fp from 'fastify-plugin'
import Swagger from '@fastify/swagger'
import SwaggerUI from '@fastify/swagger-ui'
const { version } = JSON.parse(readFileSync(join(import.meta.url, '../package.json')))
async function swaggerGenerator (fastify, opts) {
// Swagger documentation generator for Fastify.
// It uses the schemas you declare in your routes to generate a swagger compliant doc.
// https://github.com/fastify/fastify-swagger
await fastify.register(Swagger, {
swagger: {
info: {
title: 'Fastify Template',
description: 'Fastify Template REST documentation',
version
},
externalDocs: {
url: 'https://github.com/shijin-p/rest-template',
description: 'Find more info here'
},
host: 'localhost', // and your deployed url
schemes: ['http', 'https'],
consumes: ['application/json'],
produces: ['application/json', 'text/html'],
securityDefinitions: {
Bearer: {
type: 'apiKey',
name: 'Bearer',
in: 'header'
},
Csrf: {
type: 'apiKey',
name: 'x-csrf-token',
in: 'header'
}
}
},
// let's expose the documentation only in development
// it's up to you decide who should see this page,
// but it's alwaysx better to start safe.
exposeRoute: fastify.config.NODE_ENV !== 'production'
})
if (fastify.config.NODE_ENV !== 'production') {
await fastify.register(SwaggerUI, {
routePrefix: '/documentation'
})
}
}
export default fp(swaggerGenerator, {
name: 'swaggerGenerator'
})