Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 80a19de

Browse files
committed
refactor: refactor core and check domain implementation
1 parent e2f558a commit 80a19de

File tree

6 files changed

+48
-28
lines changed

6 files changed

+48
-28
lines changed

src/emails/check-domain.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,50 @@
11
import type { MailChannels } from "../mailchannels";
22
import type { EmailsCheckDomainOptions, EmailsCheckDomainResponse } from "../types/emails";
33

4-
export default (mailchannels: MailChannels) => {
4+
export const checkDomain = (mailchannels: MailChannels) => {
55
/**
66
* Validates a domain's email authentication setup by retrieving its DKIM, SPF, and Domain Lockdown status. This endpoint checks whether the domain is properly configured for secure email delivery.
77
* @param options - The domain options to check
88
* @example
99
* ```ts
1010
* const mailchannels = new MailChannels("your-api-key");
11-
* const { success } = await mailchannels.emails.checkDomain({
12-
* dkim: {
11+
* const { results } = await mailchannels.emails.checkDomain({
12+
* dkim: [{
1313
* domain: "example.com",
1414
* privateKey
1515
* selector: "mailchannels"
16-
* },
16+
* }],
1717
* domain: "example.com",
1818
* senderId: "sender-id"
1919
* })
2020
* ```
2121
*/
2222
return async (options: EmailsCheckDomainOptions) => {
2323
const { dkim, domain, senderId } = options;
24-
return mailchannels.post<EmailsCheckDomainResponse>("/tx/v1/check-domain", {
24+
const dkimOptions = Array.isArray(dkim) ? dkim : [dkim];
25+
26+
const check = await mailchannels.post<EmailsCheckDomainResponse>("/tx/v1/check-domain", {
2527
body: {
26-
dkim_settings: [{
27-
dkim_domain: dkim.domain,
28-
dkim_private_key: dkim.privateKey,
29-
dkim_selector: dkim.selector
30-
}],
28+
dkim_settings: dkimOptions.map(({ domain, privateKey, selector }) => ({
29+
dkim_domain: domain,
30+
dkim_private_key: privateKey,
31+
dkim_selector: selector
32+
})),
3133
domain,
3234
sender_id: senderId
3335
}
3436
});
37+
38+
return {
39+
results: {
40+
spf: check.check_results.spf,
41+
domainLockdown: check.check_results.domain_lockdown,
42+
dkim: check.check_results.dkim.map(({ dkim_domain, dkim_selector, verdict }) => ({
43+
dkimDomain: dkim_domain,
44+
dkimSelector: dkim_selector,
45+
verdict
46+
}))
47+
}
48+
};
3549
};
3650
};

src/emails/index.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import type { MailChannels } from "../mailchannels";
2-
import checkDomain from "./check-domain";
3-
import send from "./send";
2+
import { send } from "./send";
3+
import { checkDomain } from "./check-domain";
4+
import { applyMailChannels } from "../utils/core";
45

5-
export function defineEmails (mailchannels: MailChannels) {
6-
return {
7-
send: send(mailchannels),
8-
checkDomain: checkDomain(mailchannels)
9-
};
10-
}
6+
7+
export const defineEmails = (mailchannels: MailChannels) => applyMailChannels(mailchannels, {
8+
send,
9+
checkDomain
10+
});

src/emails/send.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { MailChannels } from "../mailchannels";
22
import type { EmailsSendOptions, EmailsSendPayload, EmailsSendContent } from "../types/emails";
3-
import { parseRecipient, parseArrayRecipients } from "../utils/helpers";
3+
import { parseRecipient, parseArrayRecipients } from "../utils/recipients";
44

5-
export default (mailchannels: MailChannels) => {
5+
export const send = (mailchannels: MailChannels) => {
66
/**
77
* Send an email using MailChannels Email API
88
* @param options - The email options to send

src/types/emails/check-domain.d.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ interface EmailsCheckDomainDkim {
55
}
66

77
export interface EmailsCheckDomainOptions {
8-
dkim: EmailsCheckDomainDkim;
8+
dkim: EmailsCheckDomainDkim[] | EmailsCheckDomainDkim;
99
domain: string;
1010
senderId: string;
1111
}
@@ -20,12 +20,10 @@ export interface EmailsCheckDomainResponse {
2020
domain_lockdown: {
2121
verdict: Extract<EmailsCheckDomainVerdict, "passed" | "failed">;
2222
};
23-
dkim: [
24-
{
25-
dkim_domain: "mappedlove.com";
26-
dkim_selector: "mailchannels";
27-
verdict: Extract<EmailsCheckDomainVerdict, "passed" | "failed">;
28-
}
29-
];
23+
dkim: {
24+
dkim_domain: "mappedlove.com";
25+
dkim_selector: "mailchannels";
26+
verdict: Extract<EmailsCheckDomainVerdict, "passed" | "failed">;
27+
}[];
3028
};
3129
}

src/utils/core.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { MailChannels } from "../mailchannels";
2+
3+
export const applyMailChannels = <T extends Record<string, (mc: MailChannels) => unknown>>(
4+
mc: MailChannels,
5+
exports: T
6+
) => Object.fromEntries(
7+
Object.entries(exports).map(([key, fn]) => [key, fn(mc)])
8+
) as { [K in keyof T]: ReturnType<T[K]> };
File renamed without changes.

0 commit comments

Comments
 (0)