UNPKG

@ideal-photography/shared

Version:

Shared MongoDB and utility logic for Ideal Photography PWAs: users, products, services, bookings, orders/cart, galleries, reviews, notifications, campaigns, settings, audit logs, minimart items/orders, and push notification subscriptions.

68 lines (59 loc) 2.39 kB
import { Resend } from 'resend'; const RESEND_API_KEY = process.env.RESEND_API_KEY || ''; const DEFAULT_FROM = process.env.EMAIL_FROM || ''; let resendClient = null; function getResendClient() { if (resendClient) return resendClient; if (!RESEND_API_KEY) { throw new Error('RESEND_API_KEY is missing. Please set it in your environment.'); } resendClient = new Resend(RESEND_API_KEY); return resendClient; } export async function sendMail({ to, subject, html, text, from, replyTo, bcc, cc, attachments, headers, tags }) { const resend = getResendClient(); const payload = { from: from || DEFAULT_FROM, to, subject, ...(html && { html }), ...(text !== undefined ? { text } : {}), ...(replyTo && { replyTo }), ...(bcc && { bcc }), ...(cc && { cc }), ...(attachments && attachments.length ? { attachments } : {}), ...(headers && Object.keys(headers).length ? { headers } : {}), ...(tags && tags.length ? { tags } : {}), }; const { data, error } = await resend.emails.send(payload); if (error) { // Ensure we throw to propagate failure to callers throw error; } // Normalize to prior shape where callers may expect messageId return { id: data?.id, messageId: data?.id }; } export async function sendBatch(emails) { const resend = getResendClient(); if (!Array.isArray(emails)) { throw new Error('sendBatch expects an array of email payloads'); } // Each item should match the sendMail payload fields const { data, error } = await resend.batch.send( emails.map(e => ({ from: e.from || DEFAULT_FROM, to: e.to, subject: e.subject, ...(e.html && { html: e.html }), ...(e.text !== undefined ? { text: e.text } : {}), ...(e.replyTo && { replyTo: e.replyTo }), ...(e.bcc && { bcc: e.bcc }), ...(e.cc && { cc: e.cc }), ...(e.attachments && e.attachments.length ? { attachments: e.attachments } : {}), ...(e.headers && Object.keys(e.headers).length ? { headers: e.headers } : {}), ...(e.tags && e.tags.length ? { tags: e.tags } : {}), })) ); if (error) throw error; return data; }