From a380613d0b56cb80f6b742ae9cb10d7b43f60117 Mon Sep 17 00:00:00 2001 From: Shinwoo PARK Date: Sun, 10 Dec 2023 18:48:14 +0900 Subject: [PATCH] fix: add metadata proxy to each logged decorators --- dist/lib/logged.js | 27 ++++++++++++++++++++------- dist/package.json | 2 +- src/logged.ts | 45 +++++++++++++++++++++++++++------------------ 3 files changed, 48 insertions(+), 26 deletions(-) diff --git a/dist/lib/logged.js b/dist/lib/logged.js index 7379e96..56fcccc 100644 --- a/dist/lib/logged.js +++ b/dist/lib/logged.js @@ -37,13 +37,11 @@ function LoggedInjectable(options) { methods.forEach((method) => { if (method !== "constructor" && typeof target.prototype[method] === "function") { - const all = Reflect.getMetadataKeys(target.prototype[method]).map((k) => [k, Reflect.getMetadata(k, target.prototype[method])]); if (options && options.verbose) logger.log(`LoggedFunction applied to ${method}`); LoggedFunction(target.prototype, method, { value: target.prototype[method], }); - all.forEach(([k, v]) => Reflect.defineMetadata(k, v, target.prototype[method])); } }); (0, common_1.Injectable)(options)(target); @@ -61,15 +59,14 @@ function LoggedController(param) { methods.forEach((method) => { if (method !== "constructor" && typeof target.prototype[method] === "function") { - const path = Reflect.getMetadata("path", target.prototype[method]); - const httpMethod = Reflect.getMetadata("method", target.prototype[method]); - const all = Reflect.getMetadataKeys(target.prototype[method]).map((k) => [k, Reflect.getMetadata(k, target.prototype[method])]); - if (verbose) + if (verbose) { + const path = Reflect.getMetadata("path", target.prototype[method]); + const httpMethod = Reflect.getMetadata("method", target.prototype[method]); logger.log(`LoggedRoute applied to ${method} (${RevRequestMethod[httpMethod]} ${path})`); + } LoggedRoute()(target.prototype, method, { value: target.prototype[method], }); - all.forEach(([k, v]) => Reflect.defineMetadata(k, v, target.prototype[method])); } }); (0, common_1.Controller)(param)(target); @@ -156,6 +153,10 @@ function LoggedFunction(_target, key, descriptor) { logger.warn(`LoggedFunction decorator applied to non-function property: ${key}`); return; } + const all = Reflect.getMetadataKeys(fn).map((k) => [ + k, + Reflect.getMetadata(k, fn), + ]); const scopedLoggerInjectableParam = Reflect.getOwnMetadata(reflected_2.scopedLogger, _target, key); const loggedParams = Reflect.getOwnMetadata(reflected_2.loggedParam, _target, key); const scopeKeys = Reflect.getOwnMetadata(reflected_1.scopeKey, _target, key); @@ -168,6 +169,10 @@ function LoggedFunction(_target, key, descriptor) { }, key); _target[key] = overrideFunction; descriptor.value = overrideFunction; + all.forEach(([k, v]) => { + Reflect.defineMetadata(k, v, _target[key]); + Reflect.defineMetadata(k, v, descriptor.value); + }); } exports.LoggedFunction = LoggedFunction; function LoggedRoute(route) { @@ -179,6 +184,10 @@ function LoggedRoute(route) { logger.warn(`LoggedRoute decorator applied to non-function property: ${key}`); return; } + const all = Reflect.getMetadataKeys(fn).map((k) => [ + k, + Reflect.getMetadata(k, fn), + ]); const httpPath = Reflect.getMetadata("path", fn); const httpMethod = Reflect.getMetadata("method", fn); const fullRoute = `${_target.constructor.name}::${route ?? httpPath}[${RevRequestMethod[httpMethod]}]`; @@ -196,6 +205,10 @@ function LoggedRoute(route) { }); _target[key] = overrideFunction; descriptor.value = overrideFunction; + all.forEach(([k, v]) => { + Reflect.defineMetadata(k, v, _target[key]); + Reflect.defineMetadata(k, v, descriptor.value); + }); }; } exports.LoggedRoute = LoggedRoute; diff --git a/dist/package.json b/dist/package.json index c8eb473..0154839 100644 --- a/dist/package.json +++ b/dist/package.json @@ -1,6 +1,6 @@ { "name": "nestlogged", - "version": "2.0.2", + "version": "2.0.3", "description": "A NestJS Logger Decorator Library", "main": "lib/index.js", "repository": "https://github.com/worplo/nestlogged", diff --git a/src/logged.ts b/src/logged.ts index 83c737c..55d88e6 100644 --- a/src/logged.ts +++ b/src/logged.ts @@ -56,17 +56,11 @@ export function LoggedInjectable( method !== "constructor" && typeof target.prototype[method] === "function" ) { - const all = Reflect.getMetadataKeys(target.prototype[method]).map( - (k) => [k, Reflect.getMetadata(k, target.prototype[method])] - ); if (options && options.verbose) logger.log(`LoggedFunction applied to ${method}`); LoggedFunction(target.prototype, method, { value: target.prototype[method], }); - all.forEach(([k, v]) => - Reflect.defineMetadata(k, v, target.prototype[method]) - ); } }); @@ -100,24 +94,19 @@ export function LoggedController(param?: any): (target: any) => void { method !== "constructor" && typeof target.prototype[method] === "function" ) { - const path = Reflect.getMetadata("path", target.prototype[method]); - const httpMethod = Reflect.getMetadata( - "method", - target.prototype[method] - ); - const all = Reflect.getMetadataKeys(target.prototype[method]).map( - (k) => [k, Reflect.getMetadata(k, target.prototype[method])] - ); - if (verbose) + if (verbose) { + const path = Reflect.getMetadata("path", target.prototype[method]); + const httpMethod = Reflect.getMetadata( + "method", + target.prototype[method] + ); logger.log( `LoggedRoute applied to ${method} (${RevRequestMethod[httpMethod]} ${path})` ); + } LoggedRoute()(target.prototype, method, { value: target.prototype[method], }); - all.forEach(([k, v]) => - Reflect.defineMetadata(k, v, target.prototype[method]) - ); } }); @@ -263,6 +252,11 @@ export function LoggedFunction, R>( return; } + const all = Reflect.getMetadataKeys(fn).map((k) => [ + k, + Reflect.getMetadata(k, fn), + ]); + const scopedLoggerInjectableParam: number = Reflect.getOwnMetadata( scopedLogger, _target, @@ -297,6 +291,11 @@ export function LoggedFunction, R>( _target[key] = overrideFunction; descriptor.value = overrideFunction; + + all.forEach(([k, v]) => { + Reflect.defineMetadata(k, v, _target[key]); + Reflect.defineMetadata(k, v, descriptor.value); + }); } export function LoggedRoute, R>(route?: string) { @@ -318,6 +317,11 @@ export function LoggedRoute, R>(route?: string) { return; } + const all = Reflect.getMetadataKeys(fn).map((k) => [ + k, + Reflect.getMetadata(k, fn), + ]); + const httpPath: string = Reflect.getMetadata("path", fn); const httpMethod: RequestMethod = Reflect.getMetadata("method", fn); @@ -362,5 +366,10 @@ export function LoggedRoute, R>(route?: string) { _target[key] = overrideFunction; descriptor.value = overrideFunction; + + all.forEach(([k, v]) => { + Reflect.defineMetadata(k, v, _target[key]); + Reflect.defineMetadata(k, v, descriptor.value); + }); }; }