easier-pokemon
Version:
An easier way to get pokemon stats.
289 lines (197 loc) • 7.55 kB
JavaScript
const fetch = require('node-fetch')
let baseURL = "https://pokeapi.co/api/v2";
async function PokeApi(options, callback) {
try {
if (options.pokemon) {
var baseArgs = baseURL + `/pokemon/${options.pokemon.toLowerCase()}`
await fetch(`${baseArgs}`).then(res => res.json()).then(res => {
const data = res
//Do nothing with the data
})
}
if (options.pokedata === true) {
var baseArgs = baseURL + `/pokemon/${options.pokemon.toLowerCase()}`
await fetch(`${baseArgs}`).then(res => res.json()).then(res => {
const data = res
callback(data) //returns pokemon data.
})
}
if (options.weight === 'all') {
await fetch(`${baseArgs}`).then(res => res.json()).then(res => {
const data = res
const {
weight
} = data;
callback(weight)
})
}
if (options.abilities === 'all') {
await fetch(`${baseArgs}`).then(res => res.json()).then(res => {
const data = res
const {
abilities
} = data;
var abilitiesarray = abilities
callback(abilitiesarray)
})
}
if (options.abilities === 'names') {
await fetch(`${baseArgs}`).then(res => res.json()).then(res => {
const data = res
const {
abilities
} = data;
const ablength = abilities.length - 1
const abilitiesarr = []
for (var i = 0; i <= ablength; i++) {
const abilitynames = abilities[i].ability.name
abilitiesarr.push(abilitynames);
}
callback(abilitiesarr)
})
}
if (options.abilities === 'urls') {
await fetch(`${baseArgs}`).then(res => res.json()).then(res => {
const data = res
const {
abilities
} = data;
const abulength = abilities.length - 1
const abilitiesunames = []
for (var i = 0; i <= abulength; i++) {
const abilityunames = abilities[i].ability.url
abilitiesunames.push(abilityunames);
}
callback(abilitiesunames)
})
}
if (options.sprites === 'back') {
await fetch(`${baseArgs}`).then(res => res.json()).then(res => {
const data = res
const {
sprites
} = data;
callback(sprites.back_default)
})
}
if (options.sprites === 'back_shiny') {
await fetch(`${baseArgs}`).then(res => res.json()).then(res => {
const data = res
const {
sprites
} = data;
callback(sprites.back_shiny)
})
}
if (options.sprites === 'front') {
await fetch(`${baseArgs}`).then(res => res.json()).then(res => {
const data = res
const {
sprites
} = data;
callback(sprites.front_default)
})
}
if (options.sprites === 'front_shiny') {
await fetch(`${baseArgs}`).then(res => res.json()).then(res => {
const data = res
const {
sprites
} = data;
callback(sprites.front_shiny)
})
}
if (options.fsprites === 'back') {
await fetch(`${baseArgs}`).then(res => res.json()).then(res => {
const data = res
const {
sprites
} = data;
callback(sprites.back_female)
})
}
if (options.fsprites === 'back_shiny') {
await fetch(`${baseArgs}`).then(res => res.json()).then(res => {
const data = res
const {
sprites
} = data;
callback(sprites.back_shiny_female)
})
}
if (options.fsprites === 'front') {
await fetch(`${baseArgs}`).then(res => res.json()).then(res => {
const data = res
const {
sprites
} = data;
callback(sprites.front_female)
})
}
if (options.fsprites === 'front_shiny') {
await fetch(`${baseArgs}`).then(res => res.json()).then(res => {
const data = res
const {
sprites
} = data;
callback(sprites.front_shiny_female)
})
}
if (options.experience === 'base') {
await fetch(`${baseArgs}`).then(res => res.json()).then(res => {
const data = res
const {
base_experience
} = data;
callback(base_experience)
})
}
if (options.types === 'all') {
await fetch(`${baseArgs}`).then(res => res.json()).then(res => {
const data = res
const {
types
} = data;
callback(types)
})
}
if (options.types === 'name') {
await fetch(`${baseArgs}`).then(res => res.json()).then(res => {
const data = res
const {
types
} = data;
const typelength = types.length - 1
const typesarr = []
for (var i = 0; i <= typelength; i++) {
const typesname = types[i].type.name
typesarr.push(typesname);
}
callback(typesarr)
})
}
if (options.types === 'url') {
await fetch(`${baseArgs}`).then(res => res.json()).then(res => {
const data = res
const {
types
} = data;
const typeulength = types.length - 1
const typesuarr = []
for (var i = 0; i <= typeulength; i++) {
const typesurl = types[i].type.url
typesuarr.push(typesurl)
}
callback(typesuarr)
})
}
} catch (err) {
if (!options.pokemon) {
throw new Error("Please choose a pokemon by adding: pokemon: 'pokemon-name'")
} else {
console.error(`Error: `, err)
throw new Error("There was an error. Probably because of an invalid pokemon. To fix this issue, try adding dashes between the spaces. If this does not work, try double checking \nthe spelling of the pokemon.")
}
}
}
module.exports.PokeApi = PokeApi;