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

Function searchInCbl

services/isbn/cbl.js:70–144  ·  view source on GitHub ↗
(isbn)

Source from the content-addressed store, hash-verified

68 * @returns The book details if found.
69 */
70export default async function searchInCbl(isbn) {
71 try {
72 const isbn13 = isbn.length === 10 ? convertIsbn10ToIsbn13(isbn) : isbn;
73 const isbn10 = isbn.length === 13 ? convertIsbn13ToIsbn10(isbn) : isbn;
74
75 // Try to mimic the CBL website request.
76 const searchPayload = {
77 count: true,
78 facets: ['Imprint,count:50', 'Authors,count:50'],
79 filter: '',
80 orderby: null,
81 queryType: 'full',
82 search: `${isbn13} OR ${isbn10}`,
83 searchFields: 'FormattedKey,RowKey',
84 searchMode: 'any',
85 select: '*',
86 skip: 0,
87 top: 12,
88 };
89
90 const response = await axios.post(API_SEARCH_URL, searchPayload, {
91 headers: {
92 Accept: 'application/json',
93 'Api-Key': API_KEY,
94 },
95 timeout: 5000,
96 });
97
98 if (!response.data.value || !response.data.value[0]) {
99 throw new NotFoundError({ message: 'ISBN não encontrado' });
100 }
101
102 const cblBook = response.data.value[0];
103
104 return {
105 isbn: cblBook.RowKey,
106 title: cblBook.Title,
107 subtitle: cblBook.Subtitle,
108 authors: cblBook.Authors,
109 publisher: cblBook.Imprint,
110 synopsis: cblBook.Sinopse,
111 dimensions: parseDimensions(cblBook.Dimensao),
112 year: cblBook.Ano ? parseInt(cblBook.Ano, 10) : null,
113 format: cblBook.Formato === 'Papel' ? 'PHYSICAL' : 'DIGITAL',
114 page_count: cblBook.Paginas ? parseInt(cblBook.Paginas, 10) : null,
115 subjects: [cblBook.Subject]
116 .concat(cblBook.PalavrasChave || [])
117 .filter(Boolean),
118 location: parseLocation(cblBook.Cidade, cblBook.UF),
119 retail_price: null,
120 cover_url: null,
121 provider: 'cbl',
122 };
123 } catch (error) {
124 // Log the error for debugging
125 // eslint-disable-next-line no-console
126 console.error('[cbl] Error fetching ISBN:', {
127 isbn,

Callers

nothing calls this directly

Calls 4

convertIsbn10ToIsbn13Function · 0.90
convertIsbn13ToIsbn10Function · 0.90
parseLocationFunction · 0.85
parseDimensionsFunction · 0.70

Tested by

no test coverage detected