Skip to content

Instantly share code, notes, and snippets.

@nitedani
Last active November 14, 2023 12:33
Show Gist options
  • Save nitedani/2176db9b318318f03ddf9943c20556c9 to your computer and use it in GitHub Desktop.
Save nitedani/2176db9b318318f03ddf9943c20556c9 to your computer and use it in GitHub Desktop.
aroundMethodDecarator
/* eslint-disable @typescript-eslint/no-explicit-any */
export const aroundMethodDecarator = (
decoratorFn: (
args: any[],
name: string | symbol,
next: (..._args: any[]) => any
) => any
): MethodDecorator => {
return (_target: any, _key: string | symbol, descriptor: PropertyDescriptor) => {
const originalMethod = descriptor.value;
const keys = Reflect.getOwnMetadataKeys(descriptor.value);
const metadata = keys.map((key) => ({
key,
value: Reflect.getOwnMetadata(key, descriptor.value),
}));
descriptor.value = function (...args: any[]) {
return decoratorFn(args, _key, originalMethod.bind(this));
};
metadata.forEach(({ key, value }) =>
Reflect.defineMetadata(key, value, descriptor.value)
);
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment