All files / src/config apiBaseUrl.ts

87.5% Statements 7/8
50% Branches 2/4
100% Functions 1/1
87.5% Lines 7/8

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 3340x                               40x 40x 40x         40x       40x       40x  
import { Platform } from 'react-native';
 
/**
 * 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(/\/$/, '');
  }
 
  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` to override. */
export const API_BASE_URL = getApiBaseUrl();