-
Notifications
You must be signed in to change notification settings - Fork 177
/
mjml.adapter.ts
47 lines (43 loc) · 1.39 KB
/
mjml.adapter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/** Dependencies **/
import { HandlebarsAdapter } from './handlebars.adapter';
import { EjsAdapter } from './ejs.adapter';
import { TemplateAdapterConfig } from '../interfaces/template-adapter-config.interface';
import { PugAdapter } from './pug.adapter';
import { TemplateAdapter } from '../interfaces/template-adapter.interface';
import { MailerOptions } from '../interfaces/mailer-options.interface';
import * as mjml2html from 'mjml';
export class MjmlAdapter implements TemplateAdapter {
private engine: TemplateAdapter | null;
constructor(
engine: TemplateAdapter | '' | 'pug' | 'handlebars' | 'ejs',
config?: TemplateAdapterConfig,
others?: {
handlebar?: {
helper?: any;
};
},
) {
this.engine = engine as TemplateAdapter;
if (typeof engine == 'string') {
if (engine === 'pug') {
this.engine = new PugAdapter(config);
} else if (engine === 'handlebars') {
this.engine = new HandlebarsAdapter(others.handlebar.helper, config);
} else if (engine === 'ejs') {
this.engine = new EjsAdapter(config);
} else if (engine === '') {
engine = null;
}
}
}
public compile(mail: any, callback: any, mailerOptions: MailerOptions): void {
this?.engine?.compile(
mail,
() => {
mail.data.html = mjml2html(mail.data.html).html;
callback();
},
mailerOptions,
);
}
}