style: prettified

This commit is contained in:
Shinwoo PARK 2023-12-03 18:46:25 +09:00
parent 185e22cb84
commit a8f7cb4f95

View File

@ -5,10 +5,10 @@ import {loggedParam, scopedLogger} from "./reflected";
import objectContainedLogged from "./functions"; import objectContainedLogged from "./functions";
function loggerInit(_target: any) { function loggerInit(_target: any) {
if (!Object.getOwnPropertyNames(_target).includes('logger')) { if (!Object.getOwnPropertyNames(_target).includes("logger")) {
const newTargetLogger = new Logger(_target.constructor.name); const newTargetLogger = new Logger(_target.constructor.name);
newTargetLogger.log('Logger Initialized.'); newTargetLogger.log("Logger Initialized.");
Object.defineProperty(_target, 'logger', { Object.defineProperty(_target, "logger", {
writable: false, writable: false,
enumerable: false, enumerable: false,
configurable: false, configurable: false,
@ -20,7 +20,7 @@ function loggerInit(_target: any) {
export function LoggedFunction<F extends Array<any>, R>( export function LoggedFunction<F extends Array<any>, R>(
_target: any, _target: any,
key: string, key: string,
descriptor: TypedPropertyDescriptor<(...args: F) => Promise<R>>, descriptor: TypedPropertyDescriptor<(...args: F) => Promise<R>>
) { ) {
loggerInit(_target); loggerInit(_target);
@ -34,50 +34,52 @@ export function LoggedFunction<F extends Array<any>, R>(
const scopedLoggerInjectableParam: number = Reflect.getOwnMetadata( const scopedLoggerInjectableParam: number = Reflect.getOwnMetadata(
scopedLogger, scopedLogger,
_target, _target,
key, key
); );
if ( if (
typeof scopedLoggerInjectableParam !== 'undefined' && typeof scopedLoggerInjectableParam !== "undefined" &&
(args.length <= scopedLoggerInjectableParam || (args.length <= scopedLoggerInjectableParam ||
!(args[scopedLoggerInjectableParam] instanceof ScopedLogger)) !(args[scopedLoggerInjectableParam] instanceof ScopedLogger))
) { ) {
args[scopedLoggerInjectableParam] = new ScopedLogger(logger, key); args[scopedLoggerInjectableParam] = new ScopedLogger(logger, key);
} else if (typeof scopedLoggerInjectableParam !== 'undefined') { } else if (typeof scopedLoggerInjectableParam !== "undefined") {
args[scopedLoggerInjectableParam] = new ScopedLogger( args[scopedLoggerInjectableParam] = new ScopedLogger(
args[scopedLoggerInjectableParam], args[scopedLoggerInjectableParam],
key, key
); );
} }
const injectedLogger = const injectedLogger =
typeof scopedLoggerInjectableParam !== 'undefined' typeof scopedLoggerInjectableParam !== "undefined"
? args[scopedLoggerInjectableParam] ? args[scopedLoggerInjectableParam]
: logger; : logger;
const loggedParams: LoggedParamReflectData[] = Reflect.getOwnMetadata( const loggedParams: LoggedParamReflectData[] = Reflect.getOwnMetadata(
loggedParam, loggedParam,
_target, _target,
key, key
) );
injectedLogger.log( injectedLogger.log(
`CALL ${key} ${ `CALL ${key} ${
loggedParams && loggedParams.length > 0 loggedParams && loggedParams.length > 0
? 'WITH ' + ? "WITH " +
( (
await Promise.all( await Promise.all(
loggedParams.map( loggedParams.map(
async ({ name, index, include, exclude }) => async ({ name, index, include, exclude }) =>
name + '=' + (await objectContainedLogged(args[index], { name +
"=" +
(await objectContainedLogged(args[index], {
include, include,
exclude, exclude,
})), }))
),
) )
).join(', ') )
: '' ).join(", ")
}`, : ""
}`
); );
try { try {
@ -95,7 +97,7 @@ export function LoggedRoute<F extends Array<any>, R>(route: string) {
return ( return (
_target: any, _target: any,
key: string, key: string,
descriptor: TypedPropertyDescriptor<(...args: F) => Promise<R>>, descriptor: TypedPropertyDescriptor<(...args: F) => Promise<R>>
) => { ) => {
loggerInit(_target); loggerInit(_target);
@ -110,11 +112,11 @@ export function LoggedRoute<F extends Array<any>, R>(route: string) {
const scopedLoggerInjectableParam: number = Reflect.getOwnMetadata( const scopedLoggerInjectableParam: number = Reflect.getOwnMetadata(
scopedLogger, scopedLogger,
_target, _target,
key, key
); );
if ( if (
typeof scopedLoggerInjectableParam !== 'undefined' && typeof scopedLoggerInjectableParam !== "undefined" &&
(args.length <= scopedLoggerInjectableParam || (args.length <= scopedLoggerInjectableParam ||
!(args[scopedLoggerInjectableParam] instanceof ScopedLogger)) !(args[scopedLoggerInjectableParam] instanceof ScopedLogger))
) { ) {
@ -122,33 +124,35 @@ export function LoggedRoute<F extends Array<any>, R>(route: string) {
} }
const injectedLogger = const injectedLogger =
typeof scopedLoggerInjectableParam !== 'undefined' typeof scopedLoggerInjectableParam !== "undefined"
? args[scopedLoggerInjectableParam] ? args[scopedLoggerInjectableParam]
: logger; : logger;
const loggedParams: LoggedParamReflectData[] = Reflect.getOwnMetadata( const loggedParams: LoggedParamReflectData[] = Reflect.getOwnMetadata(
loggedParam, loggedParam,
_target, _target,
key, key
) );
injectedLogger.log( injectedLogger.log(
`HIT HTTP ${fullRoute} (${key}) ${ `HIT HTTP ${fullRoute} (${key}) ${
loggedParams && loggedParams.length > 0 loggedParams && loggedParams.length > 0
? 'WITH ' + ? "WITH " +
( (
await Promise.all( await Promise.all(
loggedParams.map( loggedParams.map(
async ({ name, index, include, exclude }) => async ({ name, index, include, exclude }) =>
name + '=' + (await objectContainedLogged(args[index], { name +
"=" +
(await objectContainedLogged(args[index], {
include, include,
exclude, exclude,
})), }))
),
) )
).join(', ') )
: '' ).join(", ")
}`, : ""
}`
); );
try { try {
@ -160,5 +164,5 @@ export function LoggedRoute<F extends Array<any>, R>(route: string) {
throw e; throw e;
} }
}; };
} };
} }