fix: validate route params and reduce duplication

This commit is contained in:
2026-04-27 09:45:47 -04:00
parent a802035ca4
commit 77b9fc9934
4 changed files with 103 additions and 63 deletions

View File

@@ -20,15 +20,23 @@ export async function facebookRoute(req: Request): Promise<Response> {
const LOCATION = reqUrl.searchParams.get("location") || "toronto";
const maxItemsParam = reqUrl.searchParams.get("maxItems");
const maxItems = maxItemsParam ? parseInt(maxItemsParam, 10) : 25;
if (maxItemsParam && Number.isNaN(maxItems)) {
return Response.json(
{ message: "Invalid maxItems parameter" },
{ status: 400 },
);
}
const hideUnstableResults =
reqUrl.searchParams.get("unstableFilter") === "true";
try {
const items = hideUnstableResults
? await fetchFacebookItems(SEARCH_QUERY, 1, LOCATION, maxItems, {
hideUnstableResults: true,
})
: await fetchFacebookItems(SEARCH_QUERY, 1, LOCATION, maxItems);
const items = await fetchFacebookItems(
SEARCH_QUERY,
1,
LOCATION,
maxItems,
...(hideUnstableResults ? [{ hideUnstableResults: true }] : []),
);
const isEmpty = hideUnstableResults
? items.results.length === 0 && items.unstableResults.length === 0