Skip to content

Commit ef8e1b9

Browse files
lib: refactor MailtrapClient to incapsulate bulk, sandbox send fnality
1 parent 0b49830 commit ef8e1b9

File tree

1 file changed

+51
-30
lines changed

1 file changed

+51
-30
lines changed

src/lib/MailtrapClient.ts

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,23 @@ import handleSendingError from "./axios-logger";
88

99
import GeneralAPI from "./api/General";
1010
import TestingAPI from "./api/Testing";
11-
import BulkAPI from "./api/Bulk";
1211

1312
import CONFIG from "../config";
1413

1514
import { Mail, SendResponse, MailtrapClientConfig } from "../types/mailtrap";
15+
import MailtrapError from "./MailtrapError";
1616

1717
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;
2128

2229
/**
2330
* Mailtrap client class. Initializes instance with available methods.
@@ -29,16 +36,20 @@ export default class MailtrapClient {
2936

3037
private accountId?: number;
3138

32-
private testingAPI: TestingAPI;
39+
private bulk: boolean;
3340

34-
public general: GeneralAPI;
35-
36-
public bulk: BulkAPI;
41+
private sandbox: boolean;
3742

3843
/**
3944
* Initalizes axios instance with Mailtrap params.
4045
*/
41-
constructor({ token, testInboxId, accountId }: MailtrapClientConfig) {
46+
constructor({
47+
token,
48+
testInboxId,
49+
accountId,
50+
bulk = false,
51+
sandbox = false,
52+
}: MailtrapClientConfig) {
4253
this.axios = axios.create({
4354
httpAgent: new http.Agent({ keepAlive: true }),
4455
httpsAgent: new https.Agent({ keepAlive: true }),
@@ -51,50 +62,60 @@ export default class MailtrapClient {
5162
timeout: TIMEOUT,
5263
});
5364

54-
/**
55-
* Init Axios interceptors for handling response.data, errors.
56-
*/
65+
/** Init Axios interceptors for handling response.data, errors. */
5766
this.axios.interceptors.response.use(
5867
(response) => response.data,
5968
handleSendingError
6069
);
70+
6171
this.testInboxId = testInboxId;
6272
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;
7475
}
7576

7677
/**
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.
7879
*/
7980
get testing() {
8081
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);
8383
}
8484

8585
if (!this.accountId) {
86-
// eslint-disable-next-line no-console
87-
console.warn(ACCOUNT_ID_MISSING);
86+
throw new MailtrapError(ACCOUNT_ID_MISSING);
8887
}
8988

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);
9197
}
9298

9399
/**
94100
* Sends mail with given `mail` params. If there is error, rejects with `MailtrapError`.
95101
*/
96102
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);
98119
const preparedMail = encodeMailBuffers(mail);
99120

100121
return this.axios.post<SendResponse, SendResponse>(url, preparedMail);

0 commit comments

Comments
 (0)