forked from UKSOURCE/ipv6
44 lines
891 B
TypeScript
44 lines
891 B
TypeScript
/**
|
|
* Lấy API URL từ environment variable
|
|
*/
|
|
const getApiUrl = (): string | null => {
|
|
const apiUrl = process.env.NEXT_PUBLIC_API_URL;
|
|
|
|
if (!apiUrl) {
|
|
return null;
|
|
}
|
|
|
|
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();
|
|
if (!apiUrl) return null;
|
|
|
|
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) {
|
|
return null; // Trả về null để fallback sang dữ liệu local
|
|
}
|
|
};
|