Create a React API JS that is responsible for the Axios fetches

1 day ago 3
ARTICLE AD BOX

React API JS does the work. I am trying to create a react api js that is responsible for the Axios fetches.

I created this to have an image about how the correct Axios fetching should look like and handle error also.

import Axios from 'axios'; const BASE_URL = "http://localhost:8080/api"; export const api = { async getAllBooks() { const response = await Axios.get(`${BASE_URL}/books`); return response.data; }, async getBookById(bookId) { const response = await Axios.get(`${BASE_URL}/books/${bookId}`); return response.data; }, async addBookToLibrary({ libraryId, bookBody }) { try{ const response = await Axios.post(`${BASE_URL}/library/${libraryId}/book`, bookBody); return response.data; } catch(error){ throw new Error(error.response.data) } }, async setOnLoan(bookId) { try{ const response = await Axios.put(`${BASE_URL}/books/${bookId}/loan`) return response.data; } catch(error){ throw new Error(error.response.data) } }, async deleteBook(bookId) { const response = await Axios.delete(`${BASE_URL}/books/${bookId}`); return response.data; } };
Read Entire Article