-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPokeAPIClient.js
48 lines (37 loc) · 1.23 KB
/
PokeAPIClient.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
const fetch = require('node-fetch');
const pokeAPIEndpoint = 'https://pokeapi.co/api/v2';
module.exports = {
async fetchPokemonList(limit = 1118) {
let url = `${pokeAPIEndpoint}/pokemon?limit=${limit}`;
const res = await fetch(url);
const { results } = await res.json();
return results;
},
async fetchPokemonListByAbility(ability) {
let url = `${pokeAPIEndpoint}/ability/${ability}`;
const res = await fetch(url);
const { pokemon } = await res.json();
return pokemon.map(({ pokemon }) => pokemon);
},
async fetchPokemonListByType(type) {
let url = `${pokeAPIEndpoint}/type/${type}`;
const res = await fetch(url);
const { pokemon } = await res.json();
return pokemon.map(({ pokemon }) => pokemon);
},
async fetchPokemonInfo(pokemon) {
const res = await fetch(pokemon.url);
// We'll only keep the fields we are interested in.
const {
name, height, weight,
abilities, types,
} = await res.json();
const simplifiedAbilities = abilities.map(({ ability }) => ability.name);
const simplifiedTypes = types.map(({ type }) => type.name);
return {
name, height, weight,
abilities: simplifiedAbilities,
types: simplifiedTypes,
};
}
}