B2B-88: add starter kit structure and elements
This commit is contained in:
53
packages/mailers/resend/src/index.ts
Normal file
53
packages/mailers/resend/src/index.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import 'server-only';
|
||||
|
||||
import { z } from 'zod';
|
||||
|
||||
import { Mailer, MailerSchema } from '@kit/mailers-shared';
|
||||
|
||||
type Config = z.infer<typeof MailerSchema>;
|
||||
|
||||
const RESEND_API_KEY = z
|
||||
.string({
|
||||
description: 'The API key for the Resend API',
|
||||
required_error: 'Please provide the API key for the Resend API',
|
||||
})
|
||||
.parse(process.env.RESEND_API_KEY);
|
||||
|
||||
export function createResendMailer() {
|
||||
return new ResendMailer();
|
||||
}
|
||||
|
||||
/**
|
||||
* A class representing a mailer using the Resend HTTP API.
|
||||
* @implements {Mailer}
|
||||
*/
|
||||
class ResendMailer implements Mailer {
|
||||
async sendEmail(config: Config) {
|
||||
const contentObject =
|
||||
'text' in config
|
||||
? {
|
||||
text: config.text,
|
||||
}
|
||||
: {
|
||||
html: config.html,
|
||||
};
|
||||
|
||||
const res = await fetch('https://api.resend.com/emails', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${RESEND_API_KEY}`,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
from: config.from,
|
||||
to: [config.to],
|
||||
subject: config.subject,
|
||||
...contentObject,
|
||||
}),
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error(`Failed to send email: ${res.statusText}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user