leonitousconforti/tinyburg

View on GitHub
packages/doorman/src/decorators/invocation-logs.ts

Summary

Maintainability
A
0 mins
Test Coverage
import type { Debugger } from "debug";

interface IEnterLogOptions {
    withArguments?: boolean;
}

interface IExitLogOptions {
    withReturnValue?: boolean;
}

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export const EnterLog = (debug: Debugger, options: IEnterLogOptions = {}) => {
    const { withArguments } = options;

    return function (__target: unknown, propertyKey: string, descriptor: PropertyDescriptor) {
        const targetMethod = descriptor.value!;

        descriptor.value = function (..._arguments: unknown[]) {
            if (withArguments) debug("%s method invoke with params: %o", propertyKey, _arguments);
            else debug("%s method invoked", propertyKey);
            return targetMethod.apply(this, _arguments);
        };

        return descriptor;
    };
};

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export const ExitLog = (debug: Debugger, options: IExitLogOptions = {}) => {
    const { withReturnValue } = options;

    return function (__target: unknown, propertyKey: string, descriptor: PropertyDescriptor) {
        const targetMethod = descriptor.value;

        descriptor.value = function (..._arguments: unknown[]) {
            const returnValue = targetMethod.apply(this, _arguments);
            if (withReturnValue) debug("%s method completed with return value %o", propertyKey, returnValue);
            else debug("%s method completed", propertyKey);
            return returnValue;
        };

        return descriptor;
    };
};