diff --git a/src/pages/api/services/proxy.js b/src/pages/api/services/proxy.js index 31c21a98f..b4b949586 100644 --- a/src/pages/api/services/proxy.js +++ b/src/pages/api/services/proxy.js @@ -16,29 +16,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 if (widget?.mappings) { const mapping = widget?.mappings?.[req.query.endpoint]; - const mappingParams = mapping.params; + const mappingParams = mapping?.params; const map = mapping?.map; const endpoint = mapping?.endpoint; 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.method = mapping?.method || "GET"; 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) { + + if (req.query.query && mappingParams) { const queryParams = JSON.parse(req.query.query); const query = new URLSearchParams(mappingParams.map((p) => [p, queryParams[p]])); req.query.endpoint = `${req.query.endpoint}?${query}`;