import {Customer} from "@/services/customers/entities/customer"; export async function validateCustomer(input: { email: string; companyName: string; street: string; zip: string; city: string; }): Promise { if (Object.values(input).every(value => !value?.trim())) { return []; } const res = await fetch('/api/customers/validate', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(input), }); const contentType = res.headers.get("content-type") ?? ""; const isJson = contentType.includes("application/json"); const rawBody = isJson ? await res.json() : await res.text(); console.log(`[api /api/customers/validate] Response:`, res.status, rawBody); if (!res.ok) { const errorMessage = isJson ? (rawBody?.message ?? rawBody?.errors?.join(", ")) ?? "Unbekannter Fehler" : String(rawBody); // Log correlation ID if available for debugging if (isJson && rawBody?.correlationId) { console.error(`[api /api/customers/validate] Error occurred [${rawBody.correlationId}]:`, errorMessage); } throw new Error(errorMessage); } return rawBody as Customer[]; }