From 2c03fc4ed4c63d2c3504e3bf2d830d7ed4a1f13e Mon Sep 17 00:00:00 2001 From: Christophe Braud Date: Fri, 2 Mar 2018 17:24:00 +0100 Subject: [PATCH] update config, new standard rules, fix all lint issues --- .editorconfig | 9 +++++ .eslintrc | 30 +++++++-------- lib/controller.js | 8 ++-- lib/entity.js | 56 +++++++++++++-------------- lib/handlers.js | 45 ++++++++++------------ lib/index.js | 43 ++++++++++----------- lib/param.js | 20 +++++----- lib/service.js | 20 +++++----- package.json | 15 +++++--- test/config/test.js | 69 ++++++++++++++++----------------- test/index.js | 22 +++++------ test/webhooks.js | 94 +++++++++++++++++---------------------------- 12 files changed, 208 insertions(+), 223 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..53b061a --- /dev/null +++ b/.editorconfig @@ -0,0 +1,9 @@ +root = true + +[*] +indent_style = space +indent_size = 2 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true \ No newline at end of file diff --git a/.eslintrc b/.eslintrc index f82bf9a..07a9b32 100644 --- a/.eslintrc +++ b/.eslintrc @@ -7,20 +7,20 @@ "plugins": [ "indexof" ], - "extends": "eslint:recommended", // Recommended rules + "extends": [ + "eslint:recommended", + "standard" + ], "rules": { - "quotes": [2, "single"], // single quotes are better - "semi": [2, "always"], - "max-len": [2, 120], - "max-lines": [2, 400], - "max-statements": [2, 20], - "no-tabs": [2], - "linebreak-style": [2, "unix"], - "no-multiple-empty-lines": [2, {"max": 1, "maxEOF": 0, "maxBOF": 0}], - "eol-last": [2, "always"], - "no-unsafe-negation": [2], - "eqeqeq": [2, "smart"], - "strict": [2, "never"], - "indexof/no-indexof": [2] + // Exception + "semi": ["error", "always"], + // Format xtras + "max-len": ["error", 120], + "max-lines": ["error", 400], + "max-statements": ["error", 20], + "linebreak-style": ["error", "unix"], + // Other xtras + "strict": ["error", "never"], + "indexof/no-indexof": ["error"] } -} \ No newline at end of file +} diff --git a/lib/controller.js b/lib/controller.js index 0535890..df02291 100644 --- a/lib/controller.js +++ b/lib/controller.js @@ -2,8 +2,8 @@ const WebHookEntity = require('./entity'); let webHooks = {}; -module.exports.launchWebHook = function(service, id, topic, callback) { - if(!webHooks[id]) { - webHooks[id] = new WebHookEntity(service, id, topic, callback); - } +module.exports.launchWebHook = function (service, id, topic, callback) { + if (!webHooks[id]) { + webHooks[id] = new WebHookEntity(service, id, topic, callback); + } }; diff --git a/lib/entity.js b/lib/entity.js index 9137c24..9b3592d 100644 --- a/lib/entity.js +++ b/lib/entity.js @@ -3,37 +3,37 @@ const http = require('http'); const url = require('url'); module.exports = class WebHookEntity { - constructor(service, id, topic, callback) { - this.enable = false; - this.service = service; - this.id = id; - this.topic = topic; - this.callback = callback; - this.init(); - } + constructor (service, id, topic, callback) { + this.enable = false; + this.service = service; + this.id = id; + this.topic = topic; + this.callback = callback; + this.init(); + } - init() { - this.options = {}; - if(this.callback.url) { - let parsedUrl = url.parse(this.callback.url); - this.options.protocol = parsedUrl.protocol; - this.options.host = parsedUrl.hostname; - this.options.path = parsedUrl.path; - this.options.method = this.callback.method || 'GET'; - this.options.port = parsedUrl.port || 80; - this.service.server.on(this.topic, this.send.bind(this)); - } + init () { + this.options = {}; + if (this.callback.url) { + let parsedUrl = url.parse(this.callback.url); + this.options.protocol = parsedUrl.protocol; + this.options.host = parsedUrl.hostname; + this.options.path = parsedUrl.path; + this.options.method = this.callback.method || 'GET'; + this.options.port = parsedUrl.port || 80; + this.service.server.on(this.topic, this.send.bind(this)); } + } - send(message) { - const req = http.request(this.options); + send (message) { + const req = http.request(this.options); - req.on('error', (e) => { - debug(`problem with request: ${e.message}`); - }); - if(message !== undefined) { - req.write(JSON.stringify(message)); - } - req.end(); + req.on('error', (e) => { + debug(`problem with request: ${e.message}`); + }); + if (message !== undefined) { + req.write(JSON.stringify(message)); } + req.end(); + } }; diff --git a/lib/handlers.js b/lib/handlers.js index 4658e64..850ed49 100644 --- a/lib/handlers.js +++ b/lib/handlers.js @@ -1,37 +1,32 @@ const serviceWebHook = require('./service'); const helpers = require('campsi/lib/modules/responseHelpers'); -module.exports.getWebHooks = function(req, res) { - - serviceWebHook.getWebHooks(req.service, req.user) - .then((data) => helpers.json(res, data)) - .catch(() => helpers.error(res)); +module.exports.getWebHooks = function (req, res) { + serviceWebHook.getWebHooks(req.service, req.user) + .then((data) => helpers.json(res, data)) + .catch(() => helpers.error(res)); }; -module.exports.postWebHook = function(req, res) { - - serviceWebHook.createWebHook(req.service, req.user) - .then((data) => helpers.json(res, data)) - .catch(() => helpers.error(res)); +module.exports.postWebHook = function (req, res) { + serviceWebHook.createWebHook(req.service, req.user) + .then((data) => helpers.json(res, data)) + .catch(() => helpers.error(res)); }; -module.exports.putWebHook = function(req, res) { - - serviceWebHook.updateWebHook(req.service, req.user) - .then((data) => helpers.json(res, data)) - .catch(() => helpers.error(res)); +module.exports.putWebHook = function (req, res) { + serviceWebHook.updateWebHook(req.service, req.user) + .then((data) => helpers.json(res, data)) + .catch(() => helpers.error(res)); }; -module.exports.getWebHook = function(req, res) { - - serviceWebHook.getWebHook(req.service, req.user) - .then((data) => helpers.json(res, data)) - .catch(() => helpers.error(res)); +module.exports.getWebHook = function (req, res) { + serviceWebHook.getWebHook(req.service, req.user) + .then((data) => helpers.json(res, data)) + .catch(() => helpers.error(res)); }; -module.exports.deleteWebHook = function(req, res) { - - serviceWebHook.deleteWebHook(req.service, req.user) - .then((data) => helpers.json(res, data)) - .catch(() => helpers.error(res)); +module.exports.deleteWebHook = function (req, res) { + serviceWebHook.deleteWebHook(req.service, req.user) + .then((data) => helpers.json(res, data)) + .catch(() => helpers.error(res)); }; diff --git a/lib/index.js b/lib/index.js index 6d1b554..93418b6 100644 --- a/lib/index.js +++ b/lib/index.js @@ -8,32 +8,31 @@ const webHookController = require('./controller'); format.extend(String.prototype); module.exports = class WebhooksService extends CampsiService { + initialize () { + this.install(); - initialize() { - this.install(); + this.collection = this.db.collection('webhooks.{0}'.format(this.path)); + this.router.use((req, res, next) => { + req.service = this; + next(); + }); + this.router.param('webhook', param.attachWebHook); + this.router.get('/', handlers.getWebHooks); + this.router.post('/', handlers.postWebHook); + this.router.put('/:webhook', handlers.putWebHook); + this.router.get('/:webhook', handlers.getWebHook); + this.router.delete('/:webhook', handlers.deleteWebHook); + return super.initialize(); + } - this.collection = this.db.collection('webhooks.{0}'.format(this.path)); - this.router.use((req, res, next) => { - req.service = this; - next(); - }); - this.router.param('webhook', param.attachWebHook); - this.router.get('/', handlers.getWebHooks); - this.router.post('/', handlers.postWebHook); - this.router.put('/:webhook', handlers.putWebHook); - this.router.get('/:webhook', handlers.getWebHook); - this.router.delete('/:webhook', handlers.deleteWebHook); - return super.initialize(); - } - - install() { - forIn(this.options.hooks, (hook, id) => { - webHookController.launchWebHook(this, id, hook.topic, hook.callback); - }); - /** TODO v2 + install () { + forIn(this.options.hooks, (hook, id) => { + webHookController.launchWebHook(this, id, hook.topic, hook.callback); + }); + /** TODO v2 * delete database if asked by config * update database based on config * lauch every hooks based on db ! **/ - } + } }; diff --git a/lib/param.js b/lib/param.js index 7d5cbe5..643ffa1 100644 --- a/lib/param.js +++ b/lib/param.js @@ -1,15 +1,15 @@ const createObjectID = require('campsi/lib/modules/createObjectID'); const helpers = require('campsi/lib/modules/responseHelpers'); -module.exports.attachWebHook = function() { - return (req, res, next) => { - if (req.params.webhook) { - req.filter = {_id: createObjectID(req.params.webhook)}; - if(!req.filter._id){ - return helpers.error(res, {message: 'Can\'t recognize webhook id'}); - } - } +module.exports.attachWebHook = function () { + return (req, res, next) => { + if (req.params.webhook) { + req.filter = {_id: createObjectID(req.params.webhook)}; + if (!req.filter._id) { + return helpers.error(res, {message: 'Can\'t recognize webhook id'}); + } + } - next(); - }; + next(); + }; }; diff --git a/lib/service.js b/lib/service.js index 116757e..f80b9b7 100644 --- a/lib/service.js +++ b/lib/service.js @@ -1,19 +1,19 @@ -module.exports.getWebHooks = function() { - // TODO get all webhooks from db + paginate +module.exports.getWebHooks = function () { + // TODO get all webhooks from db + paginate }; -module.exports.createWebHook = function() { - // TODO insert a webhook in db + subscription +module.exports.createWebHook = function () { + // TODO insert a webhook in db + subscription }; -module.exports.updateWebHook = function() { - // TODO update db + subscription +module.exports.updateWebHook = function () { + // TODO update db + subscription }; -module.exports.getWebHook = function() { - // TODO get webhook informations +module.exports.getWebHook = function () { + // TODO get webhook informations }; -module.exports.deleteWebHook = function() { - // TODO unsubscribe + delete from db +module.exports.deleteWebHook = function () { + // TODO unsubscribe + delete from db }; diff --git a/package.json b/package.json index ba25d8f..fb84736 100644 --- a/package.json +++ b/package.json @@ -10,19 +10,24 @@ "dependencies": { "debug": "^3.1.0", "for-in": "^1.0.2", - "mongodb": "^3.0.1", + "mongodb": "^3.0.3", "string-format": "^0.5.0" }, "devDependencies": { - "campsi": "^1.0.2", + "campsi": "^1.0.5", "campsi-service-trace": "^1.0.1", "chai": "^4.1.2", "chai-http": "^3.0.0", - "config": "^1.28.0", + "config": "^1.30.0", "coveralls": "^3.0.0", - "depcheck": "^0.6.7", - "eslint": "^4.11.0", + "depcheck": "^0.6.9", + "eslint": "^4.18.1", + "eslint-config-standard": "^11.0.0", + "eslint-plugin-import": "^2.9.0", "eslint-plugin-indexof": "^0.1.1", + "eslint-plugin-node": "^6.0.1", + "eslint-plugin-promise": "^3.6.0", + "eslint-plugin-standard": "^3.0.1", "istanbul": "^0.4.5", "mocha": "^5.0.0", "mocha-lcov-reporter": "^1.3.0", diff --git a/test/config/test.js b/test/config/test.js index d35a04e..27e3500 100644 --- a/test/config/test.js +++ b/test/config/test.js @@ -1,43 +1,42 @@ const host = 'http://localhost:3000'; module.exports = { - port: 3000, - host: host, - title: 'Test Assets', - campsi: { - mongo: { - 'host': 'localhost', - 'port': 27017, - 'database': 'relationships' - } + port: 3000, + campsi: { + publicURL: host, + mongo: { + 'host': 'localhost', + 'port': 27017, + 'database': 'relationships' + } + }, + services: { + trace: { + title: 'trace' }, - services: { - trace: { - title: 'trace', - }, - webHooks: { - title: 'WebHooks', - options: { - preserveHooks: false, - hooks: { - wh1: { - topic: 'webhooks/test/topic', - callback: { - url: 'http://localhost:3000/trace/my-wh1' - } - }, - wh2: { - topic: 'webhooks/test/topic/other', - callback: { - method: 'POST', - url: 'http://localhost:3000/trace/my-wh2?foo=bar', - headers: { - 'x-foo': 'bar' - } - } - } - } + webHooks: { + title: 'WebHooks', + options: { + preserveHooks: false, + hooks: { + wh1: { + topic: 'webhooks/test/topic', + callback: { + url: 'http://localhost:3000/trace/my-wh1' + } + }, + wh2: { + topic: 'webhooks/test/topic/other', + callback: { + method: 'POST', + url: 'http://localhost:3000/trace/my-wh2?foo=bar', + headers: { + 'x-foo': 'bar' + } } + } } + } } + } }; diff --git a/test/index.js b/test/index.js index 6b37066..e0e18b4 100644 --- a/test/index.js +++ b/test/index.js @@ -6,8 +6,8 @@ const config = require('config'); const debug = require('debug')('campsi:test'); const services = { - Trace: require('campsi-service-trace'), - WebHooks: require('../lib/index'), + Trace: require('campsi-service-trace'), + WebHooks: require('../lib/index') }; let campsi = new CampsiServer(config.campsi); @@ -16,21 +16,21 @@ campsi.mount('trace', new services.Trace(config.services.trace)); campsi.mount('webhooks', new services.WebHooks(config.services.webHooks)); campsi.on('campsi/ready', () => { - debug('ready'); - campsi.listen(config.port); + debug('ready'); + campsi.listen(config.port); }); process.on('uncaughtException', function () { - debug('uncaughtException'); + debug('uncaughtException'); }); process.on('unhandledRejection', (reason) => { - debug('unhandledRejection'); - debug(reason); - process.exit(1); + debug('unhandledRejection'); + debug(reason); + process.exit(1); }); campsi.start() - .catch((error) => { - debug(error); - }); + .catch((error) => { + debug(error); + }); diff --git a/test/webhooks.js b/test/webhooks.js index bf2a57c..0058c3d 100644 --- a/test/webhooks.js +++ b/test/webhooks.js @@ -16,72 +16,50 @@ chai.use(chaiHttp); chai.should(); const services = { - Trace: require('campsi-service-trace'), - WebHooks: require('../lib'), + Trace: require('campsi-service-trace'), + WebHooks: require('../lib') }; describe('Assets API', () => { - beforeEach((done) => { - const mongoUri = mongoUriBuilder(config.campsi.mongo); - MongoClient.connect(mongoUri, (err, client) => { - let db = client.db(config.campsi.mongo.database); - db.dropDatabase(() => { - client.close(); - campsi = new CampsiServer(config.campsi); - campsi.mount('trace', new services.Trace(config.services.trace)); - campsi.mount('webhooks', new services.WebHooks(config.services.webHooks)); + beforeEach((done) => { + const mongoUri = mongoUriBuilder(config.campsi.mongo); + MongoClient.connect(mongoUri, (err, client) => { + if (err) throw err; + let db = client.db(config.campsi.mongo.database); + db.dropDatabase(() => { + client.close(); + campsi = new CampsiServer(config.campsi); + campsi.mount('trace', new services.Trace(config.services.trace)); + campsi.mount('webhooks', new services.WebHooks(config.services.webHooks)); - campsi.on('campsi/ready', () => { - server = campsi.listen(config.port); - done(); - }); - - campsi.start() - .catch((err) => { - debug('Error: %s', err); - }); - }); + campsi.on('campsi/ready', () => { + server = campsi.listen(config.port); + done(); }); - }); - afterEach((done) => { - server.close(); - done(); + campsi.start() + .catch((err) => { + debug('Error: %s', err); + }); + }); }); - /* + }); + + afterEach((done) => { + server.close(); + done(); + }); + /* * Test a simple webhook */ - describe('Basic webhook', () => { - it('it should call a trace event when emiting a hooked event.', (done) => { - campsi.on('trace/request', (payload) => { - payload.should.be.a('object'); - payload.method.should.be.eq('GET'); - done(); - }); - campsi.emit('webhooks/test/topic'); - }); + describe('Basic webhook', () => { + it('it should call a trace event when emiting a hooked event.', (done) => { + campsi.on('trace/request', (payload) => { + payload.should.be.a('object'); + payload.method.should.be.eq('GET'); + done(); + }); + campsi.emit('webhooks/test/topic'); }); - /* - * Test the /GET / route - */ - /*describe('/GET /', () => { - it('it should return a list of assets', (done) => { - createWebhooks(Array(5).fill('./test/rsrc/logo_agilitation.png')) - .then(() => { - chai.request(campsi.app) - .get('/webhooks/') - .query({page: 3, perPage: 2}) - .end((err, res) => { - res.should.have.status(200); - res.should.have.header('x-total-count', '5'); - res.should.have.header('link'); - res.should.be.json; - res.body.should.be.an('array'); - res.body.length.should.be.eq(1); - done(); - }); - }); - - }); - });*/ + }); });