@brilab-mailer/template-handlebars 0.0.2-1 → 0.0.2-12
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.
|
@@ -4,9 +4,11 @@ interface HandlebarsConfig extends MailerTemplateEngineExtendsOptionsType {
|
|
|
4
4
|
}
|
|
5
5
|
export declare class HandlebarsTemplateEngine implements MailerTemplateEngine {
|
|
6
6
|
private readonly options;
|
|
7
|
+
private readonly hbs;
|
|
7
8
|
private readonly templatesDir;
|
|
8
9
|
private readonly layoutsDir;
|
|
9
10
|
constructor(options: HandlebarsConfig);
|
|
11
|
+
private registerI18nHelpers;
|
|
10
12
|
private loadTemplate;
|
|
11
13
|
private loadLayout;
|
|
12
14
|
render(templateKey: string, context?: Record<string, any>, options?: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handlebars-template.engine.d.ts","sourceRoot":"","sources":["../../src/lib/handlebars-template.engine.ts"],"names":[],"mappings":"AACA,OAAO,EAEN,oBAAoB,EACpB,MAAM,0BAA0B,CAAC;AAClC,OAAO,KAAK,EACX,sCAAsC,EACtC,MAAM,0BAA0B,CAAC;AAKlC,UAAU,gBACT,SAAQ,sCAAsC;CAAG;AAElD,qBACa,wBAAyB,YAAW,oBAAoB;
|
|
1
|
+
{"version":3,"file":"handlebars-template.engine.d.ts","sourceRoot":"","sources":["../../src/lib/handlebars-template.engine.ts"],"names":[],"mappings":"AACA,OAAO,EAEN,oBAAoB,EACpB,MAAM,0BAA0B,CAAC;AAClC,OAAO,KAAK,EACX,sCAAsC,EACtC,MAAM,0BAA0B,CAAC;AAKlC,UAAU,gBACT,SAAQ,sCAAsC;CAAG;AAElD,qBACa,wBAAyB,YAAW,oBAAoB;IAQnE,OAAO,CAAC,QAAQ,CAAC,OAAO;IAPzB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAuB;IAE3C,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAS;IACtC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;gBAIlB,OAAO,EAAE,gBAAgB;IAa3C,OAAO,CAAC,mBAAmB;IAiB3B,OAAO,CAAC,YAAY;IASpB,OAAO,CAAC,UAAU;IAQZ,MAAM,CACX,WAAW,EAAE,MAAM,EACnB,OAAO,GAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAM,EACjC,OAAO,GAAE;QAAE,SAAS,CAAC,EAAE,MAAM,CAAA;KAA6B,GACxD,OAAO,CAAC,MAAM,CAAC;CASlB"}
|
|
@@ -3,33 +3,50 @@ import { Inject, Injectable } from '@nestjs/common';
|
|
|
3
3
|
import { MAILER_TEMPLATE_ENGINE_OPTIONS, } from '@brilab-mailer/contracts';
|
|
4
4
|
import * as fs from 'fs';
|
|
5
5
|
import * as path from 'path';
|
|
6
|
-
import
|
|
6
|
+
import Handlebars from 'handlebars';
|
|
7
7
|
let HandlebarsTemplateEngine = class HandlebarsTemplateEngine {
|
|
8
8
|
options;
|
|
9
|
+
hbs = Handlebars.create();
|
|
9
10
|
templatesDir;
|
|
10
11
|
layoutsDir;
|
|
11
12
|
constructor(options) {
|
|
12
13
|
this.options = options;
|
|
14
|
+
this.registerI18nHelpers();
|
|
13
15
|
const rootDir = this.options?.rootDir ?? process.cwd();
|
|
14
16
|
const baseDir = path.join(rootDir, 'templates', 'emails');
|
|
15
17
|
this.templatesDir = baseDir;
|
|
16
18
|
const layoutsFolder = options?.folders?.layouts || 'layouts';
|
|
17
19
|
this.layoutsDir = path.join(baseDir, layoutsFolder);
|
|
18
20
|
}
|
|
21
|
+
registerI18nHelpers() {
|
|
22
|
+
const translator = this.options?.translator;
|
|
23
|
+
if (!translator)
|
|
24
|
+
return;
|
|
25
|
+
this.hbs.registerHelper('t', (key, opts) => {
|
|
26
|
+
const root = opts?.data?.root ?? {};
|
|
27
|
+
const hash = opts?.hash ?? {};
|
|
28
|
+
const lang = hash.lang ?? root.lang ?? translator.getLang?.() ?? 'en';
|
|
29
|
+
const { defaultValue, lang: _lang, ...args } = hash;
|
|
30
|
+
const value = translator.t(key, args, lang, defaultValue);
|
|
31
|
+
if (value === key)
|
|
32
|
+
return defaultValue ?? key;
|
|
33
|
+
return value;
|
|
34
|
+
});
|
|
35
|
+
}
|
|
19
36
|
loadTemplate(key) {
|
|
20
37
|
const filePath = path.join(this.templatesDir, `${key}.hbs`);
|
|
21
38
|
if (!fs.existsSync(filePath)) {
|
|
22
39
|
throw new Error(`Template not found: ${filePath}`);
|
|
23
40
|
}
|
|
24
41
|
const source = fs.readFileSync(filePath, 'utf8');
|
|
25
|
-
return
|
|
42
|
+
return this.hbs.compile(source);
|
|
26
43
|
}
|
|
27
44
|
loadLayout(key) {
|
|
28
45
|
const filePath = path.join(this.layoutsDir, `${key}.hbs`);
|
|
29
46
|
if (!fs.existsSync(filePath))
|
|
30
47
|
return null;
|
|
31
48
|
const source = fs.readFileSync(filePath, 'utf8');
|
|
32
|
-
return
|
|
49
|
+
return this.hbs.compile(source);
|
|
33
50
|
}
|
|
34
51
|
async render(templateKey, context = {}, options = { layoutKey: 'default' }) {
|
|
35
52
|
const template = this.loadTemplate(templateKey);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@brilab-mailer/template-handlebars",
|
|
3
|
-
"version": "0.0.2-
|
|
3
|
+
"version": "0.0.2-12",
|
|
4
4
|
"author": "Bohdan Radchenko <radchenkobs@gmail.com>",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./index.js",
|
|
@@ -26,8 +26,8 @@
|
|
|
26
26
|
"peerDependencies": {
|
|
27
27
|
"@nestjs/common": "^10.0.0",
|
|
28
28
|
"@nestjs/config": "^3.0.0",
|
|
29
|
-
"@brilab-mailer/contracts": "^0.0.
|
|
30
|
-
"@brilab-mailer/core": "^0.0.
|
|
29
|
+
"@brilab-mailer/contracts": "^0.0.2",
|
|
30
|
+
"@brilab-mailer/core": "^0.0.2"
|
|
31
31
|
},
|
|
32
32
|
"peerDependenciesMeta": {
|
|
33
33
|
"@brilab-mailer/contracts": {
|