Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: z0mt3c/hapi-swaggered-ui
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 86dcc3c7bebefb89b744441e4d8ddc7e9b2dd26f
Choose a base ref
...
head repository: z0mt3c/hapi-swaggered-ui
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 9371f656446b39176a239cf45c645d1e2564fa92
Choose a head ref
  • 9 commits
  • 11 files changed
  • 2 contributors

Commits on Oct 11, 2017

  1. swagger-ui 3

    z0mt3c committed Oct 11, 2017

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    ddde091 View commit details

Commits on Nov 23, 2017

  1. add ignore files

    felixheck committed Nov 23, 2017
    Copy the full SHA
    0956530 View commit details
  2. update dependencies

    felixheck committed Nov 23, 2017
    Copy the full SHA
    22674e2 View commit details
  3. update docs

    felixheck committed Nov 23, 2017
    Copy the full SHA
    14887e9 View commit details
  4. migrate tests

    felixheck committed Nov 23, 2017
    Copy the full SHA
    02497e6 View commit details
  5. migrate implementation

    felixheck committed Nov 23, 2017
    Copy the full SHA
    f869c3c View commit details

Commits on Nov 28, 2017

  1. Merge pull request #126 from felixheck/v17

    Support Hapi v17
    z0mt3c authored Nov 28, 2017
    Copy the full SHA
    587f2e0 View commit details
  2. updates

    z0mt3c committed Nov 28, 2017
    Copy the full SHA
    8016bd8 View commit details
  3. 3.0.0

    z0mt3c committed Nov 28, 2017
    Copy the full SHA
    9371f65 View commit details
Showing with 997 additions and 574 deletions.
  1. +2 −0 .gitignore
  2. +3 −0 .npmignore
  3. +1 −1 .travis.yml
  4. +7 −16 README.md
  5. +46 −32 lib/index.js
  6. +762 −306 package-lock.json
  7. +14 −13 package.json
  8. +5 −6 templates/error.hbs
  9. +77 −91 templates/index.hbs
  10. +76 −100 test/index-test.js
  11. +4 −9 test/plugin-test.js
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
@@ -4,5 +4,5 @@ script:
- "npm test"
matrix:
include:
- node_js: '6'
- node_js: '8'
- node_js: 'node'
23 changes: 7 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# hapi-swaggered-ui
Easy swagger-ui drop-in plugin for hapi to be used with [hapi-swaggered](https://github.com/z0mt3c/hapi-swaggered).
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)
@@ -19,12 +19,10 @@ npm install hapi-swaggered-ui
* `basePath`: string, optional url base path (e.g. used to fix reverse proxy routes)
* `swaggerEndpoint`: Override the auto-detection of [hapi-swaggered](https://github.com/z0mt3c/hapi-swaggered) with a specific URL. (not recommended in use with hapi-swaggered; optional)
* `swaggerOptions`: object (according to [swagger-ui](https://github.com/swagger-api/swagger-ui#parameters))
* `apisSorter`: Apply a sort to the API list. It can be 'alpha' (sort paths alphanumerically) or null (server side sorting).
* `operationsSorter`: Apply a sort to the operation list of each API. It can be 'alpha' (sort by paths alphanumerically), 'method' (sort by HTTP method) or null (server side sorting).
* `operationsSorter`: Apply a sort to the operation list of each API. It can be 'alpha' (sort by paths alphanumerically), 'method' (sort by HTTP method), null (server side sorting) or a custom function (see link above).
* `docExpansion`: Controls how the API listing is displayed. It can be set to 'none' (default), 'list' (shows operations for each resource), or 'full' (fully expanded: shows operations and their details).
* `supportedSubmitMethods`: Routes which differ will be listed as readonly - default: ['get', 'post', 'put', 'patch', 'delete', 'head']
* `highlightSizeThreshold`: Any size response below this threshold will be highlighted syntactically, attempting to highlight large responses can lead to browser hangs, not including a threshold will default to highlight all returned responses.
* `validatorUrl`: By default, Swagger-UI attempts to validate specs against swagger.io's online validator (disabled for localhost). You can use this parameter to set a different validator URL, for example for locally deployed validators (Validator Badge). Setting it to false will disable validation. This parameter is relevant for Swagger 2.0 specs only.
* In addition the following options are supported: `oauth2RedirectUrl`, `configUrl`, `displayOperationId`, `displayRequestDuration`, `filter`, `deepLinking`, `maxDisplayedTags`, `tagsSorter`, `parameterMacro`, `modelPropertyMacro` - have a look at [swagger-ui](https://github.com/swagger-api/swagger-ui#parameters)
* `authorization`: object - can be null or false to disable authorization through swagger-ui (e.g. in case of public apis without auth)
* `scope`: string, 'query' or 'header'
* `field`: string, name of the field
@@ -37,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',
@@ -57,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
78 changes: 46 additions & 32 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict'

const path = require('path')
const Hoek = require('hoek')
const swaggerUiPath = require('swagger-ui/index').dist
const swaggerUiPath = require('swagger-ui-dist').getAbsoluteFSPath()
const Joi = require('joi')
const packageInfo = require('../package.json')
const _ = require('lodash')

const optionsSchema = Joi.object({
title: Joi.string().required(),
@@ -13,17 +13,21 @@ const optionsSchema = Joi.object({
basePath: Joi.string().optional(),
swaggerEndpoint: Joi.string().optional(),
swaggerOptions: Joi.object({
booleanValues: Joi.array().items(Joi.any()).optional(),
validatorUrl: Joi.string().allow(null, false).optional(),
docExpansion: Joi.string().allow(null).valid(['none', 'list', 'full']).optional(),
defaultModelRendering: Joi.string().allow(null).valid(['model', 'schema']).optional(),
apisSorter: Joi.string().allow(null).valid(['none', 'alpha']).optional(),
operationsSorter: Joi.string().allow(null).valid(['none', 'alpha', 'method']).optional(),
highlightSizeThreshold: Joi.number().optional(),
supportedSubmitMethods: Joi.array().items(Joi.string()).optional(),
oauth2RedirectUrl: Joi.string().optional(),
showRequestHeaders: Joi.boolean().optional(),
jsonEditor: Joi.boolean().optional()
configUrl: Joi.string().optional(),
displayOperationId: Joi.boolean().optional(),
displayRequestDuration: Joi.boolean().optional(),
filter: Joi.string().allow(null, true, false).optional(),
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(),
// 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({
@@ -56,8 +60,7 @@ const optionsSchema = Joi.object({
const defaultOptions = {
title: 'swagger',
swaggerOptions: {
supportedSubmitMethods: ['head', 'get', 'post', 'put', 'patch', 'delete'],
apisSorter: 'alpha',
tagsSorter: 'alpha',
operationsSorter: 'alpha',
docExpansion: 'none'
},
@@ -71,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
@@ -87,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) {
@@ -100,6 +110,7 @@ exports.register = (plugin, options, next) => {
swaggerEndpoint = settings.basePath + swaggerEndpoint
}
}

const context = {
routePrefix: routePrefix,
title: settings.title,
@@ -116,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
}

@@ -130,12 +143,21 @@ exports.register = (plugin, options, next) => {

Object.assign(swaggerOptions, settings.swaggerOptions)

context.swaggerOptions = JSON.stringify(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') + '}'

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)
}
}

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

next()
}

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