refactor: simplified and renamed get function

This commit is contained in:
Roberto Tonino 2021-03-01 21:58:43 +01:00
parent efefa7bbf7
commit b846b96f7a
6 changed files with 39 additions and 42 deletions

File diff suppressed because one or more lines are too long

View file

@ -19,13 +19,13 @@ import router from '@/router'
import store from '@/store' import store from '@/store'
import { socket } from '@/utils/socket' import { socket } from '@/utils/socket'
import { get } from '@/utils/api' import { fetchApi } from '@/utils/api'
import { toast } from '@/utils/toasts' import { toast } from '@/utils/toasts'
import { isValidURL } from '@/utils/utils' import { isValidURL } from '@/utils/utils'
import { sendAddToQueue } from '@/utils/downloads' import { sendAddToQueue } from '@/utils/downloads'
/* ===== App initialization ===== */ /* ===== App initialization ===== */
function startApp() { async function startApp() {
new Vue({ new Vue({
store, store,
router, router,
@ -33,29 +33,28 @@ function startApp() {
render: h => h(App) render: h => h(App)
}).$mount('#app') }).$mount('#app')
fetch('connect') const connectResponse = await (await fetch('connect')).json()
.then(response => response.json())
.then(data => {
store.dispatch('setAppInfo', data.update)
if (data.autologin) { store.dispatch('setAppInfo', connectResponse.update)
console.log('Autologin')
let arl = localStorage.getItem('arl')
let accountNum = localStorage.getItem('accountNum')
if (arl) { if (connectResponse.autologin) {
arl = arl.trim() console.info('Autologin successful')
let result let arl = localStorage.getItem('arl')
const accountNum = localStorage.getItem('accountNum')
if (accountNum != 0) { if (arl) {
result = get('login', { arl: arl, force: true, child: accountNum || 0 }) arl = arl.trim()
} else { let result
result = get('login', { arl })
} if (accountNum !== 0) {
result.then(loggedIn) result = fetchApi('login', { arl, force: true, child: accountNum || 0 })
} } else {
result = fetchApi('login', { arl })
} }
})
result.then(loggedIn)
}
}
} }
function initClient() { function initClient() {

View file

@ -1,11 +1,10 @@
import { ref } from '@vue/composition-api' import { ref } from '@vue/composition-api'
import { get } from '@/utils/api' import { fetchApi } from '@/utils/api'
const searchResult = ref({}) const searchResult = ref({})
function performMainSearch(searchTerm) { function performMainSearch(searchTerm) {
get('mainSearch', { term: searchTerm }) fetchApi('mainSearch', { term: searchTerm }).then(data => {
.then(data => {
searchResult.value = data searchResult.value = data
}) })
} }

View file

@ -1,10 +1,10 @@
import { ref } from '@vue/composition-api' import { ref } from '@vue/composition-api'
import { get } from '@/utils/api' import { fetchApi } from '@/utils/api'
const result = ref({}) const result = ref({})
function performSearch({ term, type, start = 0, nb = 30 }) { function performSearch({ term, type, start = 0, nb = 30 }) {
get('search', { fetchApi('search', {
term, term,
type, type,
start, start,

View file

@ -1,10 +1,9 @@
export const get = function(key, data){ export function fetchApi(key, data) {
let url = `/api/${key}` const url = new URL(`${window.location.origin}/api/${key}`)
if (data){
let query = Object.keys(data) Object.keys(data).forEach(key => {
.map(k => encodeURIComponent(k) + '=' + encodeURIComponent(data[k])) url.searchParams.append(key, data[key])
.join('&') })
url += '?'+query
} return fetch(url.href).then(response => response.json())
return fetch(url).then(response => response.json())
} }

View file

@ -1,4 +1,4 @@
import { get } from '@/utils/api' import { fetchApi } from '@/utils/api'
/** /**
* @param {string} url * @param {string} url
@ -7,7 +7,7 @@ import { get } from '@/utils/api'
export function sendAddToQueue(url, bitrate = null) { export function sendAddToQueue(url, bitrate = null) {
if (!url) throw new Error('No URL given to sendAddToQueue function!') if (!url) throw new Error('No URL given to sendAddToQueue function!')
get('addToQueue', { url, bitrate }) fetchApi('addToQueue', { url, bitrate })
} }
export function aggregateDownloadLinks(releases) { export function aggregateDownloadLinks(releases) {