Docker images / prepare-release (push) Successful in 6s
Docker images / backend-image (push) Successful in 1m36s
Docker images / frontend-image (push) Successful in 1m39s
Docker images / updater-image (push) Successful in 50s
Docker images / notify-webhook (push) Has been skipped
Docker images / publish-release (push) Successful in 8s
51 lines
1.7 KiB
TypeScript
51 lines
1.7 KiB
TypeScript
import type { NextRequest } from "next/server"
|
|
import { backendInternalUrl } from "@/lib/backend-internal-url"
|
|
|
|
type ProxyBackendRequestOptions = {
|
|
method?: string
|
|
forwardRequestBody?: boolean
|
|
}
|
|
|
|
export async function proxyBackendRequest(
|
|
request: NextRequest,
|
|
backendPath: string,
|
|
options: ProxyBackendRequestOptions = {},
|
|
): Promise<Response> {
|
|
const method = options.method ?? request.method
|
|
const headers = new Headers()
|
|
for (const [key, value] of request.headers.entries()) {
|
|
const lower = key.toLowerCase()
|
|
if (lower === "host" || lower === "connection") continue
|
|
headers.set(key, value)
|
|
}
|
|
|
|
const fetchInit: RequestInit & { duplex?: "half" } = {
|
|
method,
|
|
headers,
|
|
cache: "no-store",
|
|
}
|
|
|
|
const forwardRequestBody = options.forwardRequestBody ?? !["GET", "HEAD"].includes(method)
|
|
if (forwardRequestBody && request.body) {
|
|
fetchInit.body = request.body
|
|
fetchInit.duplex = "half"
|
|
}
|
|
|
|
try {
|
|
const upstream = await fetch(`${backendInternalUrl()}${backendPath}`, fetchInit)
|
|
const responseHeaders = new Headers()
|
|
const contentType = upstream.headers.get("Content-Type")
|
|
const contentDisposition = upstream.headers.get("Content-Disposition")
|
|
if (contentType) responseHeaders.set("Content-Type", contentType)
|
|
if (contentDisposition) responseHeaders.set("Content-Disposition", contentDisposition)
|
|
|
|
return new Response(upstream.body, {
|
|
status: upstream.status,
|
|
headers: responseHeaders,
|
|
})
|
|
} catch (error) {
|
|
const message = error instanceof Error ? error.message : "Не удалось связаться с бекендом"
|
|
return Response.json({ error: message }, { status: 502 })
|
|
}
|
|
}
|