2022-09-25 16:15:47 -07:00
|
|
|
import { formatApiCall } from "utils/api-helpers";
|
2022-09-21 23:37:17 -07:00
|
|
|
import createLogger from "utils/logger";
|
2022-09-04 21:58:42 +03:00
|
|
|
import genericProxyHandler from "utils/proxies/generic";
|
2022-09-25 19:43:47 +03:00
|
|
|
import widgets from "widgets/widgets";
|
2022-09-04 21:58:42 +03:00
|
|
|
|
2022-09-25 19:43:47 +03:00
|
|
|
const logger = createLogger("servicesProxy");
|
2022-09-04 21:58:42 +03:00
|
|
|
|
|
|
|
export default async function handler(req, res) {
|
2022-09-20 20:16:04 -07:00
|
|
|
try {
|
|
|
|
const { type } = req.query;
|
2022-09-25 19:43:47 +03:00
|
|
|
const widget = widgets[type];
|
|
|
|
|
|
|
|
if (!widget) {
|
|
|
|
logger.debug("Unknown proxy service type: %s", type);
|
|
|
|
return res.status(403).json({ error: "Unkown proxy service type" });
|
|
|
|
}
|
|
|
|
|
|
|
|
const serviceProxyHandler = widget.proxyHandler || genericProxyHandler;
|
2022-09-25 14:31:41 -07:00
|
|
|
req.method = "GET";
|
2022-09-04 21:58:42 +03:00
|
|
|
|
2022-09-25 19:43:47 +03:00
|
|
|
if (serviceProxyHandler instanceof Function) {
|
|
|
|
// map opaque endpoints to their actual endpoint
|
|
|
|
const mapping = widget?.mappings?.[req.query.endpoint];
|
2022-09-25 14:31:41 -07:00
|
|
|
const mappingParams = mapping.params;
|
2022-09-25 19:43:47 +03:00
|
|
|
const map = mapping?.map;
|
|
|
|
const endpoint = mapping?.endpoint;
|
2022-09-25 14:31:41 -07:00
|
|
|
const endpointProxy = mapping?.proxyHandler || serviceProxyHandler;
|
|
|
|
req.method = mapping?.method || "GET";
|
2022-09-04 21:58:42 +03:00
|
|
|
|
2022-09-25 19:43:47 +03:00
|
|
|
if (!endpoint) {
|
|
|
|
logger.debug("Unsupported service endpoint: %s", type);
|
|
|
|
return res.status(403).json({ error: "Unsupported service endpoint" });
|
2022-09-20 20:16:04 -07:00
|
|
|
}
|
2022-09-25 19:43:47 +03:00
|
|
|
|
2022-09-25 16:15:47 -07:00
|
|
|
req.query.endpoint = endpoint;
|
|
|
|
if (req.query.segments) {
|
|
|
|
const segments = JSON.parse(req.query.segments);
|
|
|
|
req.query.endpoint = formatApiCall(endpoint, segments);
|
|
|
|
}
|
|
|
|
if (req.query.query) {
|
|
|
|
const queryParams = JSON.parse(req.query.query);
|
2022-09-26 00:35:54 +03:00
|
|
|
const query = new URLSearchParams(mappingParams.map((p) => [p, queryParams[p]]));
|
2022-09-25 16:15:47 -07:00
|
|
|
req.query.endpoint = `${req.query.endpoint}?${query}`;
|
2022-09-25 14:31:41 -07:00
|
|
|
}
|
2022-09-25 19:43:47 +03:00
|
|
|
|
|
|
|
if (endpointProxy instanceof Function) {
|
|
|
|
return endpointProxy(req, res, map);
|
2022-09-20 20:16:04 -07:00
|
|
|
}
|
2022-09-25 19:43:47 +03:00
|
|
|
|
|
|
|
return serviceProxyHandler(req, res, map);
|
2022-09-16 11:33:11 -07:00
|
|
|
}
|
|
|
|
|
2022-09-20 20:16:04 -07:00
|
|
|
logger.debug("Unknown proxy service type: %s", type);
|
|
|
|
return res.status(403).json({ error: "Unkown proxy service type" });
|
2022-09-25 19:43:47 +03:00
|
|
|
} catch (ex) {
|
2022-09-20 20:16:04 -07:00
|
|
|
logger.error(ex);
|
|
|
|
return res.status(500).send({ error: "Unexpected error" });
|
2022-09-04 21:58:42 +03:00
|
|
|
}
|
|
|
|
}
|