UNPKG

studiocms

Version:

Astro Native CMS for AstroDB. Built from the ground up by the Astro community.

62 lines (61 loc) 2.16 kB
import { SDKCore } from "studiocms:sdk"; import TemplateEngine from "@withstudiocms/template-lang"; import { Effect } from "../../effect.js"; import defaultTemplates from "./default-templates.js"; const engine = new TemplateEngine({ strict: true }); const templateEngine = Effect.gen(function* () { const sdk = yield* SDKCore; let config = yield* sdk.CONFIG.templateConfig.get(); if (!config) { config = yield* sdk.CONFIG.templateConfig.init(defaultTemplates); } if (!config) { return yield* Effect.fail(new Error("Failed to initialize template configuration.")); } const { _config_version, ...templates } = config.data; const templateKeys = Object.keys(templates); return { /** * Retrieves the specified email template. * * @param key - The key of the template to retrieve. * @returns The specified email template. */ getTemplate: (key) => templates[key], /** * Retrieves the default email template. * * @param key - The key of the default template to retrieve. * @returns The default email template. */ getDefaultTemplate: (key) => defaultTemplates[key], availableTemplates: templateKeys, allTemplates: templates, defaultTemplates, /** * Updates the email templates with new templates. * * @param newTemplates - An object containing the new templates to update. */ updateTemplates: (newTemplates) => sdk.CONFIG.templateConfig.update(newTemplates), /** * Renders the specified email template with the provided context data. * * @param key - The key of the template to render. * @param context - The context data to use for rendering the template. * @returns The rendered email content. */ render: (key, context) => { const template = templates[key] || defaultTemplates[key]; return Effect.try({ try: () => engine.render(template, context), catch: (error) => new Error(`Failed to render template "${key}": ${error.message}`) }); } }; }); var template_engine_default = templateEngine; export { template_engine_default as default, templateEngine };