Skip to content
This repository has been archived by the owner on Jul 6, 2022. It is now read-only.

Commit

Permalink
Merge pull request #11 from campsi/feature/Standard_Indent
Browse files Browse the repository at this point in the history
Standard linter
  • Loading branch information
ChristopheBraud committed Mar 2, 2018
2 parents d891105 + 2c03fc4 commit ddc7238
Show file tree
Hide file tree
Showing 12 changed files with 208 additions and 223 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
30 changes: 15 additions & 15 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
}
}
8 changes: 4 additions & 4 deletions lib/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
56 changes: 28 additions & 28 deletions lib/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
};
45 changes: 20 additions & 25 deletions lib/handlers.js
Original file line number Diff line number Diff line change
@@ -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));
};
43 changes: 21 additions & 22 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 !
**/
}
}
};
20 changes: 10 additions & 10 deletions lib/param.js
Original file line number Diff line number Diff line change
@@ -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();
};
};
20 changes: 10 additions & 10 deletions lib/service.js
Original file line number Diff line number Diff line change
@@ -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
};
15 changes: 10 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading

0 comments on commit ddc7238

Please sign in to comment.