UNPKG

bc-node-sdk

Version:

BetterCommerce's NodeJS SDK encapsulates the base framework for all the Next.js applications.

129 lines (128 loc) 5.43 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); // Other Imports const constants_1 = require("../domain/constants"); const cipher_util_1 = __importDefault(require("./cipher-util")); /** * Class {@link AppUtil} encapsulates the utility methods for various application-related tasks. */ class AppUtil { /** * Retrieves the user id from the given SSO user id. * * @param ssoUserId the SSO user id to decrypt * @returns the user id if the decryption is successful, otherwise an empty string */ static getLoggedInUserId(ssoUserId) { const userId = cipher_util_1.default.decrypt(ssoUserId); if (userId) { return userId; } return constants_1.Defaults.String.Value; } /** * Returns all cookies from the given cookie object as a JavaScript object. * @param {ReadonlyRequestCookies} cookies The cookie object to get the cookies from. * @returns {object} A JavaScript object containing all the cookies, where each * property is a cookie name and the corresponding value is the cookie's value. */ static allCookies(cookies) { const allCookies = cookies.getAll(); const objCookies = allCookies === null || allCookies === void 0 ? void 0 : allCookies.reduce((acc, elem) => { acc[elem === null || elem === void 0 ? void 0 : elem.name] = elem === null || elem === void 0 ? void 0 : elem.value; return acc; }, constants_1.Defaults.Object.Value); return objCookies; } /** * Disables React Developer Tools. * * This method is a modified version of the method described in * https://stackoverflow.com/questions/56708167/how-to-disable-react-devtools-in-production-mode * to disable React Developer Tools without causing a console error. * * It replaces all properties of the global hook with a no-op function or a null * value, except for the 'renderers' property which is replaced with a new Map * to prevent a console error when the dev tools try to iterate over it. */ static disableReactDevTools() { if (typeof window !== 'undefined' && window.document) { // Ensure the React Developer Tools global hook exists if (!window.__REACT_DEVTOOLS_GLOBAL_HOOK__) return; const devtoolsHook = window.__REACT_DEVTOOLS_GLOBAL_HOOK__; // Replace all global hook properties with a no-op function or a null value for (const prop in devtoolsHook) { if (prop === 'renderers') { // prevents console error when dev tools try to iterate of renderers devtoolsHook[prop] = new Map(); continue; } devtoolsHook[prop] = typeof devtoolsHook[prop] === 'function' ? Function.prototype : constants_1.Defaults.Null.Value; } } } /** * Counts the number of decimal places of a given number. * @param {number} value The number to count the decimal places of. * @returns {number} The number of decimal places of the given number. */ static countDecimals(value) { if (Math.floor(value.valueOf()) === value.valueOf()) return 0; { return value.toString().split(".")[1].length || 0; } } /** * Sanitizes the given amount by converting it to an integer representation of the lowest currency unit. * * This function handles amounts with up to two decimal places. If the amount has more than two decimals, * it is rounded to two decimal places before conversion. The sanitized amount is returned as an integer * by multiplying the value by 100 to handle the currency in the lowest denomination (e.g., cents for USD). * * @param value - The monetary amount to be sanitized. * @returns The sanitized amount as an integer. */ static sanitizeAmount(value) { let amount = 0; if (value) { const decimals = AppUtil.countDecimals(value); if (decimals > 2) { amount = Number.parseFloat(value.toFixed(2)) * 100; } else { if (decimals == 0) { amount = value * 100; } else { amount = parseInt((value * 100).toFixed(0)); } } } return amount; } /** * Logs information to the console when in development mode. * @param info The information to be logged. * @param tag An optional tag to prepend to the log message. */ static logInfo(info, tag) { if (process.env.NODE_ENV === "development") { console.log(!tag ? "info" : tag, info); } } /** * Logs an error to the console when in development mode. * @param error The error object or message to be logged. */ static logError(error) { if (process.env.NODE_ENV === "development") { console.log("error", error); } } } exports.default = AppUtil;