(isbn)
| 47 | * @returns The book details if found. |
| 48 | */ |
| 49 | export default async function searchInGoogleBooks(isbn) { |
| 50 | try { |
| 51 | const response = await axios.get(API_SEARCH_URL, { |
| 52 | params: { q: `isbn:${isbn}`, country: 'BR' }, |
| 53 | headers: { Accept: 'application/json' }, |
| 54 | timeout: 5000, |
| 55 | }); |
| 56 | |
| 57 | if (!response.data.items || !response.data.items[0]) { |
| 58 | throw new NotFoundError({ message: 'ISBN não encontrado' }); |
| 59 | } |
| 60 | |
| 61 | const gbBook = response.data.items[0]; |
| 62 | |
| 63 | const { volumeInfo } = gbBook; |
| 64 | |
| 65 | const coverUrl = |
| 66 | volumeInfo.imageLinks && |
| 67 | (volumeInfo.imageLinks.extraLarge || |
| 68 | volumeInfo.imageLinks.large || |
| 69 | volumeInfo.imageLinks.medium || |
| 70 | volumeInfo.imageLinks.small || |
| 71 | volumeInfo.imageLinks.thumbnail || |
| 72 | volumeInfo.imageLinks.smallThumbnail); |
| 73 | |
| 74 | return { |
| 75 | isbn, |
| 76 | title: volumeInfo.title.trim(), |
| 77 | subtitle: null, |
| 78 | authors: volumeInfo.authors, |
| 79 | publisher: volumeInfo.publisher, |
| 80 | synopsis: volumeInfo.description, |
| 81 | dimensions: parseDimensions(volumeInfo.dimensions), |
| 82 | year: |
| 83 | volumeInfo.publishedDate && |
| 84 | parseInt(volumeInfo.publishedDate.substring(0, 4), 10), |
| 85 | format: volumeInfo.dimensions ? 'PHYSICAL' : 'DIGITAL', |
| 86 | page_count: volumeInfo.pageCount, |
| 87 | subjects: volumeInfo.categories, |
| 88 | location: null, |
| 89 | retail_price: parsePrice(gbBook.saleInfo), |
| 90 | cover_url: coverUrl && coverUrl.replace('http://', 'https://'), |
| 91 | provider: 'google-books', |
| 92 | }; |
| 93 | } catch (error) { |
| 94 | // Log the error for debugging |
| 95 | // eslint-disable-next-line no-console |
| 96 | console.error('[google-books] Error fetching ISBN:', { |
| 97 | isbn, |
| 98 | error: error.message, |
| 99 | code: error.code, |
| 100 | status: error.response?.status, |
| 101 | }); |
| 102 | |
| 103 | // If it's already a NotFoundError, re-throw it |
| 104 | if (error instanceof NotFoundError) { |
| 105 | throw error; |
| 106 | } |
nothing calls this directly
no test coverage detected