-
Notifications
You must be signed in to change notification settings - Fork 0
/
pokemons.js
53 lines (44 loc) · 1.69 KB
/
pokemons.js
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
class Pokemon {
constructor(pokemonData){
this.name = pokemonData.data.name;
this.url = pokemonData.url;
this.generation = pokemonData.species.generation.name;
this.image = pokemonData.data.sprites.front_default;
this.id = pokemonData.data.id;
this.abilities = pokemonData.data.abilities;
}
}
class PokemonFactory {
static async getPokemons (count = 1) {
const pokemonsUrls = [];
for (let i = 0; i < count; i++) {
pokemonsUrls.push("https://pokeapi.co/api/v2/pokemon?limit=1&offset=" + Math.floor(Math.random() * 1000));
}
const pokemons = await Promise.all(pokemonsUrls.map(async (pokemonUrl) => {
const pokemonUrlResponse = await fetch(pokemonUrl);
const pokemon = await pokemonUrlResponse.json();
const pokemonResponse = await fetch(pokemon.results[0].url);
const pokemonData = await pokemonResponse.json();
const pokemonSpeciesResponse = await fetch(pokemonData.species.url);
const pokemonSpecies = await pokemonSpeciesResponse.json();
return {
species: pokemonSpecies,
data: pokemonData,
url: pokemon.url,
};
}));
return pokemons;
}
static makePokemon (pokemonData) {
return new Pokemon(pokemonData);
}
static async makePokemons(count) {
const pokemonsData = await PokemonFactory.getPokemons(count);
const pokemons = [];
pokemonsData.forEach(pokemonData => {
const pokemon = PokemonFactory.makePokemon(pokemonData);
pokemons.push(pokemon);
});
return pokemons;
}
};