2023-03-09 08:14:29 +00:00
|
|
|
import Handlebars from "handlebars";
|
2023-05-05 06:15:47 +00:00
|
|
|
import { createLogger } from "@saleor/apps-shared";
|
2023-03-09 08:14:29 +00:00
|
|
|
|
2023-05-05 06:15:47 +00:00
|
|
|
const logger = createLogger({
|
2023-06-16 09:07:17 +00:00
|
|
|
name: "compileHandlebarsTemplate",
|
2023-03-09 08:14:29 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
export const compileHandlebarsTemplate = (template: string, variables: any) => {
|
|
|
|
logger.debug("Compiling handlebars template");
|
|
|
|
try {
|
|
|
|
const templateDelegate = Handlebars.compile(template);
|
|
|
|
const htmlTemplate = templateDelegate(variables);
|
2023-05-05 06:15:47 +00:00
|
|
|
|
2023-03-09 08:14:29 +00:00
|
|
|
logger.debug("Template successfully compiled");
|
|
|
|
return {
|
|
|
|
template: htmlTemplate,
|
|
|
|
};
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(error);
|
|
|
|
return {
|
|
|
|
errors: [{ message: "Error during the using the handlebars template" }],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|