Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2020
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function host(endpoint) {
  2.     return `https://softuniremotedb-66064.firebaseio.com/${endpoint}.json`;
  3. }
  4.  
  5. const api = {
  6.     books: 'books',
  7.     createBook: 'books/',
  8.     deleteBook: 'books/'
  9. }
  10.  
  11. export async function getBooks() {
  12.     const response = await fetch(host(api.books));
  13.     const books = await response.json();
  14.  
  15.     return books;
  16. }
  17.  
  18. export async function createBook(book) {
  19.     const createdBook = await fetch(host(api.books), {
  20.         method: 'POST',
  21.         headers: {'Content-Type': 'application/json'},
  22.         body: JSON.stringify(book)
  23.     });
  24.  
  25.     return createdBook;
  26. }
  27.  
  28. export async function editBook(id, book) {
  29.     const response = await fetch(host(`${api.editBook + id}`), {
  30.         method: 'PUT',
  31.         headers: { 'Content-type': 'application/json' },
  32.         body: JSON.stringify(book)
  33.     });
  34.  
  35.     const editedBook = await response.json();
  36.  
  37.     return editedBook;
  38. }
  39.  
  40. export async function deleteBook(id) {
  41.     return await fetch(host(`/${api.deleteBook + id}`), {
  42.         method: 'DELETE'
  43.     });
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement