hyperswitch
Advanced tools
Comparing version 0.6.0 to 0.6.1
@@ -110,2 +110,10 @@ 'use strict'; | ||
function getDocBasePath(req, spec) { | ||
if (req.params.domain === req.headers.host.replace(/:[0-9]+$/, '') | ||
&& spec['x-host-basePath']) { | ||
// This is a host-based request. Set an appropriate base path. | ||
return spec['x-host-basePath']; | ||
} | ||
return req.uri.toString().replace(/\/$/, ''); | ||
} | ||
// A default listing handler for URIs that end in / and don't have any | ||
@@ -117,18 +125,25 @@ // handlers associated with it otherwise. | ||
&& match.value.specRoot && !match.value.specRoot['x-listing']) { | ||
var spec = Object.assign({}, match.value.specRoot, { | ||
// Set the base path dynamically | ||
basePath: req.uri.toString().replace(/\/$/, '') | ||
}); | ||
if (req.params.domain === req.headers.host.replace(/:[0-9]+$/, '')) { | ||
// This is a host-based request. Set an appropriate base path. | ||
spec.basePath = spec['x-host-basePath'] || spec.basePath; | ||
} | ||
return P.resolve({ | ||
status: 200, | ||
body: spec | ||
body: Object.assign({}, match.value.specRoot, { | ||
// Set the base path dynamically | ||
basePath: getDocBasePath(req, match.value.specRoot) | ||
}) | ||
}); | ||
} else if (rq.doc !== undefined | ||
&& (match.value.specRoot && !match.value.specRoot['x-listing'] || rq.path)) { | ||
} else if (rq.path || | ||
(match.value.specRoot | ||
&& !match.value.specRoot['x-listing'] | ||
&& match.value.specRoot.basePath === req.uri.toString().replace(/\/$/, '') | ||
&& /\btext\/html\b/.test(req.headers.accept))) { | ||
// If there's ane query parameters except ?path - redirect to the basePath | ||
if (Object.keys(req.query).filter(function(paramName) { | ||
return paramName !== 'path'; | ||
}).length) { | ||
return { | ||
status: 301, | ||
headers: { | ||
location: getDocBasePath(req, match.value.specRoot) + '/' | ||
} | ||
}; | ||
} | ||
// Return swagger UI & load spec from /?spec | ||
@@ -154,3 +169,3 @@ if (!req.query.path) { | ||
return '<li><a href="' + encodeURIComponent(api) | ||
+ '/?doc">' + api + '</a></li>'; | ||
+ '/">' + api + '</a></li>'; | ||
}).join('\n') | ||
@@ -157,0 +172,0 @@ + '</ul>'; |
@@ -26,3 +26,3 @@ "use strict"; | ||
body = body.toString() | ||
.replace(/((?:src|href)=['"])/g, '$1?doc=&path=') | ||
.replace(/((?:src|href)=['"])/g, '$1?path=') | ||
// Some self-promotion | ||
@@ -44,3 +44,3 @@ .replace(/<a id="logo".*?<\/a>/, | ||
body = body.toString() | ||
.replace(/underscore\-min\.map/, '?doc=&path=lib/underscore-min.map'); | ||
.replace(/underscore\-min\.map/, '?path=lib/underscore-min.map'); | ||
} else if (/\.png$/.test(reqPath)) { | ||
@@ -54,3 +54,3 @@ contentType = 'image/png'; | ||
contentType = 'text/css'; | ||
body = body.toString().replace(/\.\.\/(images|fonts)\//g, '?doc&path=$1/'); | ||
body = body.toString().replace(/\.\.\/(images|fonts)\//g, '?path=$1/'); | ||
} | ||
@@ -57,0 +57,0 @@ return P.resolve({ |
{ | ||
"name": "hyperswitch", | ||
"version": "0.6.0", | ||
"version": "0.6.1", | ||
"description": "REST API creation framework", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -39,3 +39,6 @@ "use strict"; | ||
return preq.get({ | ||
uri: server.hostPort + '/v1/?doc' | ||
uri: server.hostPort + '/v1/', | ||
headers: { | ||
accept: 'text/html' | ||
} | ||
}) | ||
@@ -46,10 +49,12 @@ .then(function(res) { | ||
assert.deepEqual(/<html/.exec(res.body)[0], '<html'); | ||
}) | ||
.catch(function (e) { | ||
console.log(e); | ||
}); | ||
}); | ||
}); | ||
it('should retrieve all dependencies of the swagger-ui main page', function() { | ||
return preq.get({ uri: server.hostPort + '/v1/?doc' }) | ||
return preq.get({ | ||
uri: server.hostPort + '/v1/', | ||
headers: { | ||
accept: 'text/html' | ||
} | ||
}) | ||
.then(function(res) { | ||
@@ -91,3 +96,3 @@ var assertions = []; | ||
return preq.get({ | ||
uri: server.hostPort + '/v1/?doc=&path=/this_is_no_a_path', | ||
uri: server.hostPort + '/v1/?path=/this_is_no_a_path', | ||
headers: { | ||
@@ -106,3 +111,3 @@ accept: 'text/html' | ||
return preq.get({ | ||
uri: server.hostPort + '/v1/?doc=&path=../../../Test', | ||
uri: server.hostPort + '/v1/?path=../../../Test', | ||
headers: { | ||
@@ -133,3 +138,3 @@ accept: 'text/html' | ||
return preq.get({ | ||
uri: server.hostPort + '/sys/?doc=', | ||
uri: server.hostPort + '/sys/', | ||
headers: { | ||
@@ -136,0 +141,0 @@ accept: 'text/html' |
177112
53
4138