46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
import { contentTypes } from "../content-types";
|
|
import { core } from "../core";
|
|
import { executionContext } from "../execution-context";
|
|
import { httpCodes } from "../http-codes";
|
|
import type { RedirectResult, Result } from "../types";
|
|
import { loadFile } from "../util";
|
|
import { request } from "./index";
|
|
|
|
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 = request.session.getUser();
|
|
const context = { user, ...ctx };
|
|
const engine = core.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 { html, redirect, render };
|