Skip to content

Instantly share code, notes, and snippets.

@wesleytodd
Created December 7, 2018 21:29
Show Gist options
  • Save wesleytodd/abfe092a0b3ea6b8c6ff4f64d7615f70 to your computer and use it in GitHub Desktop.
Save wesleytodd/abfe092a0b3ea6b8c6ff4f64d7615f70 to your computer and use it in GitHub Desktop.
Proposal for an open api package for express
'use strict'
const app = require('express')()
const openapi = require('@express/openapi')
const oapi = openapi('/api/swagger.json', {
info: {
title: 'Test swagger',
description: 'testing the express swagger api',
version: '0.0.1'
},
externalDocs: {
url: 'https://swagger.io',
description: 'Find more info here'
},
host: 'localhost',
schemes: ['http'],
consumes: ['application/json'],
produces: ['application/json']
})
// Register the general swagger handling
app.use(oapi)
// Define the hello world route schema
const helloWorldSchema = oapi.schema({
description: 'get some data',
summary: 'Hello World',
response: {
200: {
description: 'Successful response',
type: 'object',
properties: {
hello: { type: 'string' }
}
}
}
})
app.get('/', helloWorldSchema, (req, res) => {
res.json({
hello: 'world'
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment