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

View File

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

View File

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

View File

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

View File

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