refactor: move fetchEpisodes in to subfolder
This commit is contained in:
60
src/controllers/episodes/getByAniListId/amvstrm.ts
Normal file
60
src/controllers/episodes/getByAniListId/amvstrm.ts
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user