feat/api-prox: add logger

This commit is contained in:
Kentai Radiquum 2025-09-06 02:12:22 +05:00
parent cbfd24146d
commit 1a439f0a77
Signed by: Radiquum
GPG key ID: 858E8EE696525EED
4 changed files with 41 additions and 9 deletions

View file

@ -0,0 +1,19 @@
export const RouteLogger = (message: string) => {
const args = message.split(" ");
const direction = args[0];
const method = args[1];
const url = new URL("http://example.com" + args[2]);
if (url.searchParams.get("token")) {
url.searchParams.set("token", "*********");
}
if (direction == "<--") {
console.log(`REQ | ${method} ${url.pathname}${url.search}`);
} else {
const status = args[3];
const time = args[4];
console.log(
`RES | ${method} ${url.pathname}${url.search} ${status} ${time}`
);
}
};

View file

@ -10,7 +10,12 @@ type Failure<E> = {
type Result<T, E = Error> = Success<T> | Failure<E>;
export async function tryCatch<T, E = Error>(
type TCError = {
message: string;
code: number;
}
export async function tryCatch<T, E = TCError>(
promise: Promise<T>
): Promise<Result<T, E>> {
try {
@ -21,15 +26,15 @@ export async function tryCatch<T, E = Error>(
}
}
function generateError(message: string, code: number) {
return {message: message, code: code}
function generateError(message: string, code: number): TCError {
return { message: message, code: code }
}
export async function tryCatchAPI<T, E = Error>(
export async function tryCatchAPI<T>(
promise: Promise<any>
): Promise<Result<object | null, object | null>> {
): Promise<Result<T | null, TCError| null>> {
const { data, error }: Awaited<Result<Response | null, Error | null>> = await tryCatch(promise);
if (!data || error) return { data: null, error: error };
if (!data || error) return { data: null, error: generateError("No data returned", 500) };
if (
data.headers.get("content-length") &&