From 93445a28316fadd90e96f0f8a050ef52adb6e8a8 Mon Sep 17 00:00:00 2001 From: Michael Shamoon <4887959+shamoon@users.noreply.github.com> Date: Fri, 16 Dec 2022 22:33:15 -0800 Subject: [PATCH] Use credentialed proxy for miniflux --- src/utils/proxy/handlers/credentialed.js | 2 ++ src/widgets/miniflux/component.jsx | 2 +- src/widgets/miniflux/proxy.js | 45 ------------------------ src/widgets/miniflux/widget.js | 16 +++++++-- 4 files changed, 17 insertions(+), 48 deletions(-) delete mode 100644 src/widgets/miniflux/proxy.js diff --git a/src/utils/proxy/handlers/credentialed.js b/src/utils/proxy/handlers/credentialed.js index 4d2007ba..5d34264d 100644 --- a/src/utils/proxy/handlers/credentialed.js +++ b/src/utils/proxy/handlers/credentialed.js @@ -36,6 +36,8 @@ export default async function credentialedProxyHandler(req, res, map) { headers["X-API-Token"] = `${widget.key}`; } else if (widget.type === "tubearchivist") { headers.Authorization = `Token ${widget.key}`; + } else if (widget.type === "miniflux") { + headers["X-Auth-Token"] = `${widget.key}`; } else { headers["X-API-Key"] = `${widget.key}`; } diff --git a/src/widgets/miniflux/component.jsx b/src/widgets/miniflux/component.jsx index e68419e0..dbfd6048 100644 --- a/src/widgets/miniflux/component.jsx +++ b/src/widgets/miniflux/component.jsx @@ -9,7 +9,7 @@ export default function Component({ service }) { const { widget } = service; - const { data: minifluxData, error: minifluxError } = useWidgetAPI(widget); + const { data: minifluxData, error: minifluxError } = useWidgetAPI(widget, "counters"); if (minifluxError) { return ; diff --git a/src/widgets/miniflux/proxy.js b/src/widgets/miniflux/proxy.js deleted file mode 100644 index 9e6ac951..00000000 --- a/src/widgets/miniflux/proxy.js +++ /dev/null @@ -1,45 +0,0 @@ -import { formatApiCall } from "utils/proxy/api-helpers"; -import { httpProxy } from "utils/proxy/http"; -import getServiceWidget from "utils/config/service-helpers"; -import createLogger from "utils/logger"; - -const logger = createLogger("minifluxProxyHandler"); - -export default async function minifluxProxyHandler(req, res) { - const { group, service } = req.query; - - if (!group || !service) { - logger.debug("Invalid or missing service '%s' or group '%s'", service, group); - return res.status(400).json({ error: "Invalid proxy service type" }); - } - - const widget = await getServiceWidget(group, service); - if (!widget) { - logger.debug("Invalid or missing widget for service '%s' in group '%s'", service, group); - return res.status(400).json({ error: "Invalid proxy service type" }); - } - - const url = new URL(formatApiCall("{url}/v1/feeds/counters", { ...widget })); - url.username = widget.username; - url.password = widget.password; - - const params = { - method: "GET", - headers: { - "X-Auth-Token": widget.token - } - }; - - // eslint-disable-next-line no-unused-vars - const [status, contentType, data] = await httpProxy(url, params); - - let read = 0; - let unread = 0; - if (status === 200) { - const parsed = JSON.parse(data.toString()); - read = Object.values(parsed.reads).reduce((acc, i) => acc + i, 0); - unread = Object.values(parsed.unreads).reduce((acc, i) => acc + i, 0); - } - - return res.status(status).send({ read, unread }); -} diff --git a/src/widgets/miniflux/widget.js b/src/widgets/miniflux/widget.js index d98fee40..d4efbe2f 100644 --- a/src/widgets/miniflux/widget.js +++ b/src/widgets/miniflux/widget.js @@ -1,7 +1,19 @@ -import minifluxProxyHandler from "./proxy"; +import { asJson } from "utils/proxy/api-helpers"; +import credentialedProxyHandler from "utils/proxy/handlers/credentialed"; const widget = { - proxyHandler: minifluxProxyHandler, + api: "{url}/v1/{endpoint}", + proxyHandler: credentialedProxyHandler, + + mappings: { + counters: { + endpoint: "feeds/counters", + map: (data) => ({ + read: Object.values(asJson(data).reads).reduce((acc, i) => acc + i, 0), + unread: Object.values(asJson(data).unreads).reduce((acc, i) => acc + i, 0) + }), + }, + } }; export default widget;