Shortened database/utils.ts by adding a 'Query Database' helper function to reduce boilerplate/copied code, also added functions form apping the Post, Account and Comment row. api/main.ts now has more unfinished endpoints and will now work using functions instead of doing everything inside of the routes, created a helpers.ts file to make it a bit more modular and easier to look over.

This commit is contained in:
Esad Mustafoski
2024-11-13 23:29:59 +01:00
parent 7a6152be51
commit b0f8a51cc8
6 changed files with 298 additions and 245 deletions

44
api/helpers.ts Normal file
View File

@@ -0,0 +1,44 @@
/// <reference lib="deno.ns" />
/**
* @author Esad Mustafoski
* @file api/helpers.ts
* @description Helper functions for the API
*
*/
import { Context } from "https://deno.land/x/oak/mod.ts";
import { hash } from "node:crypto";
export type ApiResponse = {
status: number;
body: unknown;
};
// --- Helper Functions --- //
/**
* @description Sends a response to the client
* @usage sendResponse(ctx, { status: 200, body: { message: "Success" } })
* Status is the HTTP Status code
* Body is the response body/message/data.
*/
const sendResponse = (ctx: Context, {status, body}: ApiResponse): void => {
ctx.response.status = status;
ctx.response.body = body as any;
};
/**
* @usage errorResponse(ctx, 401, "Unauthorized")
* @see sendResponse
*/
const errorResponse = (ctx: Context, status: number, message: string): void => {
sendResponse(ctx, { status, body: { error: message } });
};
/**
* @description Hashing Function for Passwords/etc
* @param password The password to hash
* Works only up to 5 Megabytes
*/
const hashPassword = async (password: string): Promise<string> => {
return hash("sha256", password);
}