fix: use explicit conditional calls and validate negative params

This commit is contained in:
2026-04-27 10:46:06 -04:00
parent a1af5d2630
commit 02b3f805b2
4 changed files with 131 additions and 28 deletions

View File

@@ -19,7 +19,7 @@ export async function kijijiRoute(req: Request): Promise<Response> {
const maxPagesParam = reqUrl.searchParams.get("maxPages");
const maxPages = maxPagesParam ? parseInt(maxPagesParam, 10) : 5;
if (maxPagesParam && Number.isNaN(maxPages)) {
if (maxPagesParam && (Number.isNaN(maxPages) || maxPages < 0)) {
return Response.json(
{ message: "Invalid maxPages parameter" },
{ status: 400 },
@@ -27,7 +27,7 @@ export async function kijijiRoute(req: Request): Promise<Response> {
}
const priceMinParam = reqUrl.searchParams.get("priceMin");
const priceMin = priceMinParam ? parseInt(priceMinParam, 10) : undefined;
if (priceMinParam && Number.isNaN(priceMin)) {
if (priceMinParam && (Number.isNaN(priceMin) || priceMin < 0)) {
return Response.json(
{ message: "Invalid priceMin parameter" },
{ status: 400 },
@@ -35,7 +35,7 @@ export async function kijijiRoute(req: Request): Promise<Response> {
}
const priceMaxParam = reqUrl.searchParams.get("priceMax");
const priceMax = priceMaxParam ? parseInt(priceMaxParam, 10) : undefined;
if (priceMaxParam && Number.isNaN(priceMax)) {
if (priceMaxParam && (Number.isNaN(priceMax) || priceMax < 0)) {
return Response.json(
{ message: "Invalid priceMax parameter" },
{ status: 400 },
@@ -65,14 +65,22 @@ export async function kijijiRoute(req: Request): Promise<Response> {
};
try {
const items = await fetchKijijiItems(
SEARCH_QUERY,
4, // 4 requests per second for faster scraping
"https://www.kijiji.ca",
searchOptions,
{},
...(hideUnstableResults ? [{ hideUnstableResults: true }] : []),
);
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 isEmpty = hideUnstableResults
? items.results.length === 0 && items.unstableResults.length === 0