Merge pull request #10 from strongloop/feature/use-restApiRoot
Use app.restApiRoot
This commit is contained in:
commit
6615013ef4
11
index.js
11
index.js
|
@ -2,8 +2,8 @@
|
|||
* Adds dynamically-updated docs as /explorer
|
||||
*/
|
||||
var path = require('path');
|
||||
var extend = require('util')._extend;
|
||||
var loopback = require('loopback');
|
||||
var swagger = requireLoopbackDependency('strong-remoting/ext/swagger');
|
||||
var express = requireLoopbackDependency('express');
|
||||
var STATIC_ROOT = path.join(__dirname, 'public');
|
||||
|
||||
|
@ -13,13 +13,14 @@ module.exports = explorer;
|
|||
* Example usage:
|
||||
*
|
||||
* var explorer = require('loopback-explorer');
|
||||
* app.use('/explorer', explorer(app));
|
||||
* app.use('/explorer', explorer(app, options));
|
||||
*/
|
||||
|
||||
function explorer(loopbackApplication, options) {
|
||||
var options = options || {};
|
||||
var remotes = loopbackApplication.remotes();
|
||||
swagger(remotes, options);
|
||||
options = extend({}, options);
|
||||
options.basePath = options.basePath || loopbackApplication.get('restApiRoot');
|
||||
|
||||
loopbackApplication.docs(options);
|
||||
|
||||
var app = express();
|
||||
app.get('/config.json', function(req, res) {
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
"test": "mocha"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"loopback": "1.x.x"
|
||||
"loopback": "1.x >=1.5"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
@ -26,7 +26,7 @@
|
|||
"url": "https://github.com/strongloop/loopback-explorer/issues"
|
||||
},
|
||||
"devDependencies": {
|
||||
"loopback": "1.x.x",
|
||||
"loopback": "1.x",
|
||||
"mocha": "~1.14.0",
|
||||
"supertest": "~0.8.1",
|
||||
"chai": "~1.8.1"
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue