@@ -6,25 +6,73 @@ import { checkSMTP } from './smtp/smtp'
6
6
import { checkDisposable } from './disposable/disposable'
7
7
import './types'
8
8
9
+ const defaultOptions = {
10
+
11
+
12
+ validateRegex : true ,
13
+ validateMx : true ,
14
+ validateTypo : true ,
15
+ validateDisposable : true ,
16
+ validateSMTP : true ,
17
+ }
18
+
19
+ type ValidatorOptions = {
20
+ email : string
21
+ sender ?: string
22
+ validateRegex ?: boolean
23
+ validateMx ?: boolean
24
+ validateTypo ?: boolean
25
+ validateDisposable ?: boolean
26
+ validateSMTP ?: boolean
27
+ }
28
+
29
+ type ValidatorOptionsFinal = {
30
+ email : string
31
+ sender : string
32
+ validateRegex : boolean
33
+ validateMx : boolean
34
+ validateTypo : boolean
35
+ validateDisposable : boolean
36
+ validateSMTP : boolean
37
+ }
38
+
9
39
export async function validate (
10
- recipient : string ,
11
- sender :
string = '[email protected] '
40
+ emailOrOptions : string | ValidatorOptions
12
41
) : Promise < OutputFormat > {
13
- console . log ( 'recipient' , recipient )
14
- if ( ! isEmail ( recipient ) ) return createOutput ( 'regex' , 'Invalid regex' )
42
+ let email : string
43
+ let options : ValidatorOptionsFinal = defaultOptions
44
+ if ( typeof emailOrOptions === 'string' ) {
45
+ email = emailOrOptions
46
+ } else {
47
+ email = emailOrOptions . email
48
+ options = { ...options , ...emailOrOptions }
49
+ }
15
50
16
- const typoResponse = await checkTypo ( recipient )
17
- if ( typoResponse ) return createOutput ( 'typo ' , typoResponse )
51
+ if ( options . validateRegex && ! isEmail ( email ) )
52
+ return createOutput ( 'regex ' , 'Invalid regex' )
18
53
19
- const domain = recipient . split ( '@' ) [ 1 ]
54
+ if ( options . validateTypo ) {
55
+ const typoResponse = await checkTypo ( email )
56
+ if ( typoResponse ) return createOutput ( 'typo' , typoResponse )
57
+ }
20
58
21
- const disposableResponse = await checkDisposable ( domain )
22
- if ( disposableResponse ) return createOutput ( 'disposable' , disposableResponse )
59
+ const domain = email . split ( '@' ) [ 1 ]
23
60
24
- const mx = await getBestMx ( domain )
25
- if ( ! mx ) return createOutput ( 'mx' , 'MX record not found' )
61
+ if ( options . validateDisposable ) {
62
+ const disposableResponse = await checkDisposable ( domain )
63
+ if ( disposableResponse )
64
+ return createOutput ( 'disposable' , disposableResponse )
65
+ }
66
+
67
+ if ( options . validateMx ) {
68
+ const mx = await getBestMx ( domain )
69
+ if ( ! mx ) return createOutput ( 'mx' , 'MX record not found' )
70
+ if ( options . validateSMTP ) {
71
+ return checkSMTP ( options . sender , email , mx . exchange )
72
+ }
73
+ }
26
74
27
- return checkSMTP ( sender , recipient , mx . exchange )
75
+ return createOutput ( )
28
76
}
29
77
30
78
async function main ( ) {
0 commit comments