Separate happy path utility functions for requests

This commit is contained in:
2026-01-17 15:43:52 -06:00
parent 03cc4cf4eb
commit 7ed05695b9
6 changed files with 64 additions and 37 deletions

View File

@@ -1,9 +1,5 @@
import { readFile } from "node:fs/promises";
import nunjucks from "nunjucks";
import { contentTypes } from "./content-types";
import { executionContext } from "./execution-context";
import { httpCodes } from "./http-codes";
import type { RedirectResult, Result } from "./types";
// FIXME: Handle the error here
const loadFile = async (path: string): Promise<string> => {
@@ -13,35 +9,8 @@ const loadFile = async (path: string): Promise<string> => {
return data;
};
const render = async (path: string, ctx?: object): Promise<string> => {
const fullPath = `${executionContext.diachron_root}/templates/${path}.html.njk`;
const template = await loadFile(fullPath);
const c = ctx === undefined ? {} : ctx;
const retval = nunjucks.renderString(template, c);
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 };
export {
loadFile,
}