🐛 Fixes auth headers for widgets (#1549) (#1561)

This commit is contained in:
Alicia Sykes 2024-05-10 00:13:59 +01:00
parent bb9bced18b
commit 46e1c2027e
1 changed files with 14 additions and 45 deletions

View File

@ -2,6 +2,7 @@
* Mixin that all pre-built and custom widgets extend from. * Mixin that all pre-built and custom widgets extend from.
* Manages loading state, error handling, data updates and user options * Manages loading state, error handling, data updates and user options
*/ */
import axios from 'axios';
import { Progress } from 'rsup-progress'; import { Progress } from 'rsup-progress';
import ErrorHandler from '@/utils/ErrorHandler'; import ErrorHandler from '@/utils/ErrorHandler';
import { serviceEndpoints } from '@/utils/defaults'; import { serviceEndpoints } from '@/utils/defaults';
@ -105,59 +106,27 @@ const WidgetMixin = {
const method = protocol || 'GET'; const method = protocol || 'GET';
const url = this.useProxy ? this.proxyReqEndpoint : endpoint; const url = this.useProxy ? this.proxyReqEndpoint : endpoint;
const data = JSON.stringify(body || {}); const data = JSON.stringify(body || {});
const CustomHeaders = options || null;
const CustomHeaders = options || {}; const headers = this.useProxy
const headers = new Headers(); ? { 'Target-URL': endpoint, CustomHeaders: JSON.stringify(CustomHeaders) } : CustomHeaders;
// If using a proxy, set the 'Target-URL' header
if (this.useProxy) {
headers.append('Target-URL', endpoint);
}
// Apply widget-specific custom headers
Object.entries(CustomHeaders).forEach(([key, value]) => {
headers.append(key, value);
});
// If the request is a GET, delete the body
const bodyContent = method.toUpperCase() === 'GET' ? undefined : data;
const timeout = this.options.timeout || this.defaultTimeout; const timeout = this.options.timeout || this.defaultTimeout;
// Setup Fetch request configuration
const requestConfig = { const requestConfig = {
method, method, url, headers, data, timeout,
headers,
body: bodyContent,
signal: undefined, // This will be set below
}; };
// Make request
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeout);
requestConfig.signal = controller.signal;
// Make request using Fetch API
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fetch(url, requestConfig) axios.request(requestConfig)
.then(async response => { .then((response) => {
const responseData = await response.json(); if (response.data.success === false) {
if (responseData.error) { this.error('Proxy returned error from target server', response.data.message);
this.error('Proxy returned error from target server', responseData.error?.message);
} }
if (responseData.success === false) { resolve(response.data);
this.error('Proxy didn\'t return success from target server', responseData.message);
}
resolve(responseData);
}) })
.catch(error => { .catch((dataFetchError) => {
if (error.name === 'AbortError') { this.error('Unable to fetch data', dataFetchError);
this.error('Request timed out', error); reject(dataFetchError);
} else {
this.error('Unable to fetch data', error);
}
reject(error);
}) })
.finally(() => { .finally(() => {
clearTimeout(timeoutId);
this.finishLoading(); this.finishLoading();
}); });
}); });