All files / log-method log-method.decorator.ts

100% Statements 9/9
100% Branches 3/3
100% Functions 3/3
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21              3x 3x 3x 3x 3x 3x 2x   3x   3x      
/**
 * @title The log method decorator.
 * @description This log is mainly useful for development and should not be used in production.
 * @param enable Indicates whether to enable the logger or not.
 * @param propertyKey The class property key.
 */
export function logMethod<TargetClass extends object = Record<string, unknown>>(enable = true) {
  return function (targetClass: TargetClass, propertyKey: string, descriptor: PropertyDescriptor) {
    const originalMethod: (...args: any[]) => unknown = descriptor.value;
    descriptor.value = function (...args: any[]) {
      const className = targetClass.constructor.name;
      const result = originalMethod.apply(this, args);
      if (enable) {
        console.log(`Executing: ${className}.${propertyKey}`, `\nArguments: ${JSON.stringify(args)}`, `\nResult: ${result}`);
      }
      return result;
    };
    return descriptor;
  };
}