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(data, error, url) { 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)); } } export function getCountMyBooks(data, error, arg) { return useFetch(data, error, baseUrl + '/mybooks/' + arg + "/count"); } export function getMyBooks(data, error, arg, limit, offset) { const queryParams = new URLSearchParams({limit: limit, offset: offset}); return useFetch(data, error, baseUrl + '/mybooks/' + arg + "?" + queryParams.toString()); } export function getCountSearchBooks(data, error, searchterm) { return useFetch(data, error,baseUrl + '/search/' + encodeURIComponent(searchterm) + '/count') } export function getSearchBooks(data, error, searchterm, limit, offset) { const queryParams = new URLSearchParams({limit: limit, offset: offset}); return useFetch(data, error, baseUrl + '/search/' + encodeURIComponent(searchterm) + "?" + queryParams.toString()); } export function getBook(data, error, id) { return useFetch(data, error, 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 putStartReadDateUnset(bookId) { return genericPayloadCall('/book/' + bookId + "/startread", {startDate: "null"}, 'PUT') } export async function putStartReadDate(bookId, startdate) { return genericPayloadCall('/book/' + bookId + "/startread", {startDate: startdate}, '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 ""; } }