75 lines
1.8 KiB
TypeScript
75 lines
1.8 KiB
TypeScript
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
|
|
|
|
import { EpisodesResponseSchema } from "~/types/episode";
|
|
import {
|
|
AniListIdQuerySchema,
|
|
ErrorResponse,
|
|
ErrorResponseSchema,
|
|
} from "~/types/schema";
|
|
|
|
const route = createRoute({
|
|
tags: ["aniplay", "episodes"],
|
|
summary: "Fetch episodes for a title",
|
|
operationId: "fetchEpisodes",
|
|
method: "get",
|
|
path: "/{aniListId}",
|
|
request: {
|
|
params: z.object({ aniListId: AniListIdQuerySchema }),
|
|
},
|
|
responses: {
|
|
200: {
|
|
content: {
|
|
"application/json": {
|
|
schema: EpisodesResponseSchema,
|
|
},
|
|
},
|
|
description: "Returns a list of episodes",
|
|
},
|
|
500: {
|
|
content: {
|
|
"application/json": {
|
|
schema: ErrorResponseSchema,
|
|
},
|
|
},
|
|
description: "Error fetching episodes",
|
|
},
|
|
},
|
|
});
|
|
|
|
const app = new OpenAPIHono<Cloudflare.Env>();
|
|
|
|
export function fetchEpisodes(aniListId: number, shouldRetry: boolean = false) {
|
|
return import("./aniwatch")
|
|
.then(({ getEpisodesFromAniwatch }) =>
|
|
getEpisodesFromAniwatch(aniListId, shouldRetry),
|
|
)
|
|
.then((episodeResults) => episodeResults?.episodes ?? []);
|
|
}
|
|
|
|
app.openapi(route, async (c) => {
|
|
const aniListId = Number(c.req.param("aniListId"));
|
|
|
|
// Check if we should use mock data
|
|
const { useMockData } = await import("~/libs/useMockData");
|
|
if (useMockData()) {
|
|
const { mockEpisodes } = await import("~/mocks");
|
|
|
|
return c.json({
|
|
success: true,
|
|
result: { providerId: "aniwatch", episodes: mockEpisodes() },
|
|
});
|
|
}
|
|
|
|
const episodes = await fetchEpisodes(aniListId);
|
|
if (episodes.length === 0) {
|
|
return c.json(ErrorResponse, { status: 404 });
|
|
}
|
|
|
|
return c.json({
|
|
success: true,
|
|
result: { providerId: "aniwatch", episodes },
|
|
});
|
|
});
|
|
|
|
export default app;
|