|
1 | 1 | import {store} from '../store'; |
2 | 2 | import {logger} from '../utils/logger'; |
3 | 3 | import {setAppInfo} from '../store/slices/configSlice'; |
| 4 | +const LOG_PREFIX = '[AppConfig]'; |
4 | 5 |
|
5 | 6 | const BASE_API_URL = process.env.REACT_APP_API_URL || window.location.origin; |
6 | 7 |
|
7 | 8 |
|
8 | 9 | export const fetchAppConfig = async (sessionId: string) => { |
9 | 10 | try { |
10 | | - logger.info('Fetching app config for session:', sessionId); |
| 11 | + logger.info(`${LOG_PREFIX} Fetching app config:`, { |
| 12 | + sessionId, |
| 13 | + baseUrl: BASE_API_URL |
| 14 | + }); |
| 15 | + |
11 | 16 | const url = new URL('/appInfo', BASE_API_URL); |
12 | 17 | url.searchParams.append('session', sessionId); |
| 18 | + |
13 | 19 | let response: Response; |
14 | | - // Add error handling for failed requests |
| 20 | + |
15 | 21 | try { |
16 | 22 | response = await fetch(url.toString(), { |
17 | 23 | headers: { |
18 | | - 'Accept': 'application/json, text/json' |
19 | | - } |
| 24 | + 'Accept': 'application/json, text/json', |
| 25 | + 'Cache-Control': 'no-cache' |
| 26 | + }, |
| 27 | + credentials: 'include' |
20 | 28 | }); |
21 | 29 | } catch (networkError) { |
22 | | - logger.warn('Network request failed:', networkError); |
| 30 | + logger.warn(`${LOG_PREFIX} Network request failed:`, { |
| 31 | + error: networkError, |
| 32 | + url: url.toString() |
| 33 | + }); |
| 34 | + // Return default config for development |
| 35 | + if (process.env.NODE_ENV === 'development') { |
| 36 | + const defaultConfig = { |
| 37 | + applicationName: 'Chat App (Offline)', |
| 38 | + singleInput: false, |
| 39 | + stickyInput: true, |
| 40 | + loadImages: true, |
| 41 | + showMenubar: true |
| 42 | + }; |
| 43 | + store.dispatch(setAppInfo(defaultConfig)); |
| 44 | + return defaultConfig; |
| 45 | + } |
23 | 46 | return null; |
24 | 47 | } |
25 | 48 |
|
26 | 49 | if (!response.ok) { |
27 | | - logger.warn(`API returned error status: ${response.status}`); |
| 50 | + logger.warn(`${LOG_PREFIX} API error response:`, { |
| 51 | + status: response.status, |
| 52 | + statusText: response.statusText, |
| 53 | + url: url.toString() |
| 54 | + }); |
| 55 | + const errorText = await response.text(); |
| 56 | + logger.debug(`${LOG_PREFIX} Error response body:`, errorText); |
28 | 57 | return null; |
29 | 58 | } |
| 59 | + |
30 | 60 | const contentType = response.headers.get('content-type'); |
31 | 61 | if (!contentType || (!contentType.includes('application/json') && !contentType.includes('text/json'))) { |
32 | | - throw new Error(`Invalid content type: ${contentType}`); |
| 62 | + logger.error(`${LOG_PREFIX} Invalid content type:`, { |
| 63 | + contentType, |
| 64 | + url: url.toString() |
| 65 | + }); |
| 66 | + throw new Error(`Invalid content type received: ${contentType}`); |
33 | 67 | } |
34 | 68 |
|
35 | 69 |
|
36 | 70 | const data = await response.json(); |
37 | 71 | if (!data || typeof data !== 'object') { |
| 72 | + logger.error(`${LOG_PREFIX} Invalid response format:`, data); |
38 | 73 | throw new Error('Invalid response format'); |
39 | 74 | } |
40 | 75 |
|
41 | | - logger.info('Received app config:', data); |
| 76 | + logger.info(`${LOG_PREFIX} Received valid config:`, data); |
42 | 77 |
|
43 | 78 | store.dispatch(setAppInfo(data)); |
44 | 79 |
|
45 | 80 | return data; |
46 | 81 | } catch (error) { |
47 | | - logger.error('Failed to fetch app config:', { |
| 82 | + logger.error(`${LOG_PREFIX} Config fetch failed:`, { |
48 | 83 | error, |
49 | 84 | sessionId, |
50 | | - url: BASE_API_URL ? `${BASE_API_URL}/appInfo` : '/api/appInfo' |
| 85 | + url: BASE_API_URL ? `${BASE_API_URL}/appInfo` : '/appInfo', |
| 86 | + env: process.env.NODE_ENV |
51 | 87 | }); |
52 | 88 | throw error; |
53 | 89 | } |
|
0 commit comments