How to wrap multiple function with additional variables

Solution 1:

Since they are all the same, you can generate them:

const exports = {};

["info", "warn", "error"].forEach(level => {
  // create the function name from level: logInfo, logWarn, logError
  const funcName = "log" + level.charAt(0).toUpperCase() + level.slice(1);
  // create the function itself and store it in the export object
  exports[funcName] = (message, additionalData) => {
    const { roomName, userId, userName } = getContextData();
    // since I don't know whether or not `logger` uses `this`, it is best to
    // be careful and preserve it, because it would be lost if we wrote simply
    // logger[level](message, { roomName, userId, userName, additionalData });
    logger[level].call(logger, message, { roomName, userId, userName, additionalData });
  };
};

module.exports = exports;

Solution 2:

You could have a generic function that can call logger.warn, logger.info, etc, and retrieve the contextData once inside that function.

const getContextData = () => {
  const roomName = asyncLocalStorage.getStore()?.get("roomName") || "";
  const userId = asyncLocalStorage.getStore()?.get("userId") || "";
  const userName = asyncLocalStorage.getStore()?.get("userName") || "";

  return { roomName, userId, userName };
};

const logSomething = (message, additionalData, type) => {
  const { roomName, userId, userName } = getContextData();
  switch (type) {
    case "info":
    case "warn":
    case "error":
      logger[type](message, {roomName, userId, userName, additionalData});
      break;
    default:
      // error invalid "type"
  }
}

const logInfo = (message, additionalData) => logSomething(message, additionalData, "info");
const logWarn = (message, additionalData) => logSomething(message, additionalData, "warn");
const logError = (message, additionalData) => logSomething(message, additionalData, "error");

module.exports = {
  logInfo,
  logWarn,
  logError,
};