@maioradv/nestjs-core 2.1.1 → 2.1.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.
@@ -3,9 +3,19 @@ export declare class EmailBuilder {
3
3
  pattern: string;
4
4
  group: string;
5
5
  locale: string;
6
+ header: string;
7
+ footer: string;
6
8
  setGroup(group: string): this;
7
9
  setPattern(pattern: string): this;
8
10
  setLocale(locale: string): this;
11
+ setHeader(header: string): this;
12
+ setFooter(footer: string): this;
13
+ private paths;
14
+ safeCheck(): Promise<void>;
15
+ initEmail(): Promise<{
16
+ subject: any;
17
+ template: HandlebarsTemplateDelegate<any>;
18
+ }>;
9
19
  build(args?: Record<string, any>): Promise<{
10
20
  subject: string;
11
21
  html: string;
@@ -10,6 +10,8 @@ const promises_1 = require("fs/promises");
10
10
  class EmailBuilder {
11
11
  constructor() {
12
12
  this.basePath = (0, path_1.join)(process.cwd(), 'templates', 'email');
13
+ this.header = 'header';
14
+ this.footer = 'footer';
13
15
  }
14
16
  setGroup(group) {
15
17
  this.group = group;
@@ -23,22 +25,56 @@ class EmailBuilder {
23
25
  this.locale = locale;
24
26
  return this;
25
27
  }
26
- async build(args) {
27
- const [Subject, Locales, Body, Doc, Header, Footer] = await Promise.all([
28
- (0, promises_1.readFile)((0, path_1.join)(this.basePath, this.group, 'locales', `subject.json`), 'utf-8'),
29
- (0, promises_1.readFile)((0, path_1.join)(this.basePath, this.group, 'locales', `${this.locale}.json`), 'utf-8'),
30
- (0, promises_1.readFile)((0, path_1.join)(this.basePath, this.group, 'partials', `${this.pattern}.hbs`), 'utf-8'),
31
- (0, promises_1.readFile)((0, path_1.join)(this.basePath, this.group, 'doc.hbs'), 'utf-8'),
32
- (0, promises_1.readFile)((0, path_1.join)(this.basePath, this.group, 'partials', `header.hbs`), 'utf-8'),
33
- (0, promises_1.readFile)((0, path_1.join)(this.basePath, this.group, 'partials', `footer.hbs`), 'utf-8'),
34
- ]);
35
- const subject = JSON.parse(Subject)[this.pattern][this.locale];
36
- const locale = JSON.parse(Locales);
37
- handlebars_1.default.registerHelper('locale', (ctx) => locale[ctx] ?? ctx);
28
+ setHeader(header) {
29
+ this.header = header;
30
+ return this;
31
+ }
32
+ setFooter(footer) {
33
+ this.footer = footer;
34
+ return this;
35
+ }
36
+ paths() {
37
+ const mainPath = (0, path_1.join)(this.basePath, this.group);
38
+ return [
39
+ (0, path_1.join)(mainPath, 'doc.hbs'),
40
+ (0, path_1.join)(mainPath, 'partials', `${this.header}.hbs`),
41
+ (0, path_1.join)(mainPath, 'partials', `${this.footer}.hbs`),
42
+ (0, path_1.join)(mainPath, 'partials', `${this.pattern}.hbs`),
43
+ (0, path_1.join)(mainPath, 'locales', `subject.json`),
44
+ (0, path_1.join)(mainPath, 'locales', `${this.locale}.json`),
45
+ ];
46
+ }
47
+ async safeCheck() {
48
+ const paths = [
49
+ (0, path_1.join)(this.basePath, this.group),
50
+ ...this.paths()
51
+ ];
52
+ const results = await Promise.all(paths.map(p => (0, promises_1.stat)(p).then(() => true).catch(() => false)));
53
+ const missing = paths.filter((_, i) => !results[i]);
54
+ if (missing.length > 0)
55
+ throw new Error(`Missing paths: ${missing.join()}`);
56
+ }
57
+ 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];
65
+ const labels = JSON.parse(Labels);
66
+ handlebars_1.default.registerHelper('locale', (ctx) => labels[ctx] ?? ctx);
38
67
  handlebars_1.default.registerPartial('header', Header);
39
68
  handlebars_1.default.registerPartial('footer', Footer);
40
69
  handlebars_1.default.registerPartial('body', Body);
41
- const template = handlebars_1.default.compile(Doc);
70
+ const template = handlebars_1.default.compile(Container);
71
+ return {
72
+ subject,
73
+ template
74
+ };
75
+ }
76
+ async build(args) {
77
+ const { subject, template } = await this.initEmail();
42
78
  return {
43
79
  subject: this.replace(subject, args),
44
80
  html: template(args)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maioradv/nestjs-core",
3
- "version": "2.1.1",
3
+ "version": "2.1.2",
4
4
  "description": "NestJS helpers by MaiorADV",
5
5
  "repository": "https://github.com/maioradv/nestjs-core.git",
6
6
  "author": "Maior ADV Srl",