UNPKG

@dbs-portal/core-api

Version:

HTTP client and API utilities for DBS Portal

195 lines 5.11 kB
/** * MSW setup and initialization */ import { initializeMockConfig, getMockConfig } from './config'; import { getEnvironmentInfo } from './config/environment'; import { setupBrowser } from './browser'; import { setupServer } from './server'; import { defaultHandlers } from './handlers/default-handlers'; /** * Global MSW instance references */ let browserWorker = null; let nodeServer = null; let isSetup = false; /** * Setup MSW based on environment */ export async function setupMocks(options = {}) { // Prevent double setup if (isSetup) { console.warn('MSW is already set up'); return; } // Initialize configuration const config = initializeMockConfig(options.config); // Check if mocking should be enabled if (!config.enabled) { if (config.logging) { console.log('MSW: Mocking is disabled'); } return; } const envInfo = getEnvironmentInfo(); if (config.logging) { console.log(`MSW: Setting up in ${envInfo.environment} environment (${config.mode} mode)`); } // Collect all handlers const allHandlers = [ ...defaultHandlers, ...(config.handlers || []), ...(options.handlers || []), ]; try { // Setup based on environment if (envInfo.environment === 'browser') { browserWorker = await setupBrowser(allHandlers, config); if (options.start !== false) { await browserWorker.start({ onUnhandledRequest: config.mode === 'development' ? 'warn' : 'bypass', quiet: !config.logging, }); } } else if (envInfo.environment === 'node') { nodeServer = setupServer(allHandlers, config); if (options.start !== false) { nodeServer.listen({ onUnhandledRequest: config.mode === 'testing' ? 'error' : 'warn', }); } } isSetup = true; if (config.logging) { console.log(`MSW: Successfully set up with ${allHandlers.length} handlers`); } } catch (error) { console.error('MSW: Failed to setup:', error); throw error; } } /** * Teardown MSW */ export async function teardownMocks() { if (!isSetup) { return; } const config = getMockConfig(); try { if (browserWorker) { browserWorker.stop(); browserWorker = null; } if (nodeServer) { nodeServer.close(); nodeServer = null; } isSetup = false; if (config.logging) { console.log('MSW: Successfully torn down'); } } catch (error) { console.error('MSW: Failed to teardown:', error); throw error; } } /** * Restart MSW with new configuration */ export async function restartMocks(options = {}) { await teardownMocks(); await setupMocks(options); } /** * Add handlers to existing MSW instance */ export function addHandlers(...handlers) { if (!isSetup) { console.warn('MSW: Cannot add handlers - MSW is not set up'); return; } if (browserWorker) { browserWorker.use(...handlers); } if (nodeServer) { nodeServer.use(...handlers); } const config = getMockConfig(); if (config.logging) { console.log(`MSW: Added ${handlers.length} handlers`); } } /** * Reset handlers to original state */ export function resetHandlers() { if (!isSetup) { console.warn('MSW: Cannot reset handlers - MSW is not set up'); return; } if (browserWorker) { browserWorker.resetHandlers(); } if (nodeServer) { nodeServer.resetHandlers(); } const config = getMockConfig(); if (config.logging) { console.log('MSW: Reset handlers to original state'); } } /** * Restore original handlers */ export function restoreHandlers() { if (!isSetup) { console.warn('MSW: Cannot restore handlers - MSW is not set up'); return; } if (browserWorker) { browserWorker.restoreHandlers(); } if (nodeServer) { nodeServer.restoreHandlers(); } const config = getMockConfig(); if (config.logging) { console.log('MSW: Restored original handlers'); } } /** * Check if MSW is set up */ export function isMswSetup() { return isSetup; } /** * Get current MSW instance */ export function getMswInstance() { const envInfo = getEnvironmentInfo(); if (envInfo.environment === 'browser') { return browserWorker; } else if (envInfo.environment === 'node') { return nodeServer; } return null; } /** * Auto-setup MSW based on environment (convenience function) */ export async function autoSetupMocks(customHandlers = []) { const envInfo = getEnvironmentInfo(); if (!envInfo.shouldMock) { return; } await setupMocks({ handlers: customHandlers, start: true, }); } //# sourceMappingURL=setup.js.map