hapi-swagger
Advanced tools
Comparing version 1.1.1 to 1.2.0
114
lib/index.js
@@ -5,3 +5,4 @@ var Hoek = require('hoek'), | ||
Path = require('path'), | ||
ShortId = require('shortid'); | ||
ShortId = require('shortid'), | ||
Url = require('url'); | ||
@@ -42,12 +43,12 @@ | ||
exports.register = function (server, options, next) { | ||
server.dependency(['inert', 'vision'], after); | ||
return next(); | ||
server.dependency(['inert', 'vision'], after); | ||
return next(); | ||
} | ||
var after = function (server, next) { | ||
var after = function (server, next) { | ||
var options = server.realm.pluginOptions, | ||
settings = Hoek.applyToDefaults(internals.defaults, options || {}); | ||
// get the prefix from route options | ||
@@ -73,3 +74,3 @@ settings.prefix = ''; | ||
method: 'GET', | ||
path: settings.documentationPath, | ||
path: settings.documentationPath, | ||
config: { | ||
@@ -163,12 +164,28 @@ auth: settings.auth, | ||
// prepend full protocol, hostname and port onto endpoints for shred | ||
var protocol = requestSettings.protocol || request.server.info.protocol || 'http'; | ||
var hostname = protocol + '://' + request.headers.host; | ||
if (!requestSettings.basePath.match(/^https?:\/\//)) { | ||
requestSettings.basePath = hostname + settings.basePath; | ||
} | ||
if (!requestSettings.endpoint.match(/^https?:\/\//)) { | ||
requestSettings.endpoint = requestSettings.basePath + settings.endpoint; | ||
} | ||
// get protocol and host from request | ||
var protocol = request.headers['x-forwarded-proto'] || | ||
request.server.info.protocol || | ||
'http'; | ||
var host = request.headers['x-forwarded-host'] || request.headers.host; | ||
var urlConfig = { | ||
protocol: protocol, | ||
host: host | ||
}; | ||
// Treat protocol and host as defaults, and override with requestSettings | ||
var baseUrlConfig = Hoek.applyToDefaults( | ||
urlConfig, | ||
Url.parse(requestSettings.basePath) | ||
); | ||
baseUrlConfig.protocol = requestSettings.protocol || baseUrlConfig.protocol; | ||
requestSettings.basePath = Url.format(baseUrlConfig); | ||
// Treat protocol and host as defaults, and override with requestSettings | ||
var endpointUrlConfig = Hoek.applyToDefaults( | ||
urlConfig, | ||
Url.parse(requestSettings.endpoint) | ||
); | ||
endpointUrlConfig.protocol = requestSettings.protocol || endpointUrlConfig.protocol; | ||
requestSettings.endpoint = Url.format(endpointUrlConfig); | ||
var routes = request.server.table()[0].table, | ||
@@ -288,10 +305,10 @@ resourceName = request.query.path; | ||
var routesData = []; | ||
"GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS" | ||
routes.forEach(function (route) { | ||
}) | ||
routes.forEach(function (route) { | ||
@@ -326,3 +343,3 @@ // only include routes tagged with "api" | ||
} | ||
// hapi wildcard or array support for methods | ||
@@ -332,3 +349,3 @@ if(routeData.method === '*' || Array.isArray(routeData.method)){ | ||
var methods = ["GET", "POST", "PUT", "PATCH", "DELETE"]; | ||
if(Array.isArray(routeData.method)){ | ||
@@ -339,3 +356,3 @@ methods = routeData.method.filter(function( value ){ | ||
} | ||
methods.forEach(function (method) { | ||
@@ -346,5 +363,5 @@ var newRoute = Hoek.clone( routeData ); | ||
}); | ||
}else{ | ||
routesData.push(routeData); | ||
routesData.push(routeData); | ||
} | ||
@@ -400,3 +417,3 @@ }); | ||
} | ||
}); | ||
}); | ||
} | ||
@@ -451,3 +468,3 @@ | ||
} | ||
} | ||
} | ||
} | ||
@@ -463,3 +480,3 @@ return undefined; | ||
"swaggerVersion": "1.2", | ||
"basePath": settings.basePath, | ||
"basePath": internals.removeTrailingSlash( settings.basePath ), | ||
"resourcePath": '/' + slug, | ||
@@ -547,4 +564,4 @@ "apis": [], | ||
responseClassName, | ||
internals.getParams(route, 'responseSchema'), | ||
swagger.models, | ||
internals.getParams(route, 'responseSchema'), | ||
swagger.models, | ||
null | ||
@@ -658,3 +675,3 @@ ); | ||
// removes forbidden properties | ||
if (param._flags | ||
if (param._flags | ||
&& param._flags.presence | ||
@@ -725,3 +742,3 @@ && param._flags.presence === 'forbidden'){ | ||
// get className of embeded array | ||
if(name === 'items' | ||
if(name === 'items' | ||
&& Hoek.reach(param, '_inner.inclusions.0._meta') | ||
@@ -736,3 +753,3 @@ && Array.isArray(param._inner.inclusions[0]._meta)){ | ||
} | ||
} | ||
} | ||
} | ||
@@ -763,3 +780,3 @@ | ||
if (property.type === 'any') { | ||
if (property.type === 'any') { | ||
var i = param._meta.length; | ||
@@ -769,7 +786,7 @@ while (i--) { | ||
&& param._meta[i].swaggerType === 'file'){ | ||
property.type = "file"; | ||
property.paramType = "body"; | ||
property.type = "file"; | ||
property.paramType = "body"; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
@@ -804,7 +821,7 @@ } | ||
if (foundModel) { | ||
// deep compare object | ||
if(Hoek.deepEqual(foundModel, model)){ | ||
// return existing id | ||
return foundModel.id; | ||
return foundModel.id; | ||
}else{ | ||
@@ -895,2 +912,17 @@ // create new model with alt name, to stop reuse of model | ||
// removes trailing slash from string based url | ||
internals.removeTrailingSlash = function ( urlStr ) { | ||
if(this.endsWith( urlStr, '/' )){ | ||
return urlStr.substring( 0, urlStr.length-1 ); | ||
} | ||
return urlStr | ||
}, | ||
// does a string ends with a word/char | ||
internals.endsWith = function( text, test ) { | ||
return(text.lastIndexOf(test) === text.length-1); | ||
}, | ||
exports._internals = internals; |
{ | ||
"name": "hapi-swagger", | ||
"description": "A swagger documentation UI generator plugin for hapi", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"author": "Glenn Jones", | ||
@@ -6,0 +6,0 @@ "repository": { |
@@ -6,2 +6,3 @@ # hapi-swagger | ||
[](https://www.npmjs.com/package/hapi-swaggered-ui) | ||
@@ -31,5 +32,5 @@ ## Install | ||
var server = new Hapi.Server(); | ||
server.connection({ | ||
host: 'localhost', | ||
port: 3000 | ||
server.connection({ | ||
host: 'localhost', | ||
port: 3000 | ||
}); | ||
@@ -49,7 +50,6 @@ | ||
server.start(function(){ | ||
// Add any server.route() config here | ||
console.log('Server running at:', server.info.uri); | ||
}); | ||
}); | ||
// Add any server.route() config here | ||
``` | ||
@@ -96,3 +96,3 @@ | ||
The all the files in the URLs below are added by the plugin, but you must server the custom page as template using `reply.view()`. | ||
The all the files in the URLs below are added by the plugin, but you must server the custom page as template using `reply.view()`. | ||
@@ -175,3 +175,4 @@ ```html | ||
* `apiVersion`: string The version of your API | ||
* `basePath`: string The base URL of the API i.e. `http://localhost:3000` | ||
* `protocol`: e.g. `http` or `https` will override all request headers and basePath | ||
* `basePath`: string The base URL of the API i.e. `http://localhost:3000` (note, this is parsed with `url`, so if you do not specify a protocol, it will be interpreted as path with no hostname). | ||
* `documentationPath`: string The path of the documentation page - default: `/documentation`, | ||
@@ -262,3 +263,3 @@ * `enableDocumentationPage`: boolean Enable the the documentation page - default: `true`, | ||
### File upload | ||
The plug-in has basic support for file uploads into your API's. Below is an example of a route with a file uplaod, the three important elements are: | ||
The plug-in has basic support for file uploads into your API's. Below is an example of a route with a file uplaod, the three important elements are: | ||
@@ -281,3 +282,3 @@ * `payloadType: 'form'` in the plugins section creates a form for upload | ||
validate: { | ||
payload: { | ||
payload: { | ||
file: Joi.any() | ||
@@ -284,0 +285,0 @@ .meta({ swaggerType: 'file' }) |
654449
41
11410
328