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

@@ -2,7 +2,7 @@ import { SESSION_COOKIE_NAME } from "../auth/token";
import { tokenLifetimes } from "../auth/types"; import { tokenLifetimes } from "../auth/types";
import { services } from "../services"; import { services } from "../services";
import type { Call, Result, Route } from "../types"; import type { Call, Result, Route } from "../types";
import { html, redirect, render } from "../util"; import { html, redirect, render } from "../request/util";
const loginHandler = async (call: Call): Promise<Result> => { const loginHandler = async (call: Call): Promise<Result> => {
if (call.method === "GET") { if (call.method === "GET") {

View File

@@ -1,7 +1,7 @@
import { SESSION_COOKIE_NAME } from "../auth/token"; import { SESSION_COOKIE_NAME } from "../auth/token";
import { services } from "../services"; import { services } from "../services";
import type { Call, Result, Route } from "../types"; import type { Call, Result, Route } from "../types";
import { redirect } from "../util"; import { redirect } from "../request/util";
const logoutHandler = async (call: Call): Promise<Result> => { const logoutHandler = async (call: Call): Promise<Result> => {
// Extract token from cookie and invalidate the session // Extract token from cookie and invalidate the session

View File

@@ -1,7 +1,7 @@
import { DateTime } from "ts-luxon"; import { DateTime } from "ts-luxon";
import { services } from "../services"; import { services } from "../services";
import type { Call, Result, Route } from "../types"; import type { Call, Result, Route } from "../types";
import { html, render } from "../util"; import { html, render } from "../request/util";
import { loginRoute } from "./login"; import { loginRoute } from "./login";
import { logoutRoute } from "./logout"; import { logoutRoute } from "./logout";

46
express/request/util.ts Normal file
View 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 };

View File

@@ -5,6 +5,17 @@ import { getCurrentUser } from "../context";
import { db, migrate, migrationStatus, PostgresAuthStore } from "../database"; import { db, migrate, migrationStatus, PostgresAuthStore } from "../database";
import { getLogs, log } from "../logging"; import { getLogs, log } from "../logging";
import type { MaybeUser } from "../user"; import type { MaybeUser } from "../user";
import nunjucks from 'nunjucks'
const conf = {
templateEngine: () => {
return {
renderTemplate: (template: string, context: object) => {
return nunjucks.renderString(template, context);
},
}
}
};
const database = { const database = {
db, db,
@@ -42,6 +53,7 @@ const auth = new AuthService(authStore);
// Keep this asciibetically sorted // Keep this asciibetically sorted
const services = { const services = {
auth, auth,
conf,
database, database,
logging, logging,
misc, misc,

View File

@@ -1,9 +1,5 @@
import { readFile } from "node:fs/promises"; import { readFile } from "node:fs/promises";
import nunjucks from "nunjucks"; 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 // FIXME: Handle the error here
const loadFile = async (path: string): Promise<string> => { const loadFile = async (path: string): Promise<string> => {
@@ -13,35 +9,8 @@ const loadFile = async (path: string): Promise<string> => {
return data; 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; export {
loadFile,
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 };