fix: require REACT_APP_API_URL in production, throw clear error if missing

This commit is contained in:
ajaysi
2026-04-04 07:08:34 +05:30
parent a3e4f5231a
commit c5e2fc3514
7 changed files with 67 additions and 16 deletions

View File

@@ -77,8 +77,14 @@ class HallucinationDetectorService {
private baseUrl: string;
constructor() {
// Consistent API URL pattern - no hardcoded localhost fallback
this.baseUrl = process.env.REACT_APP_API_URL || process.env.REACT_APP_BACKEND_URL || '';
const getApiBaseUrl = () => {
const url = process.env.REACT_APP_API_URL;
if (process.env.NODE_ENV === 'production' && !url) {
throw new Error('REACT_APP_API_URL environment variable is required for production');
}
return url || 'http://localhost:8000';
};
this.baseUrl = getApiBaseUrl();
}
/**

View File

@@ -12,8 +12,16 @@ import {
CopilotSuggestion
} from '../types/seoCopilotTypes';
// Consistent API URL pattern - use same env vars as other services
const API_BASE_URL = process.env.REACT_APP_API_URL || process.env.REACT_APP_BACKEND_URL || '';
// API URL - require REACT_APP_API_URL in production
const getApiBaseUrl = () => {
const url = process.env.REACT_APP_API_URL;
if (process.env.NODE_ENV === 'production' && !url) {
throw new Error('REACT_APP_API_URL environment variable is required for production');
}
return url || 'http://localhost:8000';
};
const API_BASE_URL = getApiBaseUrl();
class SEOApiService {
private baseUrl: string;

View File

@@ -21,8 +21,14 @@ export interface WASuggestResponse {
class WritingAssistantService {
private baseUrl: string;
constructor() {
// Consistent API URL pattern - no hardcoded localhost fallback
this.baseUrl = process.env.REACT_APP_API_URL || process.env.REACT_APP_BACKEND_URL || '';
const getApiBaseUrl = () => {
const url = process.env.REACT_APP_API_URL;
if (process.env.NODE_ENV === 'production' && !url) {
throw new Error('REACT_APP_API_URL environment variable is required for production');
}
return url || 'http://localhost:8000';
};
this.baseUrl = getApiBaseUrl();
}
async suggest(text: string): Promise<WASuggestion[]> {