Files
ipv6-sims/api/homeApi.ts
T
2026-05-11 16:24:14 +07:00

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
}
};