diff --git a/README.md b/README.md index 243cb09..45a186f 100644 --- a/README.md +++ b/README.md @@ -162,6 +162,7 @@ Click on the desired API to see usage and more information * [Templates](/docs/resources/templates.md) - `client.templates` ([examples](/examples/templates)) * [Transmissions](/docs/resources/transmissions.md) - `client.transmissions` ([examples](/examples/transmissions)) * [Webhooks](/docs/resources/webhooks.md) - `client.webhooks` ([examples](/examples/webhooks)) +* [Recipient Validations](/docs/resources/recipientValidation.md) - `client.recipientValidation` ([examples](/examples/recipientValidation)) ## Development diff --git a/docs/resources/recipientValidation.md b/docs/resources/recipientValidation.md new file mode 100644 index 0000000..152f4fc --- /dev/null +++ b/docs/resources/recipientValidation.md @@ -0,0 +1,15 @@ +# Recipient Validation + +This library provides easy access to the [Recipient Validation](https://developers.sparkpost.com/api/recipient-validation/) 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 +* **validate(email)**
+ Validation of email address provided. + * `email` - the email address that you want to validate **required** + +## Examples + +Visit our examples section to see all of [our recipient validation examples](/examples/recipientValidation). + diff --git a/examples/recipientValidation/validate.js b/examples/recipientValidation/validate.js new file mode 100644 index 0000000..0d8974a --- /dev/null +++ b/examples/recipientValidation/validate.js @@ -0,0 +1,27 @@ +'use strict'; + +var key = 'YOURAPIKEY' + , SparkPost = require('sparkpost') + , client = new SparkPost(key); + +// Promise +client.recipientValidation.validate('test@sparkpost.com') + .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.recipientValidation.validate('test@sparkpost.com', 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/lib/recipientValidation.js b/lib/recipientValidation.js new file mode 100644 index 0000000..c6df3a5 --- /dev/null +++ b/lib/recipientValidation.js @@ -0,0 +1,25 @@ +'use strict'; + +const api = 'recipient-validation'; + +module.exports = function(client) { + return { + /** + * To validate a given email address + * + * @param {string} address - the email address of the recipient that requires validation + * @param {RequestCb} [callback] + * @returns {Promise} + */ + validate: function(address, callback) { + if (!address || typeof address !== 'string') { + return client.reject(new Error('Email Address is required'), callback); + } + + const options = { + uri: `${api}/single/${address}` + }; + return client.get(options, callback); + } + }; +}; diff --git a/lib/sparkpost.js b/lib/sparkpost.js index e83f894..78ec303 100644 --- a/lib/sparkpost.js +++ b/lib/sparkpost.js @@ -87,6 +87,7 @@ const SparkPost = function(apiKey, options) { this.templates = require('./templates')(this); this.transmissions = require('./transmissions')(this); this.webhooks = require('./webhooks')(this); + this.recipientValidation = require('./recipientValidation')(this); }; SparkPost.prototype.request = function(options, callback) { diff --git a/test/spec/recipientValidation.spec.js b/test/spec/recipientValidation.spec.js new file mode 100644 index 0000000..97bd4ae --- /dev/null +++ b/test/spec/recipientValidation.spec.js @@ -0,0 +1,45 @@ +'use strict'; + +var _ = require('lodash') + , chai = require('chai') + , expect = chai.expect + , SparkPost = require('../../lib/sparkpost') + , sinon = require('sinon'); + +require('sinon-as-promised'); + +chai.use(require('sinon-chai')); +chai.use(require('chai-as-promised')); + +describe('Recipient Validation Library', function() { + var client, recipientValidation, callback; + + beforeEach(function() { + client = { + get: sinon.stub().resolves({}), + post: sinon.stub().resolves({}), + put: sinon.stub().resolves({}), + delete: sinon.stub().resolves({}), + reject: SparkPost.prototype.reject + }; + + callback = function() {}; + + recipientValidation = require('../../lib/recipientValidation')(client); + }); + + describe('validate', function() { + it('should call client get method with the appropriate uri', function() { + return recipientValidation.validate('test@sparkpost.com', callback) + .then(function() { + expect(client.get.firstCall.args[0]).to.deep.equal({uri: 'recipient-validation/single/test@sparkpost.com'}); + expect(client.get.firstCall.args[1]).to.equal(callback); + }); + }); + + it('should throw an error if domain is missing', function() { + return expect(recipientValidation.validate()).to.be.rejectedWith('Email Address is required'); + }); + }); + +});