68 lines
1.7 KiB
TypeScript
68 lines
1.7 KiB
TypeScript
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
|
|
|
|
import {
|
|
ErrorResponse,
|
|
PaginatedResponseSchema,
|
|
SuccessResponseSchema,
|
|
} from "~/types/schema";
|
|
import { HomeTitle } from "~/types/title/homeTitle";
|
|
|
|
import { fetchPopularTitlesFromAnilist } from "./anilist";
|
|
import { PopularCategory } from "./enum";
|
|
|
|
const BrowsePopularResponse = PaginatedResponseSchema(HomeTitle);
|
|
|
|
const app = new OpenAPIHono();
|
|
|
|
const route = createRoute({
|
|
tags: ["aniplay", "title"],
|
|
operationId: "browsePopularTitlesWithCategory",
|
|
summary: "Get a preview of popular titles for a category",
|
|
method: "get",
|
|
path: "/{category}",
|
|
request: {
|
|
query: z.object({
|
|
limit: z
|
|
.number({ coerce: true })
|
|
.int()
|
|
.default(10)
|
|
.describe("The number of titles to return"),
|
|
page: z.number({ coerce: true }).int().min(1).default(1),
|
|
}),
|
|
params: z.object({ category: PopularCategory }),
|
|
},
|
|
responses: {
|
|
200: {
|
|
content: {
|
|
"application/json": {
|
|
schema: BrowsePopularResponse,
|
|
},
|
|
},
|
|
description: "Returns an object containing a preview of popular titles",
|
|
},
|
|
},
|
|
});
|
|
|
|
app.openapi(route, async (c) => {
|
|
const page = Number(c.req.query("page") ?? 1);
|
|
const limit = Number(c.req.query("limit") ?? 10);
|
|
const popularCategory = c.req.param("category") as PopularCategory;
|
|
|
|
const response = await fetchPopularTitlesFromAnilist(
|
|
popularCategory,
|
|
page,
|
|
limit,
|
|
);
|
|
if (!response) {
|
|
return c.json(ErrorResponse, { status: 500 });
|
|
}
|
|
|
|
return c.json({
|
|
success: true,
|
|
results: response.results,
|
|
hasNextPage: response.hasNextPage ?? false,
|
|
});
|
|
});
|
|
|
|
export default app;
|