templates in files

This commit is contained in:
Philipp Dormann 2024-11-21 18:46:35 +01:00
parent bbf58e52ce
commit aff8084692
Signed by: philipp
GPG Key ID: 3BB9ADD52DCA4314
11 changed files with 89 additions and 9 deletions

BIN
bun.lockb

Binary file not shown.

View File

@ -12,6 +12,7 @@
"@hono/zod-openapi": "0.18.0",
"@hono/zod-validator": "0.4.1",
"bullmq": "5.28.1",
"handlebars": "^4.7.8",
"hono": "4.6.11",
"ioredis": "5.4.1",
"nodemailer": "^6.9.16",

View File

@ -1,16 +1,41 @@
import { Language } from '../types'
import { welcomeTemplate } from './welcome'
import { passwordResetTemplate } from './password-reset'
import { readFileSync } from 'fs'
import { join } from 'path'
import { compile } from 'handlebars'
const templates = {
welcome: welcomeTemplate,
passwordReset: passwordResetTemplate
interface TemplateCache {
[key: string]: {
html: HandlebarsTemplateDelegate
text: HandlebarsTemplateDelegate
}
}
const templateCache: TemplateCache = {}
function loadTemplate(name: string, language: Language) {
const cacheKey = `${name}:${language}`
if (!templateCache[cacheKey]) {
const htmlPath = join(process.cwd(), 'src', 'templates', name, `${language}.html`)
const textPath = join(process.cwd(), 'src', 'templates', name, `${language}.txt`)
const htmlTemplate = readFileSync(htmlPath, 'utf-8')
const textTemplate = readFileSync(textPath, 'utf-8')
templateCache[cacheKey] = {
html: compile(htmlTemplate),
text: compile(textTemplate)
}
}
return templateCache[cacheKey]
}
export function getEmailTemplate(name: string, language: Language) {
const template = templates[name as keyof typeof templates]
if (!template) {
throw new Error(`Template ${name} not found`)
const template = loadTemplate(name, language)
return {
html: (data: any) => template.html(data),
text: (data: any) => template.text(data)
}
return template[language]
}

View File

@ -0,0 +1,7 @@
<h1>Anfrage zum Zurücksetzen des Passworts</h1>
<p>Hallo {{name}},</p>
<p>Wir haben eine Anfrage zum Zurücksetzen Ihres Passworts erhalten. Klicken Sie auf den untenstehenden Link, um ein neues Passwort zu erstellen:</p>
<a href="{{resetLink}}" style="display: inline-block; background: #007bff; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px; margin: 15px 0;">Passwort zurücksetzen</a>
<p>Dieser Link läuft in {{expiresIn}} ab.</p>
<p>Falls Sie diese Anfrage nicht gestellt haben, ignorieren Sie bitte diese E-Mail oder kontaktieren Sie den Support, wenn Sie Bedenken haben.</p>
<p>Mit freundlichen Grüßen,<br>Ihr Support-Team</p>

View File

@ -0,0 +1,13 @@
Anfrage zum Zurücksetzen des Passworts
Hallo {{name}},
Wir haben eine Anfrage zum Zurücksetzen Ihres Passworts erhalten. Verwenden Sie den folgenden Link, um ein neues Passwort zu erstellen:
{{resetLink}}
Dieser Link läuft in {{expiresIn}} ab.
Falls Sie diese Anfrage nicht gestellt haben, ignorieren Sie bitte diese E-Mail oder kontaktieren Sie den Support, wenn Sie Bedenken haben.
Mit freundlichen Grüßen,
Ihr Support-Team

View File

@ -0,0 +1,7 @@
<h1>Password Reset Request</h1>
<p>Hello {{name}},</p>
<p>We received a request to reset your password. Click the link below to create a new password:</p>
<a href="{{resetLink}}" style="display: inline-block; background: #007bff; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px; margin: 15px 0;">Reset Password</a>
<p>This link will expire in {{expiresIn}}.</p>
<p>If you didn't request this password reset, please ignore this email or contact support if you have concerns.</p>
<p>Best regards,<br>Your Support Team</p>

View File

@ -0,0 +1,13 @@
Password Reset Request
Hello {{name}},
We received a request to reset your password. Use the following link to create a new password:
{{resetLink}}
This link will expire in {{expiresIn}}.
If you didn't request this password reset, please ignore this email or contact support if you have concerns.
Best regards,
Your Support Team

View File

@ -0,0 +1,3 @@
<h1>Willkommen {{name}}!</h1>
<p>Danke, dass Sie sich bei uns registriert haben. Bitte klicken Sie auf den Link unten, um Ihr Konto zu aktivieren:</p>
<a href="{{activationLink}}" style="display: inline-block; background: #007bff; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px; margin: 15px 0;">Konto aktivieren</a>

View File

@ -0,0 +1,4 @@
Willkommen {{name}}!
Danke, dass Sie sich bei uns registriert haben. Bitte verwenden Sie den folgenden Link, um Ihr Konto zu aktivieren:
{{activationLink}}

View File

@ -0,0 +1,3 @@
<h1>Welcome {{name}}!</h1>
<p>Thank you for joining us. Please click the link below to activate your account:</p>
<a href="{{activationLink}}" style="display: inline-block; background: #007bff; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px; margin: 15px 0;">Activate Account</a>

View File

@ -0,0 +1,4 @@
Welcome {{name}}!
Thank you for joining us. Please use the following link to activate your account:
{{activationLink}}