refactor: remove api cookie query overrides
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
import { fetchEbayItems } from "@marketplace-scrapers/core";
|
||||
|
||||
/**
|
||||
* GET /api/ebay?q={query}&minPrice={minPrice}&maxPrice={maxPrice}&strictMode={strictMode}&exclusions={exclusions}&keywords={keywords}&buyItNowOnly={buyItNowOnly}&canadaOnly={canadaOnly}&cookies={cookies}
|
||||
* GET /api/ebay?q={query}&minPrice={minPrice}&maxPrice={maxPrice}&strictMode={strictMode}&exclusions={exclusions}&keywords={keywords}&buyItNowOnly={buyItNowOnly}&canadaOnly={canadaOnly}
|
||||
* Search eBay for listings (default: Buy It Now only, Canada only)
|
||||
* Optional: Pass cookies parameter to bypass bot detection
|
||||
*/
|
||||
export async function ebayRoute(req: Request): Promise<Response> {
|
||||
try {
|
||||
@@ -38,8 +37,6 @@ export async function ebayRoute(req: Request): Promise<Response> {
|
||||
|
||||
const maxItemsParam = reqUrl.searchParams.get("maxItems");
|
||||
const maxItems = maxItemsParam ? parseInt(maxItemsParam, 10) : undefined;
|
||||
const cookies = reqUrl.searchParams.get("cookies") || undefined;
|
||||
|
||||
const items = await fetchEbayItems(SEARCH_QUERY, 1, {
|
||||
minPrice,
|
||||
maxPrice,
|
||||
@@ -48,7 +45,6 @@ export async function ebayRoute(req: Request): Promise<Response> {
|
||||
keywords,
|
||||
buyItNowOnly,
|
||||
canadaOnly,
|
||||
cookies,
|
||||
});
|
||||
|
||||
const results = maxItems ? items.slice(0, maxItems) : items;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { fetchFacebookItems } from "@marketplace-scrapers/core";
|
||||
|
||||
/**
|
||||
* GET /api/facebook?q={query}&location={location}&cookies={cookies}
|
||||
* GET /api/facebook?q={query}&location={location}
|
||||
* Search Facebook Marketplace for listings
|
||||
*/
|
||||
export async function facebookRoute(req: Request): Promise<Response> {
|
||||
@@ -18,19 +18,11 @@ export async function facebookRoute(req: Request): Promise<Response> {
|
||||
);
|
||||
|
||||
const LOCATION = reqUrl.searchParams.get("location") || "toronto";
|
||||
const COOKIES_SOURCE = reqUrl.searchParams.get("cookies") || undefined;
|
||||
const maxItemsParam = reqUrl.searchParams.get("maxItems");
|
||||
const maxItems = maxItemsParam ? parseInt(maxItemsParam, 10) : 25;
|
||||
|
||||
try {
|
||||
const items = await fetchFacebookItems(
|
||||
SEARCH_QUERY,
|
||||
1,
|
||||
LOCATION,
|
||||
maxItems,
|
||||
COOKIES_SOURCE,
|
||||
undefined,
|
||||
);
|
||||
const items = await fetchFacebookItems(SEARCH_QUERY, 1, LOCATION, maxItems);
|
||||
if (!items || items.length === 0)
|
||||
return Response.json(
|
||||
{ message: "Search didn't return any results!" },
|
||||
|
||||
53
packages/api-server/test/routes.test.ts
Normal file
53
packages/api-server/test/routes.test.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { afterEach, beforeEach, describe, expect, mock, test } from "bun:test";
|
||||
|
||||
const fetchFacebookItems = mock(() => Promise.resolve([{ title: "item" }]));
|
||||
const fetchEbayItems = mock(() => Promise.resolve([{ title: "item" }]));
|
||||
|
||||
mock.module("@marketplace-scrapers/core", () => ({
|
||||
fetchFacebookItems,
|
||||
fetchEbayItems,
|
||||
}));
|
||||
|
||||
describe("API routes", () => {
|
||||
beforeEach(() => {
|
||||
fetchFacebookItems.mockReset();
|
||||
fetchFacebookItems.mockImplementation(() => Promise.resolve([{ title: "item" }]));
|
||||
fetchEbayItems.mockReset();
|
||||
fetchEbayItems.mockImplementation(() => Promise.resolve([{ title: "item" }]));
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
fetchFacebookItems.mockClear();
|
||||
fetchEbayItems.mockClear();
|
||||
});
|
||||
|
||||
test("facebookRoute ignores cookies query parameter", async () => {
|
||||
const { facebookRoute } = await import("../src/routes/facebook");
|
||||
|
||||
await facebookRoute(
|
||||
new Request(
|
||||
"http://localhost/api/facebook?q=laptop&location=toronto&maxItems=3&cookies=c_user=1",
|
||||
),
|
||||
);
|
||||
|
||||
expect(fetchFacebookItems).toHaveBeenCalledWith("laptop", 1, "toronto", 3);
|
||||
});
|
||||
|
||||
test("ebayRoute ignores cookies query parameter", async () => {
|
||||
const { ebayRoute } = await import("../src/routes/ebay");
|
||||
|
||||
await ebayRoute(
|
||||
new Request("http://localhost/api/ebay?q=laptop&cookies=s%3D1&buyItNowOnly=true"),
|
||||
);
|
||||
|
||||
expect(fetchEbayItems).toHaveBeenCalledWith("laptop", 1, {
|
||||
minPrice: undefined,
|
||||
maxPrice: undefined,
|
||||
strictMode: false,
|
||||
exclusions: [],
|
||||
keywords: ["laptop"],
|
||||
buyItNowOnly: true,
|
||||
canadaOnly: true,
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user