132 lines
3.0 KiB
JavaScript
132 lines
3.0 KiB
JavaScript
import { ref } from 'vue'
|
|
import { useAuthStore } from './auth.store.js'
|
|
|
|
const baseUrl = "http://localhost:8080"
|
|
|
|
export function getImagePathOrDefault(path) {
|
|
return (path == "" || typeof path === 'undefined') ?
|
|
"../defaultbook.png" : baseUrl + path;
|
|
}
|
|
|
|
function useFetch(url) {
|
|
const data = ref(null);
|
|
const error = ref(null);
|
|
|
|
const { user } = useAuthStore();
|
|
|
|
if (user != null) {
|
|
fetch(url, {
|
|
method: 'GET',
|
|
headers: {
|
|
'Authorization': 'Bearer ' + user.token
|
|
}
|
|
})
|
|
.then((res) => res.json())
|
|
.then((json) => (data.value = json))
|
|
.catch((err) => (error.value = err));
|
|
}
|
|
|
|
return { data, error }
|
|
}
|
|
|
|
export function getMyBooks(arg) {
|
|
return useFetch(baseUrl + '/mybooks/' + arg);
|
|
}
|
|
|
|
export function getSearchBooks(searchterm) {
|
|
return useFetch(baseUrl + '/search/' + searchterm);
|
|
}
|
|
|
|
export function getBook(id) {
|
|
return useFetch(baseUrl + '/book/' + id);
|
|
}
|
|
|
|
export function postBook(book) {
|
|
return genericPayloadCall('/book', book.value, 'POST')
|
|
}
|
|
|
|
export async function putReadBook(bookId, payload) {
|
|
return genericPayloadCall('/book/' + bookId + "/read", payload, 'PUT')
|
|
}
|
|
|
|
export async function putWantReadBook(bookId, payload) {
|
|
return genericPayloadCall('/book/' + bookId + "/wantread", payload, 'PUT')
|
|
}
|
|
|
|
export async function putRateBook(bookId, payload) {
|
|
return genericPayloadCall('/book/' + bookId + "/rate", payload, 'PUT')
|
|
}
|
|
|
|
export function postLogin(user) {
|
|
return genericPostCallNoAuth('/auth/login', user.value)
|
|
}
|
|
|
|
export function postSignUp(user) {
|
|
return genericPostCallNoAuth('/auth/signup', user.value)
|
|
}
|
|
|
|
export function postImage(file) {
|
|
const { user } = useAuthStore();
|
|
const formData = new FormData();
|
|
formData.append('file', file);
|
|
if (user != null) {
|
|
return fetch(baseUrl + "/upload/cover", {
|
|
method: 'POST',
|
|
headers: {
|
|
'Authorization': 'Bearer ' + user.token
|
|
},
|
|
body: formData
|
|
})
|
|
} else {
|
|
return Promise.resolve();
|
|
}
|
|
}
|
|
|
|
export function genericPostCallNoAuth(apiRoute, object) {
|
|
return fetch(baseUrl + apiRoute, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify(object)
|
|
})
|
|
}
|
|
|
|
export function genericPayloadCall(apiRoute, object, method) {
|
|
const { user } = useAuthStore();
|
|
|
|
if (user != null) {
|
|
return fetch(baseUrl + apiRoute, {
|
|
method: method,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer ' + user.token
|
|
},
|
|
body: JSON.stringify(object)
|
|
})
|
|
}
|
|
else {
|
|
return Promise.resolve();
|
|
}
|
|
}
|
|
|
|
export function extractFormErrorFromField(fieldName, errors) {
|
|
if (errors === null) {
|
|
return "";
|
|
}
|
|
const titleErr = errors.find((e) => e["field"] === fieldName);
|
|
if (typeof titleErr !== 'undefined') {
|
|
return titleErr.error;
|
|
} else {
|
|
return "";
|
|
}
|
|
}
|
|
|
|
export function extractGlobalFormError(errors) {
|
|
if (errors !== null && "error" in errors) {
|
|
return errors["error"];
|
|
} else {
|
|
return "";
|
|
}
|
|
}
|