fix: popular category response properly supports pagination

This commit is contained in:
2024-10-28 05:08:52 -04:00
parent 9fafa5b45d
commit 4c784e9038
2 changed files with 32 additions and 8 deletions

View File

@@ -14,6 +14,9 @@ const TrendingQuery = graphql(
media(sort: TRENDING_DESC, type: ANIME, isAdult: false) {
...HomeTitle
}
pageInfo {
hasNextPage
}
}
}
`,
@@ -38,6 +41,9 @@ const PopularQuery = graphql(
) {
...HomeTitle
}
pageInfo {
hasNextPage
}
}
}
`,
@@ -62,6 +68,9 @@ const UpcomingQuery = graphql(
) {
...HomeTitle
}
pageInfo {
hasNextPage
}
}
}
`,
@@ -78,9 +87,10 @@ export function fetchPopularTitlesFromAnilist(
const { current, next } = getCurrentAndNextSeason();
switch (category) {
case "trending":
return client
.request(TrendingQuery, { limit, page })
.then((data) => data?.trending?.media?.map((title) => mapTitle(title)));
return client.request(TrendingQuery, { limit, page }).then((data) => ({
results: data?.trending?.media?.map((title) => mapTitle(title)),
hasNextPage: data?.trending?.pageInfo?.hasNextPage,
}));
case "popular":
return client
.request(PopularQuery, {
@@ -89,7 +99,10 @@ export function fetchPopularTitlesFromAnilist(
season: current.season,
seasonYear: current.year,
})
.then((data) => data?.Page?.media?.map((title) => mapTitle(title)));
.then((data) => ({
results: data?.Page?.media?.map((title) => mapTitle(title)),
hasNextPage: data?.Page?.pageInfo?.hasNextPage,
}));
case "upcoming":
return client
.request(UpcomingQuery, {
@@ -98,7 +111,10 @@ export function fetchPopularTitlesFromAnilist(
nextSeason: next.season,
nextSeasonYear: next.year,
})
.then((data) => data?.Page?.media?.map((title) => mapTitle(title)));
.then((data) => ({
results: data?.Page?.media?.map((title) => mapTitle(title)),
hasNextPage: data?.Page?.pageInfo?.hasNextPage,
}));
default:
throw new Error(`Unknown category: ${category}`);
}

View File

@@ -1,12 +1,16 @@
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
import { ErrorResponse, SuccessResponseSchema } from "~/types/schema";
import {
ErrorResponse,
PaginatedResponseSchema,
SuccessResponseSchema,
} from "~/types/schema";
import { HomeTitle } from "~/types/title/homeTitle";
import { fetchPopularTitlesFromAnilist } from "./anilist";
import { PopularCategory } from "./enum";
const BrowsePopularResponse = SuccessResponseSchema(z.array(HomeTitle));
const BrowsePopularResponse = PaginatedResponseSchema(HomeTitle);
const app = new OpenAPIHono();
@@ -53,7 +57,11 @@ app.openapi(route, async (c) => {
return c.json(ErrorResponse, { status: 500 });
}
return c.json({ success: true, result: response });
return c.json({
success: true,
results: response.results,
hasNextPage: response.hasNextPage ?? false,
});
});
export default app;