From 7cede1318e3f272877c15fd73bb87141914f1485 Mon Sep 17 00:00:00 2001
From: shamoon <4887959+shamoon@users.noreply.github.com>
Date: Tue, 22 Aug 2023 21:42:53 -0700
Subject: [PATCH] Support extra headers in widgets

---
 src/pages/api/services/proxy.js     | 4 ++++
 src/utils/proxy/handlers/generic.js | 7 +++----
 2 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/src/pages/api/services/proxy.js b/src/pages/api/services/proxy.js
index dbe6fe9e..7ac6772a 100644
--- a/src/pages/api/services/proxy.js
+++ b/src/pages/api/services/proxy.js
@@ -55,6 +55,10 @@ export default async function handler(req, res) {
           req.query.endpoint = `${req.query.endpoint}?${query}`;
         }
 
+        if (mapping?.headers) {
+          req.extraHeaders = mapping.headers;
+        }
+
         if (endpointProxy instanceof Function) {
           return endpointProxy(req, res, map);
         }
diff --git a/src/utils/proxy/handlers/generic.js b/src/utils/proxy/handlers/generic.js
index 91e0fa94..ce1eba9b 100644
--- a/src/utils/proxy/handlers/generic.js
+++ b/src/utils/proxy/handlers/generic.js
@@ -20,11 +20,10 @@ export default async function genericProxyHandler(req, res, map) {
     if (widget) {
       const url = new URL(formatApiCall(widgets[widget.type].api, { endpoint, ...widget }));
 
-      let headers;
+      const headers = req.extraHeaders ?? {};
+      
       if (widget.username && widget.password) {
-        headers = {
-          Authorization: `Basic ${Buffer.from(`${widget.username}:${widget.password}`).toString("base64")}`,
-        };
+        headers.Authorization = `Basic ${Buffer.from(`${widget.username}:${widget.password}`).toString("base64")}`;
       }
 
       const params = {