refactor: update VITE_API_URL configuration and enhance path handling in API service

This commit is contained in:
Tudor Stanciu 2025-10-04 18:31:18 +03:00
parent 5059780048
commit a1a7da1b41
4 changed files with 25 additions and 10 deletions

View File

@ -34,6 +34,6 @@ DEBOUNCE_MS=2000 # Debounce delay for frontend input
# SEQ_API_KEY=your-seq-api-key # SEQ_API_KEY=your-seq-api-key
# Development Overrides (used in docker-compose.dev.yml) # Development Overrides (used in docker-compose.dev.yml)
# VITE_API_URL=http://localhost:5172/api # VITE_API_URL=http://localhost:5172
# VITE_API_KEY=frontend-dev-key # VITE_API_KEY=frontend-dev-key
# VITE_DEBOUNCE_MS=1000 # VITE_DEBOUNCE_MS=1000

View File

@ -268,7 +268,7 @@ The frontend is configured via environment variables, typically stored in `src/f
```env ```env
VITE_API_KEY=frontend-dev-key VITE_API_KEY=frontend-dev-key
VITE_API_URL=http://localhost:5172/geoip-ui/api VITE_API_URL=http://localhost:5172/geoip-ui
VITE_DEBOUNCE_MS=2000 VITE_DEBOUNCE_MS=2000
``` ```
@ -282,12 +282,12 @@ VITE_DEBOUNCE_MS=2000
**`VITE_API_URL`** **`VITE_API_URL`**
- **Purpose**: Full URL to the backend API (including base path and `/api` suffix) - **Purpose**: Base URL to the backend server
- **Format**: HTTP/HTTPS URL - **Format**: HTTP/HTTPS URL
- **Required**: Yes - **Required**: Yes
- **Development Example**: `http://localhost:5172/geoip-ui/api` - **Development Example**: `http://localhost:5172/geoip-ui`
- **Production Example**: `https://your-domain.com/geoip-ui/api` - **Production Example**: `https://your-domain.com/geoip-ui`
- **Note**: Must match backend's `PORT` and `BASE_PATH` configuration - **Note**: Must match backend's `PORT` and `BASE_PATH` configuration.
**`VITE_DEBOUNCE_MS`** **`VITE_DEBOUNCE_MS`**
@ -332,7 +332,7 @@ VITE_DEBOUNCE_MS=2000
```env ```env
VITE_API_KEY=frontend-dev-key VITE_API_KEY=frontend-dev-key
VITE_API_URL=http://localhost:5172/geoip-ui/api VITE_API_URL=http://localhost:5172/geoip-ui
VITE_DEBOUNCE_MS=2000 VITE_DEBOUNCE_MS=2000
``` ```
@ -380,7 +380,7 @@ DEBOUNCE_MS=2000
```env ```env
VITE_API_KEY=<same-as-backend-FRONTEND_API_KEY> VITE_API_KEY=<same-as-backend-FRONTEND_API_KEY>
VITE_API_URL=https://your-domain.com/api VITE_API_URL=https://your-domain.com
VITE_DEBOUNCE_MS=2000 VITE_DEBOUNCE_MS=2000
``` ```

View File

@ -1,5 +1,5 @@
# Frontend Environment Variables for Local Development # Frontend Environment Variables for Local Development
VITE_API_KEY=frontend-dev-key VITE_API_KEY=frontend-dev-key
VITE_API_URL=http://localhost:5172/api VITE_API_URL=http://localhost:5172
VITE_DEBOUNCE_MS=2000 VITE_DEBOUNCE_MS=2000
VITE_BASE_PATH=/ VITE_BASE_PATH=/

View File

@ -8,8 +8,23 @@ import {
OverviewResponse, OverviewResponse,
} from '../types'; } from '../types';
/**
* Combines base URL and path, ensuring proper slash handling
* @param baseUrl - Base URL (e.g., 'http://localhost:5172' or 'http://localhost:5172/')
* @param path - Path to append (e.g., '/api' or 'api')
* @returns Combined URL path
*/
const pathCombine = (baseUrl: string, path: string): string => {
const trimmedBase = baseUrl.replace(/\/+$/, ''); // Remove trailing slashes
const trimmedPath = path.replace(/^\/+/, ''); // Remove leading slashes
return `${trimmedBase}/${trimmedPath}`;
};
const isDevelopment = import.meta.env.MODE === 'development';
const API_KEY = import.meta.env.VITE_API_KEY || 'frontend-default-key'; const API_KEY = import.meta.env.VITE_API_KEY || 'frontend-default-key';
const BASE_URL = import.meta.env.VITE_API_URL || '/api'; const BASE_URL = isDevelopment
? pathCombine(import.meta.env.VITE_API_URL, '/api')
: '/api';
const apiClient = axios.create({ const apiClient = axios.create({
baseURL: BASE_URL, baseURL: BASE_URL,