pre-commit fixes

This commit is contained in:
Karl Hudgell 2025-04-14 20:17:42 +01:00
parent 6e10127683
commit 926d3d5103
3 changed files with 91 additions and 95 deletions

View File

@ -1,8 +1,8 @@
/* eslint-disable no-underscore-dangle */ /* eslint-disable no-underscore-dangle */
import { formatApiCall } from "utils/proxy/api-helpers";
import { httpProxy } from "utils/proxy/http";
import getServiceWidget from "utils/config/service-helpers"; import getServiceWidget from "utils/config/service-helpers";
import createLogger from "utils/logger"; import createLogger from "utils/logger";
import { formatApiCall } from "utils/proxy/api-helpers";
import { httpProxy } from "utils/proxy/http";
import widgets from "widgets/widgets"; import widgets from "widgets/widgets";
const proxyName = "portainerProxyHandler"; const proxyName = "portainerProxyHandler";
@ -23,20 +23,19 @@ async function getWidget(req) {
return widget; return widget;
} }
async function getAllEnvIds(widget) { async function getAllEnvIds(widget) {
const api = widgets?.[widget.type]?.api; const api = widgets?.[widget.type]?.api;
if (!api) { if (!api) {
return [403, null]; return [403, null];
} }
const endpoint = 'endpoints' const endpoint = "endpoints";
let url = new URL(formatApiCall(api, { endpoint, ...widget })); let url = new URL(formatApiCall(api, { endpoint, ...widget }));
let [status, contentType, data] = await httpProxy(url, { let [status, contentType, data] = await httpProxy(url, {
method: "GET", method: "GET",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
'x-api-key': widget.key, "x-api-key": widget.key,
} },
}); });
if (status !== 200) { if (status !== 200) {
@ -51,27 +50,26 @@ async function getAllEnvIds(widget) {
logger.error("Error decoding NextPVR API data. Data: %s", data.toString()); logger.error("Error decoding NextPVR API data. Data: %s", data.toString());
return [status, null]; return [status, null];
} }
const ids = await dataAsJson.map(item => item.Id); const ids = await dataAsJson.map((item) => item.Id);
return ids; return ids;
} }
async function getAllContainers(ids, widget) { async function getAllContainers(ids, widget) {
let items = [] let items = [];
const api = widgets?.[widget.type]?.api; const api = widgets?.[widget.type]?.api;
if (!api) { if (!api) {
return [403, null]; return [403, null];
} }
for (let i = 0; i < ids.length; i++) { for (let i = 0; i < ids.length; i++) {
const endpoint = "endpoints/" + ids[i] + "/docker/containers/json?all=1" const endpoint = "endpoints/" + ids[i] + "/docker/containers/json?all=1";
let url = new URL(formatApiCall(api, { endpoint, ...widget })); let url = new URL(formatApiCall(api, { endpoint, ...widget }));
let [status, contentType, data] = await httpProxy(url, { let [status, contentType, data] = await httpProxy(url, {
method: "GET", method: "GET",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
'x-api-key': widget.key, "x-api-key": widget.key,
} },
}); });
if (status !== 200) { if (status !== 200) {
@ -82,7 +80,7 @@ async function getAllContainers(ids, widget) {
try { try {
const dataDecoded = data.toString(); const dataDecoded = data.toString();
dataAsJson = JSON.parse(dataDecoded); dataAsJson = JSON.parse(dataDecoded);
items.push(dataAsJson) items.push(dataAsJson);
} catch (e) { } catch (e) {
logger.error("Error decoding NextPVR API data. Data: %s", data.toString()); logger.error("Error decoding NextPVR API data. Data: %s", data.toString());
return [status, null]; return [status, null];
@ -94,9 +92,9 @@ async function getAllContainers(ids, widget) {
export default async function portainerProxyHandler(req, res) { export default async function portainerProxyHandler(req, res) {
try { try {
const widget = await getWidget(req); const widget = await getWidget(req);
let ids let ids;
if (('env' in widget)) { if ("env" in widget) {
ids = [widget.env]; ids = [widget.env];
} else { } else {
ids = await getAllEnvIds(widget); ids = await getAllEnvIds(widget);
@ -110,10 +108,8 @@ export default async function portainerProxyHandler(req, res) {
const total = containerList.length; const total = containerList.length;
return res.status(200).send({ running, stopped, total }); return res.status(200).send({ running, stopped, total });
} catch (error) { } catch (error) {
console.error("portainerProxyHandler error:", error); console.error("portainerProxyHandler error:", error);
return res.status(500).send({ error: "Internal Server Error" }); return res.status(500).send({ error: "Internal Server Error" });
} }
} }

View File

@ -6,8 +6,8 @@ const widget = {
mappings: { mappings: {
"docker/containers/json": { "docker/containers/json": {
endpoint: "endpoints/{env}/docker/containers/json" endpoint: "endpoints/{env}/docker/containers/json",
} },
}, },
}; };