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

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) {