refactor: move fetchEpisodes in to subfolder

This commit is contained in:
2024-05-29 08:37:51 -04:00
parent d19f76689c
commit 8b6aecca5c
7 changed files with 1 additions and 1 deletions

View File

@@ -0,0 +1,60 @@
import { Episode, type EpisodesResponse } from "./episode";
export async function getEpisodesFromAmvstrm(
aniListId: number,
): Promise<EpisodesResponse | null> {
try {
const episodes: Episode[] | null = await fetch(
`https://api-amvstrm.nyt92.eu.org/api/v2/episode/${aniListId}`,
)
.then((res) => res.json<AmvstrmEpisodesResponse>())
.then(({ code, message, episodes }) => {
if (code >= 400) {
console.error(
`Error trying to load episodes from amvstrm; aniListId: ${aniListId}, code: ${code}, message: ${message}`,
);
return null;
}
return episodes.map<Episode>(
({ id, description, image, title, episode, airDate }) => ({
id,
number: episode,
description,
img: image,
title,
updatedAt: airDate ?? 0,
}),
);
});
if (!episodes || episodes.length === 0) {
return null;
}
return { providerId: "amvstrm", episodes };
} catch (error) {
console.error(
new Error(
`Error trying to load episodes from amvstrm; aniListId: ${aniListId}`,
{ cause: error },
),
);
}
return null;
}
interface AmvstrmEpisodesResponse {
code: number;
message: string;
episodes: AmvstrmEpisode[];
}
interface AmvstrmEpisode {
id: string;
title: string;
description: string | null;
episode: number;
image: string;
airDate: null;
}