// Copyright (c) 2026 Gordon Bolton. MIT License. const express = require('express'); const axios = require('axios'); const router = express.Router(); const requireAuth = require('../middleware/requireAuth'); const sanitizeError = require('../utils/sanitizeError'); const { getSABnzbdInstances } = require('../utils/config'); // Helper to get first SABnzbd instance function getFirstSABnzbdInstance() { const instances = getSABnzbdInstances(); if (!instances || instances.length === 0) { return null; } return instances[0]; } /** * @openapi * /api/sabnzbd/queue: * get: * tags: [SABnzbd] * summary: Get SABnzbd queue * description: Proxy to SABnzbd's queue endpoint. Requires authentication. * security: * - CookieAuth: [] * responses: * '200': * description: Queue data from SABnzbd * content: * application/json: * schema: * type: object * '500': * description: Proxy error * content: * application/json: * schema: * $ref: '#/components/schemas/ErrorResponse' */ router.use(requireAuth); // GET /api/sabnzbd/queue router.get('/queue', async (req, res) => { const instance = getFirstSABnzbdInstance(); if (!instance) { return res.status(503).json({ error: 'SABnzbd not configured' }); } try { const response = await axios.get(`${instance.url}/api`, { params: { mode: 'queue', apikey: instance.apiKey, output: 'json' } }); res.json(response.data); } catch (error) { res.status(500).json({ error: 'Failed to fetch SABnzbd queue', details: sanitizeError(error) }); } }); /** * @openapi * /api/sabnzbd/history: * get: * tags: [SABnzbd] * summary: Get SABnzbd history * description: Proxy to SABnzbd's history endpoint. Requires authentication. * security: * - CookieAuth: [] * parameters: * - name: limit * in: query * schema: * type: integer * default: 50 * description: Number of history records to return * responses: * '200': * description: History data from SABnzbd * content: * application/json: * schema: * type: object */ // GET /api/sabnzbd/history router.get('/history', async (req, res) => { const instance = getFirstSABnzbdInstance(); if (!instance) { return res.status(503).json({ error: 'SABnzbd not configured' }); } try { const response = await axios.get(`${instance.url}/api`, { params: { mode: 'history', apikey: instance.apiKey, output: 'json', limit: req.query.limit || 50 } }); res.json(response.data); } catch (error) { res.status(500).json({ error: 'Failed to fetch SABnzbd history', details: sanitizeError(error) }); } }); module.exports = router;