Separate happy path utility functions for requests
This commit is contained in:
@@ -2,7 +2,7 @@ import { SESSION_COOKIE_NAME } from "../auth/token";
|
||||
import { tokenLifetimes } from "../auth/types";
|
||||
import { services } from "../services";
|
||||
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> => {
|
||||
if (call.method === "GET") {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { SESSION_COOKIE_NAME } from "../auth/token";
|
||||
import { services } from "../services";
|
||||
import type { Call, Result, Route } from "../types";
|
||||
import { redirect } from "../util";
|
||||
import { redirect } from "../request/util";
|
||||
|
||||
const logoutHandler = async (call: Call): Promise<Result> => {
|
||||
// Extract token from cookie and invalidate the session
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { DateTime } from "ts-luxon";
|
||||
import { services } from "../services";
|
||||
import type { Call, Result, Route } from "../types";
|
||||
import { html, render } from "../util";
|
||||
import { html, render } from "../request/util";
|
||||
import { loginRoute } from "./login";
|
||||
import { logoutRoute } from "./logout";
|
||||
|
||||
|
||||
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 };
|
||||
@@ -5,6 +5,17 @@ import { getCurrentUser } from "../context";
|
||||
import { db, migrate, migrationStatus, PostgresAuthStore } from "../database";
|
||||
import { getLogs, log } from "../logging";
|
||||
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 = {
|
||||
db,
|
||||
@@ -42,6 +53,7 @@ const auth = new AuthService(authStore);
|
||||
// Keep this asciibetically sorted
|
||||
const services = {
|
||||
auth,
|
||||
conf,
|
||||
database,
|
||||
logging,
|
||||
misc,
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user