add temp fix for non-json api responses & removed specific api method

This commit is contained in:
Axolotle 2020-08-27 18:25:40 +02:00
parent 57198f5cb0
commit 33a0820c4b

View file

@ -6,8 +6,15 @@ function objectToParams (object) {
return urlParams return urlParams
} }
function handleResponse (response, type = 'json') { async function handleResponse (response) {
return response.ok ? response[type]() : handleErrors(response) if (!response.ok) return handleErrors(response)
// FIXME the api should always return json objects
const responseText = await response.text()
try {
return JSON.parse(responseText)
} catch {
return responseText
}
} }
async function handleErrors (response) { async function handleErrors (response) {
@ -35,11 +42,9 @@ export default {
}, },
get (uri) { get (uri) {
return fetch('/api/' + uri, this.options) return fetch(
.then(response => handleResponse(response)) '/api/' + uri, this.options
.catch(err => { ).then(handleResponse)
console.log(err)
})
}, },
getAll (uris) { getAll (uris) {
@ -51,7 +56,7 @@ export default {
...this.options, ...this.options,
method: 'POST', method: 'POST',
body: objectToParams(data) body: objectToParams(data)
}).then(response => handleResponse(response)) }).then(handleResponse)
}, },
put (uri, data = {}) { put (uri, data = {}) {
@ -59,7 +64,7 @@ export default {
...this.options, ...this.options,
method: 'PUT', method: 'PUT',
body: objectToParams(data) body: objectToParams(data)
}).then(response => handleResponse(response)) }).then(handleResponse)
}, },
delete (uri, data = {}) { delete (uri, data = {}) {
@ -68,21 +73,5 @@ export default {
method: 'DELETE', method: 'DELETE',
body: objectToParams(data) body: objectToParams(data)
}).then(response => response.ok ? 'ok' : handleErrors(response)) }).then(response => response.ok ? 'ok' : handleErrors(response))
},
login (password) {
return fetch('/api/login', {
...this.options,
method: 'POST',
body: objectToParams({ password })
}).then(response => (response.ok))
},
logout () {
return fetch('/api/logout', this.options).then(response => (response.ok))
},
getVersion () {
return fetch('/api/versions', this.options).then(response => handleResponse(response))
} }
} }