Skip to content
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
9 changes: 5 additions & 4 deletions src/lib/MailtrapClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import TestingAPI from "./api/Testing";
import ContactsBaseAPI from "./api/Contacts";
import ContactListsBaseAPI from "./api/ContactLists";
import TemplatesBaseAPI from "./api/Templates";
import SuppressionsBaseAPI from "./api/Suppressions";

import CONFIG from "../config";

Expand All @@ -22,16 +23,15 @@ import {
BatchSendResponse,
BatchSendRequest,
} from "../types/mailtrap";
import SuppressionsBaseAPI from "./api/Suppressions";

const { CLIENT_SETTINGS, ERRORS } = CONFIG;
const {
SENDING_ENDPOINT,
MAX_REDIRECTS,
USER_AGENT,
TIMEOUT,
TESTING_ENDPOINT,
BULK_ENDPOINT,
USER_AGENT,
} = CLIENT_SETTINGS;
const { ACCOUNT_ID_MISSING, BULK_SANDBOX_INCOMPATIBLE, TEST_INBOX_ID_MISSING } =
ERRORS;
Expand Down Expand Up @@ -59,14 +59,15 @@ export default class MailtrapClient {
accountId,
bulk = false,
sandbox = false,
userAgent,
}: MailtrapClientConfig) {
this.axios = axios.create({
httpAgent: new http.Agent({ keepAlive: true }),
httpsAgent: new https.Agent({ keepAlive: true }),
headers: {
Authorization: `Bearer ${token}`,
Connection: "keep-alive",
"User-Agent": USER_AGENT,
"User-Agent": userAgent || USER_AGENT,
},
maxRedirects: MAX_REDIRECTS,
timeout: TIMEOUT,
Expand Down Expand Up @@ -182,12 +183,12 @@ export default class MailtrapClient {
*/
public async send(mail: Mail): Promise<SendResponse> {
const host = this.determineHost();

this.validateTestInboxIdPresence();

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

const preparedMail = encodeMailBuffers(mail);

return this.axios.post<SendResponse, SendResponse>(url, preparedMail);
Expand Down
1 change: 1 addition & 0 deletions src/types/mailtrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export type MailtrapClientConfig = {
accountId?: number;
bulk?: boolean;
sandbox?: boolean;
userAgent?: string;
};

export type BatchMail = Mail[];
Expand Down