@astralibx/email-rule-engine 12.5.0 → 12.5.2

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.d.mts CHANGED
@@ -534,12 +534,13 @@ declare class TemplateService {
534
534
  update(id: string, input: UpdateEmailTemplateInput): Promise<EmailTemplateDocument | null>;
535
535
  delete(id: string): Promise<boolean>;
536
536
  toggleActive(id: string): Promise<EmailTemplateDocument | null>;
537
+ private _buildSampleData;
537
538
  preview(id: string, sampleData: Record<string, unknown>): Promise<{
538
539
  html: string;
539
540
  text: string;
540
541
  subject: string;
541
542
  } | null>;
542
- previewRaw(subject: string, body: string, sampleData: Record<string, unknown>, textBody?: string): Promise<{
543
+ previewRaw(subject: string, body: string, sampleData: Record<string, unknown>, variables?: string[], textBody?: string): Promise<{
543
544
  html: string;
544
545
  text: string;
545
546
  subject: string;
package/dist/index.d.ts CHANGED
@@ -534,12 +534,13 @@ declare class TemplateService {
534
534
  update(id: string, input: UpdateEmailTemplateInput): Promise<EmailTemplateDocument | null>;
535
535
  delete(id: string): Promise<boolean>;
536
536
  toggleActive(id: string): Promise<EmailTemplateDocument | null>;
537
+ private _buildSampleData;
537
538
  preview(id: string, sampleData: Record<string, unknown>): Promise<{
538
539
  html: string;
539
540
  text: string;
540
541
  subject: string;
541
542
  } | null>;
542
- previewRaw(subject: string, body: string, sampleData: Record<string, unknown>, textBody?: string): Promise<{
543
+ previewRaw(subject: string, body: string, sampleData: Record<string, unknown>, variables?: string[], textBody?: string): Promise<{
543
544
  html: string;
544
545
  text: string;
545
546
  subject: string;
package/dist/index.mjs CHANGED
@@ -481,7 +481,20 @@ var TemplateRenderService = class {
481
481
  return { html, text, subject };
482
482
  }
483
483
  renderPreview(subject, body, data, textBody) {
484
- return this.renderSingle(subject, body, data, textBody);
484
+ const subjectFn = Handlebars.compile(subject, { strict: false });
485
+ const resolvedSubject = subjectFn(data);
486
+ const bodyFn = Handlebars.compile(body, { strict: false });
487
+ const resolvedBody = bodyFn(data);
488
+ const mjmlSource = wrapInMjml(resolvedBody);
489
+ const html = compileMjml(mjmlSource);
490
+ let text;
491
+ if (textBody) {
492
+ const textFn = Handlebars.compile(textBody, { strict: false });
493
+ text = textFn(data);
494
+ } else {
495
+ text = htmlToPlainText(html);
496
+ }
497
+ return { html, text, subject: resolvedSubject };
485
498
  }
486
499
  htmlToText(html) {
487
500
  return htmlToPlainText(html);
@@ -690,18 +703,30 @@ var TemplateService = class {
690
703
  await template.save();
691
704
  return template;
692
705
  }
706
+ _buildSampleData(variables, provided) {
707
+ const data = { ...provided };
708
+ for (const v of variables) {
709
+ if (!(v in data)) {
710
+ data[v] = `[${v}]`;
711
+ }
712
+ }
713
+ return data;
714
+ }
693
715
  async preview(id, sampleData) {
694
716
  const template = await this.EmailTemplate.findById(id);
695
717
  if (!template) return null;
718
+ const variables = template.variables ?? [];
719
+ const data = this._buildSampleData(variables, sampleData);
696
720
  return this.renderService.renderPreview(
697
721
  template.subjects[0],
698
722
  template.bodies[0],
699
- sampleData,
723
+ data,
700
724
  template.textBody
701
725
  );
702
726
  }
703
- async previewRaw(subject, body, sampleData, textBody) {
704
- return this.renderService.renderPreview(subject, body, sampleData, textBody);
727
+ async previewRaw(subject, body, sampleData, variables, textBody) {
728
+ const data = this._buildSampleData(variables ?? [], sampleData);
729
+ return this.renderService.renderPreview(subject, body, data, textBody);
705
730
  }
706
731
  async validate(body) {
707
732
  const validation = this.renderService.validateTemplate(body);
@@ -1824,11 +1849,11 @@ function createTemplateController(templateService, options) {
1824
1849
  }
1825
1850
  async function previewRaw(req, res) {
1826
1851
  try {
1827
- const { subject, body, textBody, sampleData } = req.body;
1852
+ const { subject, body, textBody, sampleData, variables } = req.body;
1828
1853
  if (!subject || !body) {
1829
1854
  return res.status(400).json({ success: false, error: "subject and body are required" });
1830
1855
  }
1831
- const result = await templateService.previewRaw(subject, body, sampleData || {}, textBody);
1856
+ const result = await templateService.previewRaw(subject, body, sampleData || {}, variables, textBody);
1832
1857
  res.json({ success: true, data: result });
1833
1858
  } catch (error) {
1834
1859
  const message = error instanceof Error ? error.message : "Unknown error";