gamenight/frontend/src/api/Api.js

90 lines
1.9 KiB
JavaScript

import fetchResource from './FetchResource'
export function unpack_api_result(promise, onError) {
promise.then(result => {
if(result.result !== 'Ok') {
onError({
type: 'Error',
message: result.message
});
}
})
.catch(error => {
onError({
type: 'Error',
message: `${error.status} ${error.message}`
});
});
return promise;
}
export function get_gamenights(token) {
return fetchResource('api/gamenights', {
method: 'GET',
headers: {
'Authorization': `Bearer ${token}`
},
});
}
export function get_gamenight(gamenight_id, token) {
return fetchResource(`api/gamenights/${gamenight_id}`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${token}`
},
});
}
export function post_gamenight(input, token) {
return fetchResource('api/gamenights', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify(input)
});
}
export function patch_gamenight(gamenight_id, input, token) {
return fetchResource(`api/gamenights/${gamenight_id}`, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify(input)
})
}
export function delete_gamenight(input, token) {
return fetchResource('api/gamenights', {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify(input)
});
}
export function get_games(token) {
return fetchResource('api/games', {
method: 'GET',
headers: {
'Authorization': `Bearer ${token}`
}
});
}
export function login(body) {
return fetchResource('api/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
});
}