-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
108 lines (91 loc) · 2.99 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"use strict";
const backoff = require("backoff");
const request = require("request");
const VERSION = require("./package.json").version;
const RETRIES = 5;
const VERIFY_URI = "https://emailverification.whoisxmlapi.com/api/v1";
/**
* Class representing a Verifier object. This object allows you to verify email
* addresses.
*/
class Verifier {
/**
* Create a verifier.
*
* @param {string} apiKey - The API key for your emailverification.whoisxmlapi.com account.
* @param {object} opts - An object that contains all Verifier options.
*/
constructor(apiKey, opts) {
this.apiKey = apiKey;
this.opts = opts || {};
this.verifyOptions();
}
/**
* Verifies the constructor's options to ensure all parameters are valid.
*
* @throws {error} Throws an error if any of the Verifier's parameters are
* invalid.
*/
verifyOptions() {
if (!this.apiKey) {
throw new Error("API key required");
} else if (typeof this.apiKey !== "string") {
throw new Error("API key must be a string");
} else if (this.opts.retries && typeof this.opts.retries !== "number") {
throw new Error("opts.retries must be a number");
}
}
/**
* Verifies an email address using the whoisxmlapi.com service.
*
* @param {string} email - The email address to verify.
* @param {object} opts - An object that contains all options.
* @param {callback} cb - The callback to run after verification has finished.
*/
verify(email, opts, cb) {
if (!cb) {
cb = opts;
opts = {};
}
let call = backoff.call(request, {
uri: VERIFY_URI,
headers: { "User-Agent": "node-email-verifier/" + VERSION },
qs: {
apiKey: this.apiKey,
emailAddress: email,
validateDNS: (this.opts.validateDNS === false ? false : true),
validateSMTP: (this.opts.validateSMTP === false ? false : true),
checkCatchAll: (this.opts.checkCatchAll === false ? false : true),
checkFree: (this.opts.checkFree === false ? false : true),
checkDisposable: (this.opts.checkDisposable === false ? false : true),
_hardRefresh: (opts.hardRefresh === true ? 1 : 0),
outputFormat: "json"
}
}, (err, resp, body) => {
if (err) {
return cb(err);
}
if (resp.statusCode !== 200) {
return cb(new Error("There was an error when making the API request. Please try again."));
}
try {
let json = JSON.parse(body);
if (json && json.ErrorMessage) {
return cb(new Error(json.ErrorMessage.msg));
}
if (json && !json.emailAddress) {
console.log(json);
return cb(new Error("Oops! It looks like whoisxmlapi.com is having issues. Sorry!"));
}
cb(null, json);
} catch(err) {
cb(err);
}
});
call.retryIf(err => { return err });
call.setStrategy(new backoff.ExponentialStrategy);
call.failAfter(this.opts.retries || RETRIES);
call.start();
}
}
module.exports = Verifier;