feat: update index with new kijiji api
Signed-off-by: Dmytro Stanchiev <git@dmytros.dev>
This commit is contained in:
49
src/index.ts
49
src/index.ts
@@ -26,17 +26,56 @@ const server = Bun.serve({
|
||||
{ status: 400 },
|
||||
);
|
||||
|
||||
const items = await fetchKijijiItems(SEARCH_QUERY, 1, undefined, {}, {
|
||||
includeImages: true,
|
||||
sellerDataDepth: 'detailed',
|
||||
includeClientSideData: false,
|
||||
});
|
||||
// Parse optional parameters with enhanced defaults
|
||||
const location = reqUrl.searchParams.get("location");
|
||||
const category = reqUrl.searchParams.get("category");
|
||||
const maxPagesParam = reqUrl.searchParams.get("maxPages");
|
||||
const maxPages = maxPagesParam
|
||||
? Number.parseInt(maxPagesParam, 10)
|
||||
: 5; // Default: 5 pages
|
||||
const sortBy = reqUrl.searchParams.get("sortBy") as 'relevancy' | 'date' | 'price' | 'distance' | undefined;
|
||||
const sortOrder = reqUrl.searchParams.get("sortOrder") as 'asc' | 'desc' | undefined;
|
||||
|
||||
// Build search options
|
||||
const locationValue = location ? (/^\d+$/.test(location) ? Number(location) : location) : 1700272;
|
||||
const categoryValue = category ? (/^\d+$/.test(category) ? Number(category) : category) : 0;
|
||||
|
||||
const searchOptions: import("@/kijiji").SearchOptions = {
|
||||
location: locationValue,
|
||||
category: categoryValue,
|
||||
keywords: SEARCH_QUERY,
|
||||
sortBy: sortBy || 'relevancy',
|
||||
sortOrder: sortOrder || 'desc',
|
||||
maxPages,
|
||||
};
|
||||
|
||||
// Build listing fetch options with enhanced defaults
|
||||
const listingOptions: import("@/kijiji").ListingFetchOptions = {
|
||||
includeImages: true, // Always include full image arrays
|
||||
sellerDataDepth: 'detailed', // Default: detailed seller info
|
||||
includeClientSideData: false, // GraphQL reviews disabled by default
|
||||
};
|
||||
|
||||
try {
|
||||
const items = await fetchKijijiItems(SEARCH_QUERY, 1, undefined, searchOptions, listingOptions);
|
||||
if (!items || items.length === 0)
|
||||
return Response.json(
|
||||
{ message: "Search didn't return any results!" },
|
||||
{ status: 404 },
|
||||
);
|
||||
return Response.json(items, { status: 200 });
|
||||
} catch (error) {
|
||||
console.error("Kijiji scraping error:", error);
|
||||
const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
|
||||
return Response.json(
|
||||
{
|
||||
message: `Scraping failed: ${errorMessage}`,
|
||||
query: SEARCH_QUERY,
|
||||
options: { searchOptions, listingOptions }
|
||||
},
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
},
|
||||
|
||||
"/api/facebook": async (req: Request) => {
|
||||
|
||||
Reference in New Issue
Block a user