UNPKG

@dbs-portal/core-api

Version:

HTTP client and API utilities for DBS Portal

138 lines (135 loc) 4.2 kB
/** * MSW browser setup for development and Storybook */ import { setupWorker } from 'msw/browser'; /** * Setup MSW for browser environment */ export async function setupBrowser(handlers, config) { try { // Create worker with handlers const worker = setupWorker(...handlers); // Configure worker based on config if (config.logging) { console.log('MSW: Setting up browser worker with', handlers.length, 'handlers'); } return worker; } catch (error) { console.error('MSW: Failed to setup browser worker:', error); throw error; } } /** * Initialize MSW service worker for browser * This should be called once during application startup */ export async function initializeBrowserMocks(handlers = [], options = {}) { try { const worker = setupWorker(...handlers); await worker.start({ serviceWorker: { url: options.serviceWorkerUrl || '/mockServiceWorker.js', }, quiet: options.quiet ?? false, onUnhandledRequest: options.onUnhandledRequest ?? 'warn', }); if (!options.quiet) { console.log('MSW: Browser mocking enabled'); } } catch (error) { console.error('MSW: Failed to initialize browser mocks:', error); throw error; } } /** * Check if service worker is available */ export function isServiceWorkerSupported() { return typeof window !== 'undefined' && 'serviceWorker' in navigator; } /** * Get service worker registration status */ export async function getServiceWorkerStatus() { const supported = isServiceWorkerSupported(); if (!supported) { return { supported: false, registered: false, active: false }; } try { const registration = await navigator.serviceWorker.getRegistration(); const registered = !!registration; const active = !!(registration?.active); return { supported, registered, active }; } catch { return { supported, registered: false, active: false }; } } /** * Install MSW service worker * This generates the service worker file in the public directory */ export async function installServiceWorker(publicDir = 'public') { // This would typically be done via CLI: npx msw init <publicDir> // For now, we'll provide instructions console.log(` MSW Service Worker Installation Required: Run the following command to install the MSW service worker: npx msw init ${publicDir} --save This will create the mockServiceWorker.js file in your ${publicDir} directory. `); } /** * Browser-specific mock utilities */ export const browserMockUtils = { /** * Check if running in development mode */ isDevelopment() { return typeof window !== 'undefined' && (window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1'); }, /** * Check if running in Storybook */ isStorybook() { return typeof window !== 'undefined' && (window.location.pathname.includes('storybook') || window.__STORYBOOK_ADDONS_MANAGER__); }, /** * Get current page URL info */ getPageInfo() { if (typeof window === 'undefined') { return null; } return { hostname: window.location.hostname, pathname: window.location.pathname, search: window.location.search, hash: window.location.hash, isDevelopment: this.isDevelopment(), isStorybook: this.isStorybook(), }; }, /** * Show mock status in console */ logMockStatus(enabled, handlersCount) { if (typeof console === 'undefined') return; const style = 'background: #1976d2; color: white; padding: 2px 6px; border-radius: 3px;'; if (enabled) { console.log(`%cMSW%c Browser mocking enabled with ${handlersCount} handlers`, style, ''); } else { console.log(`%cMSW%c Browser mocking disabled`, style, ''); } }, }; //# sourceMappingURL=browser.js.map