first commit
This commit is contained in:
44
src/BooksAPI.js
Executable file
44
src/BooksAPI.js
Executable file
@@ -0,0 +1,44 @@
|
||||
|
||||
const api = "https://reactnd-books-api.udacity.com"
|
||||
|
||||
|
||||
// Generate a unique token for storing your bookshelf data on the backend server.
|
||||
let token = localStorage.token
|
||||
if (!token)
|
||||
token = localStorage.token = Math.random().toString(36).substr(-8)
|
||||
|
||||
const headers = {
|
||||
'Accept': 'application/json',
|
||||
'Authorization': token
|
||||
}
|
||||
|
||||
export const get = (bookId) =>
|
||||
fetch(`${api}/books/${bookId}`, { headers })
|
||||
.then(res => res.json())
|
||||
.then(data => data.book)
|
||||
|
||||
export const getAll = () =>
|
||||
fetch(`${api}/books`, { headers })
|
||||
.then(res => res.json())
|
||||
.then(data => data.books)
|
||||
|
||||
export const update = (book, shelf) =>
|
||||
fetch(`${api}/books/${book.id}`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
...headers,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ shelf })
|
||||
}).then(res => res.json())
|
||||
|
||||
export const search = (query) =>
|
||||
fetch(`${api}/search`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
...headers,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ query })
|
||||
}).then(res => res.json())
|
||||
.then(data => data.books)
|
||||
Reference in New Issue
Block a user