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

@@ -30,16 +30,16 @@ export async function kijijiRoute(req: Request): Promise<Response> {
location: reqUrl.searchParams.get("location") || undefined,
category: reqUrl.searchParams.get("category") || undefined,
keywords: reqUrl.searchParams.get("keywords") || undefined,
sortBy: reqUrl.searchParams.get("sortBy") as
sortBy: (reqUrl.searchParams.get("sortBy") as
| "relevancy"
| "date"
| "price"
| "distance"
| undefined,
sortOrder: reqUrl.searchParams.get("sortOrder") as
| undefined) || undefined,
sortOrder: (reqUrl.searchParams.get("sortOrder") as
| "desc"
| "asc"
| undefined,
| undefined) || undefined,
maxPages,
priceMin,
priceMax,
@@ -47,22 +47,14 @@ export async function kijijiRoute(req: Request): Promise<Response> {
};
try {
const items = hideUnstableResults
? await fetchKijijiItems(
SEARCH_QUERY,
4, // 4 requests per second for faster scraping
"https://www.kijiji.ca",
searchOptions,
{},
{ hideUnstableResults: true },
)
: await fetchKijijiItems(
SEARCH_QUERY,
4, // 4 requests per second for faster scraping
"https://www.kijiji.ca",
searchOptions,
{},
);
const items = await fetchKijijiItems(
SEARCH_QUERY,
4, // 4 requests per second for faster scraping
"https://www.kijiji.ca",
searchOptions,
{},
...(hideUnstableResults ? [{ hideUnstableResults: true }] : []),
);
const isEmpty = hideUnstableResults
? items.results.length === 0 && items.unstableResults.length === 0