api: reconfigered api handling
This commit is contained in:
@@ -1,22 +1,25 @@
|
||||
|
||||
export type RawSeasonParticipant = {
|
||||
id: { seasonId: number; personId: number };
|
||||
person: {
|
||||
personId: number;
|
||||
name: string;
|
||||
birthDate?: string;
|
||||
imageUrl?: string | null;
|
||||
partner?: {
|
||||
personId: number;
|
||||
name: string;
|
||||
birthDate: string;
|
||||
imageUrl: string | null;
|
||||
birthDate?: string;
|
||||
imageUrl?: string | null;
|
||||
};
|
||||
partner: unknown | null;
|
||||
};
|
||||
|
||||
export type RawSeason = {
|
||||
seasonId: number;
|
||||
show: number;
|
||||
seasonNumber: number;
|
||||
startDate?: string;
|
||||
endDate?: string | null;
|
||||
moderators: unknown[];
|
||||
seasonParticipants: RawSeasonParticipant[];
|
||||
show?: number;
|
||||
moderators?: unknown[];
|
||||
};
|
||||
|
||||
export type SeasonParticipant = {
|
||||
@@ -43,27 +46,37 @@ export async function getSeason(
|
||||
): 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);
|
||||
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[] = raw.seasonParticipants.map(
|
||||
(p) => ({
|
||||
id: p.person.personId,
|
||||
name: p.person.name,
|
||||
birthYear: p.person.birthDate
|
||||
? Number(p.person.birthDate.slice(0, 4))
|
||||
: undefined,
|
||||
imageUri:
|
||||
p.person.imageUrl ??
|
||||
`https://i.pravatar.cc/300?img=${Math.random() * 70}`,
|
||||
})
|
||||
);
|
||||
|
||||
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: raw.show,
|
||||
showId,
|
||||
seasonNumber: raw.seasonNumber,
|
||||
startDate: raw.startDate,
|
||||
endDate: raw.endDate,
|
||||
@@ -74,3 +87,4 @@ export async function getSeason(
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user