Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
101 lines (94 loc) · 3.36 KB

File metadata and controls

101 lines (94 loc) · 3.36 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
* This file contain some functions to interact with the Book Lender API provided by Udacity.
*
* From the URL https://reactnd-books-api.udacity.com follows the API calls specs:
*
* Use an Authorization header to work with your own data:
*
* fetch(url, { headers: { 'Authorization': 'whatever-you-want' }})
*
* The following endpoints are available:
*
* GET /status
* GET /books
* GET /books/:id
* PUT /books/:id { shelf }
* POST /search { query, maxResults }
*/
import * as BookUtils from './BookUtils';
const api = "https://reactnd-books-api.udacity.com";
// Generate a unique token for retrieving get one book API call since it doesn't need the any user related data
// a not logged user could access it.
let token = localStorage.token;
if (!token)
token = Math.random().toString(36).substr(-8);
const headers = {
'Accept': 'application/json',
'Authorization': token,
};
/**
* @description Returns the details of a book. Implement the API call GET /books/:id. Note: this is the only function
* that use token randomly generated by this script instead of the user address because the BookInfo components
* that uses this function needs to request data for logged and not logged users.
* @param {string} bookId - The id of the book.
* @returns {Promise.object} The book object if successfully or an object with errorCode key with the http status error.
*/
export const get = (bookId) =>
fetch(`${api}/books/${bookId}`, {headers})
.then(res => {
if (res.status !== 200) {
return res;
}
return res.json();
})
.then(data => {
// Only happen if status is 200
if (data.book) {
return data.book;
}
// Return the object with with http status (error code)
return {errorCode: data.status};
});
/**
* @description Get all classified books (books with shelf data) available for a particular address. Implements the call
* to API GET /books.
* @returns {Promise.array} Array of book objects (same book object returned by get()).
*/
export const getAll = () => {
const headers = BookUtils.getAccountHeaders();
return fetch(`${api}/books`, {headers})
.then(res => res.json())
.then(data => data.books);
};
/**
* @description Update a book with the informed shelf. Implements the call PUT /books/:id { shelf }.
* @param {object} book - The book object (same object returned at get() and getAll()).
* @param {string} shelf - The shelf ID (only available.
*/
export const update = (book, shelf) =>
fetch(`${api}/books/${book.id}`, {
method: 'PUT',
headers: {
...BookUtils.getAccountHeaders(),
'Content-Type': 'application/json'
},
body: JSON.stringify({shelf})
}).then(res => res.json());
/**
* @description Search books that contains a particular query term. Implements the call POST /search {query, maxResults}
* Note: The backend API uses a fixed set of cached search results and is limited to a particular set of search terms,
* which can be found in ./SEARCH_TERMS.md.
* @param {string} query - The search terms.
* @param {number} maxResults - Due to the nature of the backend server, search results are capped at 20, even if this
* is set higher.
*/
export const search = (query, maxResults) =>
fetch(`${api}/search`, {
method: 'POST',
headers: {
...BookUtils.getAccountHeaders(),
'Content-Type': 'application/json'
},
body: JSON.stringify({query, maxResults})
}).then(res => res.json())
.then(data => data.books);
Morty Proxy This is a proxified and sanitized view of the page, visit original site.