fix(core): update
This commit is contained in:
@ -1,19 +1,58 @@
|
||||
import * as plugins from './smartmail.plugins';
|
||||
import * as paths from './smartmail.paths';
|
||||
|
||||
export interface IEmailValidationResult {
|
||||
valid: boolean;
|
||||
disposable: boolean;
|
||||
freemail: boolean;
|
||||
reason: string;
|
||||
}
|
||||
|
||||
export class EmailAddressValidator {
|
||||
public domainMap: { [key: string]: 'disposable' | 'freemail'};
|
||||
|
||||
public smartdns = new plugins.smartdns.Smartdns({});
|
||||
|
||||
public async validate(emailArg: string): Promise<IEmailValidationResult> {
|
||||
await this.fetchDomains();
|
||||
const emailArray = emailArg.split('@');
|
||||
const result = await this.smartdns.getRecord(emailArray[1], 'MX');
|
||||
// console.log(emailArray);
|
||||
// console.log(this.domainMap[emailArray[1]]);
|
||||
return {
|
||||
valid: !!result,
|
||||
reason: 'todo'
|
||||
reason: 'todo',
|
||||
disposable: this.domainMap[emailArray[1]] === 'disposable',
|
||||
freemail: this.domainMap[emailArray[1]] === 'freemail'
|
||||
};
|
||||
}
|
||||
|
||||
public async fetchDomains() {
|
||||
if (!this.domainMap) {
|
||||
const localFileString = plugins.smartfile.fs.toStringSync(
|
||||
plugins.path.join(paths.assetDir, 'domains.json')
|
||||
);
|
||||
const localFileObject = JSON.parse(localFileString);
|
||||
|
||||
|
||||
let onlineFileObject: any;
|
||||
try {
|
||||
onlineFileObject = (
|
||||
await plugins.smartrequest.getJson(
|
||||
'https://raw.githubusercontent.com/romainsimon/emailvalid/master/domains.json'
|
||||
)
|
||||
).body;
|
||||
this.domainMap = onlineFileObject;
|
||||
console.log(
|
||||
'smartmail EmailAddressValidator: Using online email list for email validation'
|
||||
);
|
||||
} catch (e) {
|
||||
this.domainMap = localFileObject;
|
||||
console.log(e);
|
||||
console.log(
|
||||
'smartmail EmailAddressValidator: Using local email list for email validation'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
4
ts/smartmail.paths.ts
Normal file
4
ts/smartmail.paths.ts
Normal file
@ -0,0 +1,4 @@
|
||||
import * as plugins from './smartmail.plugins';
|
||||
|
||||
export const packageDir = plugins.path.join(__dirname, '../');
|
||||
export const assetDir = plugins.path.join(packageDir, './assets');
|
@ -1,5 +1,14 @@
|
||||
// node native scope
|
||||
import * as path from 'path';
|
||||
|
||||
export {
|
||||
path
|
||||
};
|
||||
|
||||
// pushrocks scope
|
||||
import * as smartdns from '@pushrocks/smartdns';
|
||||
import * as smartfile from '@pushrocks/smartfile';
|
||||
import * as smartmustache from '@pushrocks/smartmustache';
|
||||
import * as smartrequest from '@pushrocks/smartrequest';
|
||||
|
||||
export { smartdns, smartfile, smartmustache };
|
||||
export { smartdns, smartfile, smartmustache, smartrequest };
|
||||
|
Reference in New Issue
Block a user