diff --git a/src/lib/api/product.ts b/src/lib/api/product.ts index dd79ea5..78a65f5 100644 --- a/src/lib/api/product.ts +++ b/src/lib/api/product.ts @@ -1,68 +1,21 @@ -interface ApiProduct { +export interface Product { id: string; name: string; - price: number; - description: string; + price: string; imageSrc: string; - category: string; + imageAlt?: string; + description?: string; } -const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || "https://api.example.com"; - -export const fetchProducts = async (): Promise => { +export async function fetchProducts(): Promise { try { - const response = await fetch(`${API_BASE_URL}/products`); + const response = await fetch('/api/products'); if (!response.ok) { - throw new Error(`API error: ${response.status}`); + throw new Error('Failed to fetch products'); } - return await response.json(); - } catch (err) { - console.error("Failed to fetch products:", err); + return response.json(); + } catch (error) { + console.error('Error fetching products:', error); return []; } -}; - -export const fetchProductById = async (id: string): Promise => { - try { - const response = await fetch(`${API_BASE_URL}/products/${id}`); - if (!response.ok) { - throw new Error(`API error: ${response.status}`); - } - return await response.json(); - } catch (err) { - console.error(`Failed to fetch product ${id}:`, err); - return null; - } -}; - -export const searchProducts = async (query: string): Promise => { - try { - const response = await fetch( - `${API_BASE_URL}/products/search?q=${encodeURIComponent(query)}` - ); - if (!response.ok) { - throw new Error(`API error: ${response.status}`); - } - return await response.json(); - } catch (err) { - console.error("Failed to search products:", err); - return []; - } -}; - -export const fetchProductsByCategory = async ( - category: string -): Promise => { - try { - const response = await fetch( - `${API_BASE_URL}/products/category/${category}` - ); - if (!response.ok) { - throw new Error(`API error: ${response.status}`); - } - return await response.json(); - } catch (err) { - console.error(`Failed to fetch products in category ${category}:`, err); - return []; - } -}; +}