@maioradv/nestjs-core 2.1.3 → 2.1.5

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.
@@ -12,8 +12,9 @@ export declare class EmailBuilder {
12
12
  setFooter(footer: string): this;
13
13
  private paths;
14
14
  safeCheck(): Promise<void>;
15
+ private getNestedValue;
15
16
  initEmail(): Promise<{
16
- subject: any;
17
+ subject: string;
17
18
  template: HandlebarsTemplateDelegate<any>;
18
19
  }>;
19
20
  build(args?: Record<string, any>): Promise<{
@@ -40,7 +40,6 @@ class EmailBuilder {
40
40
  (0, path_1.join)(mainPath, 'partials', `${this.header}.hbs`),
41
41
  (0, path_1.join)(mainPath, 'partials', `${this.footer}.hbs`),
42
42
  (0, path_1.join)(mainPath, 'partials', `${this.pattern}.hbs`),
43
- (0, path_1.join)(mainPath, 'locales', `subject.json`),
44
43
  (0, path_1.join)(mainPath, 'locales', `${this.locale}.json`),
45
44
  ];
46
45
  }
@@ -54,22 +53,26 @@ class EmailBuilder {
54
53
  if (missing.length > 0)
55
54
  throw new Error(`Missing paths: ${missing.join()}`);
56
55
  }
56
+ getNestedValue(obj, path) {
57
+ return path.split('.').reduce((acc, key) => acc?.[key], obj) ?? path;
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 = this.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 = this.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.5",
4
4
  "description": "NestJS helpers by MaiorADV",
5
5
  "repository": "https://github.com/maioradv/nestjs-core.git",
6
6
  "author": "Maior ADV Srl",