|
| 1 | +const id: number = 10; |
| 2 | +const urljson: string = `https://jsonplaceholder.typicode.com/posts/${id}`; |
| 3 | + |
| 4 | +fetch(urljson) |
| 5 | +.then((response: Response) => { |
| 6 | + if (!response.ok) { |
| 7 | + throw new Error("Request error: " + response.status); |
| 8 | + } |
| 9 | + return response.json(); |
| 10 | +}) |
| 11 | +.then((data: any) => { |
| 12 | + console.log(data); |
| 13 | +}) |
| 14 | +.catch((error: Error) => { |
| 15 | + console.error("There was a problem with the request Fetch:", error); |
| 16 | +}); |
| 17 | + |
| 18 | +// ** Extra Exercise ** // |
| 19 | + |
| 20 | +interface PokemonType { |
| 21 | + type: { |
| 22 | + name: string; |
| 23 | + }; |
| 24 | +} |
| 25 | + |
| 26 | +interface PokemonData { |
| 27 | + forms: { name: string }[]; |
| 28 | + name: string; |
| 29 | + id: number; |
| 30 | + weight: number; |
| 31 | + height: number; |
| 32 | + types: PokemonType[]; |
| 33 | + species: { |
| 34 | + url: string; |
| 35 | + }; |
| 36 | + game_indices: { |
| 37 | + version: { |
| 38 | + name: string; |
| 39 | + }; |
| 40 | + }[]; |
| 41 | +} |
| 42 | + |
| 43 | +interface EvolutionChain { |
| 44 | + species: { |
| 45 | + name: string; |
| 46 | + }; |
| 47 | + evolves_to: EvolutionChain[]; |
| 48 | +} |
| 49 | + |
| 50 | +async function getPokemonInfo(pokemon: string): Promise<void> { |
| 51 | + try { |
| 52 | + const response: Response = await fetch(`https://pokeapi.co/api/v2/pokemon/${pokemon.toLowerCase()}`); |
| 53 | + const pokemonData: PokemonData = await response.json(); |
| 54 | + |
| 55 | + console.log('Pokemon data:', pokemonData.forms[0]["name"]); |
| 56 | + console.log('Name:', pokemonData.name); |
| 57 | + console.log('ID:', pokemonData.id); |
| 58 | + console.log('Weight:', pokemonData.weight); |
| 59 | + console.log('Height:', pokemonData.height); |
| 60 | + console.log('Types:'); |
| 61 | + pokemonData.types.forEach((type: PokemonType) => { |
| 62 | + console.log('-', type.type.name); |
| 63 | + }); |
| 64 | + |
| 65 | + const speciesResponse: Response = await fetch(pokemonData.species.url); |
| 66 | + const speciesData: any = await speciesResponse.json(); |
| 67 | + const evolutionChainUrl: string = speciesData.evolution_chain.url; |
| 68 | + const evolutionChainResponse: Response = await fetch(evolutionChainUrl); |
| 69 | + const evolutionChainData: { chain: EvolutionChain } = await evolutionChainResponse.json(); |
| 70 | + |
| 71 | + console.log('Evolution chain:'); |
| 72 | + let evolutionChain: EvolutionChain | null = evolutionChainData.chain; |
| 73 | + while (evolutionChain) { |
| 74 | + console.log('-', evolutionChain.species.name); |
| 75 | + evolutionChain = evolutionChain.evolves_to[0] || null; |
| 76 | + } |
| 77 | + |
| 78 | + console.log('Games in which it appears:'); |
| 79 | + pokemonData.game_indices.forEach(game => { |
| 80 | + console.log('-', game.version.name); |
| 81 | + }); |
| 82 | + |
| 83 | + } catch (error) { |
| 84 | + console.error('Error:', error.message); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +const pokemon: string = '3'; |
| 89 | +getPokemonInfo(pokemon); |
0 commit comments