From edfabf45ead1cc86ed5cdf50713ac6a371c6e879 Mon Sep 17 00:00:00 2001 From: Carol Soares <79403621+csoaresdg@users.noreply.github.com> Date: Tue, 6 Jul 2021 02:57:41 -0300 Subject: [PATCH] carol-soares-exercicio-APi-pokedex MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Entrega de exercicio proposto //coisas que não consegui entender muito bem, mas preciso pesquisar novamente //como posso personalizar um json?..pq eu tentei e não consegui //fetch //metodo que será convertido em json. --- app.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 app.js diff --git a/app.js b/app.js new file mode 100644 index 0000000..fae75ee --- /dev/null +++ b/app.js @@ -0,0 +1,51 @@ +let pokemonList = []; +let pokemonTypes = []; + +/*Método assíncrono>>pode iniciar um processo primeiro e finalizar esse processo depois>> para obter tipos via api. Utilize o sufixo Async no fim do nome do método é um bom padrão*/ +async function fetchTypesAsync() { + + + const response = await fetch("https://pokeapi.co/api/v2/type"); + const data = await response.json(); + + pokemonTypes = data.results.map(function (type) { + return type.name; + }); +} + +async function fetchPokemonsAsync() { + // https://borgesdn.github.io/pokedex-source/pokedex.json + try { + const response = await fetch( + "https://borgesdn.github.io/pokedex-source/pokedex.json" + ); + const pokemons = await response.json(); + pokemonList = pokemons; + } catch (error) { + console.error(error); + } +} + +async function getPokemonAsync(id) { + // https://pokeapi.co/api/v2/pokemon/{id} >> exemplo a função fetchTypescriptAsync() + try { + const response = await fetch(`https://pokeapi.co/api/v2/pokemon/${id}`); + const pokemon = await response.json(); + return pokemon; + } catch (error) { + console.error(error); + } +} + +function filterPokemon(name, type) { + const filteredList = pokemonList.filter((pokemon) => { + const searchName = new RegExp(name, "i"); + const checkName = searchName.test(pokemon.name); + const checkType = type.length == 0 ? true : pokemon.type.includes(type); + return checkName && checkType; + }); + return filteredList; +} + +//Promisses vc tem mais previsibilidade detalhamento no tratamento de erros +//metodo then(()) => responsável por receber a resposta de sucesso da Promise pode ser encadeada pode compor outro then no final de outro .then anterior