Files
brb-store/app/api/search/route.ts
T
Alireza HassaniandCursor 24b5649abd Polish storefront SEO, specials, and homepage UX.
Publish Meshkee sitemap-config, map product-source specials without variants, restore hero autoplay, and tighten page heroes, product images, and section order.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-22 21:48:00 +03:30

51 lines
1.5 KiB
TypeScript

import { meshkeeApiBase, websiteDomain } from "@/lib/config";
export async function GET(request: Request) {
const url = new URL(request.url);
const query = url.searchParams.get("q")?.trim() ?? "";
if (query.length <= 2) {
return Response.json({ items: [] });
}
const encoded = encodeURIComponent(query);
const base = `${meshkeeApiBase}/tenants/${encodeURIComponent(websiteDomain)}`;
try {
const storeResponse = await fetch(`${base}/store-items?name=${encoded}&pageSize=8`, {
cache: "no-store",
});
const storeData = (await storeResponse.json()) as { items?: unknown[] };
if (storeData.items?.length) {
return Response.json(storeData);
}
const catalogResponse = await fetch(`${base}/products?name=${encoded}&pageSize=8`, {
cache: "no-store",
});
const catalogData = (await catalogResponse.json()) as {
items?: Array<{
id: string;
nameFa?: string | null;
title?: string | null;
slug?: string | null;
thumbnailUrl?: string | null;
image?: string | null;
store?: { minPrice?: number | null; maxPrice?: number | null };
}>;
};
return Response.json({
items: (catalogData.items ?? []).map((item) => ({
id: item.slug || item.id,
productId: item.id,
productNameFa: item.nameFa,
productTitle: item.title,
productImage: item.thumbnailUrl || item.image,
price: item.store?.minPrice ?? item.store?.maxPrice ?? null,
})),
});
} catch {
return Response.json({ items: [] });
}
}