Files
Evandro Fonseca Junior 2b790d8eb4 feat(library): add vocation filter, normalize categories, improve grid layout
Added vocation state and included it in query string builder, updated onFilter to accept a vocation parameter, improved grid layout with grid flow classes, normalized category names for consistent item icons, and refined stats mapping to handle type safety.
2025-10-30 18:44:52 -03:00

73 lines
2.2 KiB
TypeScript

import { NextRequest } from "next/server";
import { searchItems } from "@/lib/items-db";
import { buildPublicItemImageUrl } from "@/lib/item-images";
function parseArray(param: string | null): string[] {
if (!param) return [];
// Suporta formatos: comma-separated (a,b) ou múltiplos parâmetros já combinados
return param
.split(",")
.map((v) => v.trim())
.filter(Boolean);
}
export async function GET(req: NextRequest) {
try {
const { searchParams } = new URL(req.url);
const q = searchParams.get("q") || undefined;
const categories = parseArray(searchParams.get("category"));
const rarities = parseArray(searchParams.get("rarity"));
const sortParam = (searchParams.get("sort") || "name") as
| "name"
| "level-asc"
| "level-desc"
| "weight-asc"
| "weight-desc"
| "category";
const page = Number(searchParams.get("page") || "1");
const pageSize = Number(searchParams.get("pageSize") || "24");
const voc = searchParams.get("voc") || undefined;
const { items, total } = searchItems({
q,
categories,
rarities,
sort: sortParam,
vocation: voc,
page,
pageSize,
});
// Enriquecer com URL pública de imagem quando possível
const itemsWithImage = items.map((it: any) => {
const img = (it.image || "").toString();
// Preferir URL absoluta do banco para teste
if (/^https?:\/\//i.test(img)) {
return { ...it, image: img };
}
if (img.startsWith("data/")) {
return { ...it, image: `/api/images?path=${encodeURIComponent(img)}` };
}
const categoryEn = (it.category || "other").toString();
return { ...it, image: buildPublicItemImageUrl(categoryEn, img) };
});
const totalPages = Math.max(1, Math.ceil(total / Math.max(1, pageSize)));
return Response.json(
{ items: itemsWithImage, page, pageSize, total, totalPages },
{ status: 200 }
);
} catch (error: any) {
console.error("[api/items] Erro:", error?.message, error?.stack);
return Response.json(
{
error: "Falha ao consultar itens",
message: error?.message || "Erro desconhecido",
},
{ status: 500 }
);
}
}