/** * Catch-all proxy: /api/engine/channels/:channelId/* → engine :3035/api/channels/:channelId/* * * Используется AutogenTab и CategoryTab для: * - GET/POST/PATCH/DELETE /api/engine/channels/:id/categories * - GET/POST/PATCH/DELETE /api/engine/channels/:id/categories/:catId/topics * - GET/POST/PATCH /api/engine/channels/:id/autogen * - POST /api/engine/channels/:id/autogen/run * - GET /api/engine/channels/:id/autogen/today * - GET /api/engine/channels/:id/autogen/rotation */ import { NextResponse } from 'next/server'; import { requireUser } from '@/lib/session'; const ENGINE = process.env.ENGINE_URL || 'http://127.0.0.1:3030'; const SECRET = process.env.ENGINE_SECRET || 'zeropost_internal_2026'; async function proxy(req, { params }) { const user = await requireUser(); if (!user) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); const { channelId, path: pathArr } = await params; const tail = pathArr?.length ? '/' + pathArr.join('/') : ''; const qs = req.url.split('?')[1]; const url = `${ENGINE}/api/channels/${channelId}${tail}${qs ? '?' + qs : ''}`; const headers = { 'Content-Type': 'application/json', 'x-internal-secret': SECRET, 'x-user-id': String(user.id), }; let body; if (req.method !== 'GET' && req.method !== 'HEAD') { body = await req.text(); } try { const res = await fetch(url, { method: req.method, headers, body, cache: 'no-store' }); const data = await res.json().catch(() => ({ error: 'invalid engine response' })); return NextResponse.json(data, { status: res.status }); } catch (err) { console.error('[engine proxy] error:', err.message); return NextResponse.json({ error: err.message }, { status: 502 }); } } export const GET = proxy; export const POST = proxy; export const PATCH = proxy; export const PUT = proxy; export const DELETE = proxy;