2020-10-11 12:13:35 +02:00
|
|
|
<template>
|
|
|
|
<div class="api-config">
|
|
|
|
<div v-if="configureApi">
|
2021-06-24 01:24:57 +02:00
|
|
|
<label class="label" for="api-url">{{ $t('apiConfig.url') }}</label>
|
2020-10-11 12:13:35 +02:00
|
|
|
<div class="field has-addons">
|
|
|
|
<div class="control is-expanded">
|
|
|
|
<input
|
2021-01-17 18:57:57 +01:00
|
|
|
class="input"
|
|
|
|
id="api-url"
|
2021-06-24 01:24:57 +02:00
|
|
|
:placeholder="$t('apiConfig.urlPlaceholder')"
|
2020-10-11 12:13:35 +02:00
|
|
|
required
|
|
|
|
type="url"
|
|
|
|
v-focus
|
|
|
|
v-model="apiUrl"
|
|
|
|
@keyup.enter="setApiUrl"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div class="control">
|
2021-01-17 18:57:57 +01:00
|
|
|
<x-button @click="setApiUrl" :disabled="apiUrl === ''">
|
2021-06-24 01:24:57 +02:00
|
|
|
{{ $t('apiConfig.change') }}
|
2021-01-17 18:57:57 +01:00
|
|
|
</x-button>
|
2020-10-11 12:13:35 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="api-url-info" v-else>
|
2021-06-24 01:24:57 +02:00
|
|
|
<i18n path="apiConfig.signInOn">
|
2021-09-08 23:48:30 +02:00
|
|
|
<span class="url" v-tooltip="apiUrl"> {{ apiDomain }} </span>
|
2021-06-24 01:24:57 +02:00
|
|
|
</i18n>
|
2021-01-17 18:57:57 +01:00
|
|
|
<br />
|
2021-06-24 01:24:57 +02:00
|
|
|
<a @click="() => (configureApi = true)">{{ $t('apiConfig.change') }}</a>
|
2020-10-11 12:13:35 +02:00
|
|
|
</div>
|
|
|
|
|
2021-01-17 18:57:57 +01:00
|
|
|
<div
|
|
|
|
class="notification is-success mt-2"
|
|
|
|
v-if="successMsg !== '' && errorMsg === ''"
|
|
|
|
>
|
2020-10-11 12:13:35 +02:00
|
|
|
{{ successMsg }}
|
|
|
|
</div>
|
2021-01-17 18:57:57 +01:00
|
|
|
<div
|
|
|
|
class="notification is-danger mt-2"
|
|
|
|
v-if="errorMsg !== '' && successMsg === ''"
|
|
|
|
>
|
2020-10-11 12:13:35 +02:00
|
|
|
{{ errorMsg }}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2021-09-23 07:09:47 +02:00
|
|
|
import { parseURL } from 'ufo'
|
|
|
|
|
2021-09-08 23:48:30 +02:00
|
|
|
const API_DEFAULT_PORT = 3456
|
|
|
|
|
2020-10-11 12:13:35 +02:00
|
|
|
export default {
|
|
|
|
name: 'apiConfig',
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
configureApi: false,
|
2021-09-08 23:48:30 +02:00
|
|
|
apiUrl: window.API_URL,
|
2020-10-11 12:13:35 +02:00
|
|
|
errorMsg: '',
|
|
|
|
successMsg: '',
|
|
|
|
}
|
|
|
|
},
|
|
|
|
created() {
|
|
|
|
if (this.apiUrl === '') {
|
|
|
|
this.configureApi = true
|
|
|
|
}
|
|
|
|
},
|
2021-09-08 23:48:30 +02:00
|
|
|
computed: {
|
2020-10-11 12:13:35 +02:00
|
|
|
apiDomain() {
|
2021-09-23 07:09:47 +02:00
|
|
|
return parseURL(this.apiUrl).host
|
2020-10-11 12:13:35 +02:00
|
|
|
},
|
2021-09-08 23:48:30 +02:00
|
|
|
},
|
|
|
|
methods: {
|
2020-10-11 12:13:35 +02:00
|
|
|
setApiUrl() {
|
|
|
|
if (this.apiUrl === '') {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let urlToCheck = this.apiUrl
|
|
|
|
|
|
|
|
// Check if the url has an http prefix
|
2021-01-17 18:57:57 +01:00
|
|
|
if (
|
|
|
|
!urlToCheck.startsWith('http://') &&
|
|
|
|
!urlToCheck.startsWith('https://')
|
|
|
|
) {
|
2020-10-11 12:13:35 +02:00
|
|
|
urlToCheck = `http://${urlToCheck}`
|
|
|
|
}
|
|
|
|
|
|
|
|
urlToCheck = new URL(urlToCheck)
|
|
|
|
const origUrlToCheck = urlToCheck
|
|
|
|
|
|
|
|
const oldUrl = window.API_URL
|
|
|
|
window.API_URL = urlToCheck.toString()
|
|
|
|
|
|
|
|
// Check if the api is reachable at the provided url
|
2021-01-17 18:57:57 +01:00
|
|
|
this.$store
|
|
|
|
.dispatch('config/update')
|
|
|
|
.catch((e) => {
|
2020-10-11 12:13:35 +02:00
|
|
|
// Check if it is reachable at /api/v1 and http
|
2021-01-17 18:57:57 +01:00
|
|
|
if (
|
|
|
|
!urlToCheck.pathname.endsWith('/api/v1') &&
|
|
|
|
!urlToCheck.pathname.endsWith('/api/v1/')
|
|
|
|
) {
|
2020-10-11 12:13:35 +02:00
|
|
|
urlToCheck.pathname = `${urlToCheck.pathname}api/v1`
|
|
|
|
window.API_URL = urlToCheck.toString()
|
|
|
|
return this.$store.dispatch('config/update')
|
|
|
|
}
|
|
|
|
return Promise.reject(e)
|
|
|
|
})
|
2021-01-17 18:57:57 +01:00
|
|
|
.catch((e) => {
|
2020-10-11 12:13:35 +02:00
|
|
|
// 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 this.$store.dispatch('config/update')
|
|
|
|
}
|
|
|
|
return Promise.reject(e)
|
|
|
|
})
|
2021-01-17 18:57:57 +01:00
|
|
|
.catch((e) => {
|
2020-10-11 12:13:35 +02:00
|
|
|
// Check if it is reachable at /api/v1 and https
|
|
|
|
urlToCheck.pathname = origUrlToCheck.pathname
|
2021-01-17 18:57:57 +01:00
|
|
|
if (
|
|
|
|
!urlToCheck.pathname.endsWith('/api/v1') &&
|
|
|
|
!urlToCheck.pathname.endsWith('/api/v1/')
|
|
|
|
) {
|
2020-10-11 12:13:35 +02:00
|
|
|
urlToCheck.pathname = `${urlToCheck.pathname}api/v1`
|
|
|
|
window.API_URL = urlToCheck.toString()
|
|
|
|
return this.$store.dispatch('config/update')
|
|
|
|
}
|
|
|
|
return Promise.reject(e)
|
|
|
|
})
|
2021-01-17 18:57:57 +01:00
|
|
|
.catch((e) => {
|
2021-09-08 23:48:30 +02:00
|
|
|
// Check if it is reachable at port API_DEFAULT_PORT and https
|
|
|
|
if (urlToCheck.port !== API_DEFAULT_PORT) {
|
2020-10-11 12:13:35 +02:00
|
|
|
urlToCheck.protocol = 'https:'
|
2021-09-08 23:48:30 +02:00
|
|
|
urlToCheck.port = API_DEFAULT_PORT
|
2020-10-11 12:13:35 +02:00
|
|
|
window.API_URL = urlToCheck.toString()
|
|
|
|
return this.$store.dispatch('config/update')
|
|
|
|
}
|
|
|
|
return Promise.reject(e)
|
|
|
|
})
|
2021-01-17 18:57:57 +01:00
|
|
|
.catch((e) => {
|
2021-09-08 23:48:30 +02:00
|
|
|
// Check if it is reachable at :API_DEFAULT_PORT and /api/v1 and https
|
2020-10-11 12:13:35 +02:00
|
|
|
urlToCheck.pathname = origUrlToCheck.pathname
|
2021-01-17 18:57:57 +01:00
|
|
|
if (
|
|
|
|
!urlToCheck.pathname.endsWith('/api/v1') &&
|
|
|
|
!urlToCheck.pathname.endsWith('/api/v1/')
|
|
|
|
) {
|
2020-10-11 12:13:35 +02:00
|
|
|
urlToCheck.pathname = `${urlToCheck.pathname}api/v1`
|
|
|
|
window.API_URL = urlToCheck.toString()
|
|
|
|
return this.$store.dispatch('config/update')
|
|
|
|
}
|
|
|
|
return Promise.reject(e)
|
|
|
|
})
|
2021-01-17 18:57:57 +01:00
|
|
|
.catch((e) => {
|
2021-09-08 23:48:30 +02:00
|
|
|
// Check if it is reachable at port API_DEFAULT_PORT and http
|
|
|
|
if (urlToCheck.port !== API_DEFAULT_PORT) {
|
2020-10-11 12:13:35 +02:00
|
|
|
urlToCheck.protocol = 'http:'
|
2021-09-08 23:48:30 +02:00
|
|
|
urlToCheck.port = API_DEFAULT_PORT
|
2020-10-11 12:13:35 +02:00
|
|
|
window.API_URL = urlToCheck.toString()
|
|
|
|
return this.$store.dispatch('config/update')
|
|
|
|
}
|
|
|
|
return Promise.reject(e)
|
|
|
|
})
|
2021-01-17 18:57:57 +01:00
|
|
|
.catch((e) => {
|
2021-09-08 23:48:30 +02:00
|
|
|
// Check if it is reachable at :API_DEFAULT_PORT and /api/v1 and http
|
2020-10-11 12:13:35 +02:00
|
|
|
urlToCheck.pathname = origUrlToCheck.pathname
|
2021-01-17 18:57:57 +01:00
|
|
|
if (
|
|
|
|
!urlToCheck.pathname.endsWith('/api/v1') &&
|
|
|
|
!urlToCheck.pathname.endsWith('/api/v1/')
|
|
|
|
) {
|
2020-10-11 12:13:35 +02:00
|
|
|
urlToCheck.pathname = `${urlToCheck.pathname}api/v1`
|
|
|
|
window.API_URL = urlToCheck.toString()
|
|
|
|
return this.$store.dispatch('config/update')
|
|
|
|
}
|
|
|
|
return Promise.reject(e)
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
// Still not found, url is still invalid
|
|
|
|
this.successMsg = ''
|
2021-09-08 23:48:30 +02:00
|
|
|
this.errorMsg = this.$t('apiConfig.error', {domain: this.apiDomain})
|
2020-10-11 12:13:35 +02:00
|
|
|
window.API_URL = oldUrl
|
|
|
|
})
|
2021-01-17 18:57:57 +01:00
|
|
|
.then((r) => {
|
2020-10-11 12:13:35 +02:00
|
|
|
if (typeof r !== 'undefined') {
|
|
|
|
// Set it + save it to local storage to save us the hoops
|
|
|
|
this.errorMsg = ''
|
2021-09-08 23:48:30 +02:00
|
|
|
this.successMsg = this.$t('apiConfig.success', {domain: this.apiDomain})
|
2020-10-11 12:13:35 +02:00
|
|
|
localStorage.setItem('API_URL', window.API_URL)
|
|
|
|
this.configureApi = false
|
|
|
|
this.apiUrl = window.API_URL
|
2021-04-21 09:44:40 +02:00
|
|
|
this.$emit('foundApi', this.apiUrl)
|
2020-10-11 12:13:35 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
</script>
|