Skip to content

Commit 0268713

Browse files
committed
beaking change: overhaul .createWebhook() to better align with other methods
1 parent c209a43 commit 0268713

File tree

2 files changed

+149
-85
lines changed

2 files changed

+149
-85
lines changed

src/Particle.js

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -884,34 +884,51 @@ class Particle {
884884

885885
/**
886886
* Create a webhook
887-
* @param {Object} options Options for this API call
888-
* @param {String} options.deviceId Trigger webhook only for this device ID or Name
889-
* @param {String} options.name Webhook name
890-
* @param {String} options.url URL the webhook should hit
891-
* @param {String} [options.requestType=POST] HTTP method to use
892-
* @param {Object} [options.headers] Additional headers to add to the webhook
893-
* @param {Object} [options.json] JSON data
894-
* @param {Object} [options.query] Query string data
895-
* @param {String} [options.body] Custom webhook request body
896-
* @param {Object} [options.responseTemplate] Webhook response template
897-
* @param {Object} [options.responseTopic] Webhook response topic
898-
* @param {Boolean} [options.rejectUnauthorized] Reject invalid HTTPS certificates
899-
* @param {Boolean} [options.noDefaults] Don't include default event data in the webhook request
900-
* @param {Object} [options.webhookAuth] HTTP Basic Auth information
901-
* @param {Object} [options.form] Form data
902-
* @param {String} [options.product] Webhook for this product ID or slug
903-
* @param {String} options.auth Access Token
904-
* @param {Object} [options.context] Request context
905-
* @returns {Promise} A promise
906-
*/
907-
createWebhook({ deviceId, name, url, requestType, headers, json, query, body, responseTemplate, responseTopic, rejectUnauthorized, webhookAuth, noDefaults, form, product, auth, context }){
908-
// deviceId: 'mine' is deprecated since webhooks only trigger on your device anyways
909-
if (deviceId === 'mine'){
910-
deviceId = undefined;
911-
}
887+
* @param {Object} options Options for this API call
888+
* @param {String} options.event The name of the Particle event that should trigger the Webhook
889+
* @param {String} options.url The web address that will be targeted when the Webhook is triggered
890+
* @param {String} [options.device] Trigger Webhook only for this device ID or Name
891+
* @param {Boolean} [options.rejectUnauthorized] Set to `false` to skip SSL certificate validation of the target URL
892+
* @param {Boolean} [options.noDefaults] Don't include default event data in the webhook request
893+
* @param {Object} [options.hook] Webhook configuration settings
894+
* @param {String} [options.hook.method=POST] Type of web request triggered by the Webhook (GET, POST, PUT, or DELETE)
895+
* @param {Object} [options.hook.auth] Auth data like `{ username: 'me', password: '1234' }` to send via basic auth header with the Webhook request
896+
* @param {Object} [options.hook.headers] Additional headers to add to the Webhook like `{ 'X-ONE': '1', X-TWO: '2' }`
897+
* @param {Object} [options.hook.query] Query params to add to the Webhook request like `{ foo: 'foo', bar: 'bar' }`
898+
* @param {Object} [options.hook.json] JSON data to send with the Webhook request - sets `Content-Type` to `application/json`
899+
* @param {Object} [options.hook.form] Form data to send with the Webhook request - sets `Content-Type` to `application/x-www-form-urlencoded`
900+
* @param {String} [options.hook.body] Custom body to send with the Webhook request
901+
* @param {Object} [options.hook.responseTemplate] Template to use to customize the Webhook response body
902+
* @param {Object} [options.hook.responseEvent] The Webhook response event name that your devices can subscribe to
903+
* @param {Object} [options.hook.errorResponseEvent] The Webhook error response event name that your devices can subscribe to
904+
* @param {String} [options.product] Webhook for this product ID or slug
905+
* @param {String} options.auth Access Token
906+
* @param {Object} [options.headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
907+
* @param {Object} [options.context] Request context
908+
* @returns {Promise} A promise
909+
*/
910+
createWebhook({ event, url, device, rejectUnauthorized, noDefaults, hook, product, auth, headers, context }){
912911
const uri = product ? `/v1/products/${product}/webhooks` : '/v1/webhooks';
913-
const data = { event: name, deviceid: deviceId, url, requestType, headers, json, query, body, responseTemplate, responseTopic, rejectUnauthorized, auth: webhookAuth, noDefaults, form };
914-
return this.post({ uri, data, auth, context });
912+
const data = { event, url, deviceId: device, rejectUnauthorized, noDefaults };
913+
914+
if (hook){
915+
data.requestType = hook.method;
916+
data.auth = hook.auth;
917+
data.headers = hook.headers;
918+
data.query = hook.query;
919+
data.json = hook.json;
920+
data.form = hook.form;
921+
data.body = hook.body;
922+
data.responseTemplate = hook.responseTemplate;
923+
data.responseTopic = hook.responseEvent;
924+
data.errorResponseTopic = hook.errorResponseEvent;
925+
}
926+
927+
if (!data.requestType){
928+
data.requestType = 'POST';
929+
}
930+
931+
return this.post({ uri, auth, headers, data, context });
915932
}
916933

917934
/**

test/Particle.spec.js

Lines changed: 105 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const props = {
2020
name: 'specialName',
2121
productId: '9001',
2222
deviceId: '1337',
23+
device: 'my-device',
2324
key: 'c1a55e5',
2425
event: 'main',
2526
token: 'Y',
@@ -31,28 +32,35 @@ const props = {
3132
},
3233
binaryId: '123456',
3334
targetVersion: '0.4.7',
34-
requestType: 'GET',
3535
headers: {
3636
test: 'header'
3737
},
38-
query: {
39-
q: 'p'
40-
},
41-
form: {
42-
f: 'd'
43-
},
44-
json: {
45-
j: 'd'
46-
},
47-
body: '{{data}}',
48-
responseTopic: 'topic',
49-
responseTemplate: 'template',
50-
webhookAuth: {
51-
username: 'u',
52-
password: 'p'
53-
},
5438
rejectUnauthorized: true,
5539
noDefaults: true,
40+
hook: {
41+
method: 'PUT',
42+
auth: {
43+
username: 'u',
44+
password: 'p'
45+
},
46+
headers: {
47+
one: '1',
48+
two: '2'
49+
},
50+
query: {
51+
q: 'p'
52+
},
53+
json: {
54+
j: 'd'
55+
},
56+
form: {
57+
f: 'd'
58+
},
59+
body: '{{data}}',
60+
responseTemplate: 'template',
61+
responseEvent: 'res-event',
62+
errorResponseEvent: 'res-err-event'
63+
},
5664
hookId: 'hook-1234567890',
5765
integrationId: 'integration-1234567890',
5866
clientId: 'client-123',
@@ -991,50 +999,86 @@ describe('ParticleAPI', () => {
991999
it('creates for a single device', () => {
9921000
return api.createWebhook(props).then((results) => {
9931001
results.should.match({
994-
method: 'post',
9951002
uri: '/v1/webhooks',
1003+
method: 'post',
9961004
auth: props.auth,
1005+
headers: props.headers,
9971006
data: {
998-
event: props.name,
1007+
event: props.event,
9991008
url: props.url,
1000-
deviceid: props.deviceId,
1001-
responseTemplate: props.responseTemplate,
1002-
responseTopic: props.responseTopic,
1003-
query: props.query,
1004-
form: props.form,
1005-
json: props.json,
1006-
headers: props.headers,
1007-
auth: props.webhookAuth,
1008-
requestType: props.requestType,
1009+
deviceId: props.device,
10091010
rejectUnauthorized: props.rejectUnauthorized,
1010-
}
1011+
noDefaults: props.noDefaults,
1012+
requestType: props.hook.method,
1013+
auth: props.hook.auth,
1014+
headers: props.hook.headers,
1015+
query: props.hook.query,
1016+
json: props.hook.json,
1017+
form: props.hook.form,
1018+
body: props.hook.body,
1019+
responseTemplate: props.hook.responseTemplate,
1020+
responseTopic: props.hook.responseEvent,
1021+
errorResponseTopic: props.hook.errorResponseEvent,
1022+
},
1023+
context: {}
10111024
});
10121025
});
10131026
});
10141027

10151028
it('creates for user\'s devices', () => {
1016-
const params = Object.assign({}, props, { deviceId: 'mine' });
1029+
const params = Object.assign({}, props);
1030+
delete params.device;
10171031
return api.createWebhook(params).then((results) => {
10181032
results.should.match({
1019-
method: 'post',
10201033
uri: '/v1/webhooks',
1034+
method: 'post',
10211035
auth: props.auth,
1036+
headers: props.headers,
10221037
data: {
1023-
event: props.name,
1038+
event: props.event,
10241039
url: props.url,
1025-
deviceid: undefined,
1026-
responseTemplate: props.responseTemplate,
1027-
responseTopic: props.responseTopic,
1028-
query: props.query,
1029-
form: props.form,
1030-
json: props.json,
1031-
body: props.body,
1032-
headers: props.headers,
1033-
auth: props.webhookAuth,
1034-
requestType: props.requestType,
1040+
deviceId: undefined,
10351041
rejectUnauthorized: props.rejectUnauthorized,
1036-
noDefaults: props.noDefaults
1037-
}
1042+
noDefaults: props.noDefaults,
1043+
requestType: props.hook.method,
1044+
auth: props.hook.auth,
1045+
headers: props.hook.headers,
1046+
query: props.hook.query,
1047+
json: props.hook.json,
1048+
form: props.hook.form,
1049+
body: props.hook.body,
1050+
responseTemplate: props.hook.responseTemplate,
1051+
responseTopic: props.hook.responseEvent,
1052+
errorResponseTopic: props.hook.errorResponseEvent,
1053+
},
1054+
context: {}
1055+
});
1056+
});
1057+
});
1058+
1059+
it('creates using defaults', () => {
1060+
const params = Object.assign({}, props);
1061+
delete params.device;
1062+
delete params.rejectUnauthorized;
1063+
delete params.noDefaults;
1064+
delete params.hook;
1065+
delete params.headers;
1066+
delete params.context;
1067+
return api.createWebhook(params).then((results) => {
1068+
results.should.match({
1069+
uri: '/v1/webhooks',
1070+
method: 'post',
1071+
auth: props.auth,
1072+
headers: undefined,
1073+
data: {
1074+
event: props.event,
1075+
url: props.url,
1076+
deviceId: undefined,
1077+
rejectUnauthorized: undefined,
1078+
noDefaults: undefined,
1079+
requestType: 'POST'
1080+
},
1081+
context: {}
10381082
});
10391083
});
10401084
});
@@ -1044,25 +1088,28 @@ describe('ParticleAPI', () => {
10441088
it('generates request', () => {
10451089
return api.createWebhook(propsWithProduct).then((results) => {
10461090
results.should.match({
1047-
method: 'post',
10481091
uri: `/v1/products/${product}/webhooks`,
1092+
method: 'post',
10491093
auth: props.auth,
1094+
headers: props.headers,
10501095
data: {
1051-
event: props.name,
1096+
event: props.event,
10521097
url: props.url,
1053-
deviceid: props.deviceId,
1054-
responseTemplate: props.responseTemplate,
1055-
responseTopic: props.responseTopic,
1056-
query: props.query,
1057-
form: props.form,
1058-
json: props.json,
1059-
body: props.body,
1060-
headers: props.headers,
1061-
auth: props.webhookAuth,
1062-
requestType: props.requestType,
1098+
deviceId: props.device,
10631099
rejectUnauthorized: props.rejectUnauthorized,
1064-
noDefaults: props.noDefaults
1065-
}
1100+
noDefaults: props.noDefaults,
1101+
requestType: props.hook.method,
1102+
auth: props.hook.auth,
1103+
headers: props.hook.headers,
1104+
query: props.hook.query,
1105+
json: props.hook.json,
1106+
form: props.hook.form,
1107+
body: props.hook.body,
1108+
responseTemplate: props.hook.responseTemplate,
1109+
responseTopic: props.hook.responseEvent,
1110+
errorResponseTopic: props.hook.errorResponseEvent,
1111+
},
1112+
context: {}
10661113
});
10671114
});
10681115
});

0 commit comments

Comments
 (0)