Skip to content

Commit

Permalink
migrate implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
felixheck committed Nov 23, 2017
1 parent 02497e6 commit f869c3c
Showing 1 changed file with 22 additions and 19 deletions.
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']
}

0 comments on commit f869c3c

Please sign in to comment.