|
| 1 | +// Author: EdiedRamos |
| 2 | + |
| 3 | +import * as process from "process"; |
| 4 | + |
| 5 | +interface PokemonType { |
| 6 | + name: string; |
| 7 | + url: string; |
| 8 | +} |
| 9 | + |
| 10 | +interface GameVersion { |
| 11 | + name: string; |
| 12 | + url: string; |
| 13 | +} |
| 14 | + |
| 15 | +interface PokemonResponse { |
| 16 | + id: number; |
| 17 | + name: string; |
| 18 | + weight: number; |
| 19 | + height: number; |
| 20 | + types: { |
| 21 | + type: PokemonType; |
| 22 | + }[]; |
| 23 | + game_indices: { |
| 24 | + version: GameVersion; |
| 25 | + }[]; |
| 26 | +} |
| 27 | + |
| 28 | +interface PokemonSpeciesResponse { |
| 29 | + evolution_chain: { |
| 30 | + url: string; |
| 31 | + }; |
| 32 | +} |
| 33 | + |
| 34 | +interface Species { |
| 35 | + name: string; |
| 36 | + url: string; |
| 37 | +} |
| 38 | + |
| 39 | +interface Chain { |
| 40 | + evolves_to: Chain[]; |
| 41 | + species: Species; |
| 42 | +} |
| 43 | + |
| 44 | +interface PokemonEvolutionResponse { |
| 45 | + chain: Chain; |
| 46 | +} |
| 47 | + |
| 48 | +class Pokemon { |
| 49 | + private name: string; |
| 50 | + private id: number; |
| 51 | + private weight: number; |
| 52 | + private height: number; |
| 53 | + private types: string[]; |
| 54 | + private evolutions: string[]; |
| 55 | + private games: string[]; |
| 56 | + |
| 57 | + constructor( |
| 58 | + private pokemonBaseInformation: PokemonResponse, |
| 59 | + private pokemonEvolutions: PokemonEvolutionResponse |
| 60 | + ) { |
| 61 | + this.loadInformation(); |
| 62 | + } |
| 63 | + |
| 64 | + private loadInformation() { |
| 65 | + this.id = this.pokemonBaseInformation.id; |
| 66 | + this.name = this.pokemonBaseInformation.name; |
| 67 | + this.weight = this.pokemonBaseInformation.weight; |
| 68 | + this.height = this.pokemonBaseInformation.height; |
| 69 | + this.types = this.pokemonBaseInformation.types.map(({ type }) => type.name); |
| 70 | + this.games = this.pokemonBaseInformation.game_indices.map( |
| 71 | + ({ version }) => version.name |
| 72 | + ); |
| 73 | + this.evolutions = this.evolutionPath(this.pokemonEvolutions.chain, []); |
| 74 | + } |
| 75 | + |
| 76 | + private evolutionPath(chain: Chain, currentPath: string[]): string[] { |
| 77 | + currentPath.push(chain.species.name); |
| 78 | + if (chain.evolves_to.length === 0) return currentPath; |
| 79 | + return this.evolutionPath(chain.evolves_to[0], currentPath); |
| 80 | + } |
| 81 | + |
| 82 | + get toString(): string { |
| 83 | + return ` |
| 84 | + ${"=".repeat(20)} |
| 85 | + Id: ${this.id} |
| 86 | + Nombre: ${this.name} |
| 87 | + Peso: ${this.weight} |
| 88 | + Altura: ${this.height} |
| 89 | + Tipos: ${this.types.join(",")} |
| 90 | + Juegos: ${this.games.join(",")} |
| 91 | + Evoluciones: ${this.evolutions.join(",")} |
| 92 | + ${"=".repeat(20)} |
| 93 | + `; |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +class Fetcher { |
| 98 | + static async get<T>(endpoint: string): Promise<T> { |
| 99 | + const response = await fetch(endpoint); |
| 100 | + const data = (await response.json()) as T; |
| 101 | + return data; |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +class PokemonFetcher { |
| 106 | + private static baseUrl = "https://pokeapi.co/api/v2/"; |
| 107 | + |
| 108 | + static async fetchPokemonInformation( |
| 109 | + pokemonTarget: string |
| 110 | + ): Promise<PokemonResponse> { |
| 111 | + return await Fetcher.get<PokemonResponse>( |
| 112 | + `${this.baseUrl}pokemon/${pokemonTarget}` |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + static async fetchPokemonSpecies( |
| 117 | + pokemonTarget: string |
| 118 | + ): Promise<PokemonSpeciesResponse> { |
| 119 | + return await Fetcher.get<PokemonSpeciesResponse>( |
| 120 | + `${this.baseUrl}pokemon-species/${pokemonTarget}` |
| 121 | + ); |
| 122 | + } |
| 123 | + |
| 124 | + static async fetchPokemonEvolutionFromURL( |
| 125 | + evolutionURL: string |
| 126 | + ): Promise<PokemonEvolutionResponse> { |
| 127 | + return await Fetcher.get<PokemonEvolutionResponse>(evolutionURL); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 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 | +} |
| 155 | + |
| 156 | +function menu() { |
| 157 | + console.log("Ingrese el nombre o id del pokémon (o 'salir' para terminar): "); |
| 158 | + |
| 159 | + process.stdin.on("data", async (data: string) => { |
| 160 | + const input = data.toString().trim().toLowerCase(); |
| 161 | + |
| 162 | + if (input === "salir") { |
| 163 | + console.log("Saliendo del programa...\n"); |
| 164 | + process.stdin.close(); |
| 165 | + } else { |
| 166 | + const information = await PokemonService.searchInformation(input); |
| 167 | + console.log(information); |
| 168 | + console.log( |
| 169 | + "Ingrese el nombre o id del pokémon (o 'salir' para terminar): " |
| 170 | + ); |
| 171 | + } |
| 172 | + }); |
| 173 | +} |
| 174 | + |
| 175 | +(() => menu())(); |
0 commit comments