refactor(actions): enhance error handling and ensure serializability in product actions
This commit is contained in:
@@ -5,50 +5,68 @@ import { getAuthHeaders } from '../../lib/auth-headers';
|
||||
const API_BASE = process.env.NEXT_PUBLIC_API_URL_INTERNAL || 'http://recipe-api:8080';
|
||||
|
||||
export async function createProductAction(name: string) {
|
||||
const authHeaders = await getAuthHeaders();
|
||||
console.log('[createProductAction] Creating product with name:', name);
|
||||
console.log('[createProductAction] Auth headers:', authHeaders ? 'YES' : 'NO');
|
||||
try {
|
||||
const authHeaders = await getAuthHeaders();
|
||||
console.log('[createProductAction] Creating product with name:', name);
|
||||
console.log('[createProductAction] Auth headers:', authHeaders ? 'YES' : 'NO');
|
||||
|
||||
const res = await fetch(`${API_BASE}/api/products`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', ...authHeaders },
|
||||
body: JSON.stringify({ name }),
|
||||
});
|
||||
const res = await fetch(`${API_BASE}/api/products`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', ...authHeaders },
|
||||
body: JSON.stringify({ name }),
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const e = await res.json().catch(() => ({}));
|
||||
throw new Error(e.message ?? `HTTP ${res.status}`);
|
||||
if (!res.ok) {
|
||||
const e = await res.json().catch(() => ({}));
|
||||
throw new Error(e.message ?? `HTTP ${res.status}`);
|
||||
}
|
||||
|
||||
const product = await res.json();
|
||||
console.log('[createProductAction] Response:', product);
|
||||
|
||||
// Explicitly convert to plain object to ensure serializability
|
||||
const result = JSON.parse(JSON.stringify({
|
||||
id: product.id,
|
||||
name: product.name,
|
||||
canonicalName: product.canonicalName ?? null,
|
||||
}));
|
||||
|
||||
console.log('[createProductAction] Returning:', result);
|
||||
return result;
|
||||
} catch (err) {
|
||||
console.error('[createProductAction] Error:', err);
|
||||
throw err;
|
||||
}
|
||||
|
||||
const product = await res.json();
|
||||
// Return only serializable fields (Dates can't be serialized across RSC boundary)
|
||||
return {
|
||||
id: product.id,
|
||||
name: product.name,
|
||||
canonicalName: product.canonicalName,
|
||||
};
|
||||
}
|
||||
|
||||
export async function updateProductCategoryAction(productId: number, categoryId: number) {
|
||||
const authHeaders = await getAuthHeaders();
|
||||
try {
|
||||
const authHeaders = await getAuthHeaders();
|
||||
|
||||
const res = await fetch(`${API_BASE}/api/products/${productId}`, {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json', ...authHeaders },
|
||||
body: JSON.stringify({ categoryId }),
|
||||
});
|
||||
const res = await fetch(`${API_BASE}/api/products/${productId}`, {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json', ...authHeaders },
|
||||
body: JSON.stringify({ categoryId }),
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const e = await res.json().catch(() => ({}));
|
||||
throw new Error(e.message ?? `HTTP ${res.status}`);
|
||||
if (!res.ok) {
|
||||
const e = await res.json().catch(() => ({}));
|
||||
throw new Error(e.message ?? `HTTP ${res.status}`);
|
||||
}
|
||||
|
||||
const product = await res.json();
|
||||
|
||||
// Explicitly convert to plain object to ensure serializability
|
||||
const result = JSON.parse(JSON.stringify({
|
||||
id: product.id,
|
||||
name: product.name,
|
||||
canonicalName: product.canonicalName ?? null,
|
||||
categoryId: product.categoryId ?? null,
|
||||
}));
|
||||
|
||||
return result;
|
||||
} catch (err) {
|
||||
console.error('[updateProductCategoryAction] Error:', err);
|
||||
throw err;
|
||||
}
|
||||
|
||||
const product = await res.json();
|
||||
// Return only serializable fields
|
||||
return {
|
||||
id: product.id,
|
||||
name: product.name,
|
||||
canonicalName: product.canonicalName,
|
||||
categoryId: product.categoryId,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user