Added shared mailer package to remove circular dependencies between packages. (#63)
This commit is contained in:
committed by
GitHub
parent
d18f810c6e
commit
4c7a3354b9
3
packages/mailers/resend/README.md
Normal file
3
packages/mailers/resend/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
# Nodemailer - @kit/nodemailer
|
||||
|
||||
This package provides a simple and easy to use mailer for sending emails using nodemailer.
|
||||
38
packages/mailers/resend/package.json
Normal file
38
packages/mailers/resend/package.json
Normal file
@@ -0,0 +1,38 @@
|
||||
{
|
||||
"name": "@kit/resend",
|
||||
"private": true,
|
||||
"version": "0.1.0",
|
||||
"scripts": {
|
||||
"clean": "git clean -xdf .turbo node_modules",
|
||||
"format": "prettier --check \"**/*.{ts,tsx}\"",
|
||||
"lint": "eslint .",
|
||||
"typecheck": "tsc --noEmit"
|
||||
},
|
||||
"prettier": "@kit/prettier-config",
|
||||
"exports": {
|
||||
".": "./src/index.ts"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@kit/eslint-config": "workspace:*",
|
||||
"@kit/mailers-shared": "workspace:^",
|
||||
"@kit/prettier-config": "workspace:*",
|
||||
"@kit/tailwind-config": "workspace:*",
|
||||
"@kit/tsconfig": "workspace:*",
|
||||
"@types/node": "^22.5.2",
|
||||
"zod": "^3.23.8"
|
||||
},
|
||||
"eslintConfig": {
|
||||
"root": true,
|
||||
"extends": [
|
||||
"@kit/eslint-config/base",
|
||||
"@kit/eslint-config/react"
|
||||
]
|
||||
},
|
||||
"typesVersions": {
|
||||
"*": {
|
||||
"*": [
|
||||
"src/*"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
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}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
8
packages/mailers/resend/tsconfig.json
Normal file
8
packages/mailers/resend/tsconfig.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"extends": "@kit/tsconfig/base.json",
|
||||
"compilerOptions": {
|
||||
"tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json"
|
||||
},
|
||||
"include": ["*.ts", "src"],
|
||||
"exclude": ["node_modules"]
|
||||
}
|
||||
Reference in New Issue
Block a user