- NEXT_PUBLIC_API_BASE: Keep public (just the endpoint URL) - SERVER_ID: Remove NEXT_PUBLIC_ prefix (private identifier) - ACCESS_TOKEN: Remove NEXT_PUBLIC_ prefix (sensitive credential) Since fetching is server-side, credentials never reach the browser. Only API_BASE needs to be public if accessed from client in future.
29 lines
874 B
TypeScript
29 lines
874 B
TypeScript
import { AppData } from './types';
|
|
|
|
export async function fetchLeaderboard(): Promise<AppData | null> {
|
|
const API_BASE = process.env.NEXT_PUBLIC_API_BASE || '';
|
|
const SERVER_ID = process.env.SERVER_ID || '';
|
|
const ACCESS_TOKEN = process.env.ACCESS_TOKEN || '';
|
|
|
|
if (!API_BASE || !SERVER_ID || !ACCESS_TOKEN) {
|
|
console.warn('Missing API configuration');
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
const res = await fetch(`${API_BASE}/server/${SERVER_ID}/leaderboard`, {
|
|
headers: {
|
|
"Access-Key": ACCESS_TOKEN
|
|
}
|
|
});
|
|
if (!res.ok) return null;
|
|
const data = await res.json();
|
|
// Treat empty leaderboard (no drivers) as null to fall back to defaults
|
|
if (!data || !data.drivers || data.drivers.length === 0) return null;
|
|
return data;
|
|
} catch (e) {
|
|
console.error("Failed to load leaderboard from API:", e);
|
|
return null;
|
|
}
|
|
}
|