Skip to content

Commit fb800b1

Browse files
Merge pull request #161 from mvanhorn/refactor/metadataTarget-consistent-79
refactor(middleware): use metadataTarget consistently in class decorator branches
2 parents 69202bb + 0d6b06d commit fb800b1

3 files changed

Lines changed: 20 additions & 10 deletions

File tree

src/websocket/middleware/filters/use-filters.decorator.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ export function UseFilters(
3838
propertyKey?: string | symbol,
3939
descriptor?: PropertyDescriptor
4040
): void | PropertyDescriptor => {
41+
// For static methods, target is the constructor itself; for instance methods, it's the prototype
42+
const metadataTarget = typeof target === 'function' ? target : (target as object).constructor;
43+
4144
if (propertyKey) {
4245
// Method decorator - merge with existing filters and deduplicate
43-
// For static methods, target is the constructor itself; for instance methods, it's the prototype
44-
const metadataTarget = typeof target === 'function' ? target : (target as object).constructor;
4546
const existingFilters: (Type<ExceptionFilter> | ExceptionFilter)[] =
4647
Reflect.getMetadata(EXCEPTION_FILTERS_METADATA, metadataTarget, propertyKey) || [];
4748

@@ -55,12 +56,12 @@ export function UseFilters(
5556
} else {
5657
// Class decorator - merge with existing filters and deduplicate
5758
const existingFilters: (Type<ExceptionFilter> | ExceptionFilter)[] =
58-
Reflect.getMetadata(EXCEPTION_FILTERS_METADATA, target) || [];
59+
Reflect.getMetadata(EXCEPTION_FILTERS_METADATA, metadataTarget) || [];
5960

6061
Reflect.defineMetadata(
6162
EXCEPTION_FILTERS_METADATA,
6263
[...new Set([...existingFilters, ...filters])],
63-
target
64+
metadataTarget
6465
);
6566
return;
6667
}

src/websocket/middleware/guards/use-guards.decorator.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ export function UseGuards(
3838
propertyKey?: string | symbol,
3939
descriptor?: PropertyDescriptor
4040
): void | PropertyDescriptor => {
41+
// For static methods, target is the constructor itself; for instance methods, it's the prototype
42+
const metadataTarget = typeof target === 'function' ? target : (target as object).constructor;
43+
4144
if (propertyKey) {
4245
// Method decorator - merge with existing guards and deduplicate
43-
// For static methods, target is the constructor itself; for instance methods, it's the prototype
44-
const metadataTarget = typeof target === 'function' ? target : (target as object).constructor;
4546
const existingGuards: (Type<CanActivate> | CanActivate)[] =
4647
Reflect.getMetadata(GUARDS_METADATA, metadataTarget, propertyKey) || [];
4748

@@ -55,9 +56,13 @@ export function UseGuards(
5556
} else {
5657
// Class decorator - merge with existing guards and deduplicate
5758
const existingGuards: (Type<CanActivate> | CanActivate)[] =
58-
Reflect.getMetadata(GUARDS_METADATA, target) || [];
59+
Reflect.getMetadata(GUARDS_METADATA, metadataTarget) || [];
5960

60-
Reflect.defineMetadata(GUARDS_METADATA, [...new Set([...existingGuards, ...guards])], target);
61+
Reflect.defineMetadata(
62+
GUARDS_METADATA,
63+
[...new Set([...existingGuards, ...guards])],
64+
metadataTarget
65+
);
6166
return;
6267
}
6368
};

src/websocket/middleware/pipes/use-pipes.decorator.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,13 @@ export function UsePipes(
7878
} else {
7979
// Class decorator - merge with existing pipes and deduplicate
8080
const existingPipes: (Type<PipeTransform> | PipeTransform)[] =
81-
Reflect.getMetadata(PIPES_METADATA, target) || [];
81+
Reflect.getMetadata(PIPES_METADATA, metadataTarget) || [];
8282

83-
Reflect.defineMetadata(PIPES_METADATA, [...new Set([...existingPipes, ...pipes])], target);
83+
Reflect.defineMetadata(
84+
PIPES_METADATA,
85+
[...new Set([...existingPipes, ...pipes])],
86+
metadataTarget
87+
);
8488
}
8589
};
8690

0 commit comments

Comments
 (0)