@brilab-mailer/template-handlebars 0.0.1-beta.47

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.
package/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # @brilab-mailer/template-handlebars
2
+
3
+ ## Overview
4
+ This package provides a Handlebars-powered template rendering engine fully compatible with the Brilab Mailer architecture.
5
+
6
+ It implements:
7
+
8
+ ```ts
9
+ export interface MailerTemplateEngine {
10
+ render(templateName: string, context: unknown): Promise<string>;
11
+ }
12
+ ```
13
+
14
+ ---
15
+
16
+ ## Features
17
+ - Layout support
18
+ - Partials
19
+ - Helpers
20
+ - Async rendering
21
+ - Template caching
22
+
23
+ ---
24
+
25
+ ## Folder Structure Recommendation
26
+
27
+ ```
28
+ templates/
29
+ welcome.hbs
30
+ reset-password.hbs
31
+ layouts/
32
+ main.hbs
33
+ partials/
34
+ footer.hbs
35
+ header.hbs
36
+ ```
37
+
38
+ ---
39
+
40
+ ## Usage in MailerModule
41
+
42
+ ```ts
43
+ MailerModule.register({
44
+ providerClass: MailtrapApiProvider,
45
+ templateEngineClass: HandlebarsTemplateEngine,
46
+ });
47
+ ```
48
+
49
+ ---
50
+
51
+ ## Example Template
52
+
53
+ `templates/welcome.hbs`
54
+ ```hbs
55
+ <h1>Hello {{name}}!</h1>
56
+ <p>Welcome to our platform.</p>
57
+ ```
58
+
59
+ ---
60
+
61
+ ## Example Render Call
62
+
63
+ ```ts
64
+ const html = await this.mailer.render('welcome', { name: 'John' });
65
+ ```
66
+
67
+ ---
68
+
69
+ ## Performance
70
+ - Precompilation optional
71
+ - Lazy loading template files
72
+ - Cache invalidation on demand
73
+
74
+ ---
75
+
76
+ ## Best Practices
77
+ - Avoid complex helper logic
78
+ - Use layouts for branding consistency
79
+ - Store templates outside the src folder in production
80
+
package/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './lib/handlebars-template.engine.js';
2
+ //# sourceMappingURL=index.d.ts.map
package/index.d.ts.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,qCAAqC,CAAC"}
package/index.js ADDED
@@ -0,0 +1 @@
1
+ export * from './lib/handlebars-template.engine.js';
@@ -0,0 +1,12 @@
1
+ import { ConfigService } from '@nestjs/config';
2
+ import { MailerTemplateEngine } from '@brilab-mailer/contracts';
3
+ export declare class HandlebarsTemplateEngine implements MailerTemplateEngine {
4
+ private readonly config;
5
+ private readonly templatesDir;
6
+ private readonly layoutsDir;
7
+ constructor(config: ConfigService);
8
+ private loadTemplate;
9
+ private loadLayout;
10
+ render(templateKey: string, context?: Record<string, any>, layoutKey?: string): Promise<string>;
11
+ }
12
+ //# sourceMappingURL=handlebars-template.engine.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"handlebars-template.engine.d.ts","sourceRoot":"","sources":["../../src/lib/handlebars-template.engine.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAC/C,OAAO,EACN,oBAAoB,EACpB,MAAM,0BAA0B,CAAC;AAKlC,qBACa,wBAAyB,YAAW,oBAAoB;IAIxD,OAAO,CAAC,QAAQ,CAAC,MAAM;IAHnC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAS;IACtC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;gBAEP,MAAM,EAAE,aAAa;IAUlD,OAAO,CAAC,YAAY;IASpB,OAAO,CAAC,UAAU;IAQZ,MAAM,CACX,WAAW,EAAE,MAAM,EACnB,OAAO,GAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAM,EACjC,SAAS,SAAY,GACnB,OAAO,CAAC,MAAM,CAAC;CASlB"}
@@ -0,0 +1,47 @@
1
+ import { __decorate, __metadata } from "tslib";
2
+ import { Injectable } from '@nestjs/common';
3
+ import { ConfigService } from '@nestjs/config';
4
+ import * as fs from 'fs';
5
+ import * as path from 'path';
6
+ import * as Handlebars from 'handlebars';
7
+ let HandlebarsTemplateEngine = class HandlebarsTemplateEngine {
8
+ config;
9
+ templatesDir;
10
+ layoutsDir;
11
+ constructor(config) {
12
+ this.config = config;
13
+ const rootDir = process.cwd();
14
+ const baseDir = this.config.get('MAIL_TEMPLATES_DIR') ||
15
+ path.join(rootDir, 'templates', 'emails');
16
+ this.templatesDir = baseDir;
17
+ this.layoutsDir = path.join(baseDir, 'layouts');
18
+ }
19
+ loadTemplate(key) {
20
+ const filePath = path.join(this.templatesDir, `${key}.hbs`);
21
+ if (!fs.existsSync(filePath)) {
22
+ throw new Error(`Template not found: ${filePath}`);
23
+ }
24
+ const source = fs.readFileSync(filePath, 'utf8');
25
+ return Handlebars.compile(source);
26
+ }
27
+ loadLayout(key) {
28
+ const filePath = path.join(this.layoutsDir, `${key}.hbs`);
29
+ if (!fs.existsSync(filePath))
30
+ return null;
31
+ const source = fs.readFileSync(filePath, 'utf8');
32
+ return Handlebars.compile(source);
33
+ }
34
+ async render(templateKey, context = {}, layoutKey = 'default') {
35
+ const template = this.loadTemplate(templateKey);
36
+ const bodyHtml = template(context);
37
+ const layout = this.loadLayout(layoutKey);
38
+ if (!layout)
39
+ return bodyHtml;
40
+ return layout({ ...context, body: bodyHtml });
41
+ }
42
+ };
43
+ HandlebarsTemplateEngine = __decorate([
44
+ Injectable(),
45
+ __metadata("design:paramtypes", [ConfigService])
46
+ ], HandlebarsTemplateEngine);
47
+ export { HandlebarsTemplateEngine };
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@brilab-mailer/template-handlebars",
3
+ "version": "0.0.1-beta.47",
4
+ "author": "Bohdan Radchenko <radchenkobs@gmail.com>",
5
+ "type": "module",
6
+ "main": "./index.js",
7
+ "module": "./index.js",
8
+ "types": "./index.d.ts",
9
+ "exports": {
10
+ "./package.json": "./package.json",
11
+ ".": {
12
+ "types": "./index.d.ts",
13
+ "import": "./index.js",
14
+ "default": "./index.js"
15
+ }
16
+ },
17
+ "files": [
18
+ "**/*",
19
+ "!**/*.tsbuildinfo"
20
+ ],
21
+ "license": "MIT",
22
+ "publishConfig": {
23
+ "access": "public",
24
+ "directory": "dist"
25
+ },
26
+ "peerDependencies": {
27
+ "@nestjs/common": "^10.0.0",
28
+ "@nestjs/config": "^3.0.0",
29
+ "@brilab-mailer/contracts": "^0.0.1",
30
+ "@brilab-mailer/core": "^0.0.1"
31
+ },
32
+ "peerDependenciesMeta": {
33
+ "@brilab-mailer/contracts": {
34
+ "optional": false
35
+ },
36
+ "@brilab-mailer/core": {
37
+ "optional": true
38
+ }
39
+ },
40
+ "dependencies": {
41
+ "handlebars": "4.7.8"
42
+ }
43
+ }