|
1 | 1 | // Author: EdiedRamos
|
2 | 2 |
|
| 3 | +import * as process from "process"; |
| 4 | + |
3 | 5 | interface PokemonType {
|
4 | 6 | name: string;
|
5 | 7 | url: string;
|
@@ -126,23 +128,50 @@ class PokemonFetcher {
|
126 | 128 | }
|
127 | 129 | }
|
128 | 130 |
|
129 |
| -(async () => { |
130 |
| - try { |
131 |
| - const pokemonTarget = "pikachu"; |
132 |
| - const pokemonBaseInformation = await PokemonFetcher.fetchPokemonInformation( |
133 |
| - pokemonTarget |
134 |
| - ); |
135 |
| - const pokemonSpecies = await PokemonFetcher.fetchPokemonSpecies( |
136 |
| - pokemonTarget |
137 |
| - ); |
138 |
| - const pokemonEvolutions = await PokemonFetcher.fetchPokemonEvolutionFromURL( |
139 |
| - pokemonSpecies.evolution_chain.url |
140 |
| - ); |
| 131 | +class PokemonService { |
| 132 | + static async searchInformation(pokemonTarget: string): Promise<string> { |
| 133 | + try { |
| 134 | + const pokemonBaseInformation = |
| 135 | + await PokemonFetcher.fetchPokemonInformation(pokemonTarget); |
| 136 | + const pokemonSpecies = await PokemonFetcher.fetchPokemonSpecies( |
| 137 | + pokemonTarget |
| 138 | + ); |
| 139 | + const pokemonEvolutions = |
| 140 | + await PokemonFetcher.fetchPokemonEvolutionFromURL( |
| 141 | + pokemonSpecies.evolution_chain.url |
| 142 | + ); |
| 143 | + |
| 144 | + const pokemon = new Pokemon(pokemonBaseInformation, pokemonEvolutions); |
| 145 | + return pokemon.toString; |
| 146 | + } catch (error) { |
| 147 | + return ` |
| 148 | + ${"=".repeat(20)} |
| 149 | + Pokémon no encontrado... |
| 150 | + ${"=".repeat(20)} |
| 151 | + `; |
| 152 | + } |
| 153 | + } |
| 154 | +} |
141 | 155 |
|
142 |
| - const pokemon = new Pokemon(pokemonBaseInformation, pokemonEvolutions); |
| 156 | +function menu() { |
| 157 | + process.stdout.write( |
| 158 | + "Ingrese el nombre o id del pokémon (o 'salir' para terminar): " |
| 159 | + ); |
| 160 | + |
| 161 | + process.stdin.on("data", async (data: string) => { |
| 162 | + const input = data.toString().trim().toLowerCase(); |
| 163 | + |
| 164 | + if (input === "salir") { |
| 165 | + process.stdout.write("Saliendo del programa...\n"); |
| 166 | + process.stdin.close(); |
| 167 | + } else { |
| 168 | + const information = await PokemonService.searchInformation(input); |
| 169 | + console.log(information); |
| 170 | + process.stdout.write( |
| 171 | + "Ingrese el nombre o id del pokémon (o 'salir' para terminar): " |
| 172 | + ); |
| 173 | + } |
| 174 | + }); |
| 175 | +} |
143 | 176 |
|
144 |
| - console.log(pokemon.toString); |
145 |
| - } catch (error) { |
146 |
| - console.error(error); |
147 |
| - } |
148 |
| -})(); |
| 177 | +(() => menu())(); |
0 commit comments