27 lines
624 B
TypeScript
27 lines
624 B
TypeScript
export async function proxyToBackend(
|
|
endpoint: string,
|
|
payload: any,
|
|
baseUrl?: string,
|
|
apiKey?: string
|
|
) {
|
|
if (!baseUrl) throw new Error("API_BASE_URL is not configured.");
|
|
|
|
const url = `${baseUrl.replace(/\/$/, "")}${endpoint}`;
|
|
|
|
const response = await fetch(url, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
"Authorization": `Bearer ${apiKey || ""}`,
|
|
},
|
|
body: JSON.stringify(payload),
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const text = await response.text();
|
|
throw new Error(`Backend error (${response.status}): ${text}`);
|
|
}
|
|
|
|
return response.json();
|
|
}
|