Skip to content

Commit 5423c6b

Browse files
committed
✨ Add typescript solution challenge-20
1 parent fa63579 commit 5423c6b

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

2024/20-peticiones-http/solution.ts

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/* eslint-disable no-console */
2+
3+
(async() => {
4+
await fetchWebsiteContent('https://rickandmortyapi.com/api');
5+
})();
6+
7+
async function fetchWebsiteContent(url: string): Promise<void> {
8+
try {
9+
const response = await fetch(url);
10+
if (!response.ok) {
11+
throw new Error(`HTTP error! Status: ${response.status}`);
12+
}
13+
const data = await response.text();
14+
console.log('Request was successful!');
15+
console.log('Website content:\n', data);
16+
} catch (error) {
17+
if (error instanceof Error) {
18+
console.error(
19+
'Failed to fetch the website content. Error:',
20+
error.message
21+
);
22+
}
23+
}
24+
}
25+
26+
27+
// Tipos de las respuestas de la API
28+
interface PokemonType {
29+
type: { name: string };
30+
}
31+
32+
interface GameIndex {
33+
version: { name: string };
34+
}
35+
36+
interface EvolutionChain {
37+
species: { name: string };
38+
evolves_to: EvolutionChain[];
39+
}
40+
41+
interface PokemonData {
42+
name: string;
43+
id: number;
44+
weight: number;
45+
height: number;
46+
types: PokemonType[];
47+
species: { url: string };
48+
game_indices: GameIndex[];
49+
}
50+
51+
async function fetchPokemonData(pokemon: string): Promise<void> {
52+
try {
53+
const response = await fetch(`https://pokeapi.co/api/v2/pokemon/${pokemon.toLowerCase()}`);
54+
if (!response.ok) {
55+
throw new Error(`Pokémon not found! Status: ${response.status}`);
56+
}
57+
58+
const pokemonData: PokemonData = await response.json();
59+
console.log(`Name: ${pokemonData.name}`);
60+
console.log(`ID: ${pokemonData.id}`);
61+
console.log(`Weight: ${pokemonData.weight}`);
62+
console.log(`Height: ${pokemonData.height}`);
63+
console.log('Types:', pokemonData.types.map((typeInfo) => typeInfo.type.name).join(', '));
64+
65+
// Obtener la cadena de evoluciones
66+
const speciesResponse = await fetch(pokemonData.species.url);
67+
const speciesData = await speciesResponse.json();
68+
const evolutionResponse = await fetch(speciesData.evolution_chain.url);
69+
const evolutionData = await evolutionResponse.json();
70+
71+
const evolutionChain: string[] = [];
72+
let currentStage: EvolutionChain | null = evolutionData.chain;
73+
74+
while (currentStage) {
75+
evolutionChain.push(currentStage.species.name);
76+
currentStage = currentStage.evolves_to[0] || null;
77+
}
78+
79+
console.log('Evolution Chain:', evolutionChain.join(' -> '));
80+
81+
// Obtener los juegos en los que aparece el Pokémon
82+
console.log('Games:', pokemonData.game_indices.map((game) => game.version.name).join(', '));
83+
} catch (error) {
84+
if (error instanceof Error) {
85+
console.error('Failed to fetch Pokémon data. Error:', error.message);
86+
}
87+
}
88+
}
89+
90+
fetchPokemonData('pikachu');

0 commit comments

Comments
 (0)