All files / src/app/api/products/search/suggestions route.ts

100% Statements 36/36
100% Branches 6/6
100% Functions 1/1
100% Lines 36/36

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 371x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 2x 2x 2x 2x 2x 2x 2x 6x 6x 5x 5x 5x 1x 1x  
export const dynamic = "force-dynamic";
 
import { NextRequest, NextResponse } from "next/server";
import { getSearchSuggestions } from "@/lib/search";
import { addSecurityHeaders } from "@/lib/security";
import {
  withErrorHandling,
  successResponse,
  ApiSuccessResponse } from "@/lib/api";
 
/**
 * GET /api/products/search/suggestions
 * Get search suggestions based on query
 */
async function handleGet(
  request: NextRequest
): Promise<NextResponse<ApiSuccessResponse<string[]>>> {
  const searchParams = request.nextUrl.searchParams;
  const query = searchParams.get("query");
  const limit = parseInt(searchParams.get("limit") || "10");
 
  if (!query || query.length < 2) {
    return addSecurityHeaders(
      NextResponse.json({
        success: true,
        data: [],
        message: "Query must be at least 2 characters"})
    ) as NextResponse<ApiSuccessResponse<string[]>>;
  }
 
  const suggestions = await getSearchSuggestions(query, limit);
 
  return addSecurityHeaders(successResponse(suggestions)) as NextResponse<ApiSuccessResponse<string[]>>;
}
 
export const GET = withErrorHandling(handleGet);