@astralibx/email-rule-engine 12.4.0 → 12.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +31 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.mjs +31 -6
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -489,7 +489,20 @@ var TemplateRenderService = class {
|
|
|
489
489
|
return { html, text, subject };
|
|
490
490
|
}
|
|
491
491
|
renderPreview(subject, body, data, textBody) {
|
|
492
|
-
|
|
492
|
+
const subjectFn = Handlebars__default.default.compile(subject, { strict: false });
|
|
493
|
+
const resolvedSubject = subjectFn(data);
|
|
494
|
+
const bodyFn = Handlebars__default.default.compile(body, { strict: false });
|
|
495
|
+
const resolvedBody = bodyFn(data);
|
|
496
|
+
const mjmlSource = wrapInMjml(resolvedBody);
|
|
497
|
+
const html = compileMjml(mjmlSource);
|
|
498
|
+
let text;
|
|
499
|
+
if (textBody) {
|
|
500
|
+
const textFn = Handlebars__default.default.compile(textBody, { strict: false });
|
|
501
|
+
text = textFn(data);
|
|
502
|
+
} else {
|
|
503
|
+
text = htmlToPlainText(html);
|
|
504
|
+
}
|
|
505
|
+
return { html, text, subject: resolvedSubject };
|
|
493
506
|
}
|
|
494
507
|
htmlToText(html) {
|
|
495
508
|
return htmlToPlainText(html);
|
|
@@ -698,18 +711,30 @@ var TemplateService = class {
|
|
|
698
711
|
await template.save();
|
|
699
712
|
return template;
|
|
700
713
|
}
|
|
714
|
+
_buildSampleData(variables, provided) {
|
|
715
|
+
const data = { ...provided };
|
|
716
|
+
for (const v of variables) {
|
|
717
|
+
if (!(v in data)) {
|
|
718
|
+
data[v] = `[${v}]`;
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
return data;
|
|
722
|
+
}
|
|
701
723
|
async preview(id, sampleData) {
|
|
702
724
|
const template = await this.EmailTemplate.findById(id);
|
|
703
725
|
if (!template) return null;
|
|
726
|
+
const variables = template.variables ?? [];
|
|
727
|
+
const data = this._buildSampleData(variables, sampleData);
|
|
704
728
|
return this.renderService.renderPreview(
|
|
705
729
|
template.subjects[0],
|
|
706
730
|
template.bodies[0],
|
|
707
|
-
|
|
731
|
+
data,
|
|
708
732
|
template.textBody
|
|
709
733
|
);
|
|
710
734
|
}
|
|
711
|
-
async previewRaw(subject, body, sampleData, textBody) {
|
|
712
|
-
|
|
735
|
+
async previewRaw(subject, body, sampleData, variables, textBody) {
|
|
736
|
+
const data = this._buildSampleData(variables ?? [], sampleData);
|
|
737
|
+
return this.renderService.renderPreview(subject, body, data, textBody);
|
|
713
738
|
}
|
|
714
739
|
async validate(body) {
|
|
715
740
|
const validation = this.renderService.validateTemplate(body);
|
|
@@ -1832,11 +1857,11 @@ function createTemplateController(templateService, options) {
|
|
|
1832
1857
|
}
|
|
1833
1858
|
async function previewRaw(req, res) {
|
|
1834
1859
|
try {
|
|
1835
|
-
const { subject, body, textBody, sampleData } = req.body;
|
|
1860
|
+
const { subject, body, textBody, sampleData, variables } = req.body;
|
|
1836
1861
|
if (!subject || !body) {
|
|
1837
1862
|
return res.status(400).json({ success: false, error: "subject and body are required" });
|
|
1838
1863
|
}
|
|
1839
|
-
const result = await templateService.previewRaw(subject, body, sampleData || {}, textBody);
|
|
1864
|
+
const result = await templateService.previewRaw(subject, body, sampleData || {}, variables, textBody);
|
|
1840
1865
|
res.json({ success: true, data: result });
|
|
1841
1866
|
} catch (error) {
|
|
1842
1867
|
const message = error instanceof Error ? error.message : "Unknown error";
|