@ackplus/nest-dynamic-templates
Version:
A powerful and flexible dynamic template rendering library for NestJS applications with support for Nunjucks, Handlebars, EJS, Pug, MJML, Markdown, and more.
95 lines • 4.43 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TemplateEngineRegistryService = void 0;
const common_1 = require("@nestjs/common");
const nunjucks_engine_1 = require("../engines/template/nunjucks.engine");
const mjml_engine_1 = require("../engines/language/mjml.engine");
const html_engine_1 = require("../engines/language/html.engine");
const language_1 = require("../engines/language");
const ejs_engine_1 = require("../engines/template/ejs.engine");
const handlebars_engine_1 = require("../engines/template/handlebars.engine");
const pug_engine_1 = require("../engines/template/pug.engine");
const constant_1 = require("../constant");
const template_errors_1 = require("../errors/template.errors");
const TEMPLATE_ENGINES = [nunjucks_engine_1.NunjucksEngine, handlebars_engine_1.HandlebarsEngine, ejs_engine_1.EjsEngine, pug_engine_1.PugEngine];
const LANGUAGE_ENGINES = [mjml_engine_1.MjmlEngine, html_engine_1.HtmlEngine, language_1.TextEngine, language_1.MarkdownEngine];
let TemplateEngineRegistryService = class TemplateEngineRegistryService {
config;
templateEngines = new Map();
languageEngines = new Map();
constructor(config) {
this.config = config;
this.registerEnabledEngines();
}
registerEnabledEngines() {
const custom = {
filters: this.config.filters,
globals: this.config.globals,
};
for (const Engine of TEMPLATE_ENGINES) {
if (!this.config.engines.template.includes(Engine.engineName))
continue;
const options = this.config.engineOptions.template[Engine.engineName];
this.templateEngines.set(Engine.engineName, new Engine(options, custom));
}
for (const Engine of LANGUAGE_ENGINES) {
if (!this.config.engines.language.includes(Engine.engineName))
continue;
const options = this.config.engineOptions.language[Engine.engineName];
this.languageEngines.set(Engine.engineName, new Engine(options));
}
}
getTemplateEngine(format) {
const engine = this.templateEngines.get(format);
if (!engine) {
throw new template_errors_1.TemplateEngineUnavailableError({
engine: format,
kind: 'template',
reason: 'not-enabled',
});
}
return engine;
}
getLanguageEngine(format) {
const engine = this.languageEngines.get(format);
if (!engine) {
throw new template_errors_1.TemplateEngineUnavailableError({
engine: format,
kind: 'language',
reason: 'not-enabled',
});
}
return engine;
}
hasTemplateEngine(format) {
return this.templateEngines.has(format);
}
hasLanguageEngine(format) {
return this.languageEngines.has(format);
}
getSupportedTemplateFormats() {
return [...this.templateEngines.keys()];
}
getSupportedLanguageFormats() {
return [...this.languageEngines.keys()];
}
};
exports.TemplateEngineRegistryService = TemplateEngineRegistryService;
exports.TemplateEngineRegistryService = TemplateEngineRegistryService = __decorate([
(0, common_1.Injectable)(),
__param(0, (0, common_1.Inject)(constant_1.NEST_DYNAMIC_TEMPLATES_OPTIONS)),
__metadata("design:paramtypes", [Object])
], TemplateEngineRegistryService);
//# sourceMappingURL=template-engine.registry.js.map