From c411c7da7a6c84d842ac431fe1212b68bf147c75 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Fri, 24 Nov 2023 23:29:38 -0800 Subject: [PATCH] Calendar proxy handler --- src/utils/proxy/handlers/generic.js | 7 +----- src/widgets/calendar/proxy.js | 33 +++++++++++++++++++++++++++++ src/widgets/calendar/widget.js | 4 ++-- src/widgets/ical/widget.js | 8 ------- src/widgets/widgets.js | 4 +--- 5 files changed, 37 insertions(+), 19 deletions(-) create mode 100644 src/widgets/calendar/proxy.js delete mode 100644 src/widgets/ical/widget.js diff --git a/src/utils/proxy/handlers/generic.js b/src/utils/proxy/handlers/generic.js index 0de36f1d..8b91049f 100644 --- a/src/utils/proxy/handlers/generic.js +++ b/src/utils/proxy/handlers/generic.js @@ -18,11 +18,9 @@ export default async function genericProxyHandler(req, res, map) { } if (widget) { - const integration = widget?.integrations?.find((i) => i.name === endpoint) ?? {}; - // if there are more than one question marks, replace others to & const url = new URL( - formatApiCall(widgets[widget.type].api, { endpoint, ...widget, ...integration }).replace(/(?<=\?.*)\?/g, "&"), + formatApiCall(widgets[widget.type].api, { endpoint, ...widget }).replace(/(?<=\?.*)\?/g, "&"), ); const headers = req.extraHeaders ?? widget.headers ?? {}; @@ -48,9 +46,6 @@ export default async function genericProxyHandler(req, res, map) { } if (status === 200) { - if (integration?.url) { - return res.status(status).json({ data: resultData.toString() }); - } if (!validateWidgetData(widget, endpoint, resultData)) { return res .status(status) diff --git a/src/widgets/calendar/proxy.js b/src/widgets/calendar/proxy.js new file mode 100644 index 00000000..996ea324 --- /dev/null +++ b/src/widgets/calendar/proxy.js @@ -0,0 +1,33 @@ +import getServiceWidget from "utils/config/service-helpers"; +import { httpProxy } from "utils/proxy/http"; +import createLogger from "utils/logger"; + +const logger = createLogger("calendarProxyHandler"); + +export default async function calendarProxyHandler(req, res) { + const { group, service, endpoint } = req.query; + + if (group && service) { + const widget = await getServiceWidget(group, service); + const integration = widget.integrations?.find((i) => i.name === endpoint); + + if (integration) { + if (!integration.url) { + return res.status(403).json({ error: "No integration URL specified" }); + } + + const [status, contentType, data] = await httpProxy(integration.url); + + if (contentType) res.setHeader("Content-Type", contentType); + + if (status !== 200) { + logger.debug(`HTTTP ${status} retrieving data from integration URL ${integration.url} : ${data}`); + return res.status(status).send(data); + } + + return res.status(status).json({ data: data.toString() }); + } + } + + return res.status(400).json({ error: "Invalid integration" }); +} diff --git a/src/widgets/calendar/widget.js b/src/widgets/calendar/widget.js index 9fdc21d9..ef382f37 100644 --- a/src/widgets/calendar/widget.js +++ b/src/widgets/calendar/widget.js @@ -1,8 +1,8 @@ -import genericProxyHandler from "utils/proxy/handlers/generic"; +import calendarProxyHandler from "./proxy"; const widget = { api: "{url}", - proxyHandler: genericProxyHandler, + proxyHandler: calendarProxyHandler, }; export default widget; diff --git a/src/widgets/ical/widget.js b/src/widgets/ical/widget.js deleted file mode 100644 index 9fdc21d9..00000000 --- a/src/widgets/ical/widget.js +++ /dev/null @@ -1,8 +0,0 @@ -import genericProxyHandler from "utils/proxy/handlers/generic"; - -const widget = { - api: "{url}", - proxyHandler: genericProxyHandler, -}; - -export default widget; diff --git a/src/widgets/widgets.js b/src/widgets/widgets.js index f967825f..0a2d24ed 100644 --- a/src/widgets/widgets.js +++ b/src/widgets/widgets.js @@ -31,7 +31,6 @@ import hdhomerun from "./hdhomerun/widget"; import homeassistant from "./homeassistant/widget"; import homebridge from "./homebridge/widget"; import healthchecks from "./healthchecks/widget"; -import ical from "./ical/widget"; import immich from "./immich/widget"; import jackett from "./jackett/widget"; import jellyseerr from "./jellyseerr/widget"; @@ -113,7 +112,6 @@ const widgets = { changedetectionio, channelsdvrserver, cloudflared, - calendar, coinmarketcap, customapi, deluge, @@ -134,7 +132,7 @@ const widgets = { homeassistant, homebridge, healthchecks, - ical, + ical: calendar, immich, jackett, jdownloader,