fix: validate route params and reduce duplication
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user