From e41263ce7bd7e54a4392b23f826bfd978b7d352d Mon Sep 17 00:00:00 2001 From: Navaneeth Mohan Date: Sun, 15 Mar 2020 11:53:33 +0530 Subject: [PATCH 1/5] [Issue 250] Recipient Validation --- lib/recipientValidation.js | 25 +++++++++++++++ test/spec/recipientValidation.spec.js | 45 +++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 lib/recipientValidation.js create mode 100644 test/spec/recipientValidation.spec.js diff --git a/lib/recipientValidation.js b/lib/recipientValidation.js new file mode 100644 index 0000000..0f58704 --- /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} + */ + get: 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/test/spec/recipientValidation.spec.js b/test/spec/recipientValidation.spec.js new file mode 100644 index 0000000..4b444c9 --- /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('get', function() { + it('should call client get method with the appropriate uri', function() { + return recipientValidation.get('sparkpost@yopmail.com', callback) + .then(function() { + expect(client.get.firstCall.args[0]).to.deep.equal({uri: 'recipient-validation/single/sparkpost@yopmail.com'}); + expect(client.get.firstCall.args[1]).to.equal(callback); + }); + }); + + it('should throw an error if domain is missing', function() { + return expect(recipientValidation.get()).to.be.rejectedWith('Email Address is required'); + }); + }); + +}); From f07a28bf1afb7a1ebdafacc804471a155aac2a77 Mon Sep 17 00:00:00 2001 From: Navaneeth Mohan Date: Sun, 15 Mar 2020 13:16:44 +0530 Subject: [PATCH 2/5] [Issue 250] Recipient Validation --- README.md | 1 + docs/resources/recipientValidation.md | 15 +++++++++++++ examples/recipientValidation/validate.js | 27 ++++++++++++++++++++++++ lib/recipientValidation.js | 2 +- lib/sparkpost.js | 1 + 5 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 docs/resources/recipientValidation.md create mode 100644 examples/recipientValidation/validate.js 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..a265c9b --- /dev/null +++ b/docs/resources/recipientValidation.md @@ -0,0 +1,15 @@ +# Sending Domains + +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)**
+ Retrieve information related to the email address whether the email address is valid or not. + * `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 index 0f58704..c6df3a5 100644 --- a/lib/recipientValidation.js +++ b/lib/recipientValidation.js @@ -11,7 +11,7 @@ module.exports = function(client) { * @param {RequestCb} [callback] * @returns {Promise} */ - get: function(address, callback) { + validate: function(address, callback) { if (!address || typeof address !== 'string') { return client.reject(new Error('Email Address is required'), 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) { From a396deed5e8e79b7c80af0627f71f484d3ce6ac8 Mon Sep 17 00:00:00 2001 From: Navaneeth Mohan Date: Sun, 15 Mar 2020 13:19:19 +0530 Subject: [PATCH 3/5] Typo Fix --- docs/resources/recipientValidation.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/resources/recipientValidation.md b/docs/resources/recipientValidation.md index a265c9b..07514e2 100644 --- a/docs/resources/recipientValidation.md +++ b/docs/resources/recipientValidation.md @@ -6,8 +6,8 @@ This library provides easy access to the [Recipient Validation](https://develope ## Methods * **validate(email)**
- Retrieve information related to the email address whether the email address is valid or not. - * `email` - the email address that you want to validate **required**\ + Validation of email address provided. + * `email` - the email address that you want to validate **required** ## Examples From d968eea6eb5946d8dc6222bc2b50306dd2328423 Mon Sep 17 00:00:00 2001 From: Navaneeth Mohan Date: Sun, 15 Mar 2020 13:25:23 +0530 Subject: [PATCH 4/5] Missing Heading Update --- docs/resources/recipientValidation.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/resources/recipientValidation.md b/docs/resources/recipientValidation.md index 07514e2..152f4fc 100644 --- a/docs/resources/recipientValidation.md +++ b/docs/resources/recipientValidation.md @@ -1,4 +1,4 @@ -# Sending Domains +# Recipient Validation This library provides easy access to the [Recipient Validation](https://developers.sparkpost.com/api/recipient-validation/) Resource. From f8490089909648bad08bf32be2e5f70c56256f55 Mon Sep 17 00:00:00 2001 From: Navaneeth Mohan Date: Sun, 15 Mar 2020 13:29:51 +0530 Subject: [PATCH 5/5] [Issue 250] Test Update --- test/spec/recipientValidation.spec.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/spec/recipientValidation.spec.js b/test/spec/recipientValidation.spec.js index 4b444c9..97bd4ae 100644 --- a/test/spec/recipientValidation.spec.js +++ b/test/spec/recipientValidation.spec.js @@ -28,17 +28,17 @@ describe('Recipient Validation Library', function() { recipientValidation = require('../../lib/recipientValidation')(client); }); - describe('get', function() { + describe('validate', function() { it('should call client get method with the appropriate uri', function() { - return recipientValidation.get('sparkpost@yopmail.com', callback) + return recipientValidation.validate('test@sparkpost.com', callback) .then(function() { - expect(client.get.firstCall.args[0]).to.deep.equal({uri: 'recipient-validation/single/sparkpost@yopmail.com'}); + 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.get()).to.be.rejectedWith('Email Address is required'); + return expect(recipientValidation.validate()).to.be.rejectedWith('Email Address is required'); }); });