Skip to content

Commit

Permalink
Merge f849008 into e41ecd6
Browse files Browse the repository at this point in the history
  • Loading branch information
navster95 committed Mar 15, 2020
2 parents e41ecd6 + f849008 commit e441d34
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions docs/resources/recipientValidation.md
Original file line number Diff line number Diff line change
@@ -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)**<br />
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).

27 changes: 27 additions & 0 deletions examples/recipientValidation/validate.js
Original file line number Diff line number Diff line change
@@ -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);
}
});
25 changes: 25 additions & 0 deletions lib/recipientValidation.js
Original file line number Diff line number Diff line change
@@ -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);
}
};
};
1 change: 1 addition & 0 deletions lib/sparkpost.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
45 changes: 45 additions & 0 deletions test/spec/recipientValidation.spec.js
Original file line number Diff line number Diff line change
@@ -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');
});
});

});

0 comments on commit e441d34

Please sign in to comment.