@maioradv/nestjs-core 2.1.3 → 2.1.4

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.
@@ -13,7 +13,7 @@ export declare class EmailBuilder {
13
13
  private paths;
14
14
  safeCheck(): Promise<void>;
15
15
  initEmail(): Promise<{
16
- subject: any;
16
+ subject: string;
17
17
  template: HandlebarsTemplateDelegate<any>;
18
18
  }>;
19
19
  build(args?: Record<string, any>): Promise<{
@@ -7,6 +7,9 @@ exports.EmailBuilder = void 0;
7
7
  const path_1 = require("path");
8
8
  const handlebars_1 = __importDefault(require("handlebars"));
9
9
  const promises_1 = require("fs/promises");
10
+ function getNestedValue(obj, path) {
11
+ return path.split('.').reduce((acc, key) => acc?.[key], obj) ?? path;
12
+ }
10
13
  class EmailBuilder {
11
14
  constructor() {
12
15
  this.basePath = (0, path_1.join)(process.cwd(), 'templates', 'email');
@@ -40,7 +43,6 @@ class EmailBuilder {
40
43
  (0, path_1.join)(mainPath, 'partials', `${this.header}.hbs`),
41
44
  (0, path_1.join)(mainPath, 'partials', `${this.footer}.hbs`),
42
45
  (0, path_1.join)(mainPath, 'partials', `${this.pattern}.hbs`),
43
- (0, path_1.join)(mainPath, 'locales', `subject.json`),
44
46
  (0, path_1.join)(mainPath, 'locales', `${this.locale}.json`),
45
47
  ];
46
48
  }
@@ -55,21 +57,22 @@ class EmailBuilder {
55
57
  throw new Error(`Missing paths: ${missing.join()}`);
56
58
  }
57
59
  async initEmail() {
58
- const [Container, Header, Footer, Body, Subjects, Labels] = await Promise.all(this.paths().map(path => (0, promises_1.readFile)(path, 'utf-8')));
59
- const parsedSubjects = JSON.parse(Subjects);
60
- if (!(this.pattern in parsedSubjects))
61
- throw new Error(`Missing subject: ${this.pattern}`);
62
- if (!(this.locale in parsedSubjects[this.pattern]))
63
- throw new Error(`Missing locale: ${this.locale} for subject ${this.pattern}`);
64
- const subject = parsedSubjects[this.pattern][this.locale];
60
+ const [Container, Header, Footer, Body, Labels] = await Promise.all(this.paths().map(path => (0, promises_1.readFile)(path, 'utf-8')));
65
61
  const labels = JSON.parse(Labels);
66
- handlebars_1.default.registerHelper('locale', (ctx) => labels[ctx] ?? ctx);
67
- handlebars_1.default.registerHelper('dateToLocaleString', function (date, locale) {
62
+ const subject = getNestedValue(labels, `${this.pattern}.subject`);
63
+ handlebars_1.default.registerHelper('fullDateTime', function (date, locale) {
68
64
  return new Date(date).toLocaleString(locale, { dateStyle: 'full', timeStyle: 'short' });
69
65
  });
70
66
  handlebars_1.default.registerHelper('ternary', function (condition, valTrue, valFalse) {
71
67
  return condition ? valTrue : valFalse;
72
68
  });
69
+ handlebars_1.default.registerHelper('t', (key, options) => {
70
+ const template = getNestedValue(labels, key);
71
+ if (options.hash && Object.keys(options.hash).length > 0) {
72
+ return template.replace(/\{\{(\w+)\}\}/g, (_, k) => options.hash[k] ?? `{{${k}}}`);
73
+ }
74
+ return template;
75
+ });
73
76
  handlebars_1.default.registerPartial('header', Header);
74
77
  handlebars_1.default.registerPartial('footer', Footer);
75
78
  handlebars_1.default.registerPartial('body', Body);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maioradv/nestjs-core",
3
- "version": "2.1.3",
3
+ "version": "2.1.4",
4
4
  "description": "NestJS helpers by MaiorADV",
5
5
  "repository": "https://github.com/maioradv/nestjs-core.git",
6
6
  "author": "Maior ADV Srl",