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

56 lines
2.0 KiB
Markdown

# F-127 — DELETE brands/categories falla con 502 desde el admin
## Diagnóstico
Reproducción:
1. Crear categoría de prueba → backend OK
2. `DELETE /api/categories/<id>` desde admin (vía proxy) → **HTTP 502 "Proxy error"**
3. Pero la categoría SÍ se elimina de la DB
Causa raíz en `apps/admin/src/app/api/[...path]/route.ts`:
```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 })`).
```ts
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
1. Editar `apps/admin/src/app/api/[...path]/route.ts` — cambiar el `return NextResponse.json(...)` por `new Response(backendRes.body, ...)` en DELETE.
2. `cd apps/admin && npx tsc --noEmit`.
3. `cd apps/admin && npm run build`.
4. Verificar con `curl -X DELETE /api/categories/<id>` → 204 + categoría borrada.
5. Cerrar gates.