All files / src/config apiBaseUrl.ts

81.81% Statements 9/11
25% Branches 1/4
100% Functions 1/1
81.81% Lines 9/11

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 3975x   75x                               75x 75x 75x       75x         75x       75x       75x  
import { Platform } from 'react-native';
 
const PRODUCTION_API_BASE_URL = 'https://www.socialeventmapper.com/api';
 
/**
 * Base URL for the backend API (includes `/api` prefix).
 *
 * **Docker Compose (local):** nginx listens on the host at port **80**, so paths are
 * `http://<host>/api/...` — not `:8000` (that is only inside the Docker network).
 *
 * | Where you run the app | Default host |
 * |------------------------|--------------|
 * | iOS Simulator | `localhost` |
 * | Android Emulator | `10.0.2.2` (special alias to your Mac) |
 * | Physical phone (Expo Go) | Set `EXPO_PUBLIC_API_BASE_URL` to `http://<your-computer-LAN-IP>/api` |
 *
 * Create `mobile/.env` from `.env.example` and restart Metro after changing.
 */
export function getApiBaseUrl(): string {
  const fromEnv = process.env.EXPO_PUBLIC_API_BASE_URL?.trim();
  Iif (fromEnv) {
    return fromEnv.replace(/\/$/, '');
  }
 
  Iif (process.env.EXPO_PUBLIC_APP_ENV === 'production') {
    return PRODUCTION_API_BASE_URL;
  }
 
  const host =
    Platform.OS === 'android'
      ? '10.0.2.2' // Android emulator → host machine (not localhost)
      : 'localhost';
 
  return `http://${host}/api`;
}
 
/** Resolved once at startup; use `EXPO_PUBLIC_API_BASE_URL` for local overrides. */
export const API_BASE_URL = getApiBaseUrl();