UNPKG

@dbs-portal/core-api

Version:

HTTP client and API utilities for DBS Portal

145 lines 4.62 kB
/** * Environment detection and configuration for MSW */ /** * Detect current environment */ export function detectEnvironment() { // Node.js environment if (typeof process !== 'undefined' && process.versions?.node) { return 'node'; } // Browser environment if (typeof window !== 'undefined' && typeof document !== 'undefined') { return 'browser'; } return 'unknown'; } /** * Determine if mocking should be enabled */ export function shouldEnableMocking() { const environment = detectEnvironment(); // Always enable in test environment if (typeof process !== 'undefined' && process.env?.['NODE_ENV'] === 'test') { return true; } // Check explicit environment variable if (typeof process !== 'undefined' && process.env?.['VITE_ENABLE_MOCKING'] === 'true') { return true; } // Enable in development for browser if (environment === 'browser') { // Check for development indicators if (typeof window !== 'undefined') { const hostname = window.location?.hostname; const isDevelopment = hostname === 'localhost' || hostname === '127.0.0.1' || hostname?.endsWith('.local'); // Check for Storybook const isStorybook = window.location?.pathname?.includes('storybook') || window.__STORYBOOK_ADDONS_MANAGER__; return isDevelopment || isStorybook; } } return false; } /** * Determine mock mode based on environment */ export function determineMockMode() { const environment = detectEnvironment(); // Test environment if (typeof process !== 'undefined' && process.env?.['NODE_ENV'] === 'test') { return 'testing'; } // Explicit mode from environment variable if (typeof process !== 'undefined' && process.env?.['VITE_MOCK_MODE']) { const mode = process.env['VITE_MOCK_MODE']; if (['development', 'testing', 'storybook', 'disabled'].includes(mode)) { return mode; } } // Browser-specific detection if (environment === 'browser' && typeof window !== 'undefined') { // Storybook if (window.location?.pathname?.includes('storybook') || window.__STORYBOOK_ADDONS_MANAGER__) { return 'storybook'; } // Development const hostname = window.location?.hostname; if (hostname === 'localhost' || hostname === '127.0.0.1' || hostname?.endsWith('.local')) { return 'development'; } } // Node.js development if (environment === 'node' && typeof process !== 'undefined' && process.env?.['NODE_ENV'] === 'development') { return 'development'; } return 'disabled'; } /** * Get complete environment information */ export function getEnvironmentInfo() { const environment = detectEnvironment(); const shouldMock = shouldEnableMocking(); const mode = shouldMock ? determineMockMode() : 'disabled'; return { environment, shouldMock, mode, }; } /** * Check if mocking is enabled */ export function isMockingEnabled() { return shouldEnableMocking(); } /** * Get current mocking mode */ export function getMockingMode() { return determineMockMode(); } /** * Check if running in specific environment */ export function isEnvironment(env) { switch (env) { case 'browser': return detectEnvironment() === 'browser'; case 'node': return detectEnvironment() === 'node'; case 'test': return typeof process !== 'undefined' && process.env?.['NODE_ENV'] === 'test'; case 'development': return typeof process !== 'undefined' && process.env?.['NODE_ENV'] === 'development'; case 'production': return typeof process !== 'undefined' && process.env?.['NODE_ENV'] === 'production'; default: return false; } } /** * Get environment-specific configuration */ export function getEnvironmentConfig() { const info = getEnvironmentInfo(); return { ...info, // Environment-specific defaults logging: info.mode === 'development' || info.mode === 'testing', delay: info.mode === 'testing' ? 0 : [100, 300], errorSimulation: { networkErrorRate: info.mode === 'testing' ? 0 : 0.01, serverErrorRate: info.mode === 'testing' ? 0 : 0.005, timeoutErrorRate: info.mode === 'testing' ? 0 : 0.001, }, }; } //# sourceMappingURL=environment.js.map