All files / src/config api.js

0% Statements 0/65
0% Branches 0/1
0% Functions 0/1
0% Lines 0/65

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                                                                                                                                                     
// API Configuration
// Centralized configuration for API endpoints
 
const getApiUrl = () => {
  // ALWAYS use runtime detection in browser (ignore build-time env vars)
  // This ensures browser uses the correct URL regardless of how it was built
  if (typeof window !== 'undefined') {
    const { protocol, hostname } = window.location;
 
    // If running on localhost, use local backend
    if (hostname === 'localhost' || hostname === '127.0.0.1') {
      return 'http://localhost:8000';
    }
 
    // For cloud deployment, use same domain (ingress routes /api to backend)
    // Works for: dev.missingtable.com, missingtable.com, www.missingtable.com
    return `${protocol}//${hostname}`;
  }
 
  // SSR/build-time fallback only (never executed in browser)
  return 'http://localhost:8000';
};
 
// CRITICAL: Must be a getter function, NOT a constant!
// Constants are evaluated at BUILD time (Node.js, no window), getters at RUNTIME (browser)
export const getApiBaseUrl = () => getApiUrl();
 
// For backwards compatibility, export as property that calls getter
export const API_BASE_URL = getApiUrl();
 
// Helper to build API URL at runtime
const buildUrl = path => `${getApiUrl()}${path}`;
 
export const API_ENDPOINTS = {
  get AUTH() {
    const base = getApiUrl();
    return {
      SIGNUP: `${base}/api/auth/signup`,
      LOGIN: `${base}/api/auth/login`,
      LOGOUT: `${base}/api/auth/logout`,
      PROFILE: `${base}/api/profile`,
    };
  },
  get INVITES() {
    const base = getApiUrl();
    return {
      VALIDATE: code => `${base}/api/invites/validate/${code}`,
      MY_INVITES: `${base}/api/invites/my-invites`,
      ADMIN_TEAM_MANAGER: `${base}/api/invites/admin/team-manager`,
      ADMIN_TEAM_PLAYER: `${base}/api/invites/admin/team-player`,
      ADMIN_TEAM_FAN: `${base}/api/invites/admin/team-fan`,
      TEAM_MANAGER_PLAYER: `${base}/api/invites/team-manager/team-player`,
      TEAM_MANAGER_FAN: `${base}/api/invites/team-manager/team-fan`,
      CANCEL: id => `${base}/api/invites/${id}`,
    };
  },
  get TEAMS() {
    return buildUrl('/api/teams');
  },
  get AGE_GROUPS() {
    return buildUrl('/api/age-groups');
  },
  get SEASONS() {
    return buildUrl('/api/seasons');
  },
  get MATCHES() {
    return buildUrl('/api/matches');
  },
};
 
console.log('API Configuration:', {
  API_BASE_URL,
  env: import.meta.env.VITE_API_URL,
});