mirror of
https://github.com/Lissy93/dashy.git
synced 2025-07-27 07:34:43 +02:00
✨ Adds proxying functionality to fix widget CORS errors (#391)
This commit is contained in:
parent
312450a898
commit
25fa6ebae8
@ -319,7 +319,10 @@ export default {
|
|||||||
},
|
},
|
||||||
/* Returns users specified widget options, or empty object */
|
/* Returns users specified widget options, or empty object */
|
||||||
widgetOptions() {
|
widgetOptions() {
|
||||||
return this.widget.options || {};
|
const options = this.widget.options || {};
|
||||||
|
const useProxy = !!this.widget.useProxy;
|
||||||
|
const updateInterval = this.widget.updateInterval || 0;
|
||||||
|
return { useProxy, updateInterval, ...options };
|
||||||
},
|
},
|
||||||
/* A unique string to reference the widget by */
|
/* A unique string to reference the widget by */
|
||||||
widgetRef() {
|
widgetRef() {
|
||||||
|
@ -2,8 +2,10 @@
|
|||||||
* 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 ProgressBar from 'rsup-progress';
|
import ProgressBar from 'rsup-progress';
|
||||||
import ErrorHandler from '@/utils/ErrorHandler';
|
import ErrorHandler from '@/utils/ErrorHandler';
|
||||||
|
import { serviceEndpoints } from '@/utils/defaults';
|
||||||
|
|
||||||
const WidgetMixin = {
|
const WidgetMixin = {
|
||||||
props: {
|
props: {
|
||||||
@ -19,6 +21,15 @@ const WidgetMixin = {
|
|||||||
mounted() {
|
mounted() {
|
||||||
this.fetchData();
|
this.fetchData();
|
||||||
},
|
},
|
||||||
|
computed: {
|
||||||
|
proxyReqEndpoint() {
|
||||||
|
const baseUrl = process.env.VUE_APP_DOMAIN || window.location.origin;
|
||||||
|
return `${baseUrl}${serviceEndpoints.corsProxy}`;
|
||||||
|
},
|
||||||
|
useProxy() {
|
||||||
|
return this.options.useProxy;
|
||||||
|
},
|
||||||
|
},
|
||||||
methods: {
|
methods: {
|
||||||
/* Re-fetches external data, called by parent. Usually overridden by widget */
|
/* Re-fetches external data, called by parent. Usually overridden by widget */
|
||||||
update() {
|
update() {
|
||||||
@ -44,9 +55,31 @@ const WidgetMixin = {
|
|||||||
fetchData() {
|
fetchData() {
|
||||||
this.finishLoading();
|
this.finishLoading();
|
||||||
},
|
},
|
||||||
|
/* Used as v-tooltip, pass text content in, and will show on hover */
|
||||||
tooltip(content) {
|
tooltip(content) {
|
||||||
return { content, trigger: 'hover focus', delay: 250 };
|
return { content, trigger: 'hover focus', delay: 250 };
|
||||||
},
|
},
|
||||||
|
/* Makes data request, returns promise */
|
||||||
|
makeRequest(endpoint, options = {}) {
|
||||||
|
// Request Options
|
||||||
|
const method = 'GET';
|
||||||
|
const url = this.useProxy ? this.proxyReqEndpoint : endpoint;
|
||||||
|
const headers = this.useProxy ? { 'Target-URL': endpoint, ...options } : options;
|
||||||
|
// Make request
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
axios.request({ method, url, headers })
|
||||||
|
.then((response) => {
|
||||||
|
resolve(response.data);
|
||||||
|
})
|
||||||
|
.catch((dataFetchError) => {
|
||||||
|
this.error('Unable to fetch data', dataFetchError);
|
||||||
|
reject(dataFetchError);
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
this.finishLoading();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user