diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..e69de29 diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..a5503ea --- /dev/null +++ b/.eslintrc @@ -0,0 +1,10 @@ +{ + "extends": "loopback", + "rules": { + "max-len": ["error", 90, 4, { + "ignoreComments": true, + "ignoreUrls": true, + "ignorePattern": "^\\s*var\\s.+=\\s*(require\\s*\\()|(/)" + }] + } +} \ No newline at end of file diff --git a/.jshintrc b/.jshintrc deleted file mode 100644 index 3f9e65d..0000000 --- a/.jshintrc +++ /dev/null @@ -1,13 +0,0 @@ -{ - "node": true, - "camelcase" : true, - "eqnull" : true, - "indent": 2, - "undef": true, - "quotmark": "single", - "maxlen": 80, - "trailing": true, - "newcap": true, - "nonew": true, - "undef": false -} diff --git a/CHANGES.md b/CHANGES.md index ef4f7d7..33cb759 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,24 @@ +2016-03-08, Version 2.4.0 +========================= + + * Add `swaggerUI` option to enable/disable UI serving (Raymond Feng) + + +2016-02-02, Version 2.3.0 +========================= + + + +2016-01-13, Version 2.2.0 +========================= + + * remove references to ubuntu font (Anthony Ettinger) + + * Update swaggerObject when a new model was added (Pradeep Kumar Tippa) + + * Refer to licenses with a link (Sam Roberts) + + 2015-10-01, Version 2.1.1 ========================= diff --git a/example/hidden.js b/example/hidden.js index 3c65808..93bfad0 100644 --- a/example/hidden.js +++ b/example/hidden.js @@ -7,13 +7,13 @@ var User = loopback.Model.extend('user', { username: 'string', email: 'string', sensitiveInternalProperty: 'string', -}, {hidden: ['sensitiveInternalProperty']}); +}, { hidden: ['sensitiveInternalProperty'] }); User.attachTo(loopback.memory()); app.model(User); var apiPath = '/api'; -explorer(app, {basePath: apiPath}); +explorer(app, { basePath: apiPath }); app.use(apiPath, loopback.rest()); console.log('Explorer mounted at localhost:' + port + '/explorer'); diff --git a/example/simple.js b/example/simple.js index 550aa2d..d0e0405 100644 --- a/example/simple.js +++ b/example/simple.js @@ -4,15 +4,15 @@ var explorer = require('../'); var port = 3000; var Product = loopback.PersistedModel.extend('product', { - foo: {type: 'string', required: true}, + foo: { type: 'string', required: true }, bar: 'string', - aNum: {type: 'number', min: 1, max: 10, required: true, default: 5} + aNum: { type: 'number', min: 1, max: 10, required: true, default: 5 }, }); Product.attachTo(loopback.memory()); app.model(Product); var apiPath = '/api'; -explorer(app, {basePath: apiPath}); +explorer(app, { basePath: apiPath }); app.use(apiPath, loopback.rest()); console.log('Explorer mounted at http://localhost:' + port + '/explorer'); diff --git a/index.js b/index.js index 177f89c..efceba8 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,4 @@ - 'use strict'; +'use strict'; /*! * Adds dynamically-updated docs as /explorer */ @@ -30,7 +30,7 @@ function explorer(loopbackApplication, options) { function routes(loopbackApplication, options) { var loopback = loopbackApplication.loopback; var loopbackMajor = loopback && loopback.version && - loopback.version.split('.')[0] || 1; + loopback.version.split('.')[0] || 1; if (loopbackMajor < 2) { throw new Error('loopback-component-explorer requires loopback 2.0 or newer'); @@ -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(); @@ -56,30 +57,32 @@ function routes(loopbackApplication, options) { source = req.originalUrl.replace(/\/config.json(\?.*)?$/, ''); } res.send({ - url: urlJoin(source, '/' + options.resourcePath) + url: urlJoin(source, '/' + options.resourcePath), }); }); - // 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; } @@ -95,6 +98,18 @@ function routes(loopbackApplication, options) { function mountSwagger(loopbackApplication, swaggerApp, opts) { var swaggerObject = createSwaggerObject(loopbackApplication, opts); + // listening to modelRemoted event for updating the swaggerObject + // with the newly created model to appear in the Swagger UI. + loopbackApplication.on('modelRemoted', function() { + 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; @@ -108,7 +123,7 @@ function mountSwagger(loopbackApplication, swaggerApp, opts) { function setupCors(swaggerApp, remotes) { var corsOptions = remotes.options && remotes.options.cors || - { origin: true, credentials: true }; + { origin: true, credentials: true }; // TODO(bajtos) Skip CORS when remotes.options.cors === false swaggerApp.use(cors(corsOptions)); diff --git a/package.json b/package.json index 742733a..60342a1 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,12 @@ { "name": "loopback-component-explorer", - "version": "2.1.1", + "version": "2.4.0", "description": "Browse and test your LoopBack app's APIs", "main": "index.js", "scripts": { - "test": "mocha" + "lint": "eslint .", + "test": "mocha", + "posttest": "npm run lint" }, "repository": { "type": "git", @@ -21,10 +23,12 @@ "url": "https://github.com/strongloop/loopback-component-explorer/issues" }, "devDependencies": { + "chai": "^3.2.0", + "eslint": "^2.8.0", + "eslint-config-loopback": "^2.0.0", "loopback": "^2.19.1", "mocha": "^2.2.5", - "supertest": "^1.0.1", - "chai": "^3.2.0" + "supertest": "^1.0.1" }, "license": "MIT", "dependencies": { diff --git a/public/css/loopbackStyles.css b/public/css/loopbackStyles.css index cf188ec..2c238d7 100644 --- a/public/css/loopbackStyles.css +++ b/public/css/loopbackStyles.css @@ -1,37 +1,5 @@ /* Styles used for loopback explorer customizations */ -/* Custom font */ - -@font-face { - font-family: 'Ubuntu'; - font-style: normal; - font-weight: 300; - src: local('Ubuntu Light'), - local('Ubuntu-Light'), - url(../fonts/_aijTyevf54tkVDLy-dlnLO3LdcAZYWl9Si6vvxL-qU.woff) - format('woff'); -} - -@font-face { - font-family: 'Ubuntu'; - font-style: normal; - font-weight: 500; - src: local('Ubuntu Medium'), - local('Ubuntu-Medium'), - url(../fonts/OsJ2DjdpjqFRVUSto6IffLO3LdcAZYWl9Si6vvxL-qU.woff) - format('woff'); -} - -@font-face { - font-family: 'Ubuntu'; - font-style: normal; - font-weight: 700; - src: local('Ubuntu Bold'), - local('Ubuntu-Bold'), - url(../fonts/0ihfXUL2emPh0ROJezvraLO3LdcAZYWl9Si6vvxL-qU.woff) - format('woff'); -} - .swagger-section .swagger-ui-wrap, .swagger-section .swagger-ui-wrap b, .swagger-section .swagger-ui-wrap strong, @@ -43,13 +11,13 @@ .swagger-section .swagger-ui-wrap p#colophon, .swagger-section .swagger-ui-wrap .markdown ul .model-signature { - font-family: "Ubuntu", sans-serif !important; + font-family: 'helvetica neue', helvetica, arial, sans-serif !important; } /* layout spacing and global colors */ body { padding-top: 60px; - font-family: "Ubuntu", sans-serif; + font-family: 'helvetica neue', helvetica, arial, sans-serif; } body #header { diff --git a/public/fonts/0ihfXUL2emPh0ROJezvraLO3LdcAZYWl9Si6vvxL-qU.woff b/public/fonts/0ihfXUL2emPh0ROJezvraLO3LdcAZYWl9Si6vvxL-qU.woff deleted file mode 100644 index 80a3fc2..0000000 Binary files a/public/fonts/0ihfXUL2emPh0ROJezvraLO3LdcAZYWl9Si6vvxL-qU.woff and /dev/null differ diff --git a/public/fonts/OsJ2DjdpjqFRVUSto6IffLO3LdcAZYWl9Si6vvxL-qU.woff b/public/fonts/OsJ2DjdpjqFRVUSto6IffLO3LdcAZYWl9Si6vvxL-qU.woff deleted file mode 100644 index 31bdbab..0000000 Binary files a/public/fonts/OsJ2DjdpjqFRVUSto6IffLO3LdcAZYWl9Si6vvxL-qU.woff and /dev/null differ diff --git a/public/fonts/_aijTyevf54tkVDLy-dlnLO3LdcAZYWl9Si6vvxL-qU.woff b/public/fonts/_aijTyevf54tkVDLy-dlnLO3LdcAZYWl9Si6vvxL-qU.woff deleted file mode 100644 index 8951646..0000000 Binary files a/public/fonts/_aijTyevf54tkVDLy-dlnLO3LdcAZYWl9Si6vvxL-qU.woff and /dev/null differ diff --git a/public/lib/loadSwaggerUI.js b/public/lib/loadSwaggerUI.js index 2a1a466..aaee90b 100644 --- a/public/lib/loadSwaggerUI.js +++ b/public/lib/loadSwaggerUI.js @@ -4,7 +4,7 @@ /*global SwaggerUi, log, ApiKeyAuthorization, hljs, window, $ */ $(function() { // Pre load translate... - if(window.SwaggerTranslator) { + if (window.SwaggerTranslator) { window.SwaggerTranslator.translate(); } @@ -17,6 +17,7 @@ $(function() { var accessToken; function loadSwaggerUi(config) { var methodOrder = ['get', 'head', 'options', 'put', 'post', 'delete']; + /* eslint-disable camelcase */ window.swaggerUi = new SwaggerUi({ validatorUrl: null, url: config.url || '/swagger/resources', @@ -28,7 +29,7 @@ $(function() { log(swaggerApi); log(swaggerUi); - if(window.SwaggerTranslator) { + if (window.SwaggerTranslator) { window.SwaggerTranslator.translate(); } @@ -56,8 +57,9 @@ $(function() { return pathCompare !== 0 ? pathCompare : methodOrder.indexOf(a.method) - methodOrder.indexOf(b.method); - } + }, }); + /* eslint-disable camelcase */ $('#explore').click(setAccessToken); $('#api_selector').submit(setAccessToken); @@ -71,9 +73,10 @@ $(function() { e.preventDefault(); var key = $('#input_accessToken')[0].value; log('key: ' + key); - if(key && key.trim() !== '') { + if (key && key.trim() !== '') { log('added accessToken ' + key); - var apiKeyAuth = new SwaggerClient.ApiKeyAuthorization('access_token', key, 'query'); + var apiKeyAuth = + new SwaggerClient.ApiKeyAuthorization('access_token', key, 'query'); window.swaggerUi.api.clientAuthorizations.add('key', apiKeyAuth); accessToken = key; $('.accessTokenDisplay').text('Token Set.').addClass('set'); @@ -83,10 +86,9 @@ $(function() { if (window.localStorage) { window.localStorage.setItem(lsKey, key); } - } - // If submitted with an empty token, remove the current token. Can be - // useful to intentionally remove authorization. - else { + } else { + // If submitted with an empty token, remove the current token. Can be + // useful to intentionally remove authorization. log('removed accessToken.'); $('.accessTokenDisplay').text('Token Not Set.').removeClass('set'); $('.accessTokenDisplay').removeAttr('data-tooltip'); @@ -116,5 +118,3 @@ $(function() { } } }); - - diff --git a/test/explorer.test.js b/test/explorer.test.js index 6751d0a..8e25858 100644 --- a/test/explorer.test.js +++ b/test/explorer.test.js @@ -8,7 +8,6 @@ var urlJoin = require('../lib/url-join'); var os = require('os'); describe('explorer', function() { - describe('with default config', function() { beforeEach(givenLoopBackAppWithExplorer()); @@ -117,7 +116,7 @@ describe('explorer', function() { beforeEach(function setupExplorerWithUiDirs() { app = loopback(); explorer(app, { - uiDirs: [path.resolve(__dirname, 'fixtures', 'dummy-swagger-ui')] + uiDirs: [path.resolve(__dirname, 'fixtures', 'dummy-swagger-ui')], }); }); @@ -138,6 +137,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() { @@ -167,7 +199,7 @@ describe('explorer', function() { it('should allow `uiDirs` to be defined as an Array', function(done) { explorer(app, { - uiDirs: [path.resolve(__dirname, 'fixtures', 'dummy-swagger-ui')] + uiDirs: [path.resolve(__dirname, 'fixtures', 'dummy-swagger-ui')], }); request(app).get('/explorer/') @@ -179,7 +211,7 @@ describe('explorer', function() { it('should allow `uiDirs` to be defined as an String', function(done) { explorer(app, { - uiDirs: path.resolve(__dirname, 'fixtures', 'dummy-swagger-ui') + uiDirs: path.resolve(__dirname, 'fixtures', 'dummy-swagger-ui'), }); request(app).get('/explorer/') @@ -205,7 +237,7 @@ describe('explorer', function() { it('can be disabled by configuration', function(done) { var app = loopback(); - app.set('remoting', { cors: { origin: false } }); + app.set('remoting', { cors: { origin: false }}); configureRestApiAndExplorer(app, '/explorer'); request(app) @@ -220,6 +252,68 @@ describe('explorer', function() { }); }); + it('updates swagger object when a new model is added', 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) { + if (err) return done(err); + + // Create a new model + var Model = loopback.PersistedModel.extend('Customer'); + Model.attachTo(loopback.memory()); + app.model(Model); + + // Request swagger.json again + request(app) + .get('/explorer/swagger.json') + .expect(200) + .end(function(err, res) { + if (err) return done(err); + var modelNames = Object.keys(res.body.definitions); + expect(modelNames).to.contain('Customer'); + var paths = Object.keys(res.body.paths); + expect(paths).to.have.contain('/Customers'); + done(); + }); + }); + }); + + 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();