Skip to content

Commit 7c3fc73

Browse files
committed
feat: implement menu
1 parent df1297d commit 7c3fc73

File tree

1 file changed

+47
-18
lines changed

1 file changed

+47
-18
lines changed

Roadmap/20 - PETICIONES HTTP/typescript/EdiedRamos.ts

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Author: EdiedRamos
22

3+
import * as process from "process";
4+
35
interface PokemonType {
46
name: string;
57
url: string;
@@ -126,23 +128,50 @@ class PokemonFetcher {
126128
}
127129
}
128130

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+
}
141155

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+
}
143176

144-
console.log(pokemon.toString);
145-
} catch (error) {
146-
console.error(error);
147-
}
148-
})();
177+
(() => menu())();

0 commit comments

Comments
 (0)