fix: correct empty-result and maxItems handling in routes
This commit is contained in:
@@ -64,27 +64,39 @@ export async function ebayRoute(req: Request): Promise<Response> {
|
||||
canadaOnly,
|
||||
});
|
||||
|
||||
let results;
|
||||
if (hideUnstableResults) {
|
||||
results = maxItems
|
||||
? {
|
||||
results: items.results.slice(0, maxItems),
|
||||
unstableResults: items.unstableResults.slice(0, maxItems),
|
||||
}
|
||||
: items;
|
||||
} else {
|
||||
results = maxItems ? items.slice(0, maxItems) : items;
|
||||
}
|
||||
|
||||
const isEmpty = hideUnstableResults
|
||||
? results.results.length === 0 && results.unstableResults.length === 0
|
||||
: !results || results.length === 0;
|
||||
? items.results.length === 0 && items.unstableResults.length === 0
|
||||
: !items || items.length === 0;
|
||||
|
||||
if (isEmpty)
|
||||
return Response.json(
|
||||
{ message: "Search didn't return any results!" },
|
||||
{ status: 404 },
|
||||
);
|
||||
|
||||
let results;
|
||||
if (hideUnstableResults) {
|
||||
const limitedResults =
|
||||
maxItems !== undefined
|
||||
? items.results.slice(0, maxItems)
|
||||
: items.results;
|
||||
const remainingSlots =
|
||||
maxItems !== undefined
|
||||
? Math.max(0, maxItems - limitedResults.length)
|
||||
: undefined;
|
||||
const limitedUnstable =
|
||||
remainingSlots !== undefined
|
||||
? items.unstableResults.slice(0, remainingSlots)
|
||||
: items.unstableResults;
|
||||
results = {
|
||||
results: limitedResults,
|
||||
unstableResults: limitedUnstable,
|
||||
};
|
||||
} else {
|
||||
results =
|
||||
maxItems !== undefined ? items.slice(0, maxItems) : items;
|
||||
}
|
||||
|
||||
return Response.json(results, { status: 200 });
|
||||
} catch (error) {
|
||||
console.error("eBay scraping error:", error);
|
||||
|
||||
@@ -29,7 +29,12 @@ export async function facebookRoute(req: Request): Promise<Response> {
|
||||
hideUnstableResults: true,
|
||||
})
|
||||
: await fetchFacebookItems(SEARCH_QUERY, 1, LOCATION, maxItems);
|
||||
if (!items || items.length === 0)
|
||||
|
||||
const isEmpty = hideUnstableResults
|
||||
? items.results.length === 0 && items.unstableResults.length === 0
|
||||
: !items || items.length === 0;
|
||||
|
||||
if (isEmpty)
|
||||
return Response.json(
|
||||
{ message: "Search didn't return any results!" },
|
||||
{ status: 404 },
|
||||
|
||||
@@ -63,7 +63,12 @@ export async function kijijiRoute(req: Request): Promise<Response> {
|
||||
searchOptions,
|
||||
{},
|
||||
);
|
||||
if (!items)
|
||||
|
||||
const isEmpty = hideUnstableResults
|
||||
? items.results.length === 0 && items.unstableResults.length === 0
|
||||
: !items || items.length === 0;
|
||||
|
||||
if (isEmpty)
|
||||
return Response.json(
|
||||
{ message: "Search didn't return any results!" },
|
||||
{ status: 404 },
|
||||
|
||||
Reference in New Issue
Block a user