From 743a07072406c567bf258f9ec851d7dc7eddfa2e Mon Sep 17 00:00:00 2001 From: Jason Fischer Date: Fri, 16 Sep 2022 11:33:11 -0700 Subject: [PATCH] Proposal to add ability to map data in a proxy --- .vscode/launch.json | 19 +++++++++++++++++++ src/pages/api/services/proxy.js | 23 +++++++++++++++++++++-- src/utils/proxies/generic.js | 9 +++++++-- 3 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 000000000..0e71c3ba1 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,19 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Next.js: debug full stack", + "type": "node", + "request": "launch", + "runtimeExecutable": "${workspaceFolder}/node_modules/.bin/next", + "serverReadyAction": { + "pattern": "started server on .+, url: (https?://.+)", + "uriFormat": "%s", + "action": "debugWithChrome" + } + } + ] +} \ No newline at end of file diff --git a/src/pages/api/services/proxy.js b/src/pages/api/services/proxy.js index 5cb1f678c..b2b9bcd05 100644 --- a/src/pages/api/services/proxy.js +++ b/src/pages/api/services/proxy.js @@ -5,6 +5,16 @@ import nzbgetProxyHandler from "utils/proxies/nzbget"; import npmProxyHandler from "utils/proxies/npm"; import transmissionProxyHandler from "utils/proxies/transmission"; +function simpleArrayMapper(endpoint, targetEndpoint, data, mapper) { + if ((data?.length > 0) && (endpoint === targetEndpoint)) { + const json = JSON.parse(data.toString()); + if (json instanceof Array) { + return json.map(mapper); + } + } + return data; +} + const serviceProxyHandlers = { // uses query param auth emby: genericProxyHandler, @@ -13,7 +23,9 @@ const serviceProxyHandlers = { radarr: genericProxyHandler, sonarr: genericProxyHandler, lidarr: genericProxyHandler, - readarr: genericProxyHandler, + readarr: { proxy: genericProxyHandler, mapper: (endpoint, data) => + simpleArrayMapper(endpoint, "book", data, d => ({ statistics: { bookFileCount: d.statistics.bookFileCount } })) + }, bazarr: genericProxyHandler, speedtest: genericProxyHandler, tautulli: genericProxyHandler, @@ -42,7 +54,14 @@ export default async function handler(req, res) { const serviceProxyHandler = serviceProxyHandlers[type]; if (serviceProxyHandler) { - return serviceProxyHandler(req, res); + if (serviceProxyHandler instanceof Function) { + return serviceProxyHandler(req, res); + } + + const { proxy, mapper } = serviceProxyHandler; + if (proxy) { + return proxy(req, res, mapper); + } } return res.status(403).json({ error: "Unkown proxy service type" }); diff --git a/src/utils/proxies/generic.js b/src/utils/proxies/generic.js index 9e0a6a676..628bfe6c5 100644 --- a/src/utils/proxies/generic.js +++ b/src/utils/proxies/generic.js @@ -2,7 +2,7 @@ import getServiceWidget from "utils/service-helpers"; import { formatApiCall } from "utils/api-helpers"; import { httpProxy } from "utils/http"; -export default async function genericProxyHandler(req, res) { +export default async function genericProxyHandler(req, res, mapper) { const { group, service, endpoint } = req.query; if (group && service) { @@ -23,13 +23,18 @@ export default async function genericProxyHandler(req, res) { headers, }); + let resultData = data; + if (mapper) { + resultData = mapper(endpoint, data); + } + if (contentType) res.setHeader("Content-Type", contentType); if (status === 204 || status === 304) { return res.status(status).end(); } - return res.status(status).send(data); + return res.status(status).send(resultData); } }