feat: fetching home data with rest APIs; refactor: renamed fetch functions

This commit is contained in:
Roberto Tonino
2021-03-01 22:32:35 +01:00
parent b846b96f7a
commit 079fd8ad64
7 changed files with 36 additions and 30 deletions

View File

@@ -1,4 +1,4 @@
export function fetchApi(key, data) {
export function fetchData(key, data = {}) {
const url = new URL(`${window.location.origin}/api/${key}`)
Object.keys(data).forEach(key => {
@@ -7,3 +7,13 @@ export function fetchApi(key, data) {
return fetch(url.href).then(response => response.json())
}
export function sendToServer(key, data) {
const url = new URL(`${window.location.origin}/api/${key}`)
Object.keys(data).forEach(key => {
url.searchParams.append(key, data[key])
})
fetch(url.href).catch(console.error)
}

View File

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