Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 13x 13x 13x 13x 13x 13x 13x 13x 1x 1x 1x 12x 12x 12x 12x 12x 13x 2x 2x 2x 2x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 9x 9x 9x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 9x 10x 10x 10x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 1x 1x | export const dynamic = "force-dynamic";
import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { addSecurityHeaders } from "@/lib/security";
import { checkRateLimit, rateLimitPresets } from "@/lib/security";
import { measureApiPerformance } from "@/lib/performance";
import { getOrSet, CACHE_KEYS, CACHE_TTL } from "@/lib/core";
import { cachedJsonResponse, CACHE_PRESETS } from "@/lib/core/http-cache";
import {
withErrorHandling,
ApiError,
ApiSuccessResponse } from "@/lib/api";
// Type for search result
interface SearchResult {
id: number;
title: string;
description: string | null;
price: number;
discountedPrice: number;
category: { id: number; title: string };
thumbnail: string | null;
averageRating: number;
reviewCount: number;
}
// GET /api/products/search - Search products by title, description, or category
async function handleGet(
request: NextRequest
): Promise<NextResponse<ApiSuccessResponse<{ results: SearchResult[] }>>> {
const startTime = Date.now();
// Rate limiting for search endpoint
const ip = request.headers.get("x-forwarded-for") || request.headers.get("x-real-ip") || "unknown";
if (!checkRateLimit(`api-search-${ip}`, rateLimitPresets.standard.limit, rateLimitPresets.standard.windowMs)) {
// const rateLimitInfo = getRateLimitInfo(`api-search-${ip}`, rateLimitPresets.standard.limit);
throw ApiError.rateLimited("Rate limit exceeded");
}
const searchParams = request.nextUrl.searchParams;
const query = searchParams.get("q");
// Require at least 2 characters for search
if (!query || query.length < 2) {
return addSecurityHeaders(
NextResponse.json({ success: true, data: { results: [] } })
);
}
// Normalize query for cache key
const normalizedQuery = query.toLowerCase().trim();
// Use cache for search results
const results = await getOrSet<SearchResult[]>(
CACHE_KEYS.search(normalizedQuery),
async () => {
// Search products by title, description, or category name
const products = await prisma.product.findMany({
where: {
OR: [
{ title: { contains: query } },
{ description: { contains: query } },
{ category: { title: { contains: query } } },
] },
select: {
id: true,
title: true,
description: true,
price: true,
discountedPrice: true,
images: {
orderBy: { order: "asc" },
take: 1,
select: { url: true, thumbnailUrl: true } },
category: {
select: { id: true, title: true } },
reviews: {
select: { rating: true } } },
take: 10, // Limit to 10 results for dropdown
orderBy: {
title: "asc" } });
// Transform results to match frontend format
return products.map((product) => {
const totalRating = product.reviews.reduce(
(sum, review) => sum + review.rating,
0
);
const averageRating =
product.reviews.length > 0 ? totalRating / product.reviews.length : 0;
return {
id: product.id,
title: product.title,
description: product.description,
price: product.price,
discountedPrice: product.discountedPrice,
category: product.category,
thumbnail: product.images[0]?.thumbnailUrl || product.images[0]?.url || null,
averageRating,
reviewCount: product.reviews.length };
});
},
CACHE_TTL.search
);
measureApiPerformance("GET /api/products/search", startTime);
const response = cachedJsonResponse(
request,
{ success: true, data: { results } },
CACHE_PRESETS.search
);
return addSecurityHeaders(response) as NextResponse<ApiSuccessResponse<{ results: SearchResult[] }>>;
}
export const GET = withErrorHandling(handleGet);
|