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
7 changes: 4 additions & 3 deletions api/contact/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { Resend } from "resend";
export interface Config {
resend: Resend | null;
fromEmail: string | undefined;
toEmail: string | string[] | undefined;
toEmails: string[];
allowedOrigins: string[];
}

const apiKey = process.env["RESEND_API_KEY"];
const fromEmail = process.env["FROM_EMAIL"];
const toEmail = process.env["TO_EMAIL"];
const toEmailsRaw = process.env["TO_EMAIL"] ?? "";
const toEmails = toEmailsRaw.split(",").map(o => o.trim()).filter(Boolean);
const allowedOriginsRaw = process.env["ALLOWED_ORIGINS"] ?? "";
const allowedOrigins = allowedOriginsRaw.split(",").map(o => o.trim()).filter(Boolean);

Expand All @@ -19,7 +20,7 @@ if (apiKey) resend = new Resend(apiKey);
export const config: Config = {
resend,
fromEmail,
toEmail,
toEmails,
allowedOrigins
}

6 changes: 3 additions & 3 deletions api/contact/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import type { Config } from "./config.js";
export interface EmailConfig {
client: Resend;
from: string;
to: string | string[];
to: string[];
}

export function getEmailConfig(config: Config): EmailConfig | null {
if (
!config.resend ||
!config.fromEmail?.trim() ||
!config.toEmail?.length
!config.toEmails?.length
) return null;
return { client: config.resend, from: config.fromEmail, to: config.toEmail };
return { client: config.resend, from: config.fromEmail, to: config.toEmails };
}

export async function sendEmail(config: EmailConfig, body: ContactBody): Promise<void> {
Expand Down
11 changes: 5 additions & 6 deletions tests/contact/email.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,17 @@ describe("email.ts", () => {

describe("getEmailConfig", () => {
it("returns null if config missing or empty props", () => {
expect(getEmailConfig({ resend: null as any, fromEmail: "a@b.com", toEmail: "c@d.com" })).toBeNull();
expect(getEmailConfig({ resend: {} as any, fromEmail: "", toEmail: "c@d.com" })).toBeNull();
expect(getEmailConfig({ resend: {} as any, fromEmail: "a@b.com", toEmail: "" })).toBeNull();
expect(getEmailConfig({ resend: {} as any, fromEmail: "a@b.com", toEmail: [] })).toBeNull();
expect(getEmailConfig({ resend: null as any, fromEmail: "a@b.com", toEmails: ["c@d.com"] })).toBeNull();
expect(getEmailConfig({ resend: {} as any, fromEmail: "", toEmails: ["c@d.com"] })).toBeNull();
expect(getEmailConfig({ resend: {} as any, fromEmail: "a@b.com", toEmails: [] })).toBeNull();
});

it("returns EmailConfig when valid", () => {
const result = getEmailConfig({ resend: mockResend, fromEmail: "from@test.com", toEmail: "to@test.com" });
const result = getEmailConfig({ resend: mockResend, fromEmail: "from@test.com", toEmails: ["to@test.com"] });
expect(result).toMatchObject({
client: mockResend,
from: 'from@test.com',
to: 'to@test.com'
to: ['to@test.com']
});
});
});
Expand Down
Loading