You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					109 lines
				
				2.1 KiB
			
		
		
			
		
	
	
					109 lines
				
				2.1 KiB
			| 
								 
											3 years ago
										 
									 | 
							
								'use strict'
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								const fastify = require('fastify')()
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								fastify.register(require('../index'), {
							 | 
						||
| 
								 | 
							
								  swagger: {
							 | 
						||
| 
								 | 
							
								    info: {
							 | 
						||
| 
								 | 
							
								      title: 'Test swagger',
							 | 
						||
| 
								 | 
							
								      description: 'testing the fastify swagger api',
							 | 
						||
| 
								 | 
							
								      version: '0.1.0'
							 | 
						||
| 
								 | 
							
								    },
							 | 
						||
| 
								 | 
							
								    securityDefinitions: {
							 | 
						||
| 
								 | 
							
								      apiKey: {
							 | 
						||
| 
								 | 
							
								        type: 'apiKey',
							 | 
						||
| 
								 | 
							
								        name: 'apiKey',
							 | 
						||
| 
								 | 
							
								        in: 'header'
							 | 
						||
| 
								 | 
							
								      }
							 | 
						||
| 
								 | 
							
								    },
							 | 
						||
| 
								 | 
							
								    host: 'localhost:3000',
							 | 
						||
| 
								 | 
							
								    schemes: ['http'],
							 | 
						||
| 
								 | 
							
								    consumes: ['application/json'],
							 | 
						||
| 
								 | 
							
								    produces: ['application/json']
							 | 
						||
| 
								 | 
							
								  },
							 | 
						||
| 
								 | 
							
								  hideUntagged: true,
							 | 
						||
| 
								 | 
							
								  exposeRoute: true
							 | 
						||
| 
								 | 
							
								})
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								fastify.addSchema({
							 | 
						||
| 
								 | 
							
								  $id: 'user',
							 | 
						||
| 
								 | 
							
								  type: 'object',
							 | 
						||
| 
								 | 
							
								  properties: {
							 | 
						||
| 
								 | 
							
								    id: {
							 | 
						||
| 
								 | 
							
								      type: 'string',
							 | 
						||
| 
								 | 
							
								      description: 'user id'
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								})
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								fastify.addSchema({
							 | 
						||
| 
								 | 
							
								  $id: 'some',
							 | 
						||
| 
								 | 
							
								  type: 'object',
							 | 
						||
| 
								 | 
							
								  properties: {
							 | 
						||
| 
								 | 
							
								    some: { type: 'string' }
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								})
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								fastify.put('/some-route/:id', {
							 | 
						||
| 
								 | 
							
								  schema: {
							 | 
						||
| 
								 | 
							
								    description: 'post some data',
							 | 
						||
| 
								 | 
							
								    tags: ['user', 'code'],
							 | 
						||
| 
								 | 
							
								    summary: 'qwerty',
							 | 
						||
| 
								 | 
							
								    security: [{ apiKey: [] }],
							 | 
						||
| 
								 | 
							
								    params: { $ref: 'user#' },
							 | 
						||
| 
								 | 
							
								    body: {
							 | 
						||
| 
								 | 
							
								      type: 'object',
							 | 
						||
| 
								 | 
							
								      properties: {
							 | 
						||
| 
								 | 
							
								        hello: { type: 'string' },
							 | 
						||
| 
								 | 
							
								        obj: { $ref: 'some#' }
							 | 
						||
| 
								 | 
							
								      }
							 | 
						||
| 
								 | 
							
								    },
							 | 
						||
| 
								 | 
							
								    response: {
							 | 
						||
| 
								 | 
							
								      201: {
							 | 
						||
| 
								 | 
							
								        description: 'Succesful response',
							 | 
						||
| 
								 | 
							
								        type: 'object',
							 | 
						||
| 
								 | 
							
								        properties: {
							 | 
						||
| 
								 | 
							
								          hello: { type: 'string' }
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								      },
							 | 
						||
| 
								 | 
							
								      default: {
							 | 
						||
| 
								 | 
							
								        description: 'Default response',
							 | 
						||
| 
								 | 
							
								        type: 'object',
							 | 
						||
| 
								 | 
							
								        properties: {
							 | 
						||
| 
								 | 
							
								          foo: { 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: { $ref: 'user#' },
							 | 
						||
| 
								 | 
							
								    body: {
							 | 
						||
| 
								 | 
							
								      type: 'object',
							 | 
						||
| 
								 | 
							
								      properties: {
							 | 
						||
| 
								 | 
							
								        hello: { type: 'string' },
							 | 
						||
| 
								 | 
							
								        obj: { $ref: 'some#' }
							 | 
						||
| 
								 | 
							
								      }
							 | 
						||
| 
								 | 
							
								    },
							 | 
						||
| 
								 | 
							
								    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')
							 | 
						||
| 
								 | 
							
								})
							 |