fix(api): parse price filters as dollars

This commit is contained in:
2026-04-30 23:17:56 -04:00
parent 5c732287c5
commit 89ad1c521f
4 changed files with 91 additions and 36 deletions

View File

@@ -3,6 +3,7 @@ import { logger } from "../logger";
import {
emptySearchResponse,
getRequiredSearchQuery,
parseDollarPriceParam,
parseNonNegativeIntegerParam,
} from "./helpers";
@@ -18,17 +19,11 @@ export async function ebayRoute(req: Request): Promise<Response> {
return SEARCH_QUERY;
}
const minPrice = parseNonNegativeIntegerParam(
reqUrl.searchParams,
"minPrice",
);
const minPrice = parseDollarPriceParam(reqUrl.searchParams, "minPrice");
if (minPrice instanceof Response) {
return minPrice;
}
const maxPrice = parseNonNegativeIntegerParam(
reqUrl.searchParams,
"maxPrice",
);
const maxPrice = parseDollarPriceParam(reqUrl.searchParams, "maxPrice");
if (maxPrice instanceof Response) {
return maxPrice;
}

View File

@@ -39,6 +39,23 @@ export function parseNonNegativeIntegerParam(
return Number(rawValue);
}
export function parseDollarPriceParam(
searchParams: URLSearchParams,
name: string,
): number | undefined | Response {
const rawValue = searchParams.get(name);
if (rawValue === null) {
return undefined;
}
if (!/^\d+(?:\.\d{1,2})?$/.test(rawValue)) {
return Response.json(
{ message: `Invalid ${name} parameter` },
{ status: 400 },
);
}
return Math.round(Number(rawValue) * 100);
}
export function emptySearchResponse(hint?: string): Response {
const message = hint
? `Search didn't return any results! ${hint}`

View File

@@ -3,6 +3,7 @@ import { logger } from "../logger";
import {
emptySearchResponse,
getRequiredSearchQuery,
parseDollarPriceParam,
parseNonNegativeIntegerParam,
} from "./helpers";
@@ -26,17 +27,11 @@ export async function kijijiRoute(req: Request): Promise<Response> {
if (maxPages instanceof Response) {
return maxPages;
}
const priceMin = parseNonNegativeIntegerParam(
reqUrl.searchParams,
"priceMin",
);
const priceMin = parseDollarPriceParam(reqUrl.searchParams, "priceMin");
if (priceMin instanceof Response) {
return priceMin;
}
const priceMax = parseNonNegativeIntegerParam(
reqUrl.searchParams,
"priceMax",
);
const priceMax = parseDollarPriceParam(reqUrl.searchParams, "priceMax");
if (priceMax instanceof Response) {
return priceMax;
}