From 09ff7fe976ef534be313e09e384c53138b21a1e8 Mon Sep 17 00:00:00 2001 From: SaltedFish Date: Wed, 20 Dec 2023 17:12:57 +0800 Subject: [PATCH] fix: Fix applyLazyDecorator return when metadataList is empty (#30) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: Fix applyLazyDecorator return when metadataList is empty * fix: Fix Reflection fail caused by invalid target method * chore: rename methodNames to propertyKeys at applyLazyDecorator * Update src/auto-aspect-executor.ts --------- Co-authored-by: 장지훈 --- src/auto-aspect-executor.ts | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/auto-aspect-executor.ts b/src/auto-aspect-executor.ts index aa0fc4a..7198a50 100644 --- a/src/auto-aspect-executor.ts +++ b/src/auto-aspect-executor.ts @@ -47,7 +47,7 @@ export class AutoAspectExecutor implements OnModuleInit { : instanceWrapper.metatype.prototype; // Use scanFromPrototype for support nestjs 8 - const methodNames = this.metadataScanner.scanFromPrototype( + const propertyKeys = this.metadataScanner.scanFromPrototype( target, instanceWrapper.isDependencyTreeStatic() ? Object.getPrototypeOf(target) : target, (name) => name, @@ -55,17 +55,24 @@ export class AutoAspectExecutor implements OnModuleInit { const metadataKey = this.reflector.get(ASPECT, lazyDecorator.constructor); // instance에 method names 를 순회하면서 lazyDecorator.wrap을 적용함 - for (const methodName of methodNames) { + for (const propertyKey of propertyKeys) { + // the target method is must be object or function + // @see: https://github.com/rbuckton/reflect-metadata/blob/9562d6395cc3901eaafaf8a6ed8bc327111853d5/Reflect.ts#L938 + const targetProperty = target[propertyKey]; + if (!targetProperty || (typeof targetProperty !== "object" && typeof targetProperty !== "function")) { + continue; + } + const metadataList: AopMetadata[] = this.reflector.get( metadataKey, - target[methodName], + targetProperty, ); if (!metadataList) { - return; + continue; } for (const aopMetadata of metadataList) { - this.wrapMethod({ lazyDecorator, aopMetadata, methodName, target }); + this.wrapMethod({ lazyDecorator, aopMetadata, methodName: propertyKey, target }); } } }