Files
agent-bounty-protocol/packages/mcp-server/src/proxy.ts
2026-06-06 22:56:21 +08:00

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();
}