Skip to content

Commit 0a2d134

Browse files
refactor: rearrange error constants and update testing getter to validate test inbox ID only in sandbox mode
1 parent 6d5ba68 commit 0a2d134

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/lib/MailtrapClient.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const {
3232
TESTING_ENDPOINT,
3333
BULK_ENDPOINT,
3434
} = CLIENT_SETTINGS;
35-
const { TEST_INBOX_ID_MISSING, ACCOUNT_ID_MISSING, BULK_SANDBOX_INCOMPATIBLE } =
35+
const { ACCOUNT_ID_MISSING, BULK_SANDBOX_INCOMPATIBLE, TEST_INBOX_ID_MISSING } =
3636
ERRORS;
3737

3838
/**
@@ -96,10 +96,6 @@ export default class MailtrapClient {
9696
* Getter for Testing API. Warns if some of the required keys are missing.
9797
*/
9898
get testing() {
99-
if (!this.testInboxId) {
100-
throw new MailtrapError(TEST_INBOX_ID_MISSING);
101-
}
102-
10399
this.validateAccountIdPresence();
104100

105101
return new TestingAPI(this.axios, this.accountId);
@@ -132,6 +128,9 @@ export default class MailtrapClient {
132128
return new ContactListsBaseAPI(this.axios, this.accountId);
133129
}
134130

131+
/**
132+
* Getter for Templates API.
133+
*/
135134
get templates() {
136135
this.validateAccountIdPresence();
137136

@@ -164,8 +163,13 @@ export default class MailtrapClient {
164163
*/
165164
public async send(mail: Mail): Promise<SendResponse> {
166165
const host = this.determineHost();
166+
167+
if (this.sandbox && !this.testInboxId) {
168+
throw new MailtrapError(TEST_INBOX_ID_MISSING);
169+
}
170+
167171
const url = `${host}/api/send${
168-
this.testInboxId ? `/${this.testInboxId}` : ""
172+
this.sandbox && this.testInboxId ? `/${this.testInboxId}` : ""
169173
}`;
170174
const preparedMail = encodeMailBuffers(mail);
171175

0 commit comments

Comments
 (0)