Add pagination to feed list

This commit is contained in:
2025-06-09 08:11:22 +09:00
parent 4f19eb8df0
commit e9e9a276c9
3 changed files with 268 additions and 21 deletions

View File

@ -509,9 +509,32 @@ app.get("/api/episode/:episodeId", async (c) => {
app.get("/api/feeds", async (c) => {
try {
const { fetchActiveFeeds } = await import("./services/database.js");
const feeds = await fetchActiveFeeds();
return c.json({ feeds });
const page = c.req.query("page");
const limit = c.req.query("limit");
const category = c.req.query("category");
// If pagination parameters are provided, use paginated endpoint
if (page || limit) {
const { fetchActiveFeedsPaginated } = await import("./services/database.js");
const pageNum = page ? Number.parseInt(page, 10) : 1;
const limitNum = limit ? Number.parseInt(limit, 10) : 10;
// Validate pagination parameters
if (Number.isNaN(pageNum) || pageNum < 1) {
return c.json({ error: "Invalid page number" }, 400);
}
if (Number.isNaN(limitNum) || limitNum < 1 || limitNum > 100) {
return c.json({ error: "Invalid limit (must be between 1-100)" }, 400);
}
const result = await fetchActiveFeedsPaginated(pageNum, limitNum, category || undefined);
return c.json(result);
} else {
// Original behavior for backward compatibility
const { fetchActiveFeeds } = await import("./services/database.js");
const feeds = await fetchActiveFeeds();
return c.json({ feeds });
}
} catch (error) {
console.error("Error fetching feeds:", error);
return c.json({ error: "Failed to fetch feeds" }, 500);