neverbounce
Version:
An API wrapper for the NeverBounce API
132 lines (131 loc) • 4.05 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Verification Object class for handling verification results
*/
class VerificationObject {
/**
* Constructor
* @param response Verification response from API
*/
constructor(response) {
this.response = response;
}
/**
* Get the full response object
* @returns The verification response
*/
getResponse() {
return this.response;
}
/**
* Returns result
* @returns The verification result
*/
getResult() {
return this.response.result;
}
/**
* Returns text code
* @returns The numeric result code
*/
getNumericResult() {
return VerificationObject[this.getResult()];
}
/**
* Returns true if result is of specified types
* @param codes Verification result codes to check
* @returns Whether the result matches any of the specified codes
*/
is(codes) {
if (Array.isArray(codes)) {
return (codes.indexOf(this.getResult()) > -1 || codes.indexOf(this.getNumericResult()) > -1);
}
else {
return (codes === this.getResult() || codes === this.getNumericResult());
}
}
/**
* Returns true if result is NOT of specified types
* @param codes Verification result codes to check
* @returns Whether the result does not match any of the specified codes
*/
not(codes) {
if (Array.isArray(codes)) {
return (codes.indexOf(this.getResult()) === -1 && codes.indexOf(this.getNumericResult()) === -1);
}
else {
return (codes !== this.getResult() && codes !== this.getNumericResult());
}
}
/**
* Returns true if the flag was returned
* @param flag Flag to check
* @returns Whether the flag is present
*/
hasFlag(flag) {
return this.response
&& this.response.flags
&& this.response.flags.indexOf(flag) > -1;
}
}
/**
* Result code constants
*/
VerificationObject.valid = 0;
VerificationObject.invalid = 1;
VerificationObject.disposable = 2;
VerificationObject.catchall = 3;
VerificationObject.unknown = 4;
/**
* Helper object for result codes and flags
* @since 4.1.4
*/
VerificationObject.helpers = {
// Numerically indexed
[VerificationObject.valid]: 'valid',
[VerificationObject.invalid]: 'invalid',
[VerificationObject.disposable]: 'disposable',
[VerificationObject.catchall]: 'catchall',
[VerificationObject.unknown]: 'unknown',
// Text indexed
valid: VerificationObject.valid,
invalid: VerificationObject.invalid,
disposable: VerificationObject.disposable,
catchall: VerificationObject.catchall,
unknown: VerificationObject.unknown,
flags: {
has_dns: 'has_dns',
has_dns_mx: 'has_dns_mx',
bad_syntax: 'bad_syntax',
free_email_host: 'free_email_host',
profanity: 'profanity',
role_account: 'role_account',
disposable_email: 'disposable_email',
government_host: 'government_host',
academic_host: 'academic_host',
military_host: 'military_host',
international_host: 'international_host',
squatter_host: 'squatter_host',
spelling_mistake: 'spelling_mistake',
bad_dns: 'bad_dns',
temporary_dns_error: 'temporary_dns_error',
connect_fails: 'connect_fails',
accepts_all: 'accepts_all',
contains_alias: 'contains_alias',
contains_subdomain: 'contains_subdomain',
smtp_connectable: 'smtp_connectable',
spamtrap_network: 'spamtrap_network',
}
};
/**
* @deprecated 4.1.4 Will be removed in next minor release
*/
VerificationObject.numericCodes = {
valid: VerificationObject.valid,
invalid: VerificationObject.invalid,
disposable: VerificationObject.disposable,
catchall: VerificationObject.catchall,
unknown: VerificationObject.unknown,
};
exports.default = VerificationObject;