node-github-webhook
Advanced tools
Comparing version 1.0.0 to 1.0.1
51
index.js
@@ -20,16 +20,2 @@ /** | ||
function create(options) { | ||
function checkType(options) { | ||
if (!isObject(options)) | ||
throw new TypeError('must provide an options object') | ||
if (typeof options.path !== 'string') | ||
throw new TypeError('must provide a \'path\' option') | ||
if (typeof options.secret !== 'string') | ||
throw new TypeError('must provide a \'secret\' option') | ||
if (Array.isArray(options.events) && options.events.indexOf('*') === -1) | ||
throw new TypeError('must provide an events array') | ||
} | ||
function findHandler(url, arr) { | ||
@@ -44,2 +30,24 @@ var ret = arr[0] | ||
if (Array.isArray(options)) { | ||
options = findHandler(req.url, options) | ||
} | ||
if (!isObject(options)) | ||
throw new TypeError('must provide an options object') | ||
if (typeof options.path !== 'string') | ||
throw new TypeError('must provide a \'path\' option') | ||
if (typeof options.secret !== 'string') | ||
throw new TypeError('must provide a \'secret\' option') | ||
if (Array.isArray(options.events) && options.events.indexOf('*') === -1) | ||
throw new TypeError('must provide an events array') | ||
// make it an EventEmitter, sort of | ||
handler.__proto__ = EventEmitter.prototype | ||
EventEmitter.call(handler) | ||
return handler | ||
function handler(req, res, callback) { | ||
@@ -100,3 +108,4 @@ function hasError(msg) { | ||
host: req.headers['host'], | ||
url: req.url | ||
url: req.url, | ||
path: options.path | ||
} | ||
@@ -108,16 +117,4 @@ | ||
} | ||
if (Array.isArray(options)) { | ||
options = findHandler(req.url, options) | ||
} | ||
checkType(options) | ||
// make it an EventEmitter, sort of | ||
handler.__proto__ = EventEmitter.prototype | ||
EventEmitter.call(handler) | ||
return handler | ||
} | ||
module.exports = create |
{ | ||
"name": "node-github-webhook", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Github Webhooks handler based on Node.js. Support multiple handlers.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# node-github-webhook | ||
Github Webhooks handler based on Node.js. Support multiple handlers. | ||
### Instructions | ||
This library is inspired by [github-webhook-handler](https://github.com/rvagg/github-webhook-handler), and it allows you set multiple handlers for different repositories. | ||
It is a small tool based on Node.js to help you handler all the logic for receiving and verifying webhook requests from GitHub. | ||
If you want to know the events of Github, please see: [events](https://developer.github.com/webhooks/#events). | ||
### Usage | ||
```js | ||
var http = require('http') | ||
var createHandler = require('node-github-webhook') | ||
var handler = createHandler([ // multiple handlers | ||
{ path: '/webhook1', secret: 'secret1' }, | ||
{ path: '/webhook2', secret: 'secret2' } | ||
]) | ||
// var handler = createHandler({ path: '/webhook1', secret: 'secret1' }) // single handler | ||
http.createServer(function (req, res) { | ||
handler(req, res, function (err) { | ||
res.statusCode = 404 | ||
res.end('no such location') | ||
}) | ||
}).listen(7777) | ||
handler.on('error', function (err) { | ||
console.error('Error:', err.message) | ||
}) | ||
handler.on('push', function (event) { | ||
console.log( | ||
'Received a push event for %s to %s', | ||
event.payload.repository.name, | ||
event.payload.ref | ||
) | ||
switch(event.path) { | ||
case '/webhook1': | ||
// do sth about webhook1 | ||
break | ||
case '/webhook2': | ||
// do sth about webhook2 | ||
break | ||
default: | ||
// do sth else or nothing | ||
break | ||
} | ||
}) | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
54
8258
6
89