forked from UKSOURCE/hailearning.edu.vn
44 lines
1018 B
TypeScript
44 lines
1018 B
TypeScript
/**
|
|
* Lấy API URL từ environment variable
|
|
*/
|
|
const getApiUrl = (): string => {
|
|
const apiUrl = process.env.NEXT_PUBLIC_API_URL;
|
|
|
|
if (!apiUrl) {
|
|
console.warn('NEXT_PUBLIC_API_URL is not set. Using default http://localhost:3001');
|
|
return 'http://localhost:3001';
|
|
}
|
|
|
|
return apiUrl;
|
|
};
|
|
|
|
/**
|
|
* Fetch home page data từ API
|
|
* @returns Promise<any>
|
|
* @throws Error nếu fetch thất bại
|
|
*/
|
|
export const fetchHomeData = async (): Promise<any> => {
|
|
const apiUrl = getApiUrl();
|
|
const url = `${apiUrl}/api/home`;
|
|
|
|
try {
|
|
const response = await fetch(url, {
|
|
method: 'GET',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
cache: 'no-store',
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
}
|
|
|
|
const data = await response.json();
|
|
return data;
|
|
} catch (error) {
|
|
console.error('Error fetching home data:', error);
|
|
return null; // Trả về null để fallback sang dữ liệu local
|
|
}
|
|
};
|