fix(items): normalize item fields and improve data integrity checks

Ensure consistent field names and types between API and client components. Add fallback values for missing fields and implement better error logging for invalid items. Improve key generation for item rendering to prevent duplicate keys.
This commit is contained in:
Evandro Fonseca Junior committed 2025-11-11 03:29:10 -03:00
1 parent 6adc8d96e8
commit b8a8d3cc17
2 files changed
+69 -15

No files matched your search

+26 -10
View File
@@ -43,23 +43,39 @@ export async function GET(req: NextRequest) {
pageSize,
});
// Normaliza campos esperados pelo cliente, preservando compatibilidade
const itemsWithImage = items.map((item) => {
const img = (item.image_url || item.image_local_path || "").toString();
if (/^https?:\/\//i.test(img)) {
return { ...item, image_url: img, image: img };
const imgRaw = (item.image_url || item.image_local_path || "").toString();
const categorySafe = item.category || "other";
const idSafe = typeof item.item_id === "number" ? item.item_id : undefined;
const nameSafe =
typeof (item as any).item_name === "string" && (item as any).item_name
? (item as any).item_name
: undefined;
const base = {
...item,
// Aliases usados pelos clientes públicos (Library, etc.)
id: idSafe,
name: nameSafe,
category: categorySafe,
} as any;
if (/^https?:\/\//i.test(imgRaw)) {
return { ...base, image_url: imgRaw, image: imgRaw };
}
if (img.startsWith("data/")) {
if (imgRaw.startsWith("data/")) {
return {
...item,
image_url: img,
image: `/api/images?path=${encodeURIComponent(img)}`,
...base,
image_url: imgRaw,
image: `/api/images?path=${encodeURIComponent(imgRaw)}`,
};
}
const category = item.category || "other";
return {
...item,
...base,
image_url: item.image_url,
image: buildPublicItemImageUrl(category, img),
image: buildPublicItemImageUrl(categorySafe, imgRaw),
};
});