mirror of
https://github.com/YunoHost/yunohost-admin.git
synced 2024-09-03 20:06:15 +02:00
add temp fix for non-json api responses & removed specific api method
This commit is contained in:
parent
57198f5cb0
commit
33a0820c4b
1 changed files with 14 additions and 25 deletions
|
@ -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))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue