UNPKG

@jakobcooldown/react-csr-sdk

Version:

Mockery SDK for dynamic bundle loading in web applications

162 lines (159 loc) 5.14 kB
const STORAGE_KEY = 'mockery_bundle_url'; const DEBUG_KEY = 'mockery_debug'; // Check if Mockery is disabled via environment variables const isMockeryDisabled = () => { var _a, _b, _c, _d, _e; try { // Check for Vite environment variable if (typeof import.meta !== 'undefined' && ((_a = import.meta.env) === null || _a === void 0 ? void 0 : _a.VITE_MOCKERY_DISABLED)) { return import.meta.env.VITE_MOCKERY_DISABLED === 'true'; } // Check for Create React App environment variable if (typeof window !== 'undefined' && ((_c = (_b = window.process) === null || _b === void 0 ? void 0 : _b.env) === null || _c === void 0 ? void 0 : _c.REACT_APP_MOCKERY_DISABLED)) { return window.process.env.REACT_APP_MOCKERY_DISABLED === 'true'; } // Check for general environment variable (Node.js) if (typeof globalThis !== 'undefined' && ((_e = (_d = globalThis.process) === null || _d === void 0 ? void 0 : _d.env) === null || _e === void 0 ? void 0 : _e.MOCKERY_DISABLED)) { return globalThis.process.env.MOCKERY_DISABLED === 'true'; } return false; } catch (error) { // If any environment check fails, default to enabled return false; } }; // Log disabled state on module load if (isMockeryDisabled()) { console.log('[Mockery] SDK disabled via environment variable'); } /** * Set a custom bundle URL to be loaded instead of the default bundle. * Call this when a user logs in or switches tenants. * * @param bundleUrl - The URL of the custom bundle, or undefined to clear * @param reload - Whether to reload the page immediately (default: true) */ function setCustomBundle(bundleUrl, reload = true) { if (isMockeryDisabled()) { console.log('[Mockery] setCustomBundle disabled'); return; } try { bundleUrl = bundleUrl || undefined; const currentBundle = getCustomBundle(); // Exit early if bundle URL is unchanged if (bundleUrl === currentBundle) { return; } if (bundleUrl) { localStorage.setItem(STORAGE_KEY, bundleUrl); console.log('[Mockery] Custom bundle set:', bundleUrl); } else { localStorage.removeItem(STORAGE_KEY); console.log('[Mockery] Custom bundle cleared'); } if (reload) { window.location.reload(); } } catch (error) { console.error('[Mockery] Failed to set custom bundle:', error); } } /** * Get the currently set custom bundle URL */ function getCustomBundle() { if (isMockeryDisabled()) { return undefined; } try { return localStorage.getItem(STORAGE_KEY) || undefined; } catch (error) { console.warn('[Mockery] Failed to get custom bundle:', error); return undefined; } } /** * Clear the custom bundle URL and optionally reload */ function clearCustomBundle(reload = true) { if (isMockeryDisabled()) { console.log('[Mockery] clearCustomBundle disabled'); return; } setCustomBundle(undefined, reload); } /** * Set a custom bundle URL and reload the page */ function reloadWithBundle(bundleUrl) { if (isMockeryDisabled()) { console.log('[Mockery] reloadWithBundle disabled'); return; } setCustomBundle(bundleUrl, true); } /** * Enable or disable debug logging */ function setDebugMode(enabled) { if (isMockeryDisabled()) { console.log('[Mockery] setDebugMode disabled'); return; } try { if (enabled) { localStorage.setItem(DEBUG_KEY, 'true'); console.log('[Mockery] Debug mode enabled'); } else { localStorage.removeItem(DEBUG_KEY); console.log('[Mockery] Debug mode disabled'); } } catch (error) { console.warn('[Mockery] Failed to set debug mode:', error); } } const BUNDLE_URL_KEY = 'mockery_bundle_url'; /** * Storage utilities for persisting bundle URLs */ const storage = { setBundleUrl(bundleUrl, storageKey = BUNDLE_URL_KEY) { try { if (bundleUrl === undefined) { localStorage.removeItem(storageKey); } else { localStorage.setItem(storageKey, bundleUrl); } } catch (error) { console.warn('Failed to store bundle URL:', error); } }, getBundleUrl(storageKey = BUNDLE_URL_KEY) { try { return localStorage.getItem(storageKey) || undefined; } catch (error) { console.warn('Failed to retrieve bundle URL:', error); return undefined; } }, clear(storageKey = BUNDLE_URL_KEY) { try { localStorage.removeItem(storageKey); } catch (error) { console.warn('Failed to clear bundle storage:', error); } } }; export { clearCustomBundle, getCustomBundle, reloadWithBundle, setCustomBundle, setDebugMode, storage }; //# sourceMappingURL=index.esm.js.map