Skip to content

Commit dc5cd84

Browse files
Merge pull request #85 from railsware/dynamic-user-agent
Dynamic user agent
2 parents e2dcd4e + 84b609a commit dc5cd84

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

src/lib/MailtrapClient.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import TestingAPI from "./api/Testing";
1212
import ContactsBaseAPI from "./api/Contacts";
1313
import ContactListsBaseAPI from "./api/ContactLists";
1414
import TemplatesBaseAPI from "./api/Templates";
15+
import SuppressionsBaseAPI from "./api/Suppressions";
1516

1617
import CONFIG from "../config";
1718

@@ -22,16 +23,15 @@ import {
2223
BatchSendResponse,
2324
BatchSendRequest,
2425
} from "../types/mailtrap";
25-
import SuppressionsBaseAPI from "./api/Suppressions";
2626

2727
const { CLIENT_SETTINGS, ERRORS } = CONFIG;
2828
const {
2929
SENDING_ENDPOINT,
3030
MAX_REDIRECTS,
31-
USER_AGENT,
3231
TIMEOUT,
3332
TESTING_ENDPOINT,
3433
BULK_ENDPOINT,
34+
USER_AGENT,
3535
} = CLIENT_SETTINGS;
3636
const { ACCOUNT_ID_MISSING, BULK_SANDBOX_INCOMPATIBLE, TEST_INBOX_ID_MISSING } =
3737
ERRORS;
@@ -59,14 +59,15 @@ export default class MailtrapClient {
5959
accountId,
6060
bulk = false,
6161
sandbox = false,
62+
userAgent,
6263
}: MailtrapClientConfig) {
6364
this.axios = axios.create({
6465
httpAgent: new http.Agent({ keepAlive: true }),
6566
httpsAgent: new https.Agent({ keepAlive: true }),
6667
headers: {
6768
Authorization: `Bearer ${token}`,
6869
Connection: "keep-alive",
69-
"User-Agent": USER_AGENT,
70+
"User-Agent": userAgent || USER_AGENT,
7071
},
7172
maxRedirects: MAX_REDIRECTS,
7273
timeout: TIMEOUT,
@@ -182,12 +183,12 @@ export default class MailtrapClient {
182183
*/
183184
public async send(mail: Mail): Promise<SendResponse> {
184185
const host = this.determineHost();
185-
186186
this.validateTestInboxIdPresence();
187187

188188
const url = `${host}/api/send${
189189
this.sandbox && this.testInboxId ? `/${this.testInboxId}` : ""
190190
}`;
191+
191192
const preparedMail = encodeMailBuffers(mail);
192193

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

src/types/mailtrap.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export type MailtrapClientConfig = {
6767
accountId?: number;
6868
bulk?: boolean;
6969
sandbox?: boolean;
70+
userAgent?: string;
7071
};
7172

7273
export type BatchMail = Mail[];

0 commit comments

Comments
 (0)