Last active
May 23, 2018 11:32
-
-
Save darahayes/70f951e12aa74bd95b2058ee4141a319 to your computer and use it in GitHub Desktop.
Gist to accompany my blogpost https://darahayes.com/document-your-hapi-application-swagger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Hapi = require('hapi') | |
const Joi = require('joi') | |
const Boom = require('boom') | |
const server = new Hapi.Server() | |
server.connection({ host: 'localhost', port: 8000 }) | |
server.register( | |
[ | |
{ | |
register: require('inert') | |
}, | |
{ | |
register: require('vision') | |
}, | |
{ | |
register: require('hapi-swagger'), | |
options: { | |
info: { // metadata rendered in the Swagger UI | |
title: 'Math API', | |
description: 'The Math API lets you perform basic arithmetic operations over HTTP', | |
version: '1.0.0' | |
} | |
} | |
} | |
] | |
) | |
// Our Math Functions | |
const operations = { | |
'add': (a, b) => { | |
return a + b | |
}, | |
'subtract': (a, b) => { | |
return a - b | |
}, | |
'multiply': (a, b) => { | |
return a * b | |
}, | |
'divide': (a, b) => { | |
if (b === 0) return Boom.badRequest('cannot divide by 0') | |
return a / b | |
} | |
} | |
// The /math endpoint's response schema | |
// This is rendered as an example response in the Swagger UI | |
const mathResponse = Joi.object({ | |
values: { | |
a: Joi.number(), | |
b: Joi.number() | |
}, | |
operation: Joi.string().valid(Object.keys(operations)).required(), | |
result: Joi.number() | |
}) | |
server.route({ | |
method: 'GET', | |
path:'/math', | |
handler: (request, reply) => { | |
let {a, b, op} = request.query | |
let operation = operations[op] | |
return reply({ | |
values: { | |
'a': a, | |
'b': b | |
}, | |
operation: op, | |
result: operation(a, b) | |
}) | |
}, | |
config: { | |
validate: { | |
query: { | |
a: Joi.number().required(), | |
b: Joi.number().required(), | |
op: Joi.string().valid(Object.keys(operations)).required() | |
} | |
}, | |
tags: ['api'], | |
description: 'Takes in two numbers (a,b) and performs the desired operation on them', | |
response: {schema: mathResponse} | |
} | |
}) | |
server.start((err) => { | |
if (err) throw err | |
console.log('Server running at:', server.info.uri) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment