feat: sidebar IT con acciones reales (agregar/actualizar/eliminar + proxy deployer)
This commit is contained in:
1 parent
e20e9d6657
commit
23df440b79
5 files changed
+398
-17
No files matched your search
@@ -0,0 +1,79 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
const DEPLOYER_URL = process.env.DEPLOYER_URL || "http://127.0.0.1:3010";
|
||||
const DEPLOYER_TOKEN = process.env.DEPLOYER_TOKEN || "";
|
||||
|
||||
// Proxy genérico al deployer: /api/deployer/<path>?<query>
|
||||
// Métodos: GET, POST, PUT, PATCH, DELETE. Permite exponer
|
||||
// futuras acciones sin agregar rutas (deploy, webhook, backend...).
|
||||
async function proxy(req: NextRequest, path: string[]) {
|
||||
const headers: Record<string, string> = {
|
||||
"Content-Type": "application/json",
|
||||
};
|
||||
if (DEPLOYER_TOKEN) headers["X-API-Token"] = DEPLOYER_TOKEN;
|
||||
try {
|
||||
const qs = req.nextUrl.search || "";
|
||||
const init: RequestInit = {
|
||||
method: req.method,
|
||||
headers,
|
||||
cache: "no-store",
|
||||
};
|
||||
if (req.method !== "GET" && req.method !== "DELETE") {
|
||||
try {
|
||||
init.body = JSON.stringify(await req.json());
|
||||
} catch {
|
||||
init.body = undefined;
|
||||
}
|
||||
}
|
||||
const res = await fetch(
|
||||
`${DEPLOYER_URL}/api/${path.join("/")}${qs}`,
|
||||
init
|
||||
);
|
||||
const text = await res.text();
|
||||
try {
|
||||
return NextResponse.json(JSON.parse(text), { status: res.status });
|
||||
} catch {
|
||||
return new NextResponse(text, { status: res.status });
|
||||
}
|
||||
} catch (e) {
|
||||
return NextResponse.json(
|
||||
{ error: e instanceof Error ? e.message : "Error al conectar con deployer" },
|
||||
{ status: 502 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function GET(
|
||||
req: NextRequest,
|
||||
{ params }: { params: Promise<{ slug: string[] }> }
|
||||
) {
|
||||
return proxy(req, (await params).slug);
|
||||
}
|
||||
|
||||
export async function POST(
|
||||
req: NextRequest,
|
||||
{ params }: { params: Promise<{ slug: string[] }> }
|
||||
) {
|
||||
return proxy(req, (await params).slug);
|
||||
}
|
||||
|
||||
export async function PUT(
|
||||
req: NextRequest,
|
||||
{ params }: { params: Promise<{ slug: string[] }> }
|
||||
) {
|
||||
return proxy(req, (await params).slug);
|
||||
}
|
||||
|
||||
export async function PATCH(
|
||||
req: NextRequest,
|
||||
{ params }: { params: Promise<{ slug: string[] }> }
|
||||
) {
|
||||
return proxy(req, (await params).slug);
|
||||
}
|
||||
|
||||
export async function DELETE(
|
||||
req: NextRequest,
|
||||
{ params }: { params: Promise<{ slug: string[] }> }
|
||||
) {
|
||||
return proxy(req, (await params).slug);
|
||||
}
|
||||
Reference in new issue
Block a user