diff --git a/docs/resources/messageEvents.md b/docs/resources/messageEvents.md index 6ef8bef..f61cf06 100644 --- a/docs/resources/messageEvents.md +++ b/docs/resources/messageEvents.md @@ -1,28 +1,17 @@ # Message Events -This library provides easy access to the [Message Events](https://www.sparkpost.com/api#/reference/message-events/) resource. +This library provides easy access to the [Message Events](https://developers.sparkpost.com/api/message-events) resource. + +*Note: All methods return promises and accept an optional last argument callback. [Read about how we handle callbacks and promises](/docs/async.md).* ## Methods -* **search(params, callback)** +* **search([params, callback])**
Search for message events using the given parameters (NOTE: all params are optional): - * `params.bounce_classes` - list of [bounce classes](https://support.sparkpost.com/customer/portal/articles/1929896) - * `params.campaign_ids` - campaign IDs - * `params.events` - event types - * `params.friendly_froms` - 'friendly' from addressess - * `params.from` - time lower bound (see below for date/time format details) - * `params.message_ids` - message IDs - * `params.page` - results page number - * `params.per_page` - number of results per page - * `params.reason` - bounce reason with '%' wildcards (see below for example) - * `params.recipients` - recipient email addresses - * `params.template_ids` - template IDs - * `params.timezone` - timezone for `from` and `to` params ([reference](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)) - * `params.to` - time upper bound (see below for date/time format details) - * `params.transmission_ids` - transmission IDs + * `params` - a hash of [Message Events URI Parameters](https://developers.sparkpost.com/api/message-events.html#message-events-message-events-get) ## Date/Time Parameter Format -The `from` and `to` search parameters accept datestamps of the form: +The `from` and `to` search parameters accept date stamps of the form: `YYYY-MM-DDTHH:MM` @@ -32,29 +21,4 @@ Note: timestamps are expressed in the timezone specified by the `timezone` param ## Examples -This example code retrieves up to 5 'invalid recipient' bounce events from the first 2 days of 2016. - -```js -var SparkPost = require('sparkpost'); -var client = new SparkPost('YOUR_API_KEY'); -var searchParams = { - from: '2016-01-01T00:00', - to: '2016-01-02T23:59', - page: 1, - per_page: 5, - events: ['bounce', 'out_of_band'], - bounce_classes: [10] -}; - -client.messageEvents.search(searchParams, function(err, res) { - if(err) { - console.log(err); - return; - } - - console.log(data); -}); - -``` - -Check out all the examples provided [here](/examples/messageEvents). +Visit our examples section to see all of [our message events resource examples](/examples/messageEvents). diff --git a/docs/resources/transmissions.md b/docs/resources/transmissions.md index b2fa235..4e0ed52 100644 --- a/docs/resources/transmissions.md +++ b/docs/resources/transmissions.md @@ -2,66 +2,24 @@ This library provides easy access to the [Transmissions](https://developers.sparkpost.com/api/transmissions) Resource. +*Note: All methods return promises and accept an optional last argument callback. [Read about how we handle callbacks and promises](/docs/async.md).* + ## Methods -* **all(options[, callback]) → `{Promise}`**
+* **list(options[, callback])**
List an overview of all transmissions in the account * `options.campaign_id` - id of the campaign used by the transmission * `options.template_id` - id of the template used by the transmission - * `callback` - executed after task is completed if provided* - * standard `callback(err, data)` - * `err` - any error that occurred - * `data` - results returned by the api -* **find(id[, callback]) → `{Promise}`**
+* **get(id[, callback])**
Retrieve the details about a transmission by its ID * `id` - id of the transmission you want to look up **required** - * `callback` - see all function -* **send(transmission[, callback]) → `{Promise}`**
+* **send(transmission[, options, callback])**
Sends a message by creating a new transmission * `transmission` - an object of [transmission attributes](https://developers.sparkpost.com/api/transmissions#header-transmission-attributes) - * `transmission.num_rcpt_errors` - maximum number of recipient errors returned - * `callback` - see all function - -*callback is optional because all methods return a Promise. - -## Getting Started: Your First Mailing - -```javascript -var SparkPost = require('sparkpost') - , client = new SparkPost('YOUR API KEY') - , options = { - campaign_id: 'first-mailing', - content: { - from: 'you@your-company.com', - subject: 'First SDK Mailing', - html: '

Congratulations, {{name}}!

You just sent your very first mailing!

', - text: 'Congratulations, {{name}}!! You just sent your very first mailing!' - }, - substitution_data: {name: 'YOUR FIRST NAME'}, - recipients: [{ address: { name: 'YOUR FULL NAME', email: 'YOUR EMAIL ADDRESS' } }] - }; + * `options.num_rcpt_errors` - maximum number of recipient errors returned -client.transmissions.send(options) - .then(data => { - console.log('Woohoo! You just sent your first mailing!'); - console.log(data); - }) - .catch(err => { - console.log('Whoops! Something went wrong'); - console.log(err); - }); +## Examples -// Using a callback -client.transmissions.send(options, function(err, data) { - if (err) { - console.log('Whoops! Something went wrong'); - console.log(err); - } else { - console.log('Woohoo! You just sent your first mailing!'); - console.log(data); - } -}); -``` -Check out all the examples provided [here](/examples/transmissions). +Visit our examples section to see all of [our transmissions resource examples](/examples/transmissions). ## Tips and Tricks * If you specify a stored recipient list and inline recipients in a Transmission, you will receive an error. diff --git a/examples/messageEvents/all_messageEvents.js b/examples/messageEvents/all_messageEvents.js deleted file mode 100644 index 113158c..0000000 --- a/examples/messageEvents/all_messageEvents.js +++ /dev/null @@ -1,15 +0,0 @@ -'use strict'; - -var key = 'YOURAPIKEY' - , SparkPost = require('sparkpost') - , client = new SparkPost(key); - -client.messageEvents.search({}, function(err, data) { - if (err) { - console.log(err); - } else { - console.log(data); - console.log('Congrats you can use our client library!'); - } -}); - diff --git a/examples/messageEvents/search_campaignClicks.js b/examples/messageEvents/search_campaignClicks.js index 49b74bd..6b15cde 100644 --- a/examples/messageEvents/search_campaignClicks.js +++ b/examples/messageEvents/search_campaignClicks.js @@ -4,16 +4,28 @@ var key = 'YOURAPIKEY' , SparkPost = require('sparkpost') , client = new SparkPost(key) , searchParams = { - events: 'click', - campaign_ids: 'monday_mailshot' - }; + events: 'click', + campaign_ids: 'monday_mailshot' + }; +// Promise +client.messageEvents.search(searchParams) + .then(data => { + console.log('Congrats you can use our client library!'); + console.log(data); + }) + .catch(err => { + console.log('Whoops! Something went wrong'); + console.log(err); + }); + +// Callback client.messageEvents.search(searchParams, function(err, data) { if (err) { + console.log('Whoops! Something went wrong'); console.log(err); } else { - console.log(data); console.log('Congrats you can use our client library!'); + console.log(data); } }); - diff --git a/examples/messageEvents/search_default.js b/examples/messageEvents/search_default.js new file mode 100644 index 0000000..9339cbb --- /dev/null +++ b/examples/messageEvents/search_default.js @@ -0,0 +1,29 @@ +'use strict'; + +var key = 'YOURAPIKEY' + , SparkPost = require('sparkpost') + , client = new SparkPost(key); + +// Returns 1000 events for the last hour + +// Promise +client.messageEvents.search({}) + .then(data => { + console.log('Congrats you can use our client library!'); + console.log(data); + }) + .catch(err => { + console.log('Whoops! Something went wrong'); + console.log(err); + }); + +// Callback +client.messageEvents.search({}, function(err, data) { + if (err) { + console.log('Whoops! Something went wrong'); + console.log(err); + } else { + console.log('Congrats you can use our client library!'); + console.log(data); + } +}); diff --git a/examples/messageEvents/search_messageEvents.js b/examples/messageEvents/search_messageEvents.js index 9b2250a..ce50e57 100644 --- a/examples/messageEvents/search_messageEvents.js +++ b/examples/messageEvents/search_messageEvents.js @@ -4,20 +4,32 @@ var key = 'YOURAPIKEY' , SparkPost = require('sparkpost') , client = new SparkPost(key) , searchParams = { - from: '2016-01-01T00:00', - to: '2016-01-02T23:59', - page: 1, - per_page: 5, - events: ['bounce', 'out_of_band'], - bounce_classes: [10] - }; + from: '2016-01-01T00:00', + to: '2016-01-02T23:59', + page: 1, + per_page: 5, + events: ['bounce', 'out_of_band'], + bounce_classes: [10] + }; +// Promise +client.messageEvents.search(searchParams) + .then(data => { + console.log('Congrats you can use our client library!'); + console.log(data); + }) + .catch(err => { + console.log('Whoops! Something went wrong'); + console.log(err); + }); + +// Callback client.messageEvents.search(searchParams, function(err, data) { if (err) { + console.log('Whoops! Something went wrong'); console.log(err); } else { - console.log(data); console.log('Congrats you can use our client library!'); + console.log(data); } }); - diff --git a/examples/subaccounts/create_subaccount.js b/examples/subaccounts/create.js similarity index 97% rename from examples/subaccounts/create_subaccount.js rename to examples/subaccounts/create.js index 3c94f4d..1374705 100644 --- a/examples/subaccounts/create_subaccount.js +++ b/examples/subaccounts/create.js @@ -12,6 +12,7 @@ var key = 'YOURAPIKEY' ] }; +// Promise client.subaccounts.create(subaccount) .then(data => { console.log('Congrats you can use our client library!'); @@ -22,7 +23,7 @@ client.subaccounts.create(subaccount) console.log(err); }); -// Using a callback +// Callback client.subaccounts.create(subaccount, function(err, data) { if (err) { console.log('Whoops! Something went wrong'); diff --git a/examples/subaccounts/get_subaccount.js b/examples/subaccounts/get.js similarity index 96% rename from examples/subaccounts/get_subaccount.js rename to examples/subaccounts/get.js index 238075c..9f0e45a 100644 --- a/examples/subaccounts/get_subaccount.js +++ b/examples/subaccounts/get.js @@ -4,6 +4,7 @@ var key = 'YOURAPIKEY' , SparkPost = require('sparkpost') , client = new SparkPost(key); +// Promise client.subaccounts.get('123') .then(data => { console.log('Congrats you can use our client library!'); @@ -14,7 +15,7 @@ client.subaccounts.get('123') console.log(err); }); -// Using a callback +// Callback client.subaccounts.get('123', function(err, data) { if (err) { console.log('Whoops! Something went wrong'); diff --git a/examples/subaccounts/list_subaccounts.js b/examples/subaccounts/list.js similarity index 96% rename from examples/subaccounts/list_subaccounts.js rename to examples/subaccounts/list.js index 78e5be7..877bdd1 100644 --- a/examples/subaccounts/list_subaccounts.js +++ b/examples/subaccounts/list.js @@ -4,6 +4,7 @@ var key = 'YOURAPIKEY' , SparkPost = require('sparkpost') , client = new SparkPost(key); +// Promise client.subaccounts.list() .then(data => { console.log('Congrats you can use our client library!'); @@ -14,7 +15,7 @@ client.subaccounts.list() console.log(err); }); -// Using a callback +// Callback client.subaccounts.list(function(err, data) { if (err) { console.log('Whoops! Something went wrong'); diff --git a/examples/subaccounts/update_subaccount.js b/examples/subaccounts/update.js similarity index 96% rename from examples/subaccounts/update_subaccount.js rename to examples/subaccounts/update.js index 96309a4..565a093 100644 --- a/examples/subaccounts/update_subaccount.js +++ b/examples/subaccounts/update.js @@ -8,6 +8,7 @@ var key = 'YOURAPIKEY' status: 'suspended' }; +// Promise client.subaccounts.update('123', subaccount) .then(data => { console.log('Congrats you can use our client library!'); @@ -18,7 +19,7 @@ client.subaccounts.update('123', subaccount) console.log(err); }); -// Using a callback +// Callback client.subaccounts.update('123', subaccount, function(err, data) { if (err) { console.log('Whoops! Something went wrong'); diff --git a/examples/transmissions/get_transmission.js b/examples/transmissions/get.js similarity index 77% rename from examples/transmissions/get_transmission.js rename to examples/transmissions/get.js index e39fa12..8aabe7c 100644 --- a/examples/transmissions/get_transmission.js +++ b/examples/transmissions/get.js @@ -4,7 +4,8 @@ var key = 'YOURAPIKEY' , SparkPost = require('sparkpost') , client = new SparkPost(key); -client.transmissions.find('YOUR-TRANsMISSION-KEY') +// Promise +client.transmissions.get('YOUR-TRANSMISSION-KEY') .then(data => { console.log('Congrats you can use our client library!'); console.log(data); @@ -14,8 +15,8 @@ client.transmissions.find('YOUR-TRANsMISSION-KEY') console.log(err); }); -// Using a callback -client.transmissions.find('YOUR-TRANSMISSION-KEY', function(err, data) { +// Callback +client.transmissions.get('YOUR-TRANSMISSION-KEY', function(err, data) { if (err) { console.log('Whoops! Something went wrong'); console.log(err); diff --git a/examples/transmissions/get_all_transmissions.js b/examples/transmissions/list.js similarity index 80% rename from examples/transmissions/get_all_transmissions.js rename to examples/transmissions/list.js index 8d3be4b..4ddeaeb 100644 --- a/examples/transmissions/get_all_transmissions.js +++ b/examples/transmissions/list.js @@ -4,7 +4,8 @@ var key = 'YOURAPIKEY' , SparkPost = require('sparkpost') , client = new SparkPost(key); -client.transmissions.all() +// Promise +client.transmissions.list() .then(data => { console.log(data); console.log('Congrats you can use our client library!'); @@ -13,8 +14,8 @@ client.transmissions.all() console.log(err); }); -// Using a callback -client.transmissions.all(function(err, data) { +// Callback +client.transmissions.list(function(err, data) { if (err) { console.log(err); } else { diff --git a/examples/transmissions/get_transmissions_by_campaign.js b/examples/transmissions/list_by_campaign.js similarity index 86% rename from examples/transmissions/get_transmissions_by_campaign.js rename to examples/transmissions/list_by_campaign.js index 47d0ed1..bd50150 100644 --- a/examples/transmissions/get_transmissions_by_campaign.js +++ b/examples/transmissions/list_by_campaign.js @@ -7,7 +7,7 @@ var key = 'YOURAPIKEY' campaign_id: 'my_campaign' }; -client.transmissions.all(options) +client.transmissions.list(options) .then(data => { console.log('Congrats you can use our client library!'); console.log(data); @@ -18,7 +18,7 @@ client.transmissions.all(options) }); // Using a callback -client.transmissions.all(options, function(err, data) { +client.transmissions.list(options, function(err, data) { if (err) { console.log('Whoops! Something went wrong'); console.log(err); diff --git a/examples/transmissions/get_transmissions_by_template.js b/examples/transmissions/list_by_template.js similarity index 82% rename from examples/transmissions/get_transmissions_by_template.js rename to examples/transmissions/list_by_template.js index bfa6a5b..28ff2ce 100644 --- a/examples/transmissions/get_transmissions_by_template.js +++ b/examples/transmissions/list_by_template.js @@ -7,7 +7,8 @@ var key = 'YOURAPIKEY' template_id: 'my_template' }; -client.transmissions.all(options) +// Promise +client.transmissions.list(options) .then(data => { console.log('Congrats you can use our client library!'); console.log(data); @@ -17,8 +18,8 @@ client.transmissions.all(options) console.log(err); }); -// Using a callback -client.transmissions.all(options, function(err, data) { +// Callback +client.transmissions.list(options, function(err, data) { if (err) { console.log('Whoops! Something went wrong'); console.log(err); diff --git a/examples/transmissions/send_transmission_all_fields.js b/examples/transmissions/send_all_fields.js similarity index 95% rename from examples/transmissions/send_transmission_all_fields.js rename to examples/transmissions/send_all_fields.js index dea0cf7..4a7b2d2 100644 --- a/examples/transmissions/send_transmission_all_fields.js +++ b/examples/transmissions/send_all_fields.js @@ -3,7 +3,7 @@ var key = 'YOURAPIKEY' , SparkPost = require('sparkpost') , client = new SparkPost(key) - , tranmission = { + , transmission = { options: { open_tracking: true, click_tracking: true @@ -50,7 +50,8 @@ var key = 'YOURAPIKEY' } }; -client.transmissions.send(tranmission) +// Promise +client.transmissions.send(transmission) .then(data => { console.log('Congrats you can use our client library!'); console.log(data); @@ -60,7 +61,7 @@ client.transmissions.send(tranmission) console.log(err); }); -// Using a callback +// Callback client.transmissions.send(tranmission, function(err, data) { if (err) { console.log('Whoops! Something went wrong'); diff --git a/examples/transmissions/mime_parts.js b/examples/transmissions/send_mime_parts.js similarity index 97% rename from examples/transmissions/mime_parts.js rename to examples/transmissions/send_mime_parts.js index 2dcfe91..6888348 100644 --- a/examples/transmissions/mime_parts.js +++ b/examples/transmissions/send_mime_parts.js @@ -17,6 +17,7 @@ var key = 'YOURAPIKEY' } }; +// Promise client.transmissions.send(transmission) .then(data => { console.log('Congrats you can use our client library!'); @@ -27,7 +28,7 @@ client.transmissions.send(transmission) console.log(err); }); -// Using a callback +// Callback client.transmissions.send(transmission, function(err, data) { if (err) { console.log('Whoops! Something went wrong'); diff --git a/examples/transmissions/rfc822.js b/examples/transmissions/send_rfc822.js similarity index 97% rename from examples/transmissions/rfc822.js rename to examples/transmissions/send_rfc822.js index bf0a6c7..02e90d1 100644 --- a/examples/transmissions/rfc822.js +++ b/examples/transmissions/send_rfc822.js @@ -10,6 +10,7 @@ var key = 'YOURAPIKEY' } }; +// Promise client.transmissions.send(transmission) .then(data => { console.log('Congrats you can use our client library!'); @@ -20,7 +21,7 @@ client.transmissions.send(transmission) console.log(err); }); -// Using a callback +// Callback client.transmissions.send(transmission, function(err, data) { if (err) { console.log('Whoops! Something went wrong'); diff --git a/examples/transmissions/stored_recipients_inline_content.js b/examples/transmissions/send_stored_recipients_inline_content.js similarity index 97% rename from examples/transmissions/stored_recipients_inline_content.js rename to examples/transmissions/send_stored_recipients_inline_content.js index 080c312..c8bb4bb 100644 --- a/examples/transmissions/stored_recipients_inline_content.js +++ b/examples/transmissions/send_stored_recipients_inline_content.js @@ -15,6 +15,7 @@ var key = 'YOURAPIKEY' } }; +// Promise client.transmissions.send(transmission) .then(data => { console.log('Congrats you can use our client library!'); @@ -25,7 +26,7 @@ client.transmissions.send(transmission) console.log(err); }); -// Using a callback +// Callback client.transmissions.send(transmission, function(err, data) { if (err) { console.log('Whoops! Something went wrong'); diff --git a/examples/transmissions/stored_recipients_stored_content.js b/examples/transmissions/send_stored_recipients_stored_content.js similarity index 90% rename from examples/transmissions/stored_recipients_stored_content.js rename to examples/transmissions/send_stored_recipients_stored_content.js index 3b4d7ba..6bf8790 100644 --- a/examples/transmissions/stored_recipients_stored_content.js +++ b/examples/transmissions/send_stored_recipients_stored_content.js @@ -3,7 +3,7 @@ var key = 'YOURAPIKEY' , SparkPost = require('sparkpost') , client = new SparkPost(key) - , tranmission = { + , transmission = { recipients: { list_id: 'example-list' }, @@ -14,7 +14,8 @@ var key = 'YOURAPIKEY' } }; -client.transmissions.send(tranmission) +// Promise +client.transmissions.send(transmission) .then(data => { console.log('Congrats you can use our client library!'); console.log(data); @@ -24,7 +25,7 @@ client.transmissions.send(tranmission) console.log(err); }); -// Using a callback +// Callback client.transmissions.send(tranmission, function(err, data) { if (err) { console.log('Whoops! Something went wrong'); diff --git a/examples/transmissions/stored_template_send.js b/examples/transmissions/send_stored_template.js similarity index 79% rename from examples/transmissions/stored_template_send.js rename to examples/transmissions/send_stored_template.js index 99d1b3a..00a242b 100644 --- a/examples/transmissions/stored_template_send.js +++ b/examples/transmissions/send_stored_template.js @@ -8,11 +8,14 @@ var key = 'YOURAPIKEY' content: { template_id: 'ricks-template' }, - 'num_rcpt_errors': 3, recipients: [{ address: { email: 'rick.sanchez@rickandmorty100years.com', name: 'Rick Sanchez' } }] + } + , options = { + num_rcpt_errors: 3 }; -client.transmissions.send(transmission) +// Promise +client.transmissions.send(transmission, options) .then(data => { console.log('What up my glib globs! SparkPost!'); console.log(data); @@ -22,8 +25,8 @@ client.transmissions.send(transmission) console.log(err); }); -// Using a callback -client.transmissions.send(transmission, function(err, data) { +// Callback +client.transmissions.send(transmission, options, function(err, data) { if (err) { console.log('Whoops! Something went wrong'); console.log(err); diff --git a/examples/transmissions/send_transmission_with_bcc.js b/examples/transmissions/send_with_bcc.js similarity index 98% rename from examples/transmissions/send_transmission_with_bcc.js rename to examples/transmissions/send_with_bcc.js index 9f24d76..e9d1804 100644 --- a/examples/transmissions/send_transmission_with_bcc.js +++ b/examples/transmissions/send_with_bcc.js @@ -35,6 +35,7 @@ var key = 'YOURAPIKEY' } }; +// Promise client.transmissions.send(transmission) .then(data => { console.log(data); @@ -45,7 +46,7 @@ client.transmissions.send(transmission) console.log(err); }); -// Using a callback +// Callback client.transmissions.send(transmission, function(err, data) { if (err) { console.log('Whoops! Something went wrong'); diff --git a/examples/transmissions/send_transmission_with_cc.js b/examples/transmissions/send_with_cc.js similarity index 90% rename from examples/transmissions/send_transmission_with_cc.js rename to examples/transmissions/send_with_cc.js index f2b5286..f7a09d8 100644 --- a/examples/transmissions/send_transmission_with_cc.js +++ b/examples/transmissions/send_with_cc.js @@ -3,7 +3,7 @@ var key = 'YOURAPIKEY' , SparkPost = require('sparkpost') , client = new SparkPost(key) - , tranmission = { + , transmission = { recipients: [ { address: { @@ -39,7 +39,8 @@ var key = 'YOURAPIKEY' } }; -client.transmissions.send(tranmission) +// Promise +client.transmissions.send(transmission) .then(data => { console.log('Congrats! You sent an email with cc using SparkPost!'); console.log(data); @@ -49,8 +50,8 @@ client.transmissions.send(tranmission) console.log(err); }); -// Using a callback -client.transmissions.send(tranmission, function(err, data) { +// Callback +client.transmissions.send(transmission, function(err, data) { if (err) { console.log('Whoops! Something went wrong'); console.log(err); diff --git a/lib/messageEvents.js b/lib/messageEvents.js index 86ce335..096cd67 100644 --- a/lib/messageEvents.js +++ b/lib/messageEvents.js @@ -1,6 +1,6 @@ 'use strict'; -var api = 'message-events'; +const api = 'message-events'; /* * "Class" declaration, Message Events API exposes one function: @@ -8,29 +8,26 @@ var api = 'message-events'; */ module.exports = function(client) { return { + /** + * Search for message events using given parameters + * + * @param {Object} parameters + * @param {RequestCb} [callback] + * @returns {Promise} + */ search: function(parameters, callback) { - var arrayParams = [ - 'bounce_classes', - 'campaign_ids', - 'events', - 'friendly_froms', - 'message_ids', - 'recipients', - 'template_ids', - 'transmission_ids' - ] - , options; + var options = { + uri: api + , qs: {} + }; - arrayParams.forEach(function(paramname) { + Object.keys(parameters).forEach(function(paramname) { if (Array.isArray(parameters[paramname])) { - parameters[paramname] = parameters[paramname].toString(); + options.qs[paramname] = parameters[paramname].join(','); + } else { + options.qs[paramname] = parameters[paramname]; } }); - - options = { - uri: api - , qs: parameters - }; return client.get(options).asCallback(callback); } }; diff --git a/lib/transmissions.js b/lib/transmissions.js index 3c36974..0625780 100644 --- a/lib/transmissions.js +++ b/lib/transmissions.js @@ -1,8 +1,7 @@ 'use strict'; -let _ = require('lodash') - , Promise = require('./Promise'); const api = 'transmissions'; +const Promise = require('./Promise'); /* * "Class" declaration, Transmissions exposes three functions, one for sending a transmission, @@ -12,29 +11,17 @@ const api = 'transmissions'; module.exports = function(client) { return { - send: function(transmission, callback) { - var reqOpts; - - if (!transmission || typeof transmission === 'function') { - return Promise.reject(new Error('transmission object is required')).asCallback(callback); - } - - reqOpts = { - uri: api, - json: _.cloneDeep(transmission), - qs: {} - }; - - if (reqOpts.json.num_rcpt_errors) { - reqOpts.qs.num_rcpt_errors = reqOpts.json.num_rcpt_errors; - delete reqOpts.json.num_rcpt_errors; - } - - return client.post(reqOpts).asCallback(callback); - }, - all: function(options, callback) { + /** + * List an overview of all transmissions in the account + * + * @param {Object} options + * @param {RequestCb} [callback] + * @returns {Promise} + */ + list: function(options, callback) { var reqOpts; + // Handle optional options argument if (typeof options === 'function') { callback = options; options = {}; @@ -47,7 +34,14 @@ module.exports = function(client) { return client.get(reqOpts).asCallback(callback); }, - find: function(id, callback) { + /** + * Retrieve the details about a transmission by its id + * + * @param {String} id + * @param {RequestCb} [callback] + * @returns {Promise} + */ + get: function(id, callback) { var options; if (typeof id !== 'string') { @@ -59,6 +53,35 @@ module.exports = function(client) { }; return client.get(options).asCallback(callback); + }, + /** + * Sends a message by creating a new transmission + * + * @param {Object} transmission + * @param {Object} options + * @param {RequestCb} [callback] + * @returns {Promise} + */ + send: function(transmission, options, callback) { + var reqOpts; + + if (!transmission || typeof transmission !== 'object') { + return Promise.reject(new Error('transmission object is required')).asCallback(callback); + } + + // Handle optional options argument + if (typeof options === 'function') { + callback = options; + options = {}; + } + + reqOpts = { + uri: api, + json: transmission, + qs: options + }; + + return client.post(reqOpts).asCallback(callback); } }; diff --git a/test/spec/messageEvents.spec.js b/test/spec/messageEvents.spec.js index 45d8540..5b9fdfc 100644 --- a/test/spec/messageEvents.spec.js +++ b/test/spec/messageEvents.spec.js @@ -1,24 +1,28 @@ +'use strict'; + var chai = require('chai') , expect = chai.expect , sinon = require('sinon') - , sinonChai = require('sinon-chai') , Promise = require('../../lib/Promise'); -chai.use(sinonChai); +require('sinon-as-promised'); + +chai.use(require('sinon-chai')); +chai.use(require('chai-as-promised')); describe('Message Events Library', function() { - var client; + let client, messageEvents; beforeEach(function() { client = { - get: sinon.stub().returns(Promise.resolve({})) + get: sinon.stub().resolves({}) }; messageEvents = require('../../lib/messageEvents')(client); }); describe('search Method', function() { - it('should call client get method with the appropriate parameters', function(done) { + it('should call client get method with the appropriate parameters', function() { var options = { bounce_classes: '10,50', campaign_ids: 'test_campaign', @@ -35,15 +39,15 @@ describe('Message Events Library', function() { to: '2016-11-14T16:15', transmission_ids: '65832150921904138' }; - messageEvents.search(options, function(err, data) { - Object.keys(options).forEach(function(key) { - expect(client.get.firstCall.args[0].qs).to.have.property(key).and.equal(options[key]); + return messageEvents.search(options) + .then(function() { + Object.keys(options).forEach(function(key) { + expect(client.get.firstCall.args[0].qs).to.have.property(key).and.equal(options[key]); + }); }); - done(); - }); }); - it('should accept arrays as parameters where appropriate', function(done) { + it('should accept arrays as parameters where appropriate', function() { var arroptions = { bounce_classes: [10,50], campaign_ids: ['campaign1', 'campaignTwo'], @@ -57,16 +61,16 @@ describe('Message Events Library', function() { per_page: 5, timezone: 'America/New_York' }; - messageEvents.search(arroptions, function(err, data) { - Object.keys(arroptions).forEach(function(key) { - var opt = arroptions[key] - , firstCallQS = client.get.firstCall.args[0].qs; - if (Array.isArray(opt)) { - expect(firstCallQS).to.have.property(key).and.equal(opt.toString()); - } + return messageEvents.search(arroptions) + .then(function() { + Object.keys(arroptions).forEach(function(key) { + var opt = arroptions[key] + , firstCallQS = client.get.firstCall.args[0].qs; + if (Array.isArray(opt)) { + expect(firstCallQS).to.have.property(key).and.equal(opt.toString()); + } + }); }); - done(); - }); }); }); }); diff --git a/test/spec/transmissions.spec.js b/test/spec/transmissions.spec.js index 6fefa24..08fc0f0 100644 --- a/test/spec/transmissions.spec.js +++ b/test/spec/transmissions.spec.js @@ -23,14 +23,14 @@ describe('Transmissions Library', function() { describe('all Method', function() { it('should call client get method with the appropriate uri', function() { - return transmissions.all() + return transmissions.list() .then(function() { expect(client.get.firstCall.args[0].uri).to.equal('transmissions'); }); }); it('should call client get method with the appropriate uri using callback', function(done) { - transmissions.all(function() { + transmissions.list(function() { expect(client.get.firstCall.args[0].uri).to.equal('transmissions'); done(); }); @@ -41,7 +41,7 @@ describe('Transmissions Library', function() { campaign_id: 'test-campaign' }; - return transmissions.all(options) + return transmissions.list(options) .then(function() { expect(client.get.firstCall.args[0].qs).to.deep.equal({campaign_id: 'test-campaign'}); }); @@ -52,7 +52,7 @@ describe('Transmissions Library', function() { template_id: 'test-template' }; - return transmissions.all(options) + return transmissions.list(options) .then(function() { expect(client.get.firstCall.args[0].qs).to.deep.equal({template_id: 'test-template'}); }); @@ -61,14 +61,14 @@ describe('Transmissions Library', function() { describe('find Method', function() { it('should call client get method with the appropriate uri', function() { - return transmissions.find('test') + return transmissions.get('test') .then(function() { expect(client.get.firstCall.args[0]).to.deep.equal({uri: 'transmissions/test'}); }); }); it('should throw an error if id is missing', function() { - return expect(transmissions.find()).to.be.rejectedWith('id is required'); + return expect(transmissions.get()).to.be.rejectedWith('id is required'); }); }); @@ -85,22 +85,34 @@ describe('Transmissions Library', function() { }); }); - it('should throw an error if options object is missing', function() { + it('should throw an error if transmission object is missing', function() { return expect(transmissions.send(function() {})).to.be.rejectedWith('transmission object is required'); }); it('should allow num_rcpt_errors to be set in options', function() { - var options = { - campaign_id: 'test-campaign', - num_rcpt_errors: 3 - }; + var transmission = { + campaign_id: 'test-campaign' + } + , options = { + num_rcpt_errors: 3 + }; - return transmissions.send(options) + return transmissions.send(transmission, options) .then(function() { expect(client.post.firstCall.args[0].qs).to.deep.equal({num_rcpt_errors: 3}); }); }); + it('should not throw an error if optional 2nd argument is a function (callback)', function() { + let cb = sinon.stub() + , transmission = { + campaign_id: 'test-campaign' + }; + return transmissions.send(transmission, cb).then(function() { + expect(cb.callCount).to.equal(1); + }); + }); + it('should leave email_rfc822 content keys intact', function() { var options = { content: {