-
Notifications
You must be signed in to change notification settings - Fork 13
Sending domains API #93
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
Open
narekhovhannisyan
wants to merge
21
commits into
main
Choose a base branch
from
create-sending-domain
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
cc608b9
examples: add example script for sending domains API usage
narekhovhannisyan c2d72be
test: add unit tests for sending domains API in MailtrapClient
narekhovhannisyan f9b0760
test: implement unit tests for SendingDomains API methods including g…
narekhovhannisyan b6449f8
lib: add getter for SendingDomainsBaseAPI in MailtrapClient
narekhovhannisyan 51a77c7
api: implement SendingDomainsBaseAPI class for managing sending domains
narekhovhannisyan 808d825
resources: add SendingDomainsApi class for managing sending domains w…
narekhovhannisyan f5e1be1
types: add TypeScript interfaces for sending domains and DNS records
narekhovhannisyan c6a2957
Merge branch 'main' of github.com:railsware/mailtrap-nodejs into crea…
narekhovhannisyan 04166b4
types: add documentation for ContactFields and ContactField interface…
narekhovhannisyan 7764c04
types: add SendingDomainsResponse interface to define the structure o…
narekhovhannisyan c641885
refactor: update getList method in SendingDomainsApi to return Sendin…
narekhovhannisyan a944630
test: update SendingDomains test to match new response structure with…
narekhovhannisyan 8c9a923
refactor: update sending domains example to improve flow and structure
narekhovhannisyan 643dcf4
types: update SendingDomain interface to allow null values for dns_ve…
narekhovhannisyan a0aa0cb
refactor: simplify getList method and update create method to use cor…
narekhovhannisyan 64b885e
test: adjust SendingDomains test to validate updated response structu…
narekhovhannisyan 89f5a27
types: remove SetupInstructionsResponse interface as it is no longer …
narekhovhannisyan 977a9d9
refactor: rename setup instructions endpoint and simplify post reques…
narekhovhannisyan 20f7294
test: update SendingDomains test to reflect changes in setup instruct…
narekhovhannisyan cae8810
refactor: enhance sending domains example with improved error handlin…
narekhovhannisyan eedec50
test: remove unused SetupInstructionsResponse import from SendingDoma…
narekhovhannisyan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { MailtrapClient } from "../../src/index"; | ||
|
||
const TOKEN = "<YOUR-TOKEN-HERE>"; | ||
const ACCOUNT_ID = "<YOUR-ACCOUNT-ID-HERE>"; | ||
|
||
const client = new MailtrapClient({ | ||
token: TOKEN, | ||
accountId: Number(ACCOUNT_ID), | ||
}); | ||
|
||
async function sendingDomainsFlow() { | ||
try { | ||
// Get all sending domains | ||
const all = await client.sendingDomains.getList(); | ||
console.log("All sending domains:", JSON.stringify(all, null, 2)); | ||
|
||
if (!all.data || all.data.length === 0) { | ||
console.log("No sending domains found for this account."); | ||
return; | ||
} | ||
|
||
// Get a specific sending domain | ||
const one = await client.sendingDomains.get(all.data[0].id); | ||
console.log("One sending domain:", JSON.stringify(one, null, 2)); | ||
|
||
// Send setup instructions | ||
const setupResponse = await client.sendingDomains.sendSetupInstructions( | ||
all.data[0].id, | ||
"admin@example.com" | ||
); | ||
console.log("Setup instructions sent"); | ||
|
||
// Create a new sending domain | ||
const created = await client.sendingDomains.create({ | ||
domain_name: "test-domain-" + Date.now() + ".com", | ||
}); | ||
console.log("Created sending domain:", JSON.stringify(created, null, 2)); | ||
|
||
// Delete the created domain | ||
await client.sendingDomains.delete(created.id); | ||
console.log("Sending domain deleted"); | ||
|
||
} catch (error) { | ||
console.error("Error in sendingDomainsFlow:", error instanceof Error ? error.message : String(error)); | ||
} | ||
} | ||
|
||
sendingDomainsFlow(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,233 @@ | ||
import axios, { AxiosInstance } from "axios"; | ||
import MockAdapter from "axios-mock-adapter"; | ||
|
||
import SendingDomainsBaseAPI from "../../../../lib/api/SendingDomains"; | ||
import { | ||
SendingDomain, | ||
DnsRecord, | ||
SendingDomainPermissions, | ||
} from "../../../../types/api/sending-domains"; | ||
|
||
describe("lib/api/SendingDomains: ", () => { | ||
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 testAccountId = 100; | ||
const sendingDomainsAPI = new SendingDomainsBaseAPI( | ||
axiosInstance, | ||
testAccountId | ||
); | ||
|
||
describe("class SendingDomainsBaseAPI(): ", () => { | ||
describe("init: ", () => { | ||
it("initializes with all necessary params.", () => { | ||
expect(sendingDomainsAPI).toHaveProperty("get"); | ||
expect(sendingDomainsAPI).toHaveProperty("getList"); | ||
expect(sendingDomainsAPI).toHaveProperty("create"); | ||
expect(sendingDomainsAPI).toHaveProperty("delete"); | ||
expect(sendingDomainsAPI).toHaveProperty("sendSetupInstructions"); | ||
}); | ||
}); | ||
|
||
describe("sendingDomains.getList(): ", () => { | ||
it("should get sending domains list.", async () => { | ||
const mockDnsRecords: DnsRecord[] = [ | ||
{ | ||
key: "verification", | ||
domain: "ve6wza2rbpe60x7z.example.com", | ||
type: "CNAME", | ||
value: "smtp.mailtrap.live", | ||
status: "pass", | ||
name: "ve6wza2rbpe60x7z", | ||
}, | ||
{ | ||
key: "spf", | ||
domain: "example.com", | ||
type: "TXT", | ||
value: "v=spf1 include:_spf.smtp.mailtrap.live ~all", | ||
status: "pass", | ||
name: "", | ||
}, | ||
]; | ||
|
||
const mockPermissions: SendingDomainPermissions = { | ||
can_read: true, | ||
can_update: true, | ||
can_destroy: true, | ||
}; | ||
|
||
const mockSendingDomains: 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: mockDnsRecords, | ||
open_tracking_enabled: true, | ||
click_tracking_enabled: true, | ||
auto_unsubscribe_link_enabled: true, | ||
custom_domain_tracking_enabled: true, | ||
health_alerts_enabled: true, | ||
critical_alerts_enabled: true, | ||
alert_recipient_email: "john.doe@example.com", | ||
permissions: mockPermissions, | ||
}, | ||
]; | ||
|
||
mock | ||
.onGet( | ||
`https://mailtrap.io/api/accounts/${testAccountId}/sending_domains` | ||
) | ||
.reply(200, { data: mockSendingDomains }); | ||
|
||
const result = await sendingDomainsAPI.getList(); | ||
|
||
expect(result).toEqual({ data: mockSendingDomains }); | ||
}); | ||
}); | ||
|
||
describe("sendingDomains.get(): ", () => { | ||
it("should get a single sending domain by id.", async () => { | ||
const mockDnsRecords: DnsRecord[] = [ | ||
{ | ||
key: "verification", | ||
domain: "ve6wza2rbpe60x7z.example.com", | ||
type: "CNAME", | ||
value: "smtp.mailtrap.live", | ||
status: "pass", | ||
name: "ve6wza2rbpe60x7z", | ||
}, | ||
]; | ||
|
||
const mockPermissions: SendingDomainPermissions = { | ||
can_read: true, | ||
can_update: true, | ||
can_destroy: true, | ||
}; | ||
|
||
const mockSendingDomain: SendingDomain = { | ||
id: 999, | ||
domain_name: "example.com", | ||
demo: false, | ||
compliance_status: "compliant", | ||
dns_verified: true, | ||
dns_verified_at: "2024-12-26T09:40:44.161Z", | ||
dns_records: mockDnsRecords, | ||
open_tracking_enabled: true, | ||
click_tracking_enabled: true, | ||
auto_unsubscribe_link_enabled: true, | ||
custom_domain_tracking_enabled: true, | ||
health_alerts_enabled: true, | ||
critical_alerts_enabled: true, | ||
alert_recipient_email: "john.doe@example.com", | ||
permissions: mockPermissions, | ||
}; | ||
|
||
mock | ||
.onGet( | ||
`https://mailtrap.io/api/accounts/${testAccountId}/sending_domains/${mockSendingDomain.id}` | ||
) | ||
.reply(200, mockSendingDomain); | ||
|
||
const result = await sendingDomainsAPI.get(mockSendingDomain.id); | ||
|
||
expect(result).toEqual(mockSendingDomain); | ||
}); | ||
}); | ||
|
||
describe("sendingDomains.create(): ", () => { | ||
it("should create a new sending domain.", async () => { | ||
const mockDnsRecords: DnsRecord[] = [ | ||
{ | ||
key: "verification", | ||
domain: "ve6wza2rbpe60x7z.newdomain.com", | ||
type: "CNAME", | ||
value: "smtp.mailtrap.live", | ||
status: "pass", | ||
name: "ve6wza2rbpe60x7z", | ||
}, | ||
]; | ||
|
||
const mockPermissions: SendingDomainPermissions = { | ||
can_read: true, | ||
can_update: true, | ||
can_destroy: true, | ||
}; | ||
|
||
const mockSendingDomain: SendingDomain = { | ||
id: 436, | ||
domain_name: "newdomain.com", | ||
demo: false, | ||
compliance_status: "pending", | ||
dns_verified: false, | ||
dns_verified_at: "", | ||
dns_records: mockDnsRecords, | ||
open_tracking_enabled: true, | ||
click_tracking_enabled: true, | ||
auto_unsubscribe_link_enabled: true, | ||
custom_domain_tracking_enabled: true, | ||
health_alerts_enabled: true, | ||
critical_alerts_enabled: true, | ||
alert_recipient_email: "admin@newdomain.com", | ||
permissions: mockPermissions, | ||
}; | ||
|
||
const createParams = { | ||
domain_name: "newdomain.com", | ||
}; | ||
|
||
mock | ||
.onPost( | ||
`https://mailtrap.io/api/accounts/${testAccountId}/sending_domains`, | ||
{ sending_domain: createParams } | ||
) | ||
.reply(201, mockSendingDomain); | ||
|
||
const result = await sendingDomainsAPI.create(createParams); | ||
|
||
expect(result).toEqual(mockSendingDomain); | ||
}); | ||
}); | ||
|
||
describe("sendingDomains.delete(): ", () => { | ||
it("should delete a sending domain by id.", async () => { | ||
const sendingDomainId = 999; | ||
|
||
mock | ||
.onDelete( | ||
`https://mailtrap.io/api/accounts/${testAccountId}/sending_domains/${sendingDomainId}` | ||
) | ||
.reply(204); | ||
|
||
const result = await sendingDomainsAPI.delete(sendingDomainId); | ||
|
||
expect(result).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe("sendingDomains.sendSetupInstructions(): ", () => { | ||
it("should send setup instructions for a sending domain.", async () => { | ||
const sendingDomainId = 999; | ||
const email = "admin@example.com"; | ||
|
||
mock | ||
.onPost( | ||
`https://mailtrap.io/api/accounts/${testAccountId}/sending_domains/${sendingDomainId}/send_setup_instructions` | ||
) | ||
.reply(204); | ||
|
||
const result = await sendingDomainsAPI.sendSetupInstructions( | ||
sendingDomainId, | ||
); | ||
|
||
expect(result).toBeUndefined(); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { AxiosInstance } from "axios"; | ||
|
||
import SendingDomainsApi from "./resources/SendingDomains"; | ||
|
||
export default class SendingDomainsBaseAPI { | ||
private client: AxiosInstance; | ||
|
||
public get: SendingDomainsApi["get"]; | ||
|
||
public getList: SendingDomainsApi["getList"]; | ||
|
||
public create: SendingDomainsApi["create"]; | ||
|
||
public delete: SendingDomainsApi["delete"]; | ||
|
||
public sendSetupInstructions: SendingDomainsApi["sendSetupInstructions"]; | ||
|
||
constructor(client: AxiosInstance, accountId: number) { | ||
this.client = client; | ||
const sendingDomains = new SendingDomainsApi(this.client, accountId); | ||
this.get = sendingDomains.get.bind(sendingDomains); | ||
this.getList = sendingDomains.getList.bind(sendingDomains); | ||
this.create = sendingDomains.create.bind(sendingDomains); | ||
this.delete = sendingDomains.delete.bind(sendingDomains); | ||
this.sendSetupInstructions = | ||
sendingDomains.sendSetupInstructions.bind(sendingDomains); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.