feat(api/facebook): add maxItems parameter support
This commit is contained in:
@@ -5,36 +5,42 @@ import { fetchFacebookItems } from "@marketplace-scrapers/core";
|
|||||||
* Search Facebook Marketplace for listings
|
* Search Facebook Marketplace for listings
|
||||||
*/
|
*/
|
||||||
export async function facebookRoute(req: Request): Promise<Response> {
|
export async function facebookRoute(req: Request): Promise<Response> {
|
||||||
const reqUrl = new URL(req.url);
|
const reqUrl = new URL(req.url);
|
||||||
|
|
||||||
const SEARCH_QUERY =
|
const SEARCH_QUERY =
|
||||||
req.headers.get("query") || reqUrl.searchParams.get("q") || null;
|
req.headers.get("query") || reqUrl.searchParams.get("q") || null;
|
||||||
if (!SEARCH_QUERY)
|
if (!SEARCH_QUERY)
|
||||||
return Response.json(
|
return Response.json(
|
||||||
{
|
{
|
||||||
message:
|
message: "Request didn't have 'query' header or 'q' search parameter!",
|
||||||
"Request didn't have 'query' header or 'q' search parameter!",
|
},
|
||||||
},
|
{ status: 400 },
|
||||||
{ status: 400 },
|
);
|
||||||
);
|
|
||||||
|
|
||||||
const LOCATION = reqUrl.searchParams.get("location") || "toronto";
|
const LOCATION = reqUrl.searchParams.get("location") || "toronto";
|
||||||
const COOKIES_SOURCE = reqUrl.searchParams.get("cookies") || undefined;
|
const COOKIES_SOURCE = reqUrl.searchParams.get("cookies") || undefined;
|
||||||
|
const maxItemsParam = reqUrl.searchParams.get("maxItems");
|
||||||
|
const maxItems = maxItemsParam ? parseInt(maxItemsParam, 10) : 25;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const items = await fetchFacebookItems(SEARCH_QUERY, 5, LOCATION, 25, COOKIES_SOURCE);
|
const items = await fetchFacebookItems(
|
||||||
if (!items || items.length === 0)
|
SEARCH_QUERY,
|
||||||
return Response.json(
|
1,
|
||||||
{ message: "Search didn't return any results!" },
|
LOCATION,
|
||||||
{ status: 404 },
|
maxItems,
|
||||||
);
|
COOKIES_SOURCE,
|
||||||
return Response.json(items, { status: 200 });
|
undefined,
|
||||||
} catch (error) {
|
);
|
||||||
console.error("Facebook scraping error:", error);
|
if (!items || items.length === 0)
|
||||||
const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
|
return Response.json(
|
||||||
return Response.json(
|
{ message: "Search didn't return any results!" },
|
||||||
{ message: errorMessage },
|
{ status: 404 },
|
||||||
{ status: 400 },
|
);
|
||||||
);
|
return Response.json(items, { status: 200 });
|
||||||
}
|
} catch (error) {
|
||||||
|
console.error("Facebook scraping error:", error);
|
||||||
|
const errorMessage =
|
||||||
|
error instanceof Error ? error.message : "Unknown error occurred";
|
||||||
|
return Response.json({ message: errorMessage }, { status: 400 });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user