/** * ServiceWorker Mock System - Main Entry Point */ import { SwMock } from './core/SwMock'; import { isServiceWorkerSupported, isSecureContext, exportCapturedRequests } from './utils/helpers'; import { DEFAULT_CONFIG, PRESET_CONFIGS } from './core/config'; // Export types export type { SwMockConfig, SwMockInitOptions, SwMockStatus, SwMockInstance, SwMockPlugin, CapturedRequest, MockResponse, NetworkFailureConfig, HttpErrorScenario, TimeoutScenario, ConnectionFailureScenario, ResponseDelayScenario, IntermittentFailureScenario, MockEndpointConfig, NetworkFailureInfo, SwMockEvent, SwMockEventType } from './types'; // Export core classes export { SwMock }; // Export utilities export { isServiceWorkerSupported, isSecureContext, exportCapturedRequests, DEFAULT_CONFIG, PRESET_CONFIGS }; /** * Create a new SwMock instance */ export function createSwMock(): SwMock { return new SwMock(); } /** * Global instance for convenience */ let globalInstance: SwMock | null = null; /** * Get or create global SwMock instance */ export function getSwMock(): SwMock { if (!globalInstance) { globalInstance = new SwMock(); } return globalInstance; } /** * Initialize SwMock with options (convenience function) */ export async function init(options?: import('./types').SwMockInitOptions): Promise { const instance = getSwMock(); await instance.init(options); return instance; } /** * Quick setup functions */ export const SwMockUtils = { /** * Quick setup for development environment */ async setupDevelopment(options: import('./types').SwMockInitOptions = {}) { const instance = await init({ enabled: true, debug: true, ...options }); await instance.enableDevelopmentMode(); console.log('[SwMock] Development mode enabled'); return instance; }, /** * Quick setup for testing environment */ async setupTesting(options: import('./types').SwMockInitOptions = {}) { const instance = await init({ enabled: true, debug: false, ...options }); await instance.enableTestingMode(); console.log('[SwMock] Testing mode enabled'); return instance; }, /** * Quick setup for stress testing */ async setupStressTesting(options: import('./types').SwMockInitOptions = {}) { const instance = await init({ enabled: true, debug: true, ...options }); await instance.enableStressTestMode(); console.log('[SwMock] Stress testing mode enabled'); return instance; }, /** * Disable all mock functionality */ async disable() { const instance = getSwMock(); await instance.disable(); await instance.disableNetworkFailure(); console.log('[SwMock] All functionality disabled'); }, /** * Check system compatibility */ checkCompatibility(): { compatible: boolean; issues: string[] } { const issues: string[] = []; if (!isServiceWorkerSupported()) { issues.push('ServiceWorker is not supported in this browser'); } if (!isSecureContext()) { issues.push('SwMock requires HTTPS or localhost environment'); } if (typeof indexedDB === 'undefined') { issues.push('IndexedDB is not supported'); } return { compatible: issues.length === 0, issues }; }, /** * Get system information */ getSystemInfo() { return { serviceWorkerSupported: isServiceWorkerSupported(), secureContext: isSecureContext(), indexedDBSupported: typeof indexedDB !== 'undefined', userAgent: navigator.userAgent, url: location.href, timestamp: new Date().toISOString() }; } }; /** * Plugin system */ export const plugins = { /** * Console logger plugin */ consoleLogger: { name: 'console-logger', install(swMock: SwMock) { swMock.on('init', () => console.log('[SwMock Plugin] Initialized')); swMock.on('enable', () => console.log('[SwMock Plugin] Enabled')); swMock.on('disable', () => console.log('[SwMock Plugin] Disabled')); swMock.on('config-update', (config: any) => console.log('[SwMock Plugin] Config updated', config)); swMock.on('error', (error: any) => console.error('[SwMock Plugin] Error:', error)); } }, /** * Performance monitor plugin */ performanceMonitor: { name: 'performance-monitor', install(swMock: SwMock) { const startTimes = new Map(); swMock.on('request-intercepted', (data: any) => { startTimes.set(data.url, performance.now()); }); swMock.on('mock-response-sent', (data: any) => { const startTime = startTimes.get(data.url); if (startTime) { const duration = performance.now() - startTime; console.log(`[SwMock Performance] ${data.url}: ${duration.toFixed(2)}ms`); startTimes.delete(data.url); } }); } } }; /** * Auto-setup for browser environments */ if (typeof window !== 'undefined') { // Attach to window for global access (window as any).SwMock = SwMock; (window as any).swMock = getSwMock; (window as any).swMockUtils = SwMockUtils; // Auto-initialize if environment variables are set if (typeof process !== 'undefined' && process.env) { const autoInit = process.env.SW_MOCK_AUTO_INIT === 'true'; const enabled = process.env.SW_MOCK_ENABLED === 'true'; const debug = process.env.SW_MOCK_DEBUG === 'true'; if (autoInit) { init({ enabled, debug }).catch(error => { console.error('[SwMock] Auto-initialization failed:', error); }); } } } // Default export export default SwMock;