From 144fc8000de5c37f306f2c80621e518766797229 Mon Sep 17 00:00:00 2001 From: Amir Jafarian Date: Fri, 22 Apr 2016 17:07:56 -0400 Subject: [PATCH 1/3] Add eslint infrastructure --- .eslintignore | 0 .eslintrc | 10 ++++++++++ .jshintrc | 13 ------------- package.json | 6 ++++-- 4 files changed, 14 insertions(+), 15 deletions(-) create mode 100644 .eslintignore create mode 100644 .eslintrc delete mode 100644 .jshintrc 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..4ed9690 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,10 @@ +{ + "extends": "loopback", + "rules": { + "max-len": ["error", 80, 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/package.json b/package.json index 71ebdd5..1cfdd22 100644 --- a/package.json +++ b/package.json @@ -21,10 +21,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": { From 2987655479686b5f1c9d09331a63bfed7770d23a Mon Sep 17 00:00:00 2001 From: Amir Jafarian Date: Fri, 22 Apr 2016 17:09:48 -0400 Subject: [PATCH 2/3] Auto-update by eslint --fix --- example/hidden.js | 4 +- example/simple.js | 6 +-- index.js | 82 ++++++++++++++++++------------------- public/lib/loadSwaggerUI.js | 8 ++-- test/explorer.test.js | 10 ++--- 5 files changed, 55 insertions(+), 55 deletions(-) diff --git a/example/hidden.js b/example/hidden.js index b152d93..16e0178 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'; -app.use('/explorer', explorer(app, {basePath: apiPath})); +app.use('/explorer', 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 70937cd..0ad339c 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'; -app.use('/explorer', explorer(app, {basePath: apiPath})); +app.use('/explorer', 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 23b130a..c85c5cd 100644 --- a/index.js +++ b/index.js @@ -2,17 +2,17 @@ /*! * Adds dynamically-updated docs as /explorer */ -var url = require('url'); -var path = require('path'); -var urlJoin = require('./lib/url-join'); -var _defaults = require('lodash').defaults; -var cors = require('cors'); -var createSwaggerObject = require('loopback-swagger').generateSwaggerSpec; -var SWAGGER_UI_ROOT = require('strong-swagger-ui/index').dist; -var STATIC_ROOT = path.join(__dirname, 'public'); + var url = require('url'); + var path = require('path'); + var urlJoin = require('./lib/url-join'); + var _defaults = require('lodash').defaults; + var cors = require('cors'); + var createSwaggerObject = require('loopback-swagger').generateSwaggerSpec; + var SWAGGER_UI_ROOT = require('strong-swagger-ui/index').dist; + var STATIC_ROOT = path.join(__dirname, 'public'); -module.exports = explorer; -explorer.routes = routes; + module.exports = explorer; + explorer.routes = routes; /** * Example usage: @@ -21,34 +21,34 @@ explorer.routes = routes; * explorer(app, options); */ -function explorer(loopbackApplication, options) { - options = _defaults({}, options, { mountPath: '/explorer' }); - loopbackApplication.use(options.mountPath, routes(loopbackApplication, options)); - loopbackApplication.set('loopback-component-explorer', options); -} + function explorer(loopbackApplication, options) { + options = _defaults({}, options, { mountPath: '/explorer' }); + loopbackApplication.use(options.mountPath, routes(loopbackApplication, options)); + loopbackApplication.set('loopback-component-explorer', options); + } -function routes(loopbackApplication, options) { - var loopback = loopbackApplication.loopback; - var loopbackMajor = loopback && loopback.version && + function routes(loopbackApplication, options) { + var loopback = loopbackApplication.loopback; + var loopbackMajor = loopback && loopback.version && loopback.version.split('.')[0] || 1; - if (loopbackMajor < 2) { + if (loopbackMajor < 2) { throw new Error('loopback-component-explorer requires loopback 2.0 or newer'); } - options = _defaults({}, options, { + options = _defaults({}, options, { resourcePath: 'swagger.json', apiInfo: loopbackApplication.get('apiInfo') || {}, - swaggerUI: true + swaggerUI: true, }); - var router = new loopback.Router(); + var router = new loopback.Router(); - mountSwagger(loopbackApplication, router, options); + mountSwagger(loopbackApplication, router, options); // config.json is loaded by swagger-ui. The server should respond // with the relative URI of the resource doc. - router.get('/config.json', function(req, res) { + router.get('/config.json', function(req, res) { // Get the path we're mounted at. It's best to get this from the referer // in case we're proxied at a deep path. var source = url.parse(req.headers.referer || '').pathname; @@ -57,11 +57,11 @@ function routes(loopbackApplication, options) { source = req.originalUrl.replace(/\/config.json(\?.*)?$/, ''); } res.send({ - url: urlJoin(source, '/' + options.resourcePath) + url: urlJoin(source, '/' + options.resourcePath), }); }); - if (options.swaggerUI) { + 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 @@ -83,8 +83,8 @@ function routes(loopbackApplication, options) { router.use(loopback.static(SWAGGER_UI_ROOT)); } - return router; -} + return router; + } /** * Setup Swagger documentation on the given express app. @@ -95,30 +95,30 @@ function routes(loopbackApplication, options) { * swagger documentation. * @param {Object} opts Options. */ -function mountSwagger(loopbackApplication, swaggerApp, opts) { - var swaggerObject = createSwaggerObject(loopbackApplication, opts); + 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() { + loopbackApplication.on('modelRemoted', function() { swaggerObject = createSwaggerObject(loopbackApplication, opts); }); - var resourcePath = opts && opts.resourcePath || 'swagger.json'; - if (resourcePath[0] !== '/') resourcePath = '/' + resourcePath; + var resourcePath = opts && opts.resourcePath || 'swagger.json'; + if (resourcePath[0] !== '/') resourcePath = '/' + resourcePath; - var remotes = loopbackApplication.remotes(); - setupCors(swaggerApp, remotes); + var remotes = loopbackApplication.remotes(); + setupCors(swaggerApp, remotes); - swaggerApp.get(resourcePath, function sendSwaggerObject(req, res) { + swaggerApp.get(resourcePath, function sendSwaggerObject(req, res) { res.status(200).send(swaggerObject); }); -} + } -function setupCors(swaggerApp, remotes) { - var corsOptions = remotes.options && remotes.options.cors || + function setupCors(swaggerApp, remotes) { + var corsOptions = remotes.options && remotes.options.cors || { origin: true, credentials: true }; // TODO(bajtos) Skip CORS when remotes.options.cors === false - swaggerApp.use(cors(corsOptions)); -} + swaggerApp.use(cors(corsOptions)); + } diff --git a/public/lib/loadSwaggerUI.js b/public/lib/loadSwaggerUI.js index 2a1a466..b1e34b1 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(); } @@ -28,7 +28,7 @@ $(function() { log(swaggerApi); log(swaggerUi); - if(window.SwaggerTranslator) { + if (window.SwaggerTranslator) { window.SwaggerTranslator.translate(); } @@ -56,7 +56,7 @@ $(function() { return pathCompare !== 0 ? pathCompare : methodOrder.indexOf(a.method) - methodOrder.indexOf(b.method); - } + }, }); $('#explore').click(setAccessToken); @@ -71,7 +71,7 @@ $(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'); window.swaggerUi.api.clientAuthorizations.add('key', apiKeyAuth); diff --git a/test/explorer.test.js b/test/explorer.test.js index c78392c..55de73f 100644 --- a/test/explorer.test.js +++ b/test/explorer.test.js @@ -117,7 +117,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')], }); }); @@ -143,7 +143,7 @@ describe('explorer', function() { beforeEach(function setupExplorerWithoutUI() { app = loopback(); explorer(app, { - swaggerUI: false + swaggerUI: false, }); }); @@ -200,7 +200,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/') @@ -212,7 +212,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/') @@ -238,7 +238,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) From c923710ce2a84cadb7bd2f153f6258b2d7101ddb Mon Sep 17 00:00:00 2001 From: Amir Jafarian Date: Fri, 22 Apr 2016 17:14:57 -0400 Subject: [PATCH 3/3] Fix linting errors --- .eslintrc | 2 +- index.js | 56 ++++++++++++++++++------------------- package.json | 4 ++- public/lib/loadSwaggerUI.js | 14 +++++----- test/explorer.test.js | 1 - 5 files changed, 39 insertions(+), 38 deletions(-) diff --git a/.eslintrc b/.eslintrc index 4ed9690..a5503ea 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,7 +1,7 @@ { "extends": "loopback", "rules": { - "max-len": ["error", 80, 4, { + "max-len": ["error", 90, 4, { "ignoreComments": true, "ignoreUrls": true, "ignorePattern": "^\\s*var\\s.+=\\s*(require\\s*\\()|(/)" diff --git a/index.js b/index.js index c85c5cd..0b65b91 100644 --- a/index.js +++ b/index.js @@ -33,14 +33,14 @@ loopback.version.split('.')[0] || 1; if (loopbackMajor < 2) { - throw new Error('loopback-component-explorer requires loopback 2.0 or newer'); - } + throw new Error('loopback-component-explorer requires loopback 2.0 or newer'); + } options = _defaults({}, options, { - resourcePath: 'swagger.json', - apiInfo: loopbackApplication.get('apiInfo') || {}, - swaggerUI: true, - }); + resourcePath: 'swagger.json', + apiInfo: loopbackApplication.get('apiInfo') || {}, + swaggerUI: true, + }); var router = new loopback.Router(); @@ -51,37 +51,37 @@ router.get('/config.json', function(req, res) { // Get the path we're mounted at. It's best to get this from the referer // in case we're proxied at a deep path. - var source = url.parse(req.headers.referer || '').pathname; + var source = url.parse(req.headers.referer || '').pathname; // If no referer is available, use the incoming url. - if (!source) { - source = req.originalUrl.replace(/\/config.json(\?.*)?$/, ''); - } - res.send({ - url: urlJoin(source, '/' + options.resourcePath), + if (!source) { + source = req.originalUrl.replace(/\/config.json(\?.*)?$/, ''); + } + res.send({ + url: urlJoin(source, '/' + options.resourcePath), + }); }); - }); 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)); - }); + 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)); + router.use(loopback.static(STATIC_ROOT)); // Swagger UI distribution - router.use(loopback.static(SWAGGER_UI_ROOT)); - } + router.use(loopback.static(SWAGGER_UI_ROOT)); + } return router; } @@ -101,8 +101,8 @@ // 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); - }); + swaggerObject = createSwaggerObject(loopbackApplication, opts); + }); var resourcePath = opts && opts.resourcePath || 'swagger.json'; if (resourcePath[0] !== '/') resourcePath = '/' + resourcePath; @@ -111,8 +111,8 @@ setupCors(swaggerApp, remotes); swaggerApp.get(resourcePath, function sendSwaggerObject(req, res) { - res.status(200).send(swaggerObject); - }); + res.status(200).send(swaggerObject); + }); } function setupCors(swaggerApp, remotes) { diff --git a/package.json b/package.json index 1cfdd22..60342a1 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,9 @@ "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", diff --git a/public/lib/loadSwaggerUI.js b/public/lib/loadSwaggerUI.js index b1e34b1..aaee90b 100644 --- a/public/lib/loadSwaggerUI.js +++ b/public/lib/loadSwaggerUI.js @@ -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', @@ -58,6 +59,7 @@ $(function() { methodOrder.indexOf(a.method) - methodOrder.indexOf(b.method); }, }); + /* eslint-disable camelcase */ $('#explore').click(setAccessToken); $('#api_selector').submit(setAccessToken); @@ -73,7 +75,8 @@ $(function() { log('key: ' + key); 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 55de73f..36261e6 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());