2.0 KiB
F-127 — DELETE brands/categories falla con 502 desde el admin
Diagnóstico
Reproducción:
- Crear categoría de prueba → backend OK
DELETE /api/categories/<id>desde admin (vía proxy) → HTTP 502 "Proxy error"- Pero la categoría SÍ se elimina de la DB
Causa raíz en apps/admin/src/app/api/[...path]/route.ts:
export async function DELETE(req: NextRequest) {
…
try {
const backendRes = await fetch(`${API}/${path}${search}`, {
method: 'DELETE',
headers: { Cookie: cookies },
});
return NextResponse.json({ ok: backendRes.ok }, { status: backendRes.status });
} catch {
return NextResponse.json({ error: 'Proxy error' }, { status: 502 });
}
}
El backend DELETE /categories/:id y DELETE /brands/:id devuelven HTTP 204 No Content (sin body, comportamiento correcto REST).
Pero el proxy hace NextResponse.json({ ok: true }, { status: 204 }). Esto es HTTP inválido — 204 No Content NO puede llevar body. Next.js lanza una excepción que es capturada por el catch y devuelta como 502 "Proxy error".
Resultado para el operador: el botón "Eliminar" muestra error, pero el borrado sí ocurre en backend. La UI queda desincronizada: refresh manual muestra que se borró.
Diseño
Cambiar el handler DELETE para no enviar body cuando el backend devuelve 204. Estrategia: reenviar la respuesta del backend tal cual (new Response(backendRes.body, { status })).
return new Response(backendRes.body, {
status: backendRes.status,
statusText: backendRes.statusText,
});
Esto funciona para 204 (body null) y para otros códigos con body.
Impacto colateral
Solo afecta al método DELETE del proxy. GET / POST / PATCH / PUT no se tocan.
Plan
- Editar
apps/admin/src/app/api/[...path]/route.ts— cambiar elreturn NextResponse.json(...)pornew Response(backendRes.body, ...)en DELETE. cd apps/admin && npx tsc --noEmit.cd apps/admin && npm run build.- Verificar con
curl -X DELETE /api/categories/<id>→ 204 + categoría borrada. - Cerrar gates.