Skip to content

Commit 57684c8

Browse files
authored
Updated webhook update method to not send id in request (#165)
1 parent ff39f79 commit 57684c8

File tree

3 files changed

+57
-40
lines changed

3 files changed

+57
-40
lines changed

lib/webhooks.js

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var api = 'webhooks'
55

66
module.exports = function(client) {
77
var webhooks = {
8-
all: function(options, callback) {
8+
all: function (options, callback) {
99
var reqOpts = {
1010
uri: api
1111
};
@@ -22,10 +22,10 @@ module.exports = function(client) {
2222

2323
client.get(reqOpts, callback);
2424
},
25-
describe: function(options, callback) {
25+
describe: function (options, callback) {
2626
options = options || {};
2727

28-
if(!options.id) {
28+
if (!options.id) {
2929
callback(new Error('id is required'));
3030
return;
3131
}
@@ -40,13 +40,13 @@ module.exports = function(client) {
4040

4141
client.get(reqOpts, callback);
4242
},
43-
create: function(webhook, callback) {
44-
if(typeof webhook === 'function') {
43+
create: function (webhook, callback) {
44+
if (typeof webhook === 'function') {
4545
callback = webhook;
4646
webhook = null;
4747
}
4848

49-
if(!webhook) {
49+
if (!webhook) {
5050
callback(new Error('webhook object is required'));
5151
return;
5252
}
@@ -57,25 +57,52 @@ module.exports = function(client) {
5757

5858
client.post(options, callback);
5959
},
60-
update: function(webhook, callback) {
61-
if(typeof webhook === 'function') {
60+
update: function (webhook, callback) {
61+
var object, options, id;
62+
63+
if (typeof webhook === 'function') {
6264
callback = webhook;
6365
webhook = null;
6466
}
6567

66-
if(!webhook) {
68+
if (!webhook) {
6769
callback(new Error('webhook object is required'));
6870
return;
6971
}
7072

71-
var object = toApiFormat(webhook)
72-
, options = {
73-
uri: api + '/' + webhook.id
74-
, json: object
75-
};
73+
if (!webhook.id) {
74+
callback(new Error('webhook.id is required'));
75+
return;
76+
}
77+
78+
id = webhook.id;
79+
delete webhook.id;
80+
81+
object = toApiFormat(webhook);
82+
options = {
83+
uri: api + '/' + id
84+
, json: object
85+
};
7686

7787
client.put(options, callback);
7888
},
89+
delete: function (id, callback) {
90+
if (typeof id === 'function') {
91+
callback = id;
92+
id = null;
93+
}
94+
95+
if (!id) {
96+
callback(new Error('id is required'));
97+
return;
98+
}
99+
100+
var options = {
101+
uri: api + '/' + id
102+
};
103+
104+
client.delete(options, callback);
105+
},
79106
validate: function(options, callback) {
80107
options = options || {};
81108

@@ -142,23 +169,5 @@ module.exports = function(client) {
142169
}
143170
};
144171

145-
webhooks['delete'] = function(id, callback) {
146-
if (typeof id === 'function') {
147-
callback = id;
148-
id = null;
149-
}
150-
151-
if (!id) {
152-
callback(new Error('id is required'));
153-
return;
154-
}
155-
156-
var options = {
157-
uri: api + '/' + id
158-
};
159-
160-
client['delete'](options, callback);
161-
};
162-
163172
return webhooks;
164173
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"url": "https://github.com/SparkPost/node-sparkpost"
1717
},
1818
"author": "Message Systems, Inc.",
19-
"license": "Apache 2.0",
19+
"license": "Apache-2.0",
2020
"bugs": {
2121
"url": "https://github.com/SparkPost/node-sparkpost/issues"
2222
},

test/spec/webhooks.spec.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('Webhooks Library', function() {
1313
get: sinon.stub().yields(),
1414
post: sinon.stub().yields(),
1515
put: sinon.stub().yields(),
16-
'delete': sinon.stub().yields()
16+
delete: sinon.stub().yields()
1717
};
1818

1919
webhooks = require('../../lib/webhooks')(client);
@@ -124,28 +124,36 @@ describe('Webhooks Library', function() {
124124
done();
125125
});
126126
});
127+
128+
it('should throw an error if webhook.id is missing', function(done) {
129+
webhooks.update({}, function (err) {
130+
expect(err.message).to.equal('webhook.id is required');
131+
expect(client.post).not.to.have.been.called;
132+
done();
133+
});
134+
});
127135
});
128136

129137
describe('delete Method', function() {
130138
it('should call client delete method with the appropriate uri', function(done) {
131-
webhooks['delete']('test', function(err, data) {
132-
expect(client['delete'].firstCall.args[0].uri).to.equal('webhooks/test');
139+
webhooks.delete('test', function(err, data) {
140+
expect(client.delete.firstCall.args[0].uri).to.equal('webhooks/test');
133141
done();
134142
});
135143
});
136144

137145
it('should throw an error if id is null', function(done) {
138-
webhooks['delete'](null, function(err) {
146+
webhooks.delete(null, function(err) {
139147
expect(err.message).to.equal('id is required');
140-
expect(client['delete']).not.to.have.been.called;
148+
expect(client.delete).not.to.have.been.called;
141149
done();
142150
});
143151
});
144152

145153
it('should throw an error if id is missing', function(done) {
146-
webhooks['delete'](function(err) {
154+
webhooks.delete(function(err) {
147155
expect(err.message).to.equal('id is required');
148-
expect(client['delete']).not.to.have.been.called;
156+
expect(client.delete).not.to.have.been.called;
149157
done();
150158
});
151159
});

0 commit comments

Comments
 (0)