UNPKG

kwikid-components-react

Version:

KwikID's Component Library in React

108 lines (101 loc) 4.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.logMethodReact = void 0; /** * Logs information about a synchronous method call. * * This function is used internally by the `logMethod` decorator to log information * about synchronous method calls, including the class name, method name, arguments, * and the return value. * * @param {string} methodName - The name of the method being called. * @param {any[]} args - The arguments passed to the method. * @param {any} result - The return value of the method. */ function logSyncMethod(methodName, args, result) { console.log("%cMethod Type: Sync -", "color: #0d47a1; font-weight: bold;", "\nMethod:", methodName, "\nArgs:", args, "\nReturn:", result); } /** * Logs an error that occurred during the execution of a synchronous method. * * @param {string} methodName - The name of the method. * @param {any[]} args - The arguments passed to the method. * @param {Error | unknown} error - The error that occurred. */ function logSyncMethodError(methodName, args, error) { console.log("%cMethod Type: Sync -", "color: #0d47a1; font-weight: bold;", "\nMethod:", methodName, "\nArgs:", args, "\nError:", error); } /** * Logs information about an asynchronous method call. * * This function is used internally by the `logMethod` decorator to log information * about asynchronous method calls, including the class name, method name, arguments, * and the return value. * * @param {string} methodName - The name of the method being called. * @param {any[]} args - The arguments passed to the method. * @param {any} result - The return value of the method. */ function logAsyncMethod(methodName, args, result) { console.log("%cMethod Type: Async -", "color: #1565c0; font-weight: bold;", "\nMethod:", methodName, "\nArgs:", args, "\nReturn:", result); } /** * Logs information about an asynchronous method call that resulted in an error. * * This function is used internally by the `logMethod` decorator to log information * about asynchronous method calls that resulted in an error. It logs the class name, * method name, arguments, and the error that occurred. * * @param {string} methodName - The name of the method that resulted in an error. * @param {any[]} args - The arguments passed to the method. * @param {Error | unknown} error - The error that occurred during the method execution. */ function logAsyncMethodError(methodName, args, error) { console.log("%cMethod Type: Async -", "color: #1565c0; font-weight: bold;", "\nMethod:", methodName, "\nArgs:", args, "\nError:", error); } const logMethodReact = customName => method => { return function () { for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } console.group("React Logger"); // start a new logging group console.time("Execution Time:"); // start a timer const methodName = customName || method.name || "Anonymous"; // get custom name or method name or use "Anonymous" let result; // declare result variable try { // execute the original method and save the result result = method(...args); } catch (error) { // if an error is thrown, log the error and throw it again logSyncMethodError(methodName, args, error); console.timeEnd("Execution Time:"); // stop the timer console.groupEnd(); // end the logging group throw error; } // if the result is a Promise, handle it as an async method if (result instanceof Promise) { result = result.then(resolvedValue => { // on success, log the resolved value and return it logAsyncMethod(methodName, args, resolvedValue); console.timeEnd("Execution Time:"); // stop the timer console.groupEnd(); // end the logging group return resolvedValue; }).catch(error => { // on error, log the error and throw it again logAsyncMethodError(methodName, args, error); console.timeEnd("Execution Time:"); // stop the timer console.groupEnd(); // end the logging group throw error; }); } else { // if the result is not a Promise, handle it as a sync method logSyncMethod(methodName, args, result); console.timeEnd("Execution Time:"); console.groupEnd(); } return result; }; }; exports.logMethodReact = logMethodReact;