All files / src/utils domain.ts

100% Statements 43/43
100% Branches 32/32
100% Functions 8/8
100% Lines 43/43

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138                        3x 79x     79x 16x       63x     63x 21x       42x                     3x 41x     41x 7x     34x     34x 7x       27x           3x 27x 27x           3x 11x 11x           3x 14x 14x                   3x 18x 18x 18x   18x 14x   4x                     3x 35x     35x 6x       29x                 3x 14x 14x     14x 14x   14x    
/**
 * Domain Utility Functions
 * Provides dynamic domain detection for both development (lvh.me) and production environments
 */
 
/**
 * Get the base domain from the current hostname
 * Examples:
 *   - platform.lvh.me:5173 → lvh.me
 *   - demo.smoothschedule.com → smoothschedule.com
 *   - localhost → localhost
 */
export const getBaseDomain = (): string => {
  const hostname = window.location.hostname;
 
  // Handle localhost
  if (hostname === 'localhost' || hostname === '127.0.0.1') {
    return 'localhost';
  }
 
  // Extract base domain from hostname
  const parts = hostname.split('.');
 
  // If only 2 parts, it's already the base domain
  if (parts.length === 2) {
    return hostname;
  }
 
  // Otherwise, take the last 2 parts (e.g., smoothschedule.com from platform.smoothschedule.com)
  return parts.slice(-2).join('.');
};
 
/**
 * Get the current subdomain (if any)
 * Examples:
 *   - platform.lvh.me → platform
 *   - demo.smoothschedule.com → demo
 *   - smoothschedule.com → null
 *   - localhost → null
 */
export const getCurrentSubdomain = (): string | null => {
  const hostname = window.location.hostname;
 
  // No subdomain for localhost
  if (hostname === 'localhost' || hostname === '127.0.0.1') {
    return null;
  }
 
  const parts = hostname.split('.');
 
  // If only 2 parts, no subdomain
  if (parts.length === 2) {
    return null;
  }
 
  // Return the first part as subdomain
  return parts[0];
};
 
/**
 * Check if we're on the root domain (no subdomain)
 */
export const isRootDomain = (): boolean => {
  const hostname = window.location.hostname;
  return hostname === 'localhost' || hostname === '127.0.0.1' || hostname.split('.').length === 2;
};
 
/**
 * Check if we're on the platform subdomain
 */
export const isPlatformDomain = (): boolean => {
  const subdomain = getCurrentSubdomain();
  return subdomain === 'platform';
};
 
/**
 * Check if we're on a business subdomain (not root, not platform, not api)
 */
export const isBusinessSubdomain = (): boolean => {
  const subdomain = getCurrentSubdomain();
  return subdomain !== null && subdomain !== 'platform' && subdomain !== 'api';
};
 
/**
 * Build a full URL with the given subdomain
 * Examples:
 *   - buildSubdomainUrl('platform') → http://platform.lvh.me:5173 (in dev)
 *   - buildSubdomainUrl('demo') → https://demo.smoothschedule.com (in prod)
 *   - buildSubdomainUrl(null) → https://smoothschedule.com (root domain)
 */
export const buildSubdomainUrl = (subdomain: string | null, path: string = '/'): string => {
  const baseDomain = getBaseDomain();
  const protocol = window.location.protocol;
  const port = window.location.port ? `:${window.location.port}` : '';
 
  if (subdomain) {
    return `${protocol}//${subdomain}.${baseDomain}${port}${path}`;
  } else {
    return `${protocol}//${baseDomain}${port}${path}`;
  }
};
 
/**
 * Get the cookie domain attribute (with leading dot for cross-subdomain access)
 * Examples:
 *   - .lvh.me (in dev)
 *   - .smoothschedule.com (in prod)
 *   - localhost (for localhost)
 */
export const getCookieDomain = (): string => {
  const baseDomain = getBaseDomain();
 
  // Don't use dot prefix for localhost
  if (baseDomain === 'localhost') {
    return 'localhost';
  }
 
  // Use dot prefix for cross-subdomain access
  return `.${baseDomain}`;
};
 
/**
 * Get the WebSocket URL for the current environment
 * Examples:
 *   - ws://lvh.me:8000/ws/ (in dev)
 *   - wss://smoothschedule.com/ws/ (in prod)
 */
export const getWebSocketUrl = (path: string = ''): string => {
  const baseDomain = getBaseDomain();
  const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
 
  // In development, WebSocket server runs on port 8000
  const isDev = baseDomain === 'lvh.me' || baseDomain === 'localhost';
  const port = isDev ? ':8000' : '';
 
  return `${protocol}//${baseDomain}${port}/ws/${path}`;
};