I can't be bothered to even explain what happene here

This commit is contained in:
2025-02-07 23:18:04 -05:00
parent d982567c7b
commit bc3443b1dc
21 changed files with 994 additions and 177 deletions

View File

@@ -1,8 +1,22 @@
import { Context, Next } from "@oak/oak";
// @ts-types = "npm:@types/express"
import express from "express";
import Logging from "@proxnet/undead-logging";
const log = new Logging('APIUtils');
interface AppRouter {
path: string,
router: express.Router
}
export function createRouter(path: string) {
const router: AppRouter = {
path: path,
router: express.Router()
}
return router;
}
export function generateRandomString(length: number) {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let randomString = '';
@@ -18,12 +32,11 @@ export function generateRandomString(length: number) {
const instanceId = generateRandomString(128);
export function checkQueryTypes<T>(typeDef: T) {
return (ctx: Context, nxt: Next) => {
return (rq: express.Request, rs: express.Response, nxt: express.NextFunction) => {
for (const key in typeDef) {
if (typeof Object.fromEntries(ctx.request.url.searchParams)[key] !== typeof (typeDef)[key]) {
ctx.response.status = 400;
setContentType(ctx, 'application/json');
ctx.response.body = JSON.stringify(genericResponseFormat(true, "One or more query parameters were invalid or not found."));
if (typeof rq.query[key] !== typeof (typeDef)[key]) {
rs.statusCode = 400;
rs.json(genericResponseFormat(true, "One or more query parameters were invalid or not found."));
return;
}
}
@@ -31,13 +44,12 @@ export function checkQueryTypes<T>(typeDef: T) {
};
}
export function checkBodyTypes<T>(typeDef: T) {
return async (ctx: Context, nxt: Next) => {
return (rq: express.Request, rs: express.Response, nxt: express.NextFunction) => {
for (const key in typeDef) {
if (typeof (await ctx.request.body.json())[key] !== typeof (typeDef)[key]) {
if (typeof rq.body[key] !== typeof (typeDef)[key]) {
log.e(`Body check for key '${key}' failed.`);
ctx.response.status = 400;
setContentType(ctx, 'application/json');
ctx.response.body = JSON.stringify(genericResponseFormat(true, "One or more body values were invalid or not found."));
rs.statusCode = 400;
rs.json(genericResponseFormat(true, "One or more body values were invalid or not found."));
return;
}
}
@@ -49,9 +61,8 @@ export function genericResponseFormat(failure: boolean, msg: string | null = nul
return { failed: failure, instance: instanceId, message: msg, data: data };
}
export function genericResponse(failure: boolean, msg: string | null = null, data = null) {
return (ctx: Context) => {
setContentType(ctx, 'application/json');
ctx.response.body = JSON.stringify({ failed: failure, instance: instanceId, message: msg, data: data });
return (_rq: express.Request, rs: express.Response) => {
rs.json({ failed: failure, instance: instanceId, message: msg, data: data });
};
}
type RecNetResponse = {
@@ -60,29 +71,35 @@ type RecNetResponse = {
};
export function RecNetResponse(success: boolean, message: string) {
const msg: RecNetResponse = { Success: success, Message: message };
return (ctx: Context) => {
setContentType(ctx, 'application/json');
ctx.response.body = JSON.stringify(msg);
return (_rq: express.Request, rs: express.Response) => {
rs.json(msg);
}
}
export async function logBody(ctx: Context, nxt: Next) {
export function logBody(rq: express.Request, _rs: express.Response, nxt: express.NextFunction) {
nxt();
log.d(`Request body: ${JSON.stringify(await ctx.request.body.text())}`);
log.d(`Request body: ${JSON.stringify(rq.body)}`);
}
export function emptyArrayResponse(ctx: Context) {
setContentType(ctx, 'application/json');
ctx.response.body = JSON.stringify([]);
export function emptyArrayResponse(_rq: express.Request, rs: express.Response) {
rs.json([]);
}
export function setJSONBody(ctx: Context, obj: object) {
ctx.response.type = 'json';
ctx.response.body = JSON.stringify(obj);
export function getSrcIpDefault(rq: express.Request) {
const cfIp = rq.header('cf-connecting-ip');
if (cfIp !== undefined) return cfIp;
const xrIp = rq.header('x-real-ip');
if (xrIp !== undefined) return xrIp;
const ip = typeof rq.ip === 'undefined' ? '(unknown source)' : rq.ip;
return ip;
}
export function setContentType(ctx: Context, type: string) {
ctx.response.headers.set('Content-Type', type);
export function statusResponse(code: number) {
return (_rq: express.Request, rs: express.Response) => {
rs.sendStatus(code);
}
}
export * as APIUtils from "./apiutils.ts"