Separate happy path utility functions for requests
This commit is contained in:
46
express/request/util.ts
Normal file
46
express/request/util.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
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 };
|
||||
Reference in New Issue
Block a user