Files
sori.studio/server/utils/resend-mail.js

43 lines
1.3 KiB
JavaScript

import { createError } from 'h3'
/**
* Resend가 서버 설정으로 사용 가능한지
* @param {{ resendApiKey?: string, resendFromEmail?: string }} config - 런타임 설정
* @returns {boolean}
*/
export const isResendConfigured = (config) => {
const key = String(config?.resendApiKey || '').trim()
const from = String(config?.resendFromEmail || '').trim()
return Boolean(key && from)
}
/**
* Resend REST API로 이메일을 발송한다.
* @param {{ apiKey: string, from: string, to: string, subject: string, html: string }} input - 발송 입력
* @returns {Promise<void>}
*/
export const sendResendEmail = async (input) => {
const res = await fetch('https://api.resend.com/emails', {
method: 'POST',
headers: {
Authorization: `Bearer ${input.apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
from: input.from,
to: [input.to],
subject: input.subject,
html: input.html
})
})
if (!res.ok) {
const detail = await res.text().catch(() => '')
throw createError({
statusCode: 502,
message: '이메일 발송에 실패했습니다. Resend 발신 주소·도메인 설정을 확인해 주세요.',
data: process.env.NODE_ENV === 'development' && detail ? { detail } : undefined
})
}
}