UNPKG

fastify-swagger

Version:

Serve Swagger/OpenAPI documentation for Fastify, supporting dynamic generation

110 lines (104 loc) 2.15 kB
'use strict' const fastify = require('fastify')() fastify.register(require('../index'), { openapi: { info: { title: 'Test swagger', description: 'testing the fastify swagger api', version: '0.1.0' }, servers: [{ url: 'http://localhost' }], components: { securitySchemes: { apiKey: { type: 'apiKey', name: 'apiKey', in: 'header' } } } }, hideUntagged: true, exposeRoute: true }) fastify.put('/some-route/:id', { schema: { description: 'post some data', tags: ['user', 'code'], summary: 'qwerty', security: [{ apiKey: [] }], params: { type: 'object', properties: { id: { type: 'string', description: 'user id' } } }, body: { type: 'object', properties: { hello: { type: 'string' }, obj: { type: 'object', properties: { some: { type: 'string' } } } } }, response: { 201: { description: 'Succesful response', type: 'object', properties: { hello: { type: 'string' } } } } } }, (req, reply) => { reply.send({ hello: `Hello ${req.body.hello}` }) }) fastify.post('/some-route/:id', { schema: { description: 'post some data', summary: 'qwerty', security: [{ apiKey: [] }], params: { type: 'object', properties: { id: { type: 'string', description: 'user id' } } }, body: { type: 'object', properties: { hello: { type: 'string' }, obj: { type: 'object', properties: { some: { type: 'string' } } } } }, response: { 201: { description: 'Succesful response', type: 'object', properties: { hello: { type: 'string' } } } } } }, (req, reply) => { reply.send({ hello: `Hello ${req.body.hello}` }) }) fastify.listen(3000, err => { if (err) throw err console.log('listening') })