Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function host(endpoint) {
- return `https://softuniremotedb-66064.firebaseio.com/${endpoint}.json`;
- }
- const api = {
- books: 'books',
- createBook: 'books/',
- deleteBook: 'books/'
- }
- export async function getBooks() {
- const response = await fetch(host(api.books));
- const books = await response.json();
- return books;
- }
- export async function createBook(book) {
- const createdBook = await fetch(host(api.books), {
- method: 'POST',
- headers: {'Content-Type': 'application/json'},
- body: JSON.stringify(book)
- });
- return createdBook;
- }
- export async function editBook(id, book) {
- const response = await fetch(host(`${api.editBook + id}`), {
- method: 'PUT',
- headers: { 'Content-type': 'application/json' },
- body: JSON.stringify(book)
- });
- const editedBook = await response.json();
- return editedBook;
- }
- export async function deleteBook(id) {
- return await fetch(host(`/${api.deleteBook + id}`), {
- method: 'DELETE'
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement