Files
fltr-app/apis/showApi.ts
2025-11-02 21:51:38 +01:00

94 lines
2.4 KiB
TypeScript

export type RawShow = {
showId: number;
title: string;
description: string;
genre: string;
thumbnailUrl: string;
bannerUrl?: string;
concept: string;
streamingServices: string;
startDate?: string;
endDate?: string | null;
running: boolean;
logoUrl?: string;
};
export type Show = {
id: number;
title: string;
description: string;
genres: string[];
thumbnailUri: string;
bannerUri: string;
streamingService: string;
concept: string;
startDate?: string;
endDate?: string | null;
logoUrl: string;
running: boolean;
};
const SHOW_API_URL = "https://fltr-app.de/api/shows";
export async function getShows(): Promise<Show[]> {
try {
const apiKey = process.env.EXPO_PUBLIC_API_KEY;
const response = await fetch(SHOW_API_URL, {
headers: {
"Content-Type": "application/json",
"X-API-Key": apiKey ?? "",
},
});
if (!response.ok) {
console.error("Fetch error:", response);
throw new Error("Network response was not ok");
}
const data: unknown = await response.json();
if (!Array.isArray(data)) {
console.warn("Expected array, got:", data);
return [];
}
return (data as RawShow[]).map((s) => ({
id: s.showId,
title: s.title,
description: s.description,
genres: s.genre ? s.genre.split(",").map((g) => g.trim()) : [],
thumbnailUri: s.thumbnailUrl,
bannerUri: s.bannerUrl ?? "",
streamingService: s.streamingServices,
concept: s.concept,
running: s.running,
logoUrl: s.logoUrl ?? "",
}));
} catch (error) {
console.error("Fetch error:", error);
throw error;
}
}
export async function getShowById(showId: number): Promise<Show | null> {
const apiKey = process.env.EXPO_PUBLIC_API_KEY;
const url = `${SHOW_API_URL}/${showId}`;
const res = await fetch(url, {
headers: {
"Content-Type": "application/json",
"X-API-Key": apiKey ?? "",
},
});
if (res.status === 404) return null;
if (!res.ok) throw new Error("getShowById failed " + res.status);
const s = (await res.json()) as RawShow;
return {
id: s.showId,
title: s.title,
description: s.description,
genres: s.genre ? s.genre.split(",").map((g) => g.trim()) : [],
thumbnailUri: s.thumbnailUrl,
bannerUri: s.bannerUrl ?? "",
streamingService: s.streamingServices,
concept: s.concept,
running: s.running,
logoUrl: s.logoUrl ?? "",
};
}