Files
fltr-app/apis/seasonApi.ts
2025-10-20 18:45:01 +02:00

91 lines
2.1 KiB
TypeScript

export type RawSeasonParticipant = {
personId: number;
name: string;
birthDate?: string;
imageUrl?: string | null;
partner?: {
personId: number;
name: string;
birthDate?: string;
imageUrl?: string | null;
};
};
export type RawSeason = {
seasonId: number;
seasonNumber: number;
startDate?: string;
endDate?: string | null;
seasonParticipants: RawSeasonParticipant[];
show?: number;
moderators?: unknown[];
};
export type SeasonParticipant = {
id: number;
name: string;
birthYear?: number;
imageUri: string;
};
export type Season = {
id: number;
showId: number;
seasonNumber: number;
startDate?: string;
endDate?: string | null;
participants: SeasonParticipant[];
};
const SEASON_BASE_URL = "http://45.157.177.99:8080/shows";
export async function getSeason(
showId: number,
seasonNumber: number
): Promise<Season | null> {
const url = `${SEASON_BASE_URL}/${showId}/seasons/${seasonNumber}`;
try {
const apiKey = process.env.EXPO_PUBLIC_API_KEY;
console.log("[getSeason] Fetch:", url);
const res = await fetch(url, {
headers: {
Accept: "application/json",
"Content-Type": "application/json",
"X-API-Key": apiKey ?? "",
},
});
console.log("[getSeason] Status:", res.status);
if (res.status === 404) return null;
if (!res.ok) throw new Error("Season fetch failed " + res.status);
const raw: RawSeason = await res.json();
const participants: SeasonParticipant[] = Array.isArray(
raw.seasonParticipants
)
? raw.seasonParticipants.map((p) => ({
id: p.personId,
name: p.name,
birthYear: p.birthDate ? Number(p.birthDate.slice(0, 4)) : undefined,
imageUri:
p.imageUrl ??
`https://i.pravatar.cc/300?img=${Math.floor(Math.random() * 70) + 1}`,
}))
: [];
return {
id: raw.seasonId,
showId,
seasonNumber: raw.seasonNumber,
startDate: raw.startDate,
endDate: raw.endDate,
participants,
};
} catch (e) {
console.error("getSeason error:", e);
throw e;
}
}