From 7c51a4559d4bf9966c02ee969886e83c0899fee4 Mon Sep 17 00:00:00 2001 From: Shinwoo PARK Date: Mon, 4 Dec 2023 16:28:44 +0900 Subject: [PATCH] fix: make logged class recover existing metadatas after applying decorator to its method --- src/logged.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/logged.ts b/src/logged.ts index b59aef4..914b887 100644 --- a/src/logged.ts +++ b/src/logged.ts @@ -49,10 +49,16 @@ export function LoggedInjectable(options?: ScopeOptions) { method !== "constructor" && typeof target.prototype[method] === "function" ) { + const all = Reflect.getMetadataKeys(target.prototype[method]).map( + (k) => [k, Reflect.getMetadata(k, target.prototype[method])] + ); 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]) + ); } }); @@ -86,14 +92,18 @@ export function LoggedController(param?: any): (target: any) => void { "method", target.prototype[method] ); + const all = Reflect.getMetadataKeys(target.prototype[method]).map( + (k) => [k, Reflect.getMetadata(k, target.prototype[method])] + ); logger.log( `LoggedRoute applied to ${method} (${RevRequestMethod[httpMethod]} ${path})` ); LoggedRoute()(target.prototype, method, { value: target.prototype[method], }); - Reflect.defineMetadata("path", path, target.prototype[method]); - Reflect.defineMetadata("method", httpMethod, target.prototype[method]); + all.forEach(([k, v]) => + Reflect.defineMetadata(k, v, target.prototype[method]) + ); } });