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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | // API client functions for products export interface ProductFilters { page?: number; limit?: number; categoryId?: number; search?: string; minPrice?: number; maxPrice?: number; } export interface Product { id: number; title: string; description: string | null; price: number; discountedPrice: number; stock: number; sku: string | null; category: { id: number; title: string; }; reviews: number; averageRating: number; imgs: { thumbnails: string[]; previews: string[]; }; createdAt: Date | string; updatedAt: Date | string; } export interface ProductResponse { products: Product[]; pagination: { page: number; limit: number; total: number; totalPages: number; }; } /** * Helper to extract data from API response */ function extractData<T>(result: { success?: boolean; data?: T } | T): T { if (result && typeof result === 'object' && 'data' in result) { return result.data as T; } return result as T; } /** * Fetch all products with optional filters */ export async function getProducts(filters: ProductFilters = {}): Promise<ProductResponse> { const params = new URLSearchParams(); if (filters.page) params.append("page", filters.page.toString()); if (filters.limit) params.append("limit", filters.limit.toString()); if (filters.categoryId) params.append("categoryId", filters.categoryId.toString()); if (filters.search) params.append("search", filters.search); if (filters.minPrice) params.append("minPrice", filters.minPrice.toString()); if (filters.maxPrice) params.append("maxPrice", filters.maxPrice.toString()); const queryString = params.toString(); const url = `/api/products${queryString ? `?${queryString}` : ""}`; const response = await fetch(url); if (!response.ok) { throw new Error("Failed to fetch products"); } const result = await response.json(); return extractData<ProductResponse>(result); } /** * Fetch a single product by ID */ export async function getProductById(id: number): Promise<Product> { const response = await fetch(`/api/products/${id}`); if (!response.ok) { throw new Error("Failed to fetch product"); } const result = await response.json(); return extractData<Product>(result); } export interface CreateProductData { title: string; description?: string; price: number; discountedPrice: number; stock: number; sku?: string; categoryId: number; images?: Array<{ url: string; type: string }>; } export interface UpdateProductData { title?: string; description?: string; price?: number; discountedPrice?: number; stock?: number; sku?: string; categoryId?: number; } /** * Create a new product (admin only) */ export async function createProduct(productData: CreateProductData): Promise<Product> { const response = await fetch("/api/products", { method: "POST", headers: { "Content-Type": "application/json"}, body: JSON.stringify(productData)}); if (!response.ok) { throw new Error("Failed to create product"); } const result = await response.json(); return extractData<Product>(result); } /** * Update a product (admin only) */ export async function updateProduct(id: number, productData: UpdateProductData): Promise<Product> { const response = await fetch(`/api/products/${id}`, { method: "PATCH", headers: { "Content-Type": "application/json"}, body: JSON.stringify(productData)}); if (!response.ok) { throw new Error("Failed to update product"); } const result = await response.json(); return extractData<Product>(result); } /** * Delete a product (admin only) */ export async function deleteProduct(id: number): Promise<{ message: string }> { const response = await fetch(`/api/products/${id}`, { method: "DELETE"}); if (!response.ok) { throw new Error("Failed to delete product"); } const result = await response.json(); return extractData<{ message: string }>(result); } |