api: fetch shows implemented
This commit is contained in:
56
apis/showApi.ts
Normal file
56
apis/showApi.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
export type RawShow = {
|
||||
showId: number;
|
||||
title: string;
|
||||
description: string;
|
||||
genre: string;
|
||||
thumbnailUrl: string;
|
||||
bannerUrl?: string;
|
||||
concept: string;
|
||||
streamingServices: string;
|
||||
startDate?: string;
|
||||
endDate?: string | null;
|
||||
};
|
||||
|
||||
export type Show = {
|
||||
id: number;
|
||||
title: string;
|
||||
description: string;
|
||||
genres: string[];
|
||||
thumbnailUri: string;
|
||||
bannerUri: string;
|
||||
streamingService: string;
|
||||
concept: string;
|
||||
startDate?: string;
|
||||
endDate?: string | null;
|
||||
};
|
||||
|
||||
const API_URL = "http://45.157.177.99:8080/shows";
|
||||
|
||||
export async function getShows(): Promise<Show[]> {
|
||||
try {
|
||||
const response = await fetch(API_URL);
|
||||
if (!response.ok) {
|
||||
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,
|
||||
startDate: s.startDate,
|
||||
endDate: s.endDate ?? null,
|
||||
}));
|
||||
} catch (error) {
|
||||
console.error("Fetch error:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user