Add AdGuard, Bazarr, and Coin Market Cap widgets

- Allow setting HTTP method in widget.js
- Allow sending allow listed query params to proxy
This commit is contained in:
Jason Fischer 2022-09-25 14:31:41 -07:00
parent f999f4a467
commit 03fa2f86d7
No known key found for this signature in database
10 changed files with 251 additions and 9 deletions

View file

@ -1,3 +1,5 @@
import { URLSearchParams } from "next/dist/compiled/@edge-runtime/primitives/url";
import createLogger from "utils/logger";
import genericProxyHandler from "utils/proxies/generic";
import widgets from "widgets/widgets";
@ -15,20 +17,30 @@ export default async function handler(req, res) {
}
const serviceProxyHandler = widget.proxyHandler || genericProxyHandler;
req.method = "GET";
if (serviceProxyHandler instanceof Function) {
// map opaque endpoints to their actual endpoint
const mapping = widget?.mappings?.[req.query.endpoint];
const mappingParams = mapping.params;
const map = mapping?.map;
const endpoint = mapping?.endpoint;
const endpointProxy = mapping?.proxyHandler;
const endpointProxy = mapping?.proxyHandler || serviceProxyHandler;
req.method = mapping?.method || "GET";
if (!endpoint) {
logger.debug("Unsupported service endpoint: %s", type);
return res.status(403).json({ error: "Unsupported service endpoint" });
}
req.query.endpoint = endpoint;
if (req.query.params) {
const queryParams = JSON.parse(req.query.params);
const query = new URLSearchParams(mappingParams.map(p => [p, queryParams[p]]));
req.query.endpoint = `${endpoint}?${query}`;
}
else {
req.query.endpoint = endpoint;
}
if (endpointProxy instanceof Function) {
return endpointProxy(req, res, map);