formatting cleanup

pull/302/head
Ben Phelps 2 years ago
parent b72dca0e2e
commit 4d790feaae

@ -16,29 +16,30 @@ export default async function handler(req, res) {
} }
const serviceProxyHandler = widget.proxyHandler || genericProxyHandler; const serviceProxyHandler = widget.proxyHandler || genericProxyHandler;
req.method = "GET";
if (serviceProxyHandler instanceof Function) { if (serviceProxyHandler instanceof Function) {
// map opaque endpoints to their actual endpoint // map opaque endpoints to their actual endpoint
if (widget?.mappings) { if (widget?.mappings) {
const mapping = widget?.mappings?.[req.query.endpoint]; const mapping = widget?.mappings?.[req.query.endpoint];
const mappingParams = mapping.params; const mappingParams = mapping?.params;
const map = mapping?.map; const map = mapping?.map;
const endpoint = mapping?.endpoint; const endpoint = mapping?.endpoint;
const endpointProxy = mapping?.proxyHandler || serviceProxyHandler; const endpointProxy = mapping?.proxyHandler || serviceProxyHandler;
req.method = mapping?.method || "GET";
if (!endpoint) { if (!endpoint) {
logger.debug("Unsupported service endpoint: %s", type); logger.debug("Unsupported service endpoint: %s", type);
return res.status(403).json({ error: "Unsupported service endpoint" }); return res.status(403).json({ error: "Unsupported service endpoint" });
} }
req.method = mapping?.method || "GET";
req.query.endpoint = endpoint; req.query.endpoint = endpoint;
if (req.query.segments) { if (req.query.segments) {
const segments = JSON.parse(req.query.segments); const segments = JSON.parse(req.query.segments);
req.query.endpoint = formatApiCall(endpoint, segments); req.query.endpoint = formatApiCall(endpoint, segments);
} }
if (req.query.query) {
if (req.query.query && mappingParams) {
const queryParams = JSON.parse(req.query.query); const queryParams = JSON.parse(req.query.query);
const query = new URLSearchParams(mappingParams.map((p) => [p, queryParams[p]])); const query = new URLSearchParams(mappingParams.map((p) => [p, queryParams[p]]));
req.query.endpoint = `${req.query.endpoint}?${query}`; req.query.endpoint = `${req.query.endpoint}?${query}`;

Loading…
Cancel
Save