mirror of
https://github.com/karl0ss/homepage.git
synced 2025-04-29 12:03:41 +01:00
Cleanup data validation
This commit is contained in:
parent
8476b97f7d
commit
a83d105764
@ -67,6 +67,10 @@ export default async function credentialedProxyHandler(req, res, map) {
|
||||
|
||||
let resultData = data;
|
||||
|
||||
if (resultData.error?.url) {
|
||||
resultData.error.url = sanitizeErrorURL(url);
|
||||
}
|
||||
|
||||
if (status === 204 || status === 304) {
|
||||
return res.status(status).end();
|
||||
}
|
||||
@ -75,15 +79,11 @@ export default async function credentialedProxyHandler(req, res, map) {
|
||||
logger.error("HTTP Error %d calling %s", status, url.toString());
|
||||
}
|
||||
|
||||
if (!validateWidgetData(widget, endpoint, data)) {
|
||||
if (data.error && data.error.url) {
|
||||
data.error.url = sanitizeErrorURL(url);
|
||||
if (status === 200) {
|
||||
if (!validateWidgetData(widget, endpoint, resultData)) {
|
||||
return res.status(500).json({error: {message: "Invalid data", url: sanitizeErrorURL(url), data: resultData}});
|
||||
}
|
||||
return res.status(500).json({error: {message: "Invalid data", url: sanitizeErrorURL(url), data}});
|
||||
}
|
||||
|
||||
if (status === 200 && map) {
|
||||
resultData = map(data);
|
||||
if (map) resultData = map(resultData);
|
||||
}
|
||||
|
||||
if (contentType) res.setHeader("Content-Type", contentType);
|
||||
|
@ -39,15 +39,15 @@ export default async function genericProxyHandler(req, res, map) {
|
||||
|
||||
let resultData = data;
|
||||
|
||||
if (!validateWidgetData(widget, endpoint, resultData)) {
|
||||
if (resultData.error && resultData.error.url) {
|
||||
if (resultData.error?.url) {
|
||||
resultData.error.url = sanitizeErrorURL(url);
|
||||
}
|
||||
|
||||
if (status === 200) {
|
||||
if (!validateWidgetData(widget, endpoint, resultData)) {
|
||||
return res.status(status).json({error: {message: "Invalid data", url: sanitizeErrorURL(url), data: resultData}});
|
||||
}
|
||||
|
||||
if (status === 200 && map) {
|
||||
resultData = map(data);
|
||||
if (map) resultData = map(resultData);
|
||||
}
|
||||
|
||||
if (contentType) res.setHeader("Content-Type", contentType);
|
||||
@ -58,7 +58,7 @@ export default async function genericProxyHandler(req, res, map) {
|
||||
|
||||
if (status >= 400) {
|
||||
logger.debug("HTTP Error %d calling %s//%s%s...", status, url.protocol, url.hostname, url.pathname);
|
||||
return res.status(status).json({error: {message: "HTTP Error", url: sanitizeErrorURL(url), data}});
|
||||
return res.status(status).json({error: {message: "HTTP Error", url: sanitizeErrorURL(url), resultData}});
|
||||
}
|
||||
|
||||
return res.status(status).send(resultData);
|
||||
|
@ -1,24 +1,34 @@
|
||||
/* eslint-disable no-console */
|
||||
import widgets from "widgets/widgets";
|
||||
|
||||
export default function validateWidgetData(widget, endpoint, data) {
|
||||
let valid = true;
|
||||
let dataParsed = data;
|
||||
let error;
|
||||
if (Buffer.isBuffer(data)) {
|
||||
try {
|
||||
dataParsed = JSON.parse(data);
|
||||
} catch (e) {
|
||||
error = e;
|
||||
valid = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (dataParsed && Object.entries(dataParsed).length) {
|
||||
const validate = widgets[widget.type]?.mappings?.[endpoint]?.validate;
|
||||
validate?.forEach(key => {
|
||||
const mappings = widgets[widget.type]?.mappings;
|
||||
if (mappings) {
|
||||
const mapping = Object.values(mappings).find(m => m.endpoint === endpoint);
|
||||
mapping?.validate?.forEach(key => {
|
||||
if (dataParsed[key] === undefined) {
|
||||
valid = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (!valid) {
|
||||
console.warn(`Invalid data for widget '${widget.type}' endpoint '${endpoint}':\nParse error: ${error ?? "none"}\nData: ${JSON.stringify(data, null, " ")}`);
|
||||
}
|
||||
|
||||
return valid;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user