Add node SDK (#19)

This commit is contained in:
KM Koushik
2024-05-23 22:02:33 +10:00
committed by GitHub
parent e0fc68d4c0
commit 5fb2448e07
11 changed files with 1295 additions and 2337 deletions

View File

@@ -49,6 +49,7 @@
"server-only": "^0.0.1",
"superjson": "^2.2.1",
"tldts": "^6.1.16",
"unsend": "workspace:*",
"zod": "^3.22.4"
},
"devDependencies": {

View File

@@ -1,4 +1,7 @@
import { env } from "~/env";
import { Unsend } from "unsend";
const unsend = new Unsend(env.UNSEND_API_KEY);
export async function sendSignUpEmail(
email: string,
@@ -26,29 +29,22 @@ async function sendMail(
html: string
) {
if (env.UNSEND_API_KEY && env.UNSEND_URL) {
const resp = await fetch(`${env.UNSEND_URL}/emails`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${env.UNSEND_API_KEY}`,
},
body: JSON.stringify({
from: "no-reply@auth.unsend.dev",
to: email,
subject,
text,
html,
}),
const resp = await unsend.emails.send({
to: email,
from: "no-reply@auth.unsend.dev",
subject,
text,
html,
});
if (resp.status === 200) {
if (resp.data) {
console.log("Email sent using unsend");
return;
} else {
console.log(
"Error sending email using unsend, so fallback to resend",
resp.status,
resp.statusText
resp.error?.code,
resp.error?.message
);
}
} else {