62 lines
1.5 KiB
TypeScript
62 lines
1.5 KiB
TypeScript
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
|
|
|
|
import { fetchFromMultipleSources } from "~/libs/fetchFromMultipleSources";
|
|
import {
|
|
AniListIdSchema,
|
|
ErrorResponseSchema,
|
|
SuccessResponseSchema,
|
|
} from "~/types/schema";
|
|
import { Title } from "~/types/title";
|
|
|
|
import { fetchTitleFromAmvstrm } from "./amvstrm";
|
|
import { fetchTitleFromAnilist } from "./anilist";
|
|
|
|
const app = new OpenAPIHono();
|
|
|
|
const route = createRoute({
|
|
tags: ["aniplay", "title"],
|
|
operationId: "fetchTitle",
|
|
summary: "Fetch title information",
|
|
method: "get",
|
|
path: "/",
|
|
request: {
|
|
query: z.object({ id: AniListIdSchema }),
|
|
headers: z.object({ "x-anilist-token": z.string().nullish() }),
|
|
},
|
|
responses: {
|
|
200: {
|
|
content: {
|
|
"application/json": {
|
|
schema: SuccessResponseSchema(Title),
|
|
},
|
|
},
|
|
description: "Returns title information",
|
|
},
|
|
"404": {
|
|
content: {
|
|
"application/json": {
|
|
schema: ErrorResponseSchema,
|
|
},
|
|
},
|
|
description: "Title could not be found",
|
|
},
|
|
},
|
|
});
|
|
|
|
app.openapi(route, async (c) => {
|
|
const aniListId = Number(c.req.query("id"));
|
|
const aniListToken = c.req.header("X-AniList-Token");
|
|
|
|
const title = await fetchFromMultipleSources([
|
|
() => fetchTitleFromAnilist(aniListId, aniListToken ?? undefined),
|
|
() => fetchTitleFromAmvstrm(aniListId),
|
|
]);
|
|
if (!title) {
|
|
return c.json({ success: false }, 404);
|
|
}
|
|
|
|
return c.json({ success: true, result: title }, 200);
|
|
});
|
|
|
|
export default app;
|