mirror of
https://github.com/Radiquum/AniX.git
synced 2025-09-10 00:13:54 +05:00
feat/api-prox: add logger
This commit is contained in:
parent
cbfd24146d
commit
1a439f0a77
4 changed files with 41 additions and 9 deletions
19
api-prox/src/utils/logger.ts
Normal file
19
api-prox/src/utils/logger.ts
Normal 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}`
|
||||
);
|
||||
}
|
||||
};
|
|
@ -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") &&
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue