2022-09-26 12:04:37 +03:00
|
|
|
import { formatApiCall } from "utils/proxy/api-helpers";
|
2022-09-21 23:37:17 -07:00
|
|
|
import createLogger from "utils/logger";
|
2022-09-26 12:04:37 +03:00
|
|
|
import genericProxyHandler from "utils/proxy/handlers/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-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
|
2022-09-26 10:58:31 +03:00
|
|
|
if (widget?.mappings) {
|
|
|
|
const mapping = widget?.mappings?.[req.query.endpoint];
|
2022-09-26 14:42:31 +03:00
|
|
|
const mappingParams = mapping?.params;
|
2023-07-13 21:39:45 -07:00
|
|
|
const optionalParams = mapping?.optionalParams;
|
2022-09-26 10:58:31 +03:00
|
|
|
const map = mapping?.map;
|
|
|
|
const endpoint = mapping?.endpoint;
|
|
|
|
const endpointProxy = mapping?.proxyHandler || serviceProxyHandler;
|
|
|
|
|
|
|
|
if (!endpoint) {
|
|
|
|
logger.debug("Unsupported service endpoint: %s", type);
|
|
|
|
return res.status(403).json({ error: "Unsupported service endpoint" });
|
|
|
|
}
|
|
|
|
|
2022-09-26 14:42:31 +03:00
|
|
|
req.method = mapping?.method || "GET";
|
2023-02-18 22:18:27 -08:00
|
|
|
if (mapping?.body) req.body = mapping?.body;
|
2022-09-26 10:58:31 +03:00
|
|
|
req.query.endpoint = endpoint;
|
2022-09-26 14:42:31 +03:00
|
|
|
|
2022-09-26 10:58:31 +03:00
|
|
|
if (req.query.segments) {
|
|
|
|
const segments = JSON.parse(req.query.segments);
|
|
|
|
req.query.endpoint = formatApiCall(endpoint, segments);
|
|
|
|
}
|
2022-09-26 14:42:31 +03:00
|
|
|
|
2023-07-13 21:39:45 -07:00
|
|
|
if (req.query.query && (mappingParams || optionalParams)) {
|
2022-09-26 10:58:31 +03:00
|
|
|
const queryParams = JSON.parse(req.query.query);
|
2023-07-13 21:39:45 -07:00
|
|
|
|
2023-10-17 23:26:55 -07:00
|
|
|
let filteredOptionalParams = [];
|
|
|
|
if (optionalParams) filteredOptionalParams = optionalParams.filter((p) => queryParams[p] !== undefined);
|
|
|
|
|
2023-07-13 21:39:45 -07:00
|
|
|
let params = [];
|
|
|
|
if (mappingParams) params = params.concat(mappingParams);
|
|
|
|
if (filteredOptionalParams) params = params.concat(filteredOptionalParams);
|
2023-10-17 23:26:55 -07:00
|
|
|
|
2023-07-13 21:39:45 -07:00
|
|
|
const query = new URLSearchParams(params.map((p) => [p, queryParams[p]]));
|
2022-09-26 10:58:31 +03:00
|
|
|
req.query.endpoint = `${req.query.endpoint}?${query}`;
|
|
|
|
}
|
|
|
|
|
2023-08-22 21:42:53 -07:00
|
|
|
if (mapping?.headers) {
|
|
|
|
req.extraHeaders = mapping.headers;
|
|
|
|
}
|
|
|
|
|
2022-09-26 10:58:31 +03:00
|
|
|
if (endpointProxy instanceof Function) {
|
|
|
|
return endpointProxy(req, res, map);
|
|
|
|
}
|
|
|
|
|
|
|
|
return serviceProxyHandler(req, res, map);
|
2022-09-20 20:16:04 -07:00
|
|
|
}
|
2022-09-25 19:43:47 +03:00
|
|
|
|
2022-09-26 10:58:31 +03:00
|
|
|
return serviceProxyHandler(req, res);
|
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" });
|
2024-02-28 11:44:32 -08:00
|
|
|
} catch (e) {
|
|
|
|
if (e) logger.error(e);
|
2022-09-20 20:16:04 -07:00
|
|
|
return res.status(500).send({ error: "Unexpected error" });
|
2022-09-04 21:58:42 +03:00
|
|
|
}
|
|
|
|
}
|