MCPcopy Create free account
hub / github.com/BrasilAPI/BrasilAPI / searchInMercadoEditorial

Function searchInMercadoEditorial

services/isbn/mercadoEditorial.js:55–119  ·  view source on GitHub ↗
(isbn)

Source from the content-addressed store, hash-verified

53 * @returns The book details if found.
54 */
55export default async function searchInMercadoEditorial(isbn) {
56 try {
57 const response = await axios.get(API_SEARCH_URL, {
58 params: { isbn },
59 headers: { Accept: 'application/json' },
60 timeout: 5000,
61 });
62
63 if (!response.data.books || !response.data.books[0]) {
64 throw new NotFoundError({ message: 'ISBN não encontrado' });
65 }
66
67 const meBook = response.data.books[0];
68
69 return {
70 isbn: meBook.isbn,
71 title: meBook.titulo,
72 subtitle:
73 meBook.subtitulo && meBook.subtitulo.length > 0
74 ? meBook.subtitulo
75 : null,
76 authors: meBook.contribuicao.map(
77 (contributor) => `${contributor.nome} ${contributor.sobrenome}`
78 ),
79 publisher: meBook.editora.nome_fantasia,
80 synopsis: meBook.sinopse,
81 dimensions: parseDimensions(meBook.medidas),
82 year: meBook.ano_edicao ? parseInt(meBook.ano_edicao, 10) : null,
83 format: meBook.formato === 'BOOK' ? 'PHYSICAL' : 'DIGITAL',
84 page_count:
85 meBook.medidas && meBook.medidas.paginas
86 ? parseInt(meBook.medidas.paginas, 10)
87 : null,
88 subjects:
89 meBook.catalogacao && meBook.catalogacao.palavras_chave.split(','),
90 location: null,
91 retail_price: parsePrice(meBook.moeda, meBook.preco),
92 cover_url:
93 meBook.imagens &&
94 meBook.imagens.imagem_primeira_capa &&
95 meBook.imagens.imagem_primeira_capa.grande,
96 provider: 'mercado-editorial',
97 };
98 } catch (error) {
99 // Log the error for debugging
100 // eslint-disable-next-line no-console
101 console.error('[mercado-editorial] Error fetching ISBN:', {
102 isbn,
103 error: error.message,
104 code: error.code,
105 status: error.response?.status,
106 });
107
108 // If it's already a NotFoundError, re-throw it
109 if (error instanceof NotFoundError) {
110 throw error;
111 }
112

Calls 2

parseDimensionsFunction · 0.70
parsePriceFunction · 0.70

Tested by

no test coverage detected