-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathindex.js
108 lines (89 loc) · 2.97 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
'use strict'
const Hoek = require('hoek')
const Joi = require('joi')
const _ = require('lodash')
const Schema = require('./schema')
const utils = require('./utils')
const SWAGGER_VERSION = '2.0'
const defaultOptions = require('./defaults')
module.exports = {
pkg: require('../package.json'),
register
}
function register (plugin, options) {
const settings = Hoek.applyToDefaults(defaultOptions, options)
Joi.assert(options, Schema.PluginOptions)
settings.pluginRoutePrefix = plugin.realm.modifiers.route.prefix || ''
const pluginRouteConfig = settings.routeConfig || {}
const internals = {
resources: require('./resources'),
keyGenerator (request) {
return `hapi-swaggered:${request.server.info.uri}:${request.url.path}`
}
}
const methods = {
resources (request) {
const serverSettings = _.get(request.server, 'settings.app.swagger')
const currentSettings = utils.getCurrentSettings(settings, serverSettings)
const resources = internals.resources(currentSettings, request.server.table(), request.query.tags)
const data = {
swagger: SWAGGER_VERSION,
schemes: currentSettings.schemes,
externalDocs: currentSettings.externalDocs,
securityDefinitions: currentSettings.securityDefinitions,
paths: resources.paths,
definitions: resources.definitions,
tags: utils.getTags(currentSettings)
}
utils.setNotEmpty(data, 'host', currentSettings.host)
utils.setNotEmpty(data, 'info', currentSettings.info)
utils.setNotEmpty(data, 'basePath', settings.stripPrefix)
if (settings.basePath) {
data.basePath = settings.basePath + (data.basePath ? data.basePath : '')
}
return data
}
}
let resourcesGenerator = methods.resources
if (settings.cache !== false) {
plugin.method('resources', resourcesGenerator, {
cache: settings.cache,
generateKey: internals.keyGenerator
})
resourcesGenerator = plugin.methods.resources
}
const handler = {
resources (request) {
return resourcesGenerator(request, (error, data) => {
Hoek.assert(!error, 'swagger-resource generation failed')
const serverSettings = _.get(request.server, 'settings.app.swagger')
const currentSettings = utils.getCurrentSettings(settings, serverSettings)
data.host = currentSettings.host
return data
})
}
}
plugin.route({
method: 'GET',
path: settings.endpoint,
options: Hoek.applyToDefaults(pluginRouteConfig, {
cors: settings.cors,
tags: settings.routeTags,
auth: settings.auth,
validate: {
query: Joi.object({
tags: Joi.string().optional()
}).options({
stripUnknown: true
})
},
handler: handler.resources,
response: settings.responseValidation === true ? {
schema: Schema.Swagger
} : undefined
})
})
// expose settings
plugin.expose('settings', settings)
return Promise.resolve()
}