Add `swaggerUI` option to enable/disable UI serving

This commit is contained in:
Raymond Feng 2016-03-08 10:13:08 -08:00
parent 9192ee638b
commit 63f446511d
2 changed files with 54 additions and 18 deletions

View File

@ -38,7 +38,8 @@ function routes(loopbackApplication, options) {
options = _defaults({}, options, {
resourcePath: 'swagger.json',
apiInfo: loopbackApplication.get('apiInfo') || {}
apiInfo: loopbackApplication.get('apiInfo') || {},
swaggerUI: true
});
var router = new loopback.Router();
@ -60,26 +61,28 @@ function routes(loopbackApplication, options) {
});
});
// Allow specifying a static file roots for swagger files. Any files in
// these folders will override those in the swagger-ui distribution.
// In this way one could e.g. make changes to index.html without having
// to worry about constantly pulling in JS updates.
if (options.uiDirs) {
if (typeof options.uiDirs === 'string') {
router.use(loopback.static(options.uiDirs));
} else if (Array.isArray(options.uiDirs)) {
options.uiDirs.forEach(function(dir) {
router.use(loopback.static(dir));
});
if (options.swaggerUI) {
// Allow specifying a static file roots for swagger files. Any files in
// these folders will override those in the swagger-ui distribution.
// In this way one could e.g. make changes to index.html without having
// to worry about constantly pulling in JS updates.
if (options.uiDirs) {
if (typeof options.uiDirs === 'string') {
router.use(loopback.static(options.uiDirs));
} else if (Array.isArray(options.uiDirs)) {
options.uiDirs.forEach(function(dir) {
router.use(loopback.static(dir));
});
}
}
// File in node_modules are overridden by a few customizations
router.use(loopback.static(STATIC_ROOT));
// Swagger UI distribution
router.use(loopback.static(SWAGGER_UI_ROOT));
}
// File in node_modules are overridden by a few customizations
router.use(loopback.static(STATIC_ROOT));
// Swagger UI distribution
router.use(loopback.static(SWAGGER_UI_ROOT));
return router;
}

View File

@ -138,6 +138,39 @@ describe('explorer', function() {
});
});
describe('with swaggerUI option', function() {
var app;
beforeEach(function setupExplorerWithoutUI() {
app = loopback();
explorer(app, {
swaggerUI: false
});
});
it('overrides swagger-ui files', function(done) {
request(app).get('/explorer/swagger-ui.js')
.expect(404, done);
});
it('should serve config.json', function(done) {
request(app)
.get('/explorer/config.json')
.expect(200)
.end(function(err, res) {
if (err) return done(err);
expect(res.body).to
.have.property('url', '/explorer/swagger.json');
done();
});
});
it('should serve swagger.json', function(done) {
request(app)
.get('/explorer/swagger.json')
.expect(200, done);
});
});
describe('explorer.routes API', function() {
var app;
beforeEach(function() {