Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix applyLazyDecorator return when metadataList is empty #30

Merged
merged 4 commits into from
Dec 20, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/auto-aspect-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class AutoAspectExecutor implements OnModuleInit {
private readonly discoveryService: DiscoveryService,
private readonly metadataScanner: MetadataScanner,
private readonly reflector: Reflector,
) {}
) { }
WhiteKiwi marked this conversation as resolved.
Show resolved Hide resolved
WhiteKiwi marked this conversation as resolved.
Show resolved Hide resolved

onModuleInit() {
this.bootstrapLazyDecorators();
Expand Down Expand Up @@ -47,25 +47,32 @@ 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,
);

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<AopMetadata[]>(
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 });
}
}
}
Expand Down