-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbookService.mjs
37 lines (31 loc) · 980 Bytes
/
bookService.mjs
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
import axios from "axios";
const getBookDetails = async (title) => {
try {
const searchUrl = `http://openlibrary.org/search.json?title=${encodeURIComponent(title)}`;
const searchResponse = await axios.get(searchUrl);
const bookData = searchResponse.data.docs[0];
if (!bookData) {
return {
coverUrl: null,
author: 'Unknown Author',
isbn: null,
};
}
const isbnList = bookData.isbn ? bookData.isbn : [];
const coverUrl = isbnList.length > 0 ? `https://covers.openlibrary.org/b/isbn/${isbnList[0]}-M.jpg` : null;
const author = bookData.author_name ? bookData.author_name[0] : 'Unknown Author';
return {
coverUrl,
author,
isbn: isbnList.length > 0 ? isbnList[0] : null,
};
} catch (error) {
console.error('Error searching for book:', error.message);
return {
coverUrl: null,
author: 'Unknown Author',
isbn: null,
};
}
};
export { getBookDetails };