-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
23 lines (19 loc) · 824 Bytes
/
api.js
File metadata and controls
23 lines (19 loc) · 824 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import axios from 'axios'; //importando os axios para puxar os dados da api
const BASE_URL = 'https://hn.algolia.com/api/v1/search'; //url da api
// Buscar livros na API
export const fetchBooks = async (searchKey) => {
try {
// Realiza a requisição GET na API
const response = await axios.get(`${BASE_URL}?query=${searchKey}`);
const data = response.data;
// Pegar os dados da API para exibir na BookList
const books = data.hits.map((hit) => ({
title: hit.title || 'Sem título', // Pega o titulo e em caso nao fornece a frase sem titulo
author: hit.author || 'Autor desconhecido', // Pega o autor e em caso nao fornece a frase Autor Desconhecido
url: hit.url || '#', //Pega o url e em caso nao fornece a #
}));
return books;
} catch (error) {
throw error;
}
};