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