From dc30de9176249f5f3cdf9881444afe7ba4ebe7f2 Mon Sep 17 00:00:00 2001 From: kolaente Date: Sun, 31 Oct 2021 21:04:00 +0100 Subject: [PATCH] feat: do api url check before anything else and move ready check to wrapper component --- src/App.vue | 47 +++--- src/components/misc/api-config.vue | 139 +++--------------- .../misc/{vikunja-loading.vue => ready.vue} | 29 +++- src/helpers/checkAndSetApiUrl.ts | 111 ++++++++++++++ src/store/index.js | 3 +- 5 files changed, 180 insertions(+), 149 deletions(-) rename src/components/misc/{vikunja-loading.vue => ready.vue} (63%) create mode 100644 src/helpers/checkAndSetApiUrl.ts diff --git a/src/App.vue b/src/App.vue index 2f91fda2e..34b975aa7 100644 --- a/src/App.vue +++ b/src/App.vue @@ -1,26 +1,27 @@ @@ -29,14 +48,14 @@ export default { height: 100vh; width: 100vw; flex-direction: column; - + img { margin-bottom: 1rem; } - + .loader-container { margin-right: 1rem; - + &.is-loading:after { border-left-color: $grey-400; border-bottom-color: $grey-400; diff --git a/src/helpers/checkAndSetApiUrl.ts b/src/helpers/checkAndSetApiUrl.ts new file mode 100644 index 000000000..8cab0f73f --- /dev/null +++ b/src/helpers/checkAndSetApiUrl.ts @@ -0,0 +1,111 @@ +const API_DEFAULT_PORT = '3456' + +export const checkAndSetApiUrl = (url: string, updateConfig: () => Promise): Promise => { + // Check if the url has an http prefix + if ( + !url.startsWith('http://') && + !url.startsWith('https://') + ) { + url = `http://${url}` + } + + const urlToCheck: URL = new URL(url) + const origUrlToCheck = urlToCheck + + const oldUrl = window.API_URL + window.API_URL = urlToCheck.toString() + + // Check if the api is reachable at the provided url + return updateConfig() + .catch(e => { + // Check if it is reachable at /api/v1 and http + if ( + !urlToCheck.pathname.endsWith('/api/v1') && + !urlToCheck.pathname.endsWith('/api/v1/') + ) { + urlToCheck.pathname = `${urlToCheck.pathname}api/v1` + window.API_URL = urlToCheck.toString() + return updateConfig() + } + throw e + }) + .catch(e => { + // Check if it has a port and if not check if it is reachable at https + if (urlToCheck.protocol === 'http:') { + urlToCheck.protocol = 'https:' + window.API_URL = urlToCheck.toString() + return updateConfig() + } + throw e + }) + .catch(e => { + // Check if it is reachable at /api/v1 and https + urlToCheck.pathname = origUrlToCheck.pathname + if ( + !urlToCheck.pathname.endsWith('/api/v1') && + !urlToCheck.pathname.endsWith('/api/v1/') + ) { + urlToCheck.pathname = `${urlToCheck.pathname}api/v1` + window.API_URL = urlToCheck.toString() + return updateConfig() + } + throw e + }) + .catch(e => { + // Check if it is reachable at port API_DEFAULT_PORT and https + if (urlToCheck.port !== API_DEFAULT_PORT) { + urlToCheck.protocol = 'https:' + urlToCheck.port = API_DEFAULT_PORT + window.API_URL = urlToCheck.toString() + return updateConfig() + } + throw e + }) + .catch(e => { + // Check if it is reachable at :API_DEFAULT_PORT and /api/v1 and https + urlToCheck.pathname = origUrlToCheck.pathname + if ( + !urlToCheck.pathname.endsWith('/api/v1') && + !urlToCheck.pathname.endsWith('/api/v1/') + ) { + urlToCheck.pathname = `${urlToCheck.pathname}api/v1` + window.API_URL = urlToCheck.toString() + return updateConfig() + } + throw e + }) + .catch(e => { + // Check if it is reachable at port API_DEFAULT_PORT and http + if (urlToCheck.port !== API_DEFAULT_PORT) { + urlToCheck.protocol = 'http:' + urlToCheck.port = API_DEFAULT_PORT + window.API_URL = urlToCheck.toString() + return updateConfig() + } + throw e + }) + .catch(e => { + // Check if it is reachable at :API_DEFAULT_PORT and /api/v1 and http + urlToCheck.pathname = origUrlToCheck.pathname + if ( + !urlToCheck.pathname.endsWith('/api/v1') && + !urlToCheck.pathname.endsWith('/api/v1/') + ) { + urlToCheck.pathname = `${urlToCheck.pathname}api/v1` + window.API_URL = urlToCheck.toString() + return updateConfig() + } + throw e + }) + .catch(e => { + window.API_URL = oldUrl + throw e + }) + .then(r => { + if (typeof r !== 'undefined') { + localStorage.setItem('API_URL', window.API_URL) + return window.API_URL + } + return '' + }) +} diff --git a/src/store/index.js b/src/store/index.js index 79c7d0177..b0589f774 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -19,6 +19,7 @@ import attachments from './modules/attachments' import labels from './modules/labels' import ListService from '../services/list' +import {checkAndSetApiUrl} from '../helpers/checkAndSetApiUrl' export const store = createStore({ strict: import.meta.env.DEV, @@ -143,7 +144,7 @@ export const store = createStore({ commit(CURRENT_LIST, currentList) }, async loadApp({commit, dispatch}) { - await dispatch('config/update') + await checkAndSetApiUrl(window.API_URL, () => dispatch('config/update')) await dispatch('auth/checkAuth') commit('vikunjaReady', true) },