feat: create route to search for titles
Summary: Test Plan:
This commit is contained in:
56
src/controllers/search/index.ts
Normal file
56
src/controllers/search/index.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
|
||||
|
||||
import { fetchFromMultipleSources } from "~/libs/fetchFromMultipleSources";
|
||||
import { PaginatedResponseSchema } from "~/types/schema";
|
||||
import { Title } from "~/types/title";
|
||||
|
||||
import { fetchSearchResultsFromAmvstrm } from "./amvstrm";
|
||||
import { fetchSearchResultsFromAnilist } from "./anilist";
|
||||
import { SearchResult } from "./searchResult";
|
||||
|
||||
const app = new OpenAPIHono();
|
||||
|
||||
const route = createRoute({
|
||||
tags: ["aniplay", "title"],
|
||||
operationId: "search",
|
||||
summary: "Search for a title",
|
||||
method: "get",
|
||||
path: "/",
|
||||
request: {
|
||||
query: z.object({ query: z.string(), page: z.number().min(1).default(1) }),
|
||||
},
|
||||
responses: {
|
||||
200: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: PaginatedResponseSchema(SearchResult),
|
||||
},
|
||||
},
|
||||
description: "Returns a list of paginated results for the query",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
app.openapi(route, async (c) => {
|
||||
const query = c.req.query("query") ?? "";
|
||||
const page = Number(c.req.query("page") ?? 1);
|
||||
|
||||
const response = await fetchFromMultipleSources([
|
||||
() => fetchSearchResultsFromAnilist(query, page),
|
||||
() => fetchSearchResultsFromAmvstrm(query, page),
|
||||
]);
|
||||
if (!response) {
|
||||
return c.json({ success: false, results: [], hasNextPage: false }, 200);
|
||||
}
|
||||
|
||||
return c.json(
|
||||
{
|
||||
success: true,
|
||||
results: response.results,
|
||||
hasNextPage: response.hasNextPage ?? false,
|
||||
},
|
||||
200,
|
||||
);
|
||||
});
|
||||
|
||||
export default app;
|
||||
Reference in New Issue
Block a user