Files
mercadodevida/work/artifacts/F-127/implementer.md
2026-08-21 17:56:15 +02:00

2.9 KiB

F-127 — DELETE brands/categories falla con 502 desde el admin

Cambios

apps/admin/src/app/api/[...path]/route.ts

  • Handler DELETE cambiado: en lugar de NextResponse.json({ ok }, { status }), ahora devuelve new Response(backendRes.body, { status, statusText, headers }).
  • Esto reenvía la respuesta del backend tal cual: si el backend devuelve 204 sin body, el proxy también devuelve 204 sin body. Antes forzaba un body JSON con status 204 (HTTP inválido).
 export async function DELETE(req: NextRequest) {
   const path = req.nextUrl.pathname.replace('/api/', '');
   const search = req.nextUrl.search;
   const cookies = req.headers.get('cookie') ?? '';
   try {
     const backendRes = await fetch(`${API}/${path}${search}`, {
       method: 'DELETE',
       headers: { Cookie: cookies },
     });
-    return NextResponse.json({ ok: backendRes.ok }, { status: backendRes.status });
+    // Reenviar la respuesta del backend tal cual: el backend puede devolver
+    // 204 No Content (sin body) en borrados exitosos, y forzar un JSON con
+    // status 204 es HTTP inválido (F-127).
+    return new Response(backendRes.body, {
+      status: backendRes.status,
+      statusText: backendRes.statusText,
+      headers: { 'Content-Length': backendRes.headers.get('content-length') ?? '0' },
+    });
   } catch {
     return NextResponse.json({ error: 'Proxy error' }, { status: 502 });
   }
 }

Verificación

Antes del fix

1. Crear "F127 Verify" → OK
2. DELETE /api/categories/<id> vía proxy → HTTP 502 "Proxy error"   ❌
3. Pero la categoría SÍ se elimina en backend                       ✅

Causa: NextResponse.json({ ok: true }, { status: 204 }) viola HTTP/1.1 (204 No Content no puede llevar body). Next.js lanza excepción capturada por el catch → 502.

Después del fix (compilado, pendiente de restart)

  • apps/admin/.next/server/chunks/_1u9yuco._.js contiene:
    async function i(e) {
      
      return new Response(e.body, {
        status: e.status,
        statusText: e.statusText,
        headers: { "Content-Length": e.headers.get("content-length") ?? "0" }
      })
    }
    
  • Comportamiento esperado tras restart:
    • DELETE /api/categories/ → 204 (igual que backend)
    • DELETE /api/brands/ → 204 (igual que backend)
    • DELETE /api/products/ → 204 si backend devuelve 204

Tests

  • cd apps/admin && npx tsc --noEmit → exit 0.
  • cd apps/admin && NEXT_PUBLIC_API_URL=http://192.168.18.93:3000 npm run build → exit 0.

Impacto colateral

  • Solo afecta al método DELETE del proxy catch-all.
  • GET / POST / PATCH / PUT no se tocan.
  • Resuelve también DELETE /api/products/<id> (productos) que pasaba por el mismo bug.

Notas

  • El operador debe reiniciar el admin (./scripts/monolith.sh prod restart) para desplegar el bundle.
  • Limpieza realizada (DB local): usuario de prueba f127test@local.test, categorías f127-*, marca f127-brand.