Spaces:
Sleeping
Sleeping
File size: 2,364 Bytes
5b75521 4a77562 5b75521 4a77562 5b75521 | 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 | import { NextRequest } from "next/server";
/**
* Generic proxy function for Core API endpoints
*/
export async function proxyToCore(
request: NextRequest,
path: string,
options: {
method?: string;
body?: any;
} = {}
) {
const base = process.env.CORE_API_BASE || process.env.NEXT_PUBLIC_API_BASE;
if (!base) {
return {
error: "Core API base URL not configured",
status: 500,
};
}
const authHeader = request.headers.get("authorization");
const method = options.method || request.method;
const coreUrl = `${base.replace(/\/$/, "")}${path}`;
const timeoutMs = 10000; // 10 second timeout
try {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
const headers: Record<string, string> = {
"Content-Type": "application/json",
};
if (authHeader) {
headers["Authorization"] = authHeader;
}
const fetchOptions: RequestInit = {
method,
cache: "no-store",
signal: controller.signal,
headers,
};
// Handle body - options.body takes priority, otherwise try to read from request
if (options.body && (method === "POST" || method === "PUT" || method === "PATCH")) {
fetchOptions.body = JSON.stringify(options.body);
} else if (method === "POST" || method === "PUT" || method === "PATCH") {
// Try to clone request to read body (if not already consumed)
try {
const clonedRequest = request.clone();
const body = await clonedRequest.json().catch(() => null);
if (body && Object.keys(body).length > 0) {
fetchOptions.body = JSON.stringify(body);
}
} catch {
// Body already consumed or not available, skip
}
}
const res = await fetch(coreUrl, fetchOptions);
clearTimeout(timeoutId);
const text = await res.text();
let data: any;
try {
data = text ? JSON.parse(text) : {};
} catch {
data = { raw: text };
}
return {
data,
status: res.status,
ok: res.ok,
};
} catch (e: any) {
if (e.name === "AbortError") {
return {
error: `Request timed out after ${timeoutMs}ms`,
status: 504,
};
}
return {
error: e?.message ?? "Failed to connect to Core service",
status: 502,
};
}
}
|