124 lines
3.2 KiB
JavaScript
124 lines
3.2 KiB
JavaScript
const fetch = require("@11ty/eleventy-fetch");
|
|
const { lang } = require("./meta.js");
|
|
|
|
const POKEDEX_SIZE = 151;
|
|
const pokemonUrl = "https://pokeapi.co/api/v2/pokemon/";
|
|
const pokemonDetailUrl = "https://pokeapi.co/api/v2/pokemon-species/";
|
|
const typeUrl = "https://pokeapi.co/api/v2/type/";
|
|
|
|
async function getPokemon(id) {
|
|
const url = `${pokemonUrl}${id}`;
|
|
let result = await fetch(url, {
|
|
duration: "7d",
|
|
type: "json",
|
|
});
|
|
let details = await getPokemonDetails(id);
|
|
|
|
let types = [];
|
|
result.types.forEach(async type => {
|
|
let typeDetails = await getTypeDetails(type.type.url.split("/")[6]);
|
|
types.push({
|
|
name: typeDetails.names.filter((text) => text.language.name == lang)[0].name,
|
|
id: type.type.url.split("/")[6]
|
|
});
|
|
});
|
|
|
|
let evolutionChain = await getEvolutionChain(details.evolution_chain.url);
|
|
let evolutions = handleEvolutionChain(evolutionChain);
|
|
|
|
return {
|
|
name: details.names.filter((text) => text.language.name == lang)[0].name,
|
|
sprite: result.sprites['front_default'],
|
|
image: result.sprites.other['official-artwork']['front_default'],
|
|
types: types,
|
|
id: result.id,
|
|
evolutions: evolutions,
|
|
description: details.flavor_text_entries.filter((text) => text.language.name == lang)[0].flavor_text,
|
|
genera: details.genera.filter((text) => text.language.name == lang)[0].genus,
|
|
};
|
|
}
|
|
|
|
async function getPokemonDetails(id) {
|
|
const url = `${pokemonDetailUrl}${id}`;
|
|
return await fetch(url, {
|
|
duration: "7d",
|
|
type: "json",
|
|
});
|
|
}
|
|
|
|
async function getEvolutionChain(url) {
|
|
return await fetch(url, {
|
|
duration: "7d",
|
|
type: "json",
|
|
});
|
|
}
|
|
|
|
function handleEvolutionChain(evolutionChain) {
|
|
const chain = evolutionChain.chain
|
|
|
|
if(chain.evolves_to.length != 0) {
|
|
let evoChain = setupEvolution(chain);
|
|
|
|
if(chain.evolves_to[0].evolves_to.length != 0) {
|
|
evoChain.push(setupEvolution(chain.evolves_to[0])[1]);
|
|
}
|
|
|
|
return evoChain;
|
|
}
|
|
}
|
|
function setupEvolution(chain) {
|
|
let evolutions = [];
|
|
chain.evolves_to.forEach(evolution => {
|
|
const method = evolution.evolution_details[0].trigger.name;
|
|
let lvl = 0;
|
|
let item = "";
|
|
if (method === "level-up") {
|
|
lvl = evolution.evolution_details[0].min_level;
|
|
} else if (method === "use-item") {
|
|
item = {
|
|
name: evolution.evolution_details[0].item.name,
|
|
id: evolution.evolution_details[0].item.url.split("/")[6],
|
|
};
|
|
}
|
|
|
|
evolutions.push({
|
|
id : evolution.species.url.split("/")[6],
|
|
method: evolution.evolution_details[0].trigger.name,
|
|
lvl,
|
|
item,
|
|
});
|
|
});
|
|
return [
|
|
{
|
|
id : chain.species.url.split("/")[6],
|
|
},
|
|
...evolutions
|
|
]
|
|
};
|
|
|
|
async function getTypeDetails(id) {
|
|
const url = `${typeUrl}${id}`;
|
|
return await fetch(url, {
|
|
duration: "7d",
|
|
type: "json",
|
|
});
|
|
}
|
|
|
|
module.exports = async function () {
|
|
try {
|
|
const pokemons = [];
|
|
for (let i = 1; i <= POKEDEX_SIZE; i++) {
|
|
pokemons.push(await getPokemon(i));
|
|
}
|
|
|
|
/**
|
|
* TODO
|
|
* Evolutions en branches (Evoli)
|
|
* Evolutions de pokemon non présent (pikachu, ronflex)
|
|
* Sprite objet evolution
|
|
*/
|
|
return pokemons
|
|
} catch (error) {
|
|
console.error(`Fetch failed in pokemon.js. ${error}`);
|
|
}
|
|
}; |