fix: Convert TemplateEngine to AsyncHandlers

This commit is contained in:
Wannes Kerckhove
2022-06-30 11:36:19 +02:00
committed by Joachim Van Herwegen
parent a3c7baf6d3
commit cf74ce3d2a
43 changed files with 484 additions and 321 deletions

View File

@@ -43,13 +43,13 @@ export class ContainerToTemplateConverter extends BaseTypedRepresentationConvert
}
public async handle({ identifier, representation }: RepresentationConverterArgs): Promise<Representation> {
const rendered = await this.templateEngine.render({
const rendered = await this.templateEngine.handleSafe({ contents: {
identifier: identifier.path,
name: this.getLocalName(identifier.path),
container: true,
children: await this.getChildResources(identifier, representation.data),
parents: this.getParentContainers(identifier),
});
}});
return new BasicRepresentation(rendered, representation.metadata, this.contentType);
}

View File

@@ -58,9 +58,9 @@ export class DynamicJsonToTemplateConverter extends RepresentationConverter {
return representation;
}
const json = JSON.parse(await readableToString(representation.data));
const contents = JSON.parse(await readableToString(representation.data));
const rendered = await this.templateEngine.render(json, { templateFile: typeMap[type] });
const rendered = await this.templateEngine.handleSafe({ contents, template: { templateFile: typeMap[type] }});
const metadata = new RepresentationMetadata(representation.metadata, { [CONTENT_TYPE]: type });
return new BasicRepresentation(rendered, metadata);

View File

@@ -65,8 +65,8 @@ export class ErrorToTemplateConverter extends BaseTypedRepresentationConverter {
try {
const templateFile = `${error.errorCode}${this.extension}`;
assert(isValidFileName(templateFile), 'Invalid error template name');
description = await this.templateEngine.render(error.details ?? {},
{ templateFile, templatePath: this.codeTemplatesPath });
description = await this.templateEngine.handleSafe({ contents: error.details ?? {},
template: { templateFile, templatePath: this.codeTemplatesPath }});
} catch {
// In case no template is found, or rendering errors, we still want to convert
}
@@ -74,8 +74,9 @@ export class ErrorToTemplateConverter extends BaseTypedRepresentationConverter {
// Render the main template, embedding the rendered error description
const { name, message, stack } = error;
const variables = { name, message, stack, description };
const rendered = await this.templateEngine.render(variables, { templateFile: this.mainTemplatePath });
const contents = { name, message, stack, description };
const rendered = await this.templateEngine
.handleSafe({ contents, template: { templateFile: this.mainTemplatePath }});
return new BasicRepresentation(rendered, representation.metadata, this.contentType);
}

View File

@@ -24,7 +24,7 @@ export class MarkdownToHtmlConverter extends BaseTypedRepresentationConverter {
public async handle({ representation }: RepresentationConverterArgs): Promise<Representation> {
const markdown = await readableToString(representation.data);
const htmlBody = marked(markdown);
const html = await this.templateEngine.render({ htmlBody });
const html = await this.templateEngine.handleSafe({ contents: { htmlBody }});
return new BasicRepresentation(html, representation.metadata, TEXT_HTML);
}