This commit is contained in:
Kenneth Jannette
2023-04-23 03:30:48 -05:00
parent 304cb3457d
commit c728db6923
5 changed files with 9773 additions and 121 deletions

View File

@@ -1,44 +1,43 @@
import SEARCH_API from "../secrets";
const api = SEARCH_API;
const api = "https://reactnd-books-api.udacity.com"
// Generate a unique token
let token = localStorage.token
if (!token)
token = localStorage.token = Math.random().toString(36).substr(-8)
// Generate a unique token
let token = localStorage.token;
if (!token) token = localStorage.token = Math.random().toString(36).substr(-8);
const headers = {
'Accept': 'application/json',
'Authorization': token
}
Accept: "application/json",
Authorization: token,
};
export const get = (bookId) =>
fetch(`${api}/books/${bookId}`, { headers })
.then(res => res.json())
.then(data => data.book)
.then((res) => res.json())
.then((data) => data.book);
export const getAll = () =>
fetch(`${api}/books`, { headers })
.then(res => res.json())
.then(data => data.books)
.then((res) => res.json())
.then((data) => data.books);
export const update = (book, shelf) =>
fetch(`${api}/books/${book.id}`, {
method: 'PUT',
method: "PUT",
headers: {
...headers,
'Content-Type': 'application/json'
"Content-Type": "application/json",
},
body: JSON.stringify({ shelf })
}).then(res => res.json())
body: JSON.stringify({ shelf }),
}).then((res) => res.json());
export const search = (query) =>
fetch(`${api}/search`, {
method: 'POST',
method: "POST",
headers: {
...headers,
'Content-Type': 'application/json'
"Content-Type": "application/json",
},
body: JSON.stringify({ query })
}).then(res => res.json())
.then(data => data.books)
body: JSON.stringify({ query }),
})
.then((res) => res.json())
.then((data) => data.books);