Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Hapi v17 #126

Merged
merged 5 commits into from
Nov 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage.html
node_modules/
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
node_modules
coverage.html
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ script:
- "npm test"
matrix:
include:
- node_js: '6'
- node_js: '8'
- node_js: 'node'
15 changes: 4 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# hapi-swaggered-ui
Easy swagger-ui (v3) drop-in plugin for hapi to be used with [hapi-swaggered](https://github.com/z0mt3c/hapi-swaggered).

Supports hapi 10.x and up
Supports hapi 17.x and up

[![Build Status](https://img.shields.io/travis/z0mt3c/hapi-swaggered-ui/master.svg)](https://travis-ci.org/z0mt3c/hapi-swaggered-ui)
[![Dependency Status](https://img.shields.io/gemnasium/z0mt3c/hapi-swaggered-ui.svg)](https://gemnasium.com/z0mt3c/hapi-swaggered-ui)
Expand Down Expand Up @@ -35,14 +35,12 @@ npm install hapi-swaggered-ui
## Example
Since [hapi-swaggered](https://github.com/z0mt3c/hapi-swaggered) exposes its plugin configuration hapi-swaggered-ui should find it's swagger endpoint automatically. In case you want to use hapi-swaggered-ui without hapi-swaggered (or the auto-detection doesn't work) you can manually set the swagger endpoint by the swaggerEndpoint option. In addition the page title can be changed through the option title.

Note: Hapi v8.x.x shipped with the `vision` and `inert` plugins already registered with the `server`, but have been removed in Hapi v9.x.x so require manual plugin registration before `hapi-swaggered-ui` as shown below.

```js
server.register([
await server.register([
require('inert'),
require('vision'),
{
register: require('hapi-swaggered-ui'),
plugin: require('hapi-swaggered-ui'),
options: {
title: 'Example API',
path: '/docs',
Expand All @@ -55,13 +53,8 @@ server.register([
},
swaggerOptions: {} // see above
}
}], {
select: 'api'
}, function (err) {
if (err) {
throw err
}
})
])
```

May have a look at the example listed at https://github.com/z0mt3c/hapi-swaggered
41 changes: 22 additions & 19 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,19 @@ const defaultOptions = {
}
}

exports.register = (plugin, options, next) => {
module.exports = {
register,
pkg: packageInfo,
multiple: true,
dependencies: ['vision', 'inert']
}

function register (plugin, options) {
const settings = Hoek.applyToDefaults(defaultOptions, options || {}, true)
Joi.assert(settings, optionsSchema, 'Invalid options for hapi-swaggered-ui')

const routeModifiers = plugin.config || Hoek.reach(plugin, 'realm.modifiers')
let routePrefix = Hoek.reach(routeModifiers, 'route.prefix') || ''
const routeModifiers = plugin.config || _.get(plugin, 'realm.modifiers')
let routePrefix = _.get(routeModifiers, 'route.prefix') || ''

if (settings.basePath != null) {
routePrefix = settings.basePath + routePrefix
Expand All @@ -90,8 +97,8 @@ exports.register = (plugin, options, next) => {
}

const internals = {
handler (request, reply) {
const hapiSwaggeredSettings = Hoek.reach(plugin, 'plugins.hapi-swaggered.settings')
handler (request, h) {
const hapiSwaggeredSettings = _.get(plugin, 'plugins.hapi-swaggered.settings')
let swaggerEndpoint = null

if (settings.swaggerEndpoint) {
Expand All @@ -103,6 +110,7 @@ exports.register = (plugin, options, next) => {
swaggerEndpoint = settings.basePath + swaggerEndpoint
}
}

const context = {
routePrefix: routePrefix,
title: settings.title,
Expand All @@ -119,7 +127,9 @@ exports.register = (plugin, options, next) => {

if (request.query.tags) {
tags = request.query.tags
} if (settings.defaultTags) {
}

if (settings.defaultTags) {
tags = Array.isArray(settings.defaultTags) ? settings.defaultTags.join(',') : settings.defaultTags
}

Expand All @@ -138,15 +148,16 @@ exports.register = (plugin, options, next) => {
delete swaggerOptions[key]
memo.push('\n' + JSON.stringify(key) + ':' + value.toString())
}

return memo
}, []).join(',\n') + '}'

context.swaggerOptions = JSON.stringify(swaggerOptions) + ',' + swaggerFunctions
context.oauthOptions = JSON.stringify(settings.oauthOptions || false)
return reply.view('index', context)
} else {
return reply.view('error', context)
return h.view('index', context)
}

return h.view('error', context)
}
}

Expand All @@ -166,7 +177,7 @@ exports.register = (plugin, options, next) => {
return {
method: 'GET',
path: path,
config: {
options: {
handler: internals.handler,
auth: settings.auth
}
Expand All @@ -177,7 +188,7 @@ exports.register = (plugin, options, next) => {
return {
method: 'GET',
path: path,
config: {
options: {
handler: {
directory: {
path: swaggerUiPath,
Expand All @@ -189,12 +200,4 @@ exports.register = (plugin, options, next) => {
}
}
}))

next()
}

exports.register.attributes = {
pkg: packageInfo,
multiple: true,
dependencies: ['vision', 'inert']
}
Loading