forked from full-stack-bcn/p2-typescript-2023
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpokemon.ts
48 lines (41 loc) · 1.85 KB
/
pokemon.ts
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
import { PokemonDetails } from "./pokemon-detail.js";
export class Pokemon {
constructor(
public id: number,
public name: string,
public codename: string,
public imageUrl: string,
public types: string[],
public is_baby: boolean,
public is_legendary: boolean,
public is_mythical: boolean,
public officialArtworkUrl: string
) {}
get displayName() {
return `${this.id}. ${this.name}`;
}
}
export const loadPokemons = async (n: number) => {
const pokemons: Array<Pokemon> = [];
for (let i = 1; i <= n; i++) {
const response = await fetch(`https://pokeapi.co/api/v2/pokemon/${i}`);
const speciesResponse = await fetch(`https://pokeapi.co/api/v2/pokemon-species/${i}`);
if (response.ok && speciesResponse.ok) {
const data = await response.json();
const speciesData = await speciesResponse.json();
const imageUrl = data.sprites.front_default;
const officialArtworkUrl = data.sprites.other["official-artwork"].front_default;
const types = data.types.map((type: any) => type.type.name);
const is_baby = speciesData.is_baby;
const is_legendary = speciesData.is_legendary;
const is_mythical = speciesData.is_mythical;
const codename = data.species.name;
const nameEntry = speciesData.names.find((entry: { language: { name: string } }) => entry.language.name === 'en');
const name = nameEntry ? nameEntry.name : codename.charAt(0).toUpperCase() + codename.slice(1);
pokemons.push(new Pokemon(i, name, codename, imageUrl, types, is_baby, is_legendary, is_mythical, officialArtworkUrl));
} else {
console.error(`Error fetching data for Pokémon ID ${i}: ${response.statusText}`);
}
}
return pokemons;
};