All files / src/api config.ts

100% Statements 26/26
100% Branches 16/16
100% Functions 4/4
100% Lines 26/26

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                45x   45x 1x       44x 44x     44x 45x   45x     45x           45x 47x 47x     47x 20x       27x 26x   26x 9x   17x     1x           45x 13x 13x           45x 13x 13x    
/**
 * API Configuration
 * Centralized configuration for API endpoints and settings
 */
 
import { getBaseDomain, isRootDomain } from '../utils/domain';
 
// Determine API base URL based on environment
const getApiBaseUrl = (): string => {
  // In production, this would be set via environment variable
  if (import.meta.env.VITE_API_URL) {
    return import.meta.env.VITE_API_URL;
  }
 
  // Development: build API URL dynamically based on current domain
  const baseDomain = getBaseDomain();
  const protocol = window.location.protocol;
 
  // For localhost or lvh.me, use port 8000
  const isDev = baseDomain === 'localhost' || baseDomain === 'lvh.me';
  const port = isDev ? ':8000' : '';
 
  return `${protocol}//api.${baseDomain}${port}`;
};
 
export const API_BASE_URL = getApiBaseUrl();
 
/**
 * Extract subdomain from current hostname
 * Returns null if on root domain or invalid subdomain
 */
export const getSubdomain = (): string | null => {
  const hostname = window.location.hostname;
  const parts = hostname.split('.');
 
  // Root domain (no subdomain) - no business context
  if (isRootDomain()) {
    return null;
  }
 
  // Has subdomain
  if (parts.length > 1) {
    const subdomain = parts[0];
    // Exclude special subdomains
    if (['www', 'api', 'platform'].includes(subdomain)) {
      return subdomain === 'platform' ? null : subdomain;
    }
    return subdomain;
  }
 
  return null;
};
 
/**
 * Check if current page is platform site
 */
export const isPlatformSite = (): boolean => {
  const hostname = window.location.hostname;
  return hostname.startsWith('platform.');
};
 
/**
 * Check if current page is business site
 */
export const isBusinessSite = (): boolean => {
  const subdomain = getSubdomain();
  return subdomain !== null && subdomain !== 'platform';
};