web3.news/app/routes/api.feed.ts

42 lines
1.0 KiB
TypeScript
Raw Normal View History

2023-12-18 03:18:17 +00:00
import { json, LoaderFunctionArgs } from "@remix-run/node";
import { fetchFeedData } from "@/lib/feed";
2023-12-18 03:18:17 +00:00
type Filter = "latest" | "day" | "week" | "month";
export async function loader({ params }: LoaderFunctionArgs) {
let filter: Filter | null = null;
let page = "0";
if (params?.searchParams) {
({ filter, page = "0" } = params.searchParams as any as {
filter: Filter;
page: string;
});
}
try {
// Define the limit of articles per page
const limit = 5;
// Calculate the `current` and `next` values based on the `page` parameter
const current = parseInt(page, 10) * limit;
const next = limit;
// Prepare the parameters for fetchFeedData
const queryParams = {
filter: filter ? { timerange: filter } : undefined,
next,
limit,
current,
};
// Fetch data using the fetchFeedData function
const dataResponse = await fetchFeedData(queryParams);
2023-12-18 03:18:17 +00:00
return json(dataResponse);
} catch (error) {
2023-12-18 03:18:17 +00:00
throw new Response("Internal Server Error", {
status: 500,
});
}
}