Skip to content

Commit 1f4942b

Browse files
committed
Refine payload generation with a #generateResource method
There aren't many details in Drip's documentation on the required schema. Discovering the correct structure has been trial and error. This new method ensures payloads are structured correctly and paves the way for passing simpler payload objects to the client
1 parent 22edce7 commit 1f4942b

21 files changed

Lines changed: 110 additions & 112 deletions

lib/campaigns.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@ module.exports = {
5959
* @returns {promise}
6060
*/
6161
subscribeToCampaign(campaignId, payload, callback) {
62-
return this.post(`v2/${this.accountId}/campaigns/${campaignId}/subscribers`, { payload }, callback);
62+
return this.post(`v2/${this.accountId}/campaigns/${campaignId}/subscribers`, this.generateResource('subscribers', payload), callback);
6363
}
6464
};

lib/events.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module.exports = {
77
* @returns {promise}
88
*/
99
recordEvent(payload, callback) {
10-
return this.post(`v2/${this.accountId}/events`, { payload }, callback);
10+
return this.post(`v2/${this.accountId}/events`, this.generateResource('events', payload), callback);
1111
},
1212
/**
1313
* Record a batch of events
@@ -17,7 +17,7 @@ module.exports = {
1717
* @returns {promise}
1818
*/
1919
recordBatchEvents(payload, callback) {
20-
return this.post(`v2/${this.accountId}/events/batches`, { payload }, callback);
20+
return this.post(`v2/${this.accountId}/events/batches`, this.generateResource('events', payload), callback);
2121
},
2222
/**
2323
* Fetch a list of all event actions

lib/helpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module.exports = {
66
return encodeURIComponent(string);
77
},
88
checkRequiredFields(payload, requiredFields, personRequest = false) {
9-
if (!requiredFields.every(prop => prop in payload)) {
9+
if (!requiredFields.every((prop) => prop in payload)) {
1010
throw new MissingAttributeError(`Fields: ${requiredFields} should all be present`);
1111
}
1212

lib/index.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const requestPromise = require('request-promise');
22

3+
// Resources
34
const Accounts = require('./accounts');
45
const Broadcasts = require('./broadcasts');
56
const Campaigns = require('./campaigns');
@@ -14,8 +15,8 @@ const Subscriptions = require('./subscriptions');
1415
const Tags = require('./tags');
1516
const Triggers = require('./workflow_triggers');
1617
const Users = require('./users');
17-
const Workflows = require('./workflows');
1818
const Webhooks = require('./webhooks');
19+
const Workflows = require('./workflows');
1920

2021
// Utilities
2122
const helper = require('./helpers');
@@ -34,13 +35,11 @@ class Client {
3435
}
3536

3637
requestHeaders() {
37-
const headers = {
38+
return {
3839
'Content-Type': 'application/json',
3940
authorization: `${this.tokenType} ${this.token}`,
4041
'User-Agent': `Drip NodeJS Wrapper ${VERSION}`
4142
};
42-
43-
return headers;
4443
}
4544

4645
request(requestOptions, callback) {
@@ -55,12 +54,15 @@ class Client {
5554
return requestPromise(requestOptions);
5655
}
5756

57+
generateResource(key, ...args) {
58+
return { [key]: args };
59+
}
60+
5861
get(url, data, callback) {
5962
return this.request({
6063
method: 'GET',
6164
headers: this.requestHeaders(),
6265
uri: helper.baseUrl + url,
63-
body: data.payload,
6466
qs: data.qs,
6567
json: true,
6668
resolveWithFullResponse: true
@@ -72,7 +74,7 @@ class Client {
7274
method: 'POST',
7375
headers: this.requestHeaders(),
7476
uri: helper.baseUrl + url,
75-
body: data.payload,
77+
body: data,
7678
qs: data.qs,
7779
json: true,
7880
resolveWithFullResponse: true
@@ -94,7 +96,7 @@ class Client {
9496
method: 'PUT',
9597
headers: this.requestHeaders(),
9698
uri: helper.baseUrl + url,
97-
body: data.payload,
99+
body: data,
98100
qs: data.qs,
99101
json: true,
100102
resolveWithFullResponse: true

lib/orders.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ module.exports = {
88
* @returns {promise}
99
*/
1010
createUpdateOrder(payload, callback) {
11-
return this.post(`v2/${this.accountId}/orders`, { payload }, callback);
11+
return this.post(`v2/${this.accountId}/orders`, this.generateResource('orders', payload), callback);
1212
},
1313
/**
1414
* Record a batch of orders
1515
* Docs: http://developer.drip.com/#create-or-update-a-batch-of-orders
1616
*
17-
* @param {object} payload - An object of an array of multiple orders
17+
* @param {object} payload - An array of multiple order objects
1818
* @param {callback} callback - An optional callback
1919
* @returns {promise}
2020
*/
2121
createUpdateBatchOrders(payload, callback) {
22-
return this.post(`v2/${this.accountId}/orders/batches`, { payload }, callback);
22+
return this.post(`v2/${this.accountId}/orders/batches`, this.generateResource('batches', { orders: payload }), callback);
2323
},
2424
/**
2525
* Record a refund for an order
@@ -30,6 +30,6 @@ module.exports = {
3030
* @returns {promise}
3131
*/
3232
createUpdateRefund(payload, callback) {
33-
return this.post(`v2/${this.accountId}/refunds`, { payload }, callback);
33+
return this.post(`v2/${this.accountId}/refunds`, this.generateResource('refunds', payload), callback);
3434
}
3535
};

lib/shopper_activity.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = {
1313
const requiredFields = ['provider', 'action', 'cart_id', 'cart_url'];
1414
helpers.checkRequiredFields(payload, requiredFields, true);
1515

16-
return this.post(`v3/${this.accountId}/shopper_activity/cart`, { payload }, callback);
16+
return this.post(`v3/${this.accountId}/shopper_activity/cart`, payload, callback);
1717
},
1818
/**
1919
* Create or update an order for a customer
@@ -27,7 +27,7 @@ module.exports = {
2727
const requiredFields = ['provider', 'action', 'order_id'];
2828
helpers.checkRequiredFields(payload, requiredFields);
2929

30-
return this.post(`v3/${this.accountId}/shopper_activity/order`, { payload }, callback);
30+
return this.post(`v3/${this.accountId}/shopper_activity/order`, payload, callback);
3131
},
3232
/**
3333
* Create or update a product
@@ -41,6 +41,6 @@ module.exports = {
4141
const requiredFields = ['provider', 'action', 'product_id', 'name', 'price', 'product_variant_id'];
4242
helpers.checkRequiredFields(payload, requiredFields);
4343

44-
return this.post(`v3/${this.accountId}/shopper_activity/product`, { payload }, callback);
44+
return this.post(`v3/${this.accountId}/shopper_activity/product`, payload, callback);
4545
}
4646
};

lib/subscribers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module.exports = {
2020
* @returns {promise}
2121
*/
2222
createUpdateSubscriber(payload, callback) {
23-
return this.post(`v2/${this.accountId}/subscribers`, { payload }, callback);
23+
return this.post(`v2/${this.accountId}/subscribers`, this.generateResource('subscribers', payload), callback);
2424
},
2525
/**
2626
* Fetch a subscriber
@@ -53,7 +53,7 @@ module.exports = {
5353
* @returns {promise}
5454
*/
5555
unsubscribeBatchSubscribers(payload, callback) {
56-
return this.post(`v2/${this.accountId}/unsubscribes/batches`, { payload }, callback);
56+
return this.post(`v2/${this.accountId}/unsubscribes/batches`, this.generateResource('batches', { subscribers: payload }), callback);
5757
},
5858
/**
5959
* Unsubscribe a subscriber from all mailings

lib/tags.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module.exports = {
1818
* @returns {promise}
1919
*/
2020
tagSubscriber(payload, callback) {
21-
return this.post(`v2/${this.accountId}/tags`, { payload }, callback);
21+
return this.post(`v2/${this.accountId}/tags`, this.generateResource('tags', payload), callback);
2222
},
2323
/**
2424
* Remove a tag from a subscriber

lib/version.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
module.exports = '2.1.3';
1+
module.exports = '3.0.0';

lib/webhooks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ module.exports = {
2727
* @returns {promise}
2828
*/
2929
createWebhook(payload, callback) {
30-
return this.post(`v2/${this.accountId}/webhooks`, { payload }, callback);
30+
return this.post(`v2/${this.accountId}/webhooks`, this.generateResource('webhooks', payload), callback);
3131
},
3232
/**
3333
* Destroy a webhook

0 commit comments

Comments
 (0)