@maioradv/nestjs-core 1.6.1 → 1.6.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.
@@ -0,0 +1,14 @@
1
+ export declare class EmailBuilder {
2
+ basePath: string;
3
+ pattern: string;
4
+ group: string;
5
+ locale: string;
6
+ setGroup(group: string): this;
7
+ setPattern(pattern: string): this;
8
+ setLocale(locale: string): this;
9
+ build(args?: Record<string, any>): Promise<{
10
+ subject: string;
11
+ html: string;
12
+ }>;
13
+ replace(ctx: string, replace?: Record<string, any>): string;
14
+ }
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.EmailBuilder = void 0;
7
+ const path_1 = require("path");
8
+ const handlebars_1 = __importDefault(require("handlebars"));
9
+ const promises_1 = require("fs/promises");
10
+ class EmailBuilder {
11
+ constructor() {
12
+ this.basePath = (0, path_1.join)(process.cwd(), 'templates', 'email');
13
+ }
14
+ setGroup(group) {
15
+ this.group = group;
16
+ return this;
17
+ }
18
+ setPattern(pattern) {
19
+ this.pattern = pattern;
20
+ return this;
21
+ }
22
+ setLocale(locale) {
23
+ this.locale = locale;
24
+ return this;
25
+ }
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);
38
+ handlebars_1.default.registerPartial('header', Header);
39
+ handlebars_1.default.registerPartial('footer', Footer);
40
+ handlebars_1.default.registerPartial('body', Body);
41
+ const template = handlebars_1.default.compile(Doc);
42
+ return {
43
+ subject: this.replace(subject, args),
44
+ html: template(args)
45
+ };
46
+ }
47
+ replace(ctx, replace) {
48
+ if (!replace)
49
+ return ctx;
50
+ var re = new RegExp(Object.keys(replace).join("|"), "gi");
51
+ return ctx.replace(re, function (matched) {
52
+ return replace[matched];
53
+ });
54
+ }
55
+ }
56
+ exports.EmailBuilder = EmailBuilder;
@@ -4,3 +4,5 @@ export * from './slug.helper';
4
4
  export * from './types.helper';
5
5
  export * from './time.helper';
6
6
  export * from './image.helper';
7
+ export * from './email.helper';
8
+ export * from './price.helper';
@@ -20,3 +20,5 @@ __exportStar(require("./slug.helper"), exports);
20
20
  __exportStar(require("./types.helper"), exports);
21
21
  __exportStar(require("./time.helper"), exports);
22
22
  __exportStar(require("./image.helper"), exports);
23
+ __exportStar(require("./email.helper"), exports);
24
+ __exportStar(require("./price.helper"), exports);
@@ -0,0 +1 @@
1
+ export declare function priceFormat(n: number): string;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.priceFormat = void 0;
4
+ const formatter = new Intl.NumberFormat('it-IT', {
5
+ style: 'currency',
6
+ currency: 'EUR',
7
+ minimumFractionDigits: 2, // (this suffices for whole numbers, but will print 2500.10 as $2,500.1)
8
+ maximumFractionDigits: 2, // (causes 2500.99 to be printed as $2,501)
9
+ });
10
+ function priceFormat(n) {
11
+ return formatter.format(n);
12
+ }
13
+ exports.priceFormat = priceFormat;
@@ -7,7 +7,7 @@ exports.toMs = void 0;
7
7
  */
8
8
  function toMs(str) {
9
9
  const matches = str.match(/(\d+)/);
10
- const number = +matches[0] ?? 0;
10
+ const number = +(matches[0] ?? 0);
11
11
  const mode = str.replace(matches[0], '');
12
12
  const unit = 1000;
13
13
  const conversion = {
@@ -16,6 +16,6 @@ function toMs(str) {
16
16
  m: unit * 60,
17
17
  s: unit,
18
18
  };
19
- return conversion[mode] * number ?? 0;
19
+ return (conversion[mode] ?? 0) * number;
20
20
  }
21
21
  exports.toMs = toMs;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maioradv/nestjs-core",
3
- "version": "1.6.1",
3
+ "version": "1.6.2",
4
4
  "description": "NestJS helpers by MaiorADV",
5
5
  "repository": "https://github.com/maioradv/nestjs-core.git",
6
6
  "author": "Maior ADV Srl",
@@ -28,6 +28,7 @@
28
28
  "bcrypt": "^5.1.1",
29
29
  "class-transformer": "^0.5.1",
30
30
  "class-validator": "^0.14.1",
31
+ "handlebars": "^4.7.8",
31
32
  "image-size": "^1.1.1",
32
33
  "nest-winston": "^1.10.0",
33
34
  "nodemailer": "^6.9.14",
@@ -58,6 +59,7 @@
58
59
  "bcrypt": "^5.1.1",
59
60
  "class-transformer": "^0.5.1",
60
61
  "class-validator": "^0.14.1",
62
+ "handlebars": "^4.7.8",
61
63
  "image-size": "^1.1.1",
62
64
  "nest-winston": "^1.10.0",
63
65
  "nodemailer": "^6.9.14",