@brilab-mailer/template-handlebars 0.0.1-beta.49 → 0.0.1-beta.51

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.
@@ -1,12 +1,17 @@
1
- import { ConfigService } from '@nestjs/config';
2
1
  import { MailerTemplateEngine } from '@brilab-mailer/contracts';
2
+ import type { MailerTemplateEngineExtendsOptionsType } from '@brilab-mailer/contracts';
3
+ interface HandlebarsConfig extends MailerTemplateEngineExtendsOptionsType {
4
+ }
3
5
  export declare class HandlebarsTemplateEngine implements MailerTemplateEngine {
4
- private readonly config;
6
+ private readonly options;
5
7
  private readonly templatesDir;
6
8
  private readonly layoutsDir;
7
- constructor(config: ConfigService);
9
+ constructor(options: HandlebarsConfig);
8
10
  private loadTemplate;
9
11
  private loadLayout;
10
- render(templateKey: string, context?: Record<string, any>, layoutKey?: string): Promise<string>;
12
+ render(templateKey: string, context?: Record<string, any>, options?: {
13
+ layoutKey?: string;
14
+ }): Promise<string>;
11
15
  }
16
+ export {};
12
17
  //# sourceMappingURL=handlebars-template.engine.d.ts.map
@@ -1 +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"}
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;IAMnE,OAAO,CAAC,QAAQ,CAAC,OAAO;IALzB,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAS;IACtC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;gBAIlB,OAAO,EAAE,gBAAgB;IAW3C,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"}
@@ -1,20 +1,20 @@
1
- import { __decorate, __metadata } from "tslib";
2
- import { Injectable } from '@nestjs/common';
3
- import { ConfigService } from '@nestjs/config';
1
+ import { __decorate, __metadata, __param } from "tslib";
2
+ import { Inject, Injectable } from '@nestjs/common';
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 * as Handlebars from 'handlebars';
6
+ import { compile } from 'handlebars';
7
7
  let HandlebarsTemplateEngine = class HandlebarsTemplateEngine {
8
- config;
8
+ options;
9
9
  templatesDir;
10
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');
11
+ constructor(options) {
12
+ this.options = options;
13
+ const rootDir = this.options?.rootDir ?? process.cwd();
14
+ const baseDir = path.join(rootDir, 'templates', 'emails');
16
15
  this.templatesDir = baseDir;
17
- this.layoutsDir = path.join(baseDir, 'layouts');
16
+ const layoutsFolder = options?.folders?.layouts || 'layouts';
17
+ this.layoutsDir = path.join(baseDir, layoutsFolder);
18
18
  }
19
19
  loadTemplate(key) {
20
20
  const filePath = path.join(this.templatesDir, `${key}.hbs`);
@@ -22,19 +22,19 @@ let HandlebarsTemplateEngine = class HandlebarsTemplateEngine {
22
22
  throw new Error(`Template not found: ${filePath}`);
23
23
  }
24
24
  const source = fs.readFileSync(filePath, 'utf8');
25
- return Handlebars.compile(source);
25
+ return compile(source);
26
26
  }
27
27
  loadLayout(key) {
28
28
  const filePath = path.join(this.layoutsDir, `${key}.hbs`);
29
29
  if (!fs.existsSync(filePath))
30
30
  return null;
31
31
  const source = fs.readFileSync(filePath, 'utf8');
32
- return Handlebars.compile(source);
32
+ return compile(source);
33
33
  }
34
- async render(templateKey, context = {}, layoutKey = 'default') {
34
+ async render(templateKey, context = {}, options = { layoutKey: 'default' }) {
35
35
  const template = this.loadTemplate(templateKey);
36
36
  const bodyHtml = template(context);
37
- const layout = this.loadLayout(layoutKey);
37
+ const layout = this.loadLayout(options?.layoutKey || 'default');
38
38
  if (!layout)
39
39
  return bodyHtml;
40
40
  return layout({ ...context, body: bodyHtml });
@@ -42,6 +42,7 @@ let HandlebarsTemplateEngine = class HandlebarsTemplateEngine {
42
42
  };
43
43
  HandlebarsTemplateEngine = __decorate([
44
44
  Injectable(),
45
- __metadata("design:paramtypes", [ConfigService])
45
+ __param(0, Inject(MAILER_TEMPLATE_ENGINE_OPTIONS)),
46
+ __metadata("design:paramtypes", [Object])
46
47
  ], HandlebarsTemplateEngine);
47
48
  export { HandlebarsTemplateEngine };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@brilab-mailer/template-handlebars",
3
- "version": "0.0.1-beta.49",
3
+ "version": "0.0.1-beta.51",
4
4
  "author": "Bohdan Radchenko <radchenkobs@gmail.com>",
5
5
  "type": "module",
6
6
  "main": "./index.js",