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

45 lines
1.2 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({ request }: LoaderFunctionArgs) {
const search = new URL(request.url).searchParams;
2023-12-18 03:18:17 +00:00
let filter: Filter | null = null;
let page = "0";
const searchEntries = Array.from(search.entries());
if (searchEntries.length) {
const searchParams = Object.fromEntries(searchEntries);
({ filter, page = "0" } = searchParams as any as {
2023-12-18 03:18:17 +00:00
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,
});
}
}