47 lines
1.2 KiB
TypeScript
47 lines
1.2 KiB
TypeScript
import { contentTypes } from "../content-types";
|
|
import { executionContext } from "../execution-context";
|
|
import { httpCodes } from "../http-codes";
|
|
import type { RedirectResult, Result } from "../types";
|
|
import {services} from '../services'
|
|
|
|
import{loadFile}from'../util'
|
|
|
|
|
|
type NoUser={
|
|
[key: string]: unknown;
|
|
} & {
|
|
user?: never;
|
|
}
|
|
|
|
const render = async (path: string, ctx?: NoUser): Promise<string> => {
|
|
const fullPath = `${executionContext.diachron_root}/templates/${path}.html.njk`;
|
|
const template = await loadFile(fullPath);
|
|
const user = services.session.getUser();
|
|
const context = {user, ...ctx}
|
|
const engine = services.conf.templateEngine()
|
|
const retval = engine.renderTemplate(template, context);
|
|
|
|
return retval;
|
|
};
|
|
|
|
const html = (payload: string): Result => {
|
|
const retval: Result = {
|
|
code: httpCodes.success.OK,
|
|
result: payload,
|
|
contentType: contentTypes.text.html,
|
|
};
|
|
|
|
return retval;
|
|
};
|
|
|
|
const redirect = (location: string): RedirectResult => {
|
|
return {
|
|
code: httpCodes.redirection.SeeOther,
|
|
contentType: contentTypes.text.plain,
|
|
result: "",
|
|
redirect: location,
|
|
};
|
|
};
|
|
|
|
export { render, html, redirect };
|