@@ -8,16 +8,23 @@ import handleSendingError from "./axios-logger";
8
8
9
9
import GeneralAPI from "./api/General" ;
10
10
import TestingAPI from "./api/Testing" ;
11
- import BulkAPI from "./api/Bulk" ;
12
11
13
12
import CONFIG from "../config" ;
14
13
15
14
import { Mail , SendResponse , MailtrapClientConfig } from "../types/mailtrap" ;
15
+ import MailtrapError from "./MailtrapError" ;
16
16
17
17
const { CLIENT_SETTINGS , ERRORS } = CONFIG ;
18
- const { SENDING_ENDPOINT , MAX_REDIRECTS , USER_AGENT , TIMEOUT } =
19
- CLIENT_SETTINGS ;
20
- const { TEST_INBOX_ID_MISSING , ACCOUNT_ID_MISSING } = ERRORS ;
18
+ const {
19
+ SENDING_ENDPOINT ,
20
+ MAX_REDIRECTS ,
21
+ USER_AGENT ,
22
+ TIMEOUT ,
23
+ TESTING_ENDPOINT ,
24
+ BULK_ENDPOINT ,
25
+ } = CLIENT_SETTINGS ;
26
+ const { TEST_INBOX_ID_MISSING , ACCOUNT_ID_MISSING , BULK_SANDBOX_INCOMPATIBLE } =
27
+ ERRORS ;
21
28
22
29
/**
23
30
* Mailtrap client class. Initializes instance with available methods.
@@ -29,16 +36,20 @@ export default class MailtrapClient {
29
36
30
37
private accountId ?: number ;
31
38
32
- private testingAPI : TestingAPI ;
39
+ private bulk : boolean ;
33
40
34
- public general : GeneralAPI ;
35
-
36
- public bulk : BulkAPI ;
41
+ private sandbox : boolean ;
37
42
38
43
/**
39
44
* Initalizes axios instance with Mailtrap params.
40
45
*/
41
- constructor ( { token, testInboxId, accountId } : MailtrapClientConfig ) {
46
+ constructor ( {
47
+ token,
48
+ testInboxId,
49
+ accountId,
50
+ bulk = false ,
51
+ sandbox = false ,
52
+ } : MailtrapClientConfig ) {
42
53
this . axios = axios . create ( {
43
54
httpAgent : new http . Agent ( { keepAlive : true } ) ,
44
55
httpsAgent : new https . Agent ( { keepAlive : true } ) ,
@@ -51,50 +62,60 @@ export default class MailtrapClient {
51
62
timeout : TIMEOUT ,
52
63
} ) ;
53
64
54
- /**
55
- * Init Axios interceptors for handling response.data, errors.
56
- */
65
+ /** Init Axios interceptors for handling response.data, errors. */
57
66
this . axios . interceptors . response . use (
58
67
( response ) => response . data ,
59
68
handleSendingError
60
69
) ;
70
+
61
71
this . testInboxId = testInboxId ;
62
72
this . accountId = accountId ;
63
-
64
- /**
65
- * Initialize APIs.
66
- */
67
- this . testingAPI = new TestingAPI (
68
- this . axios ,
69
- this . testInboxId ,
70
- this . accountId
71
- ) ;
72
- this . general = new GeneralAPI ( this . axios , this . accountId ) ;
73
- this . bulk = new BulkAPI ( this . axios ) ;
73
+ this . bulk = bulk ;
74
+ this . sandbox = sandbox ;
74
75
}
75
76
76
77
/**
77
- * Getter for testing API. Warns if some of the required keys are missing.
78
+ * Getter for Testing API. Warns if some of the required keys are missing.
78
79
*/
79
80
get testing ( ) {
80
81
if ( ! this . testInboxId ) {
81
- // eslint-disable-next-line no-console
82
- console . warn ( TEST_INBOX_ID_MISSING ) ;
82
+ throw new MailtrapError ( TEST_INBOX_ID_MISSING ) ;
83
83
}
84
84
85
85
if ( ! this . accountId ) {
86
- // eslint-disable-next-line no-console
87
- console . warn ( ACCOUNT_ID_MISSING ) ;
86
+ throw new MailtrapError ( ACCOUNT_ID_MISSING ) ;
88
87
}
89
88
90
- return this . testingAPI ;
89
+ return new TestingAPI ( this . axios , this . accountId ) ;
90
+ }
91
+
92
+ /**
93
+ * Getter for General API.
94
+ */
95
+ get general ( ) {
96
+ return new GeneralAPI ( this . axios , this . accountId ) ;
91
97
}
92
98
93
99
/**
94
100
* Sends mail with given `mail` params. If there is error, rejects with `MailtrapError`.
95
101
*/
96
102
public async send ( mail : Mail ) : Promise < SendResponse > {
97
- const url = `${ SENDING_ENDPOINT } /api/send` ;
103
+ let host ;
104
+
105
+ if ( this . bulk && this . sandbox ) {
106
+ throw new MailtrapError ( BULK_SANDBOX_INCOMPATIBLE ) ;
107
+ } else if ( this . sandbox ) {
108
+ host = TESTING_ENDPOINT ;
109
+ } else if ( this . bulk ) {
110
+ host = BULK_ENDPOINT ;
111
+ } else {
112
+ host = SENDING_ENDPOINT ;
113
+ }
114
+
115
+ const url = `${ host } /api/send${
116
+ this . testInboxId ? `/${ this . testInboxId } ` : ""
117
+ } `;
118
+ console . log ( url ) ;
98
119
const preparedMail = encodeMailBuffers ( mail ) ;
99
120
100
121
return this . axios . post < SendResponse , SendResponse > ( url , preparedMail ) ;
0 commit comments