Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Url parameter interpolation feature #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ webHooks.add('shortname2', 'http://127.0.0.1:9000/prova2/').then(function(){
console.log(err)
});

// add a webHook with url parameter interpolation
webHooks.add('shortname3', 'http://127.0.0.1:9000/:id?search=:query').then(function(){
// done
}).catch(function(err){
console.log(err)
});

// remove a single url attached to the given shortname
// webHooks.remove('shortname3', 'http://127.0.0.1:9000/query/').catch(function(err){console.error(err);})

Expand All @@ -62,6 +69,9 @@ webHooks.add('shortname2', 'http://127.0.0.1:9000/prova2/').then(function(){
webHooks.trigger('shortname1', {data: 123})
webHooks.trigger('shortname2', {data: 123456}, {header: 'header'}) // payload will be sent as POST request with JSON body (Content-Type: application/json) and custom header

// trigger a webhook with url parameter interpolation
webHooks.trigger('shortname3', {data: 123456}, {header: 'header'}, {id: 123, query: 'xyz'})

```

## Available events
Expand Down
18 changes: 13 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,25 @@ function _setListeners (self) {
// console.log(_functions[0] == _functions[2]);
}

function _interpolate (url, params) {
return _.reduce(params, function (currentUrl, value, key) {
return currentUrl.replace(new RegExp(':' + key, 'g'), value)
}, url)
}

function _getRequestFunction (self, url) {
// return the function then called by the event listener.
var func = function (shortname, jsonData, headersData) { // argument required when eventEmitter.emit()
var func = function (shortname, jsonData, headersData, paramsData) { // argument required when eventEmitter.emit()
var obj = {'Content-Type': 'application/json'}
var headers = headersData ? _.merge(obj, headersData) : obj
var params = paramsData || {}
var resolvedUrl = _interpolate(url, params)

debug('POST request to:', url)
debug('POST request to:', resolvedUrl)
// POST request to the instantiated URL with custom headers if provided
request({
method: 'POST',
uri: url,
uri: resolvedUrl,
strictSSL: false,
headers: headers,
body: JSON.stringify(jsonData)
Expand All @@ -141,9 +149,9 @@ function _getRequestFunction (self, url) {

// 'prototype' has improved performances, let's declare the methods

WebHooks.prototype.trigger = function (shortname, jsonData, headersData) {
WebHooks.prototype.trigger = function (shortname, jsonData, headersData, paramsData) {
// trigger a webHook
this.emitter.emit(shortname, shortname, jsonData, headersData)
this.emitter.emit(shortname, shortname, jsonData, headersData, paramsData)
}

WebHooks.prototype.add = function (shortname, url) { // url is required
Expand Down
21 changes: 19 additions & 2 deletions test/webhooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,9 @@ function deleteHook1 (done) {

describe('Tests >', function () {
before(function (done) {
// Lets start our server
// Lets start our server
server.listen(PORT, function () {
// Callback triggered when server is successfully listening. Hurray!
// Callback triggered when server is successfully listening. Hurray!
debug('Server listening on: http://localhost:%s', PORT)
done()
})
Expand Down Expand Up @@ -325,6 +325,23 @@ describe('Tests >', function () {
}, 1000)
})

context('with url parameter interpolation', function () {
before(() => {
return webHooks.add('hook1', URI + '/:id/aaa')
})
it('should fire the webHook with url parameters', function (done) {
this.timeout(3000)
OUTCOMES = {}
webHooks.trigger('hook1', {}, {}, {
id: '123'
})
setTimeout(function () {
should.exist(OUTCOMES['/123/aaa'])
done()
}, 1000)
})
})

it('should fire the webHook with both custom body and headers', function (done) {
this.timeout(3000)
OUTCOMES = {}
Expand Down