UNPKG

@dbs-portal/core-api

Version:

HTTP client and API utilities for DBS Portal

121 lines 3.27 kB
/** * MSW configuration management */ const { VITE_NODE_ENV: NODE_ENV, VITE_MOCK_DELAY: MOCK_DELAY, VITE_MOCK_LOGGING: MOCK_LOGGING, VITE_ENABLE_MOCKING: ENABLE_MOCKING, VITE_MOCK_API_BASE_URL: MOCK_API_BASE_URL, } = import.meta.env; /** * Default mock configuration */ const defaultConfig = { enabled: false, mode: 'disabled', delay: [100, 300], logging: true, handlers: [], errorSimulation: { networkErrorRate: 0, serverErrorRate: 0, timeoutErrorRate: 0, customErrors: [], }, }; /** * Current mock configuration */ let currentConfig = { ...defaultConfig }; /** * Get current mock configuration */ export function getMockConfig() { return { ...currentConfig }; } /** * Update mock configuration */ export function updateMockConfig(updates) { currentConfig = { ...currentConfig, ...updates, errorSimulation: { ...currentConfig.errorSimulation, ...updates.errorSimulation, }, }; } /** * Reset mock configuration to defaults */ export function resetMockConfig() { currentConfig = { ...defaultConfig }; } /** * Create mock configuration from environment */ export function createConfigFromEnvironment() { const config = {}; // Check environment variables if (typeof process !== 'undefined' && process.env) { // Enable mocking based on environment if (ENABLE_MOCKING === 'true' || NODE_ENV === 'test') { config.enabled = true; } // Set mode based on environment if (NODE_ENV === 'test') { config.mode = 'testing'; } else if (NODE_ENV === 'development') { config.mode = 'development'; } // Set base URL if (MOCK_API_BASE_URL) { config.baseUrl = MOCK_API_BASE_URL; } // Set delay if (MOCK_DELAY) { const delay = Number.parseInt(MOCK_DELAY, 10); if (!Number.isNaN(delay)) { config.delay = delay; } } // Set logging if (MOCK_LOGGING === 'false') { config.logging = false; } } // Check for browser globals (Storybook, etc.) if (typeof window !== 'undefined') { // Check for Storybook if (window.location?.pathname?.includes('storybook') || window.__STORYBOOK_ADDONS_MANAGER__) { config.enabled = true; config.mode = 'storybook'; } // Check for development mode indicators if (window.location?.hostname === 'localhost' || window.location?.hostname === '127.0.0.1') { if (!config.mode || config.mode === 'disabled') { config.mode = 'development'; } } } return { ...defaultConfig, ...config, }; } /** * Initialize mock configuration */ export function initializeMockConfig(overrides) { const envConfig = createConfigFromEnvironment(); const finalConfig = { ...envConfig, ...overrides, }; updateMockConfig(finalConfig); return getMockConfig(); } /** * Export current config as default */ export const mockConfig = currentConfig; //# sourceMappingURL=index.js.map