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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ Email API:
- Batch send (Bulk) – [`batch/bulk.ts`](examples/bulk/bulk.ts)
- Batch send with Template (Transactional) – [`batch/template.ts`](examples/batch/template.ts)
- Batch send with Template (Bulk) – [`batch/template.ts`](examples/batch/template.ts)
- Sending domain management CRUD – [`sending-domains/everything.ts`](examples/sending-domains/everything.ts)
- Sending domain management CRUD and settings – [`sending-domains/everything.ts`](examples/sending-domains/everything.ts)
- Sending domain company info – [`company-info/everything.ts`](examples/company-info/everything.ts)
- Sending stats (aggregated and by domain, category, ESP, date) – [`stats/everything.ts`](examples/stats/everything.ts)
- Email logs (list with filters, get by message ID) – [`email-logs/everything.ts`](examples/email-logs/everything.ts)
- Webhooks CRUD – [`webhooks/everything.ts`](examples/webhooks/everything.ts)
Expand Down
43 changes: 43 additions & 0 deletions examples/company-info/everything.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { MailtrapClient } from "mailtrap";

const TOKEN = "<YOUR-TOKEN-HERE>";
const DOMAIN_ID = Number("<YOUR-DOMAIN-ID-HERE>");

const client = new MailtrapClient({ token: TOKEN });

async function companyInfoFlow() {
try {
// Create the company info required for compliance verification
const created = await client.companyInfo.create(DOMAIN_ID, {
name: "Mailtrap",
address: "123 Main St",
city: "San Francisco",
country: "US",
zip_code: "94105",
website_url: "https://mailtrap.io",
phone: "+1-555-0100",
privacy_policy_url: "https://mailtrap.io/privacy",
terms_of_service_url: "https://mailtrap.io/terms",
info_level: "business",
});
console.log("Created company info:", JSON.stringify(created, null, 2));

// Get the company info of a sending domain
const companyInfo = await client.companyInfo.get(DOMAIN_ID);
console.log("Company info:", JSON.stringify(companyInfo, null, 2));

// Update only some of the fields
const updated = await client.companyInfo.update(DOMAIN_ID, {
city: "New York",
zip_code: "10001",
});
console.log("Updated company info:", JSON.stringify(updated, null, 2));
} catch (error) {
console.error(
"Error in companyInfoFlow:",
error instanceof Error ? error.message : String(error)
);
}
}

companyInfoFlow();
12 changes: 11 additions & 1 deletion examples/sending-domains/everything.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,17 @@ async function sendingDomainsFlow() {
domain_name: "test-domain-" + Date.now() + ".com",
});
console.log("Created sending domain:", JSON.stringify(created, null, 2));


// Update the settings of the created domain
const updated = await client.sendingDomains.update(created.id, {
open_tracking_enabled: true,
click_tracking_enabled: true,
tracking_opt_out_enabled: true,
auto_unsubscribe_link_enabled: false,
inbound_enabled: false,
});
console.log("Updated sending domain:", JSON.stringify(updated, null, 2));

// Delete the created domain
await client.sendingDomains.delete(created.id);
console.log("Sending domain deleted");
Expand Down
109 changes: 109 additions & 0 deletions src/__tests__/lib/api/resources/CompanyInfo.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import axios, { AxiosInstance } from "axios";
import MockAdapter from "axios-mock-adapter";

import CompanyInfoBaseAPI from "../../../../lib/api/CompanyInfo";
import { CompanyInfo } from "../../../../types/api/company-info";

describe("lib/api/CompanyInfo: ", () => {
const axiosInstance: AxiosInstance = axios.create();
const mock = new MockAdapter(axiosInstance);

// Add the response interceptor that returns response.data
axiosInstance.interceptors.response.use((response) => response.data);

const companyInfoAPI = new CompanyInfoBaseAPI(axiosInstance);
const domainId = 999;
const companyInfoURL = `https://mailtrap.io/api/domains/${domainId}/company_info`;

describe("class CompanyInfoBaseAPI(): ", () => {
describe("init: ", () => {
it("initializes with all necessary params.", () => {
expect(companyInfoAPI).toHaveProperty("get");
expect(companyInfoAPI).toHaveProperty("create");
expect(companyInfoAPI).toHaveProperty("update");
});
});

describe("companyInfo.get(): ", () => {
it("should get the company info of a sending domain.", async () => {
const mockCompanyInfo: CompanyInfo = {
name: "Mailtrap",
address: "123 Main St",
city: "San Francisco",
country: "US",
phone: "+1-555-0100",
zip_code: "94105",
privacy_policy_url: "https://mailtrap.io/privacy",
terms_of_service_url: "https://mailtrap.io/terms",
website_url: "https://mailtrap.io",
info_level: "business",
};

mock.onGet(companyInfoURL).reply(200, { data: mockCompanyInfo });

const result = await companyInfoAPI.get(domainId);

expect(result).toEqual({ data: mockCompanyInfo });
});
});

describe("companyInfo.create(): ", () => {
it("should create the company info of a sending domain.", async () => {
const createParams = {
name: "Mailtrap",
address: "123 Main St",
city: "San Francisco",
country: "US",
zip_code: "94105",
website_url: "https://mailtrap.io",
info_level: "business" as const,
};

const mockCompanyInfo: CompanyInfo = {
...createParams,
phone: null,
privacy_policy_url: null,
terms_of_service_url: null,
};

mock
.onPost(companyInfoURL, { company_info: createParams })
.reply(200, { data: mockCompanyInfo });

const result = await companyInfoAPI.create(domainId, createParams);

expect(result).toEqual({ data: mockCompanyInfo });
});
});

describe("companyInfo.update(): ", () => {
it("should update the company info of a sending domain.", async () => {
const updateParams = {
city: "New York",
zip_code: "10001",
};

const mockCompanyInfo: CompanyInfo = {
name: "Mailtrap",
address: "123 Main St",
city: "New York",
country: "US",
phone: null,
zip_code: "10001",
privacy_policy_url: null,
terms_of_service_url: null,
website_url: "https://mailtrap.io",
info_level: "business",
};

mock
.onPatch(companyInfoURL, { company_info: updateParams })
.reply(200, { data: mockCompanyInfo });

const result = await companyInfoAPI.update(domainId, updateParams);

expect(result).toEqual({ data: mockCompanyInfo });
});
});
});
});
57 changes: 57 additions & 0 deletions src/__tests__/lib/api/resources/SendingDomains.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe("lib/api/SendingDomains: ", () => {
expect(sendingDomainsAPI).toHaveProperty("get");
expect(sendingDomainsAPI).toHaveProperty("getList");
expect(sendingDomainsAPI).toHaveProperty("create");
expect(sendingDomainsAPI).toHaveProperty("update");
expect(sendingDomainsAPI).toHaveProperty("delete");
expect(sendingDomainsAPI).toHaveProperty("sendSetupInstructions");
});
Expand Down Expand Up @@ -70,6 +71,7 @@ describe("lib/api/SendingDomains: ", () => {
dns_records: mockDnsRecords,
open_tracking_enabled: true,
click_tracking_enabled: true,
tracking_opt_out_enabled: false,
auto_unsubscribe_link_enabled: true,
custom_domain_tracking_enabled: true,
health_alerts_enabled: true,
Expand Down Expand Up @@ -122,6 +124,7 @@ describe("lib/api/SendingDomains: ", () => {
dns_records: mockDnsRecords,
open_tracking_enabled: true,
click_tracking_enabled: true,
tracking_opt_out_enabled: false,
auto_unsubscribe_link_enabled: true,
custom_domain_tracking_enabled: true,
health_alerts_enabled: true,
Expand Down Expand Up @@ -173,6 +176,7 @@ describe("lib/api/SendingDomains: ", () => {
dns_records: mockDnsRecords,
open_tracking_enabled: true,
click_tracking_enabled: true,
tracking_opt_out_enabled: false,
auto_unsubscribe_link_enabled: true,
custom_domain_tracking_enabled: true,
health_alerts_enabled: true,
Expand Down Expand Up @@ -200,6 +204,59 @@ describe("lib/api/SendingDomains: ", () => {
});
});

describe("sendingDomains.update(): ", () => {
it("should update a sending domain.", async () => {
const mockPermissions: SendingDomainPermissions = {
can_read: true,
can_update: true,
can_destroy: true,
};

const mockSendingDomain: SendingDomain = {
id: 435,
domain_name: "example.com",
demo: false,
compliance_status: "compliant",
dns_verified: true,
dns_verified_at: "2024-12-26T09:40:44.161Z",
dns_records: [],
open_tracking_enabled: true,
click_tracking_enabled: true,
tracking_opt_out_enabled: true,
auto_unsubscribe_link_enabled: false,
custom_domain_tracking_enabled: true,
health_alerts_enabled: true,
critical_alerts_enabled: true,
alert_recipient_email: "john.doe@example.com",
inbound_enabled: true,
inbound_verified: true,
permissions: mockPermissions,
};

const updateParams = {
open_tracking_enabled: true,
click_tracking_enabled: true,
tracking_opt_out_enabled: true,
auto_unsubscribe_link_enabled: false,
inbound_enabled: true,
};

mock
.onPatch(
`https://mailtrap.io/api/accounts/${testAccountId}/sending_domains/${mockSendingDomain.id}`,
{ sending_domain: updateParams }
)
.reply(200, mockSendingDomain);

const result = await sendingDomainsAPI.update(
mockSendingDomain.id,
updateParams
);

expect(result).toEqual(mockSendingDomain);
});
});

describe("sendingDomains.delete(): ", () => {
it("should delete a sending domain by id.", async () => {
const sendingDomainId = 999;
Expand Down
8 changes: 8 additions & 0 deletions src/lib/MailtrapClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import encodeMailBuffers from "./mail-buffer-encoder";
import handleSendingError from "./axios-logger";
import MailtrapError from "./MailtrapError";

import CompanyInfoBaseAPI from "./api/CompanyInfo";
import ContactEventsBaseAPI from "./api/ContactEvents";
import ContactExportsBaseAPI from "./api/ContactExports";
import ContactFieldsBaseAPI from "./api/ContactFields";
Expand Down Expand Up @@ -259,6 +260,13 @@ export default class MailtrapClient {
return new EmailCampaignsBaseAPI(this.axios);
}

/**
* Getter for Company Info API.
*/
get companyInfo() {
return new CompanyInfoBaseAPI(this.axios);
}
Comment thread
mklocek marked this conversation as resolved.

/**
* Getter for Organizations API. Requires `organizationId` in config.
*/
Expand Down
21 changes: 21 additions & 0 deletions src/lib/api/CompanyInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { AxiosInstance } from "axios";

import CompanyInfoApi from "./resources/CompanyInfo";

export default class CompanyInfoBaseAPI {
private client: AxiosInstance;

public get: CompanyInfoApi["get"];

public create: CompanyInfoApi["create"];

public update: CompanyInfoApi["update"];

constructor(client: AxiosInstance) {
this.client = client;
const companyInfo = new CompanyInfoApi(this.client);
this.get = companyInfo.get.bind(companyInfo);
this.create = companyInfo.create.bind(companyInfo);
this.update = companyInfo.update.bind(companyInfo);
}
}
3 changes: 3 additions & 0 deletions src/lib/api/SendingDomains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export default class SendingDomainsBaseAPI {

public create: SendingDomainsApi["create"];

public update: SendingDomainsApi["update"];

public delete: SendingDomainsApi["delete"];

public sendSetupInstructions: SendingDomainsApi["sendSetupInstructions"];
Expand All @@ -21,6 +23,7 @@ export default class SendingDomainsBaseAPI {
this.get = sendingDomains.get.bind(sendingDomains);
this.getList = sendingDomains.getList.bind(sendingDomains);
this.create = sendingDomains.create.bind(sendingDomains);
this.update = sendingDomains.update.bind(sendingDomains);
this.delete = sendingDomains.delete.bind(sendingDomains);
this.sendSetupInstructions =
sendingDomains.sendSetupInstructions.bind(sendingDomains);
Expand Down
Loading
Loading