-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.js
More file actions
59 lines (49 loc) · 1.59 KB
/
config.js
File metadata and controls
59 lines (49 loc) · 1.59 KB
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
/**
* API Configuration
* Configure backend URLs and settings
*/
// Backend URLs
export const JAVA_API_URL = 'http://localhost:8080/api/yfdata';
export const FLASK_API_URL = 'http://localhost:5000/api';
// API Mode - set to 'mock' for development or 'backend' for production
export const API_MODE = process.env.REACT_APP_API_MODE || 'backend';
// Request timeout in milliseconds
export const REQUEST_TIMEOUT = 30000;
// Common headers
export const API_HEADERS = {
'Content-Type': 'application/json',
};
/**
* Helper function to create fetch request with timeout
*/
export const fetchWithTimeout = async (url, options = {}, timeout = REQUEST_TIMEOUT) => {
const controller = new AbortController();
const id = setTimeout(() => controller.abort(), timeout);
try {
const response = await fetch(url, {
...options,
signal: controller.signal
});
clearTimeout(id);
return response;
} catch (error) {
clearTimeout(id);
if (error.name === 'AbortError') {
throw new Error('Request timeout');
}
throw error;
}
};
/**
* Helper function to handle API errors
*/
export const handleApiError = (error) => {
console.error('API Error:', error);
if (error.message === 'Request timeout') {
return { error: 'Request timed out. Please try again.' };
}
if (error.message.includes('Failed to fetch')) {
return { error: 'Cannot connect to server. Please ensure the backend is running.' };
}
return { error: error.message || 'An unexpected error occurred' };
};