Files
gamenight/frontend/src/api/Api.js
T
2022-06-05 16:15:01 +02:00

126 lines
2.8 KiB
JavaScript

import fetchResource from './FetchResource'
export function unpack_api_result(promise, onError) {
return promise.then(result => {
if(result.result !== 'Ok') {
throw new Error(result.message);
}
return result;
})
.catch(error => {
onError({
type: 'Error',
message: `${error.status === null ?? error.status} ${error.message}`
});
});
}
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)
});
}
export function get_registration_tokens(token) {
return fetchResource('api/admin/registration_tokens', {
method: 'GET',
headers: {
'Authorization': `Bearer ${token}`
}
});
}
export function add_registration_token(token, registration_token) {
return fetchResource('api/admin/registration_tokens', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify(registration_token)
});
}
export function delete_registration_token(token, registration_token_id) {
return fetchResource(`api/admin/registration_tokens/${registration_token_id}`, {
method: 'DELETE',
headers: {
'Authorization': `Bearer ${token}`,
}
});
}
export function register(registration_token, input) {
return fetchResource(`api/register/${registration_token}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(input)
});
}