/
StaticTemplateEngine.ts
36 lines (32 loc) · 1.42 KB
/
StaticTemplateEngine.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
import type { AsyncHandler } from '../handlers/AsyncHandler';
import type { TemplateEngineInput, Template } from './TemplateEngine';
import { TemplateEngine } from './TemplateEngine';
import Dict = NodeJS.Dict;
/**
* Template engine that renders output based on a static template file.
*/
export class StaticTemplateEngine<T extends Dict<any> = Dict<any>> extends TemplateEngine<T> {
private readonly template: Template;
private readonly templateEngine: AsyncHandler<TemplateEngineInput<T>, string>;
/**
* Creates a new StaticTemplateEngine.
*
* @param templateEngine - The template engine that should be used for processing the template.
* @param template - The static template to be used.
*/
public constructor(templateEngine: AsyncHandler<TemplateEngineInput<T>, string>, template: Template) {
super();
this.template = template;
this.templateEngine = templateEngine;
}
public async canHandle({ contents, template }: TemplateEngineInput<T>): Promise<void> {
if (typeof template !== 'undefined') {
throw new Error('StaticTemplateEngine does not support template as handle input, ' +
'provide a template via the constructor instead!');
}
return this.templateEngine.canHandle({ contents, template: this.template });
}
public async handle({ contents }: TemplateEngineInput<T>): Promise<string> {
return this.templateEngine.handle({ contents, template: this.template });
}
}