-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); | ||
|
||
}); |