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

upgrade to hapi latest, including fixing audit warnings #143

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
118 changes: 63 additions & 55 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict'
const path = require('path')
const Hoek = require('hoek')
const Hoek = require('@hapi/hoek')
const swaggerUiPath = require('swagger-ui-dist').getAbsoluteFSPath()
const Joi = require('joi')
const Joi = require('@hapi/joi')
const packageInfo = require('../package.json')
const _ = require('lodash')

Expand All @@ -14,7 +14,7 @@ const optionsSchema = Joi.object({
swaggerEndpoint: Joi.string().optional(),
swaggerOptions: Joi.object({
validatorUrl: Joi.string().allow(null, false).optional(),
docExpansion: Joi.string().allow(null).valid(['none', 'list', 'full']).optional(),
docExpansion: Joi.string().allow(null).valid('none', 'list', 'full').optional(),
oauth2RedirectUrl: Joi.string().optional(),
configUrl: Joi.string().optional(),
displayOperationId: Joi.boolean().optional(),
Expand All @@ -23,35 +23,32 @@ const optionsSchema = Joi.object({
deepLinking: Joi.boolean().optional(),
maxDisplayedTags: Joi.number().integer().positive().optional(),
// the following can be functions
tagsSorter: Joi.alternatives(Joi.func(), Joi.string().allow(null).valid(['none', 'alpha'])).optional(),
operationsSorter: Joi.alternatives(Joi.func(), Joi.string().allow(null).valid(['none', 'alpha', 'method'])).optional(),
tagsSorter: Joi.alternatives(Joi.func(), Joi.string().allow(null).valid('none', 'alpha')).optional(),
operationsSorter: Joi.alternatives(Joi.func(), Joi.string().allow(null).valid('none', 'alpha', 'method')).optional(),
// the following must be functions
parameterMacro: Joi.func().optional(),
modelPropertyMacro: Joi.func().optional()
}).optional(),
oauthOptions: Joi.any(),
authorization: Joi.alternatives([Joi.any().valid([null, false]), Joi.object({
field: Joi.string().required(),
scope: Joi.string().valid(['query', 'header']).required(),
valuePrefix: Joi.string().optional(),
defaultValue: Joi.string().optional(),
placeholder: Joi.string().optional()
}).optional()]),
authorization: Joi.alternatives(
Joi.any().valid(null, false),
Joi.object({
field: Joi.string().required(),
scope: Joi.string().valid('query', 'header').required(),
valuePrefix: Joi.string().optional(),
defaultValue: Joi.string().optional(),
placeholder: Joi.string().optional()
}).optional()
),
auth: Joi.alternatives([
Joi.string(),
Joi.object({
mode: Joi.string().valid('required', 'optional', 'try'),
scope: Joi.alternatives([
Joi.string(),
Joi.array()
]).allow(false),
scope: Joi.alternatives(Joi.string(), Joi.array()).allow(false),
entity: Joi.string().valid('user', 'app', 'any'),
strategy: Joi.string(),
strategies: Joi.array().min(1),
payload: [
Joi.string().valid('required', 'optional'),
Joi.boolean()
]
payload: [Joi.string().valid('required', 'optional'), Joi.boolean()]
})
]).allow(false),
templates: Joi.string().optional()
Expand All @@ -78,11 +75,11 @@ module.exports = {
register,
pkg: packageInfo,
multiple: true,
dependencies: ['vision', 'inert']
dependencies: ['@hapi/vision', '@hapi/inert']
}

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

const routeModifiers = plugin.config || _.get(plugin, 'realm.modifiers')
Expand Down Expand Up @@ -143,14 +140,21 @@ function register (plugin, options) {

Object.assign(swaggerOptions, settings.swaggerOptions)

var swaggerFunctions = '{' + _.reduce(swaggerOptions, function (memo, value, key) {
if (_.isFunction(value)) {
delete swaggerOptions[key]
memo.push('\n' + JSON.stringify(key) + ':' + value.toString())
}

return memo
}, []).join(',\n') + '}'
var swaggerFunctions =
'{' +
_.reduce(
swaggerOptions,
function (memo, value, key) {
if (_.isFunction(value)) {
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)
Expand All @@ -173,31 +177,35 @@ function register (plugin, options) {

const routePath = settings.path || ''

plugin.route([settings.path || '/', `${routePath}/index.html`].map((path) => {
return {
method: 'GET',
path: path,
options: {
handler: internals.handler,
auth: settings.auth
plugin.route(
[settings.path || '/', `${routePath}/index.html`].map((path) => {
return {
method: 'GET',
path: path,
options: {
handler: internals.handler,
auth: settings.auth
}
}
}
}))

plugin.route([`${routePath}/{path}`, `${routePath}/{path*2}`].map((path) => {
return {
method: 'GET',
path: path,
options: {
handler: {
directory: {
path: swaggerUiPath,
index: false,
listing: false
}
},
auth: settings.auth
})
)

plugin.route(
[`${routePath}/{path}`, `${routePath}/{path*2}`].map((path) => {
return {
method: 'GET',
path: path,
options: {
handler: {
directory: {
path: swaggerUiPath,
index: false,
listing: false
}
},
auth: settings.auth
}
}
}
}))
})
)
}
Loading