refactor: remove api cookie query overrides
This commit is contained in:
@@ -1,9 +1,8 @@
|
|||||||
import { fetchEbayItems } from "@marketplace-scrapers/core";
|
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)
|
* 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> {
|
export async function ebayRoute(req: Request): Promise<Response> {
|
||||||
try {
|
try {
|
||||||
@@ -38,8 +37,6 @@ export async function ebayRoute(req: Request): Promise<Response> {
|
|||||||
|
|
||||||
const maxItemsParam = reqUrl.searchParams.get("maxItems");
|
const maxItemsParam = reqUrl.searchParams.get("maxItems");
|
||||||
const maxItems = maxItemsParam ? parseInt(maxItemsParam, 10) : undefined;
|
const maxItems = maxItemsParam ? parseInt(maxItemsParam, 10) : undefined;
|
||||||
const cookies = reqUrl.searchParams.get("cookies") || undefined;
|
|
||||||
|
|
||||||
const items = await fetchEbayItems(SEARCH_QUERY, 1, {
|
const items = await fetchEbayItems(SEARCH_QUERY, 1, {
|
||||||
minPrice,
|
minPrice,
|
||||||
maxPrice,
|
maxPrice,
|
||||||
@@ -48,7 +45,6 @@ export async function ebayRoute(req: Request): Promise<Response> {
|
|||||||
keywords,
|
keywords,
|
||||||
buyItNowOnly,
|
buyItNowOnly,
|
||||||
canadaOnly,
|
canadaOnly,
|
||||||
cookies,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const results = maxItems ? items.slice(0, maxItems) : items;
|
const results = maxItems ? items.slice(0, maxItems) : items;
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { fetchFacebookItems } from "@marketplace-scrapers/core";
|
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
|
* Search Facebook Marketplace for listings
|
||||||
*/
|
*/
|
||||||
export async function facebookRoute(req: Request): Promise<Response> {
|
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 LOCATION = reqUrl.searchParams.get("location") || "toronto";
|
||||||
const COOKIES_SOURCE = reqUrl.searchParams.get("cookies") || undefined;
|
|
||||||
const maxItemsParam = reqUrl.searchParams.get("maxItems");
|
const maxItemsParam = reqUrl.searchParams.get("maxItems");
|
||||||
const maxItems = maxItemsParam ? parseInt(maxItemsParam, 10) : 25;
|
const maxItems = maxItemsParam ? parseInt(maxItemsParam, 10) : 25;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const items = await fetchFacebookItems(
|
const items = await fetchFacebookItems(SEARCH_QUERY, 1, LOCATION, maxItems);
|
||||||
SEARCH_QUERY,
|
|
||||||
1,
|
|
||||||
LOCATION,
|
|
||||||
maxItems,
|
|
||||||
COOKIES_SOURCE,
|
|
||||||
undefined,
|
|
||||||
);
|
|
||||||
if (!items || items.length === 0)
|
if (!items || items.length === 0)
|
||||||
return Response.json(
|
return Response.json(
|
||||||
{ message: "Search didn't return any results!" },
|
{ 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