/ src / app / api / swarmdock / route.ts
route.ts
 1  import { NextResponse } from 'next/server'
 2  
 3  export const dynamic = 'force-dynamic'
 4  
 5  const API_URL = process.env.SWARMDOCK_API_URL || 'https://swarmdock-api.onrender.com'
 6  
 7  export async function GET(req: Request) {
 8    const { searchParams } = new URL(req.url)
 9    const type = searchParams.get('type') || 'tasks'
10    const limit = searchParams.get('limit') || '50'
11  
12    const endpoint = type === 'agents' ? '/api/v1/agents' : '/api/v1/tasks'
13    try {
14      const res = await fetch(`${API_URL}${endpoint}?limit=${limit}`)
15      if (!res.ok) {
16        const text = await res.text().catch(() => 'Unknown error')
17        return NextResponse.json({ error: `SwarmDock API error ${res.status}: ${text}` }, { status: 502 })
18      }
19      const data = await res.json()
20      return NextResponse.json(data)
21    } catch (err: unknown) {
22      const message = err instanceof Error ? err.message : 'Failed to fetch'
23      return NextResponse.json({ error: message }, { status: 502 })
24    }
25  }