Skip to content

Make testInboxID optional param #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 50 additions & 13 deletions src/__tests__/lib/mailtrap-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import TemplatesBaseAPI from "../../lib/api/Templates";

const { ERRORS, CLIENT_SETTINGS } = CONFIG;
const { TESTING_ENDPOINT, BULK_ENDPOINT, SENDING_ENDPOINT } = CLIENT_SETTINGS;
const { TEST_INBOX_ID_MISSING, ACCOUNT_ID_MISSING, BULK_SANDBOX_INCOMPATIBLE } =
const { ACCOUNT_ID_MISSING, BULK_SANDBOX_INCOMPATIBLE, TEST_INBOX_ID_MISSING } =
ERRORS;

describe("lib/mailtrap-client: ", () => {
Expand Down Expand Up @@ -331,7 +331,49 @@ describe("lib/mailtrap-client: ", () => {
}
});

it("throws MailtrapError(TEST_INBOX_ID_MISSING) when sending in sandbox mode without testInboxId", async () => {
const client = new MailtrapClient({
token: "MY_API_TOKEN",
sandbox: true,
accountId: 123,
});

await expect(
client.send({
from: { email: "[email protected]", name: "Sender" },
to: [{ email: "[email protected]" }],
subject: "Test",
text: "Body",
})
).rejects.toEqual(new MailtrapError(TEST_INBOX_ID_MISSING));
});

describe("batch sending:", () => {
it("throws MailtrapError(TEST_INBOX_ID_MISSING) when batch sending in sandbox mode without testInboxId", async () => {
const client = new MailtrapClient({
token: "MY_API_TOKEN",
sandbox: true,
accountId: 123,
});

const batchData = {
base: {
from: { email: "[email protected]", name: "Sender" },
subject: "Test",
text: "Body",
},
requests: [
{
to: [{ email: "[email protected]" }],
},
],
};

await expect(client.batchSend(batchData)).rejects.toEqual(
new MailtrapError(TEST_INBOX_ID_MISSING)
);
});

it("rejects with Mailtrap error when bulk and sandbox modes are used together", async () => {
const batchClient = new MailtrapClient({
token: "MY_API_TOKEN",
Expand Down Expand Up @@ -664,7 +706,7 @@ describe("lib/mailtrap-client: ", () => {
});

describe("get testing(): ", () => {
it("rejects with Mailtrap error, when `testInboxId` is missing.", () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would also be nice to have a test that the absence of testInboxId doesn't raise an error when client.testing is called.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And remove testInboxId from all client setup cases where it's not needed anymore (like here).

it("rejects with Mailtrap error, when `accountId` is missing.", () => {
const client = new MailtrapClient({
token: "MY_API_TOKEN",
});
Expand All @@ -674,30 +716,25 @@ describe("lib/mailtrap-client: ", () => {
try {
client.testing;
} catch (error) {
expect(error).toEqual(new MailtrapError(TEST_INBOX_ID_MISSING));
expect(error).toEqual(new MailtrapError(ACCOUNT_ID_MISSING));
}
});

it("rejects with Mailtrap error, when `accountId` is missing.", () => {
it("returns testing API object when accountId is provided, even without testInboxId", () => {
const client = new MailtrapClient({
token: "MY_API_TOKEN",
testInboxId: 5,
accountId: 123,
// testInboxId is intentionally omitted
});

expect.assertions(1);

try {
client.testing;
} catch (error) {
expect(error).toEqual(new MailtrapError(ACCOUNT_ID_MISSING));
}
const testingClient = client.testing;
expect(testingClient).toBeInstanceOf(TestingAPI);
});

it("returns testing API object, console warn is called twice.", () => {
const client = new MailtrapClient({
token: "MY_API_TOKEN",
sandbox: true,
testInboxId: 10,
accountId: 10,
});
expect.assertions(1);
Expand Down
29 changes: 22 additions & 7 deletions src/lib/MailtrapClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const {
TESTING_ENDPOINT,
BULK_ENDPOINT,
} = CLIENT_SETTINGS;
const { TEST_INBOX_ID_MISSING, ACCOUNT_ID_MISSING, BULK_SANDBOX_INCOMPATIBLE } =
const { ACCOUNT_ID_MISSING, BULK_SANDBOX_INCOMPATIBLE, TEST_INBOX_ID_MISSING } =
ERRORS;

/**
Expand Down Expand Up @@ -93,13 +93,18 @@ export default class MailtrapClient {
}

/**
* Getter for Testing API. Warns if some of the required keys are missing.
* Validates that test inbox ID is present, throws MailtrapError if missing.
*/
get testing() {
if (!this.testInboxId) {
private validateTestInboxIdPresence(): void {
if (this.sandbox && !this.testInboxId) {
throw new MailtrapError(TEST_INBOX_ID_MISSING);
}
}

/**
* Getter for Testing API. Warns if some of the required keys are missing.
*/
get testing() {
this.validateAccountIdPresence();

return new TestingAPI(this.axios, this.accountId);
Expand Down Expand Up @@ -132,6 +137,9 @@ export default class MailtrapClient {
return new ContactListsBaseAPI(this.axios, this.accountId);
}

/**
* Getter for Templates API.
*/
get templates() {
this.validateAccountIdPresence();

Expand Down Expand Up @@ -164,8 +172,11 @@ export default class MailtrapClient {
*/
public async send(mail: Mail): Promise<SendResponse> {
const host = this.determineHost();

this.validateTestInboxIdPresence();

const url = `${host}/api/send${
this.testInboxId ? `/${this.testInboxId}` : ""
this.sandbox && this.testInboxId ? `/${this.testInboxId}` : ""
}`;
const preparedMail = encodeMailBuffers(mail);

Expand All @@ -181,9 +192,13 @@ export default class MailtrapClient {
): Promise<BatchSendResponse> {
const { requests, base } = request;
const host = this.determineHost();
const ifSandbox =

this.validateTestInboxIdPresence();

const sandbox =
this.sandbox && this.testInboxId ? `/${this.testInboxId}` : "";
const url = `${host}/api/batch${ifSandbox}`;

const url = `${host}/api/batch${sandbox}`;

const preparedBase = base ? encodeMailBuffers(base) : undefined;
const preparedRequests = requests.map((singleRequest) =>
Expand Down