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

View File

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