mirror of
https://github.com/CompassConnections/Compass.git
synced 2026-03-25 18:13:48 -04:00
58 lines
1.4 KiB
TypeScript
58 lines
1.4 KiB
TypeScript
import {IS_LOCAL} from 'common/hosting/constants'
|
|
import {debug} from 'common/logger'
|
|
import {sleep} from 'common/util/time'
|
|
import {type CreateEmailOptions, CreateEmailRequestOptions, Resend} from 'resend'
|
|
import {log} from 'shared/utils'
|
|
|
|
/*
|
|
* typically: { subject: string, to: string | string[] } & ({ text: string } | { react: ReactNode })
|
|
*/
|
|
export const sendEmail = async (
|
|
payload: CreateEmailOptions,
|
|
options?: CreateEmailRequestOptions,
|
|
) => {
|
|
const resend = getResend()
|
|
debug(resend, payload, options)
|
|
|
|
const skip = IS_LOCAL
|
|
if (skip) {
|
|
console.warn('Skipping email send')
|
|
return null
|
|
}
|
|
|
|
if (!resend) return null
|
|
|
|
const {data, error} = await resend.emails.send(
|
|
{replyTo: 'Compass <hello@compassmeet.com>', ...payload},
|
|
options,
|
|
)
|
|
debug('resend.emails.send', data, error)
|
|
|
|
if (error) {
|
|
log.error(`Failed to send email to ${payload.to} with subject ${payload.subject}`)
|
|
log.error(error)
|
|
return null
|
|
}
|
|
|
|
log(`Sent email to ${payload.to} with subject ${payload.subject}`)
|
|
|
|
await sleep(1000) // to avoid rate limits (2 / second in resend free plan)
|
|
|
|
return data
|
|
}
|
|
|
|
let resend: Resend | null = null
|
|
const getResend = () => {
|
|
if (resend) return resend
|
|
|
|
if (!process.env.RESEND_KEY) {
|
|
debug('No RESEND_KEY, skipping email send')
|
|
return
|
|
}
|
|
|
|
const apiKey = process.env.RESEND_KEY as string
|
|
// console.debug(`RESEND_KEY: ${apiKey}`)
|
|
resend = new Resend(apiKey)
|
|
return resend
|
|
}
|