From ff60f8bf91e17407e1697310a3e1ef3f40840a8d Mon Sep 17 00:00:00 2001 From: Supasate Choochaisri Date: Sat, 30 Apr 2016 04:01:48 +0700 Subject: [PATCH] Add feature to hide disabled remote methods after explorer is initialized Signed-off-by: Supasate Choochaisri --- index.js | 6 ++++++ test/explorer.test.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/index.js b/index.js index 26ddf9b..efceba8 100644 --- a/index.js +++ b/index.js @@ -104,6 +104,12 @@ function mountSwagger(loopbackApplication, swaggerApp, opts) { swaggerObject = createSwaggerObject(loopbackApplication, opts); }); + // listening to remoteMethodDisabled event for updating the swaggerObject + // when a remote method is disabled to hide that method in the Swagger UI. + loopbackApplication.on('remoteMethodDisabled', function() { + swaggerObject = createSwaggerObject(loopbackApplication, opts); + }); + var resourcePath = opts && opts.resourcePath || 'swagger.json'; if (resourcePath[0] !== '/') resourcePath = '/' + resourcePath; diff --git a/test/explorer.test.js b/test/explorer.test.js index 36261e6..8e25858 100644 --- a/test/explorer.test.js +++ b/test/explorer.test.js @@ -283,6 +283,37 @@ describe('explorer', function() { }); }); + it('updates swagger object when a remote method is disabled', function(done) { + var app = loopback(); + configureRestApiAndExplorer(app, '/explorer'); + + // Ensure the swagger object was built + request(app) + .get('/explorer/swagger.json') + .expect(200) + .end(function(err, res) { + if (err) return done(err); + + // Check the method that will be disabled + var paths = Object.keys(res.body.paths); + expect(paths).to.contain('/products/findOne'); + + var Product = app.models.Product; + Product.disableRemoteMethod('findOne', true); + + // Request swagger.json again + request(app) + .get('/explorer/swagger.json') + .expect(200) + .end(function(err, res) { + if (err) return done(err); + var paths = Object.keys(res.body.paths); + expect(paths).to.not.contain('/products/findOne'); + done(); + }); + }); + }); + function givenLoopBackAppWithExplorer(explorerBase) { return function(done) { var app = this.app = loopback();