Use `app.get('restApiRoot')` as default basePath

This commit is contained in:
Miroslav Bajtoš 2014-01-07 16:56:58 +01:00
parent 6c4e780ab8
commit 8b6fc55de8
2 changed files with 42 additions and 17 deletions

View File

@ -2,6 +2,7 @@
* Adds dynamically-updated docs as /explorer
*/
var path = require('path');
var extend = require('util')._extend;
var loopback = require('loopback');
var express = requireLoopbackDependency('express');
var STATIC_ROOT = path.join(__dirname, 'public');
@ -16,7 +17,8 @@ module.exports = explorer;
*/
function explorer(loopbackApplication, options) {
var options = options || {};
options = extend({}, options);
options.basePath = options.basePath || loopbackApplication.get('restApiRoot');
loopbackApplication.docs(options);

View File

@ -41,7 +41,7 @@ describe('explorer', function() {
done();
});
});
})
});
describe('with custom baseUrl', function() {
beforeEach(givenLoopBackAppWithExplorer('/api'));
@ -60,25 +60,48 @@ describe('explorer', function() {
});
});
describe('with custom app.restApiRoot', function() {
it('should serve correct swagger-ui config', function(done) {
var app = loopback();
app.set('restApiRoot', '/rest-api-root');
configureRestApiAndExplorer(app);
request(app)
.get('/explorer/config.json')
.expect(200)
.end(function(err, res) {
if (err) return done(err);
expect(res.body).to
.have.property('discoveryUrl', '/rest-api-root/swagger/resources');
done();
});
});
});
function givenLoopBackAppWithExplorer(restUrlBase) {
return function(done) {
var app = this.app = loopback();
var Product = loopback.Model.extend('product');
Product.attachTo(loopback.memory());
app.model(Product);
if (restUrlBase) {
app.use(restUrlBase, loopback.rest());
app.use('/explorer', explorer(app, { basePath: restUrlBase }));
} else {
// LoopBack REST adapter owns the whole URL space and does not
// let other middleware handle same URLs.
// It's possible to circumvent this measure by installing
// the explorer middleware before the REST middleware.
app.use('/explorer', explorer(app));
app.use(loopback.rest());
}
configureRestApiAndExplorer(app, restUrlBase);
done();
};
}
function configureRestApiAndExplorer(app, restUrlBase) {
var Product = loopback.Model.extend('product');
Product.attachTo(loopback.memory());
app.model(Product);
if (restUrlBase) {
app.use(restUrlBase, loopback.rest());
app.use('/explorer', explorer(app, { basePath: restUrlBase }));
} else {
// LoopBack REST adapter owns the whole URL space and does not
// let other middleware handle same URLs.
// It's possible to circumvent this measure by installing
// the explorer middleware before the REST middleware.
// This way we can acess `/explorer` even when REST is mounted at `/`
app.use('/explorer', explorer(app));
app.use(app.get('restApiRoot') || '/', loopback.rest());
}
}
});