Pokemon API Fetch
Pokemons Fetch
data[]
keys
name, sprites, types
Sprites
data.sprites{} key: 'https:// ...'
keys
back_default, back_shiny, front_default, front_shiny
Types
data.types[] { slot, type = {name, url} }
keys
slot, name = type.name
Generator Funktion
generator.js
const outputSet = new Set()
let outputObj = {}
async function doFetchGen() {
let {
name,
sprites,
types
} = await fetchData(randNum(1, 400))
let output = Object.assign({}, {
name,
sprites,
types
})
delete output.sprites.other
delete output.sprites.versions
let newSpriteObj = {}
for (const key in output.sprites) {
if (Object.hasOwnProperty.call(output.sprites, key)) {
const url = output.sprites[key]
if (url && !key.includes('female')) {
newSpriteObj[key] = url
}
}
}
output.sprites = newSpriteObj
outputObj = output
outputSet.add(output)
return output
}
helper
helper.js
const fetchData = async (n = 1) => {
try {
const res = await fetch(`https://pokeapi.co/api/v2/pokemon/${n}`)
const data = await res.json()
fetchedNumber = number
return data
} catch (error) {
throw new Error(error)
}
}
const randNum = (min, max) => {
min = Math.ceil(min)
max = Math.floor(max)
return Math.floor(Math.random() * (max - min)) + min
}
Example
~pokemon.json
{
"name": "machoke",
"sprites": {
"back_default": "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/back/67.png",
"back_shiny": "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/back/shiny/67.png",
"front_default": "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/67.png",
"front_shiny": "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/shiny/67.png"
},
"types": [
{
"slot": 1,
"type": {
"name": "fighting",
"url": "https://pokeapi.co/api/v2/type/2/"
}
}
]
}
API Data
pokemon.min.json
[{"name":"machoke","sprites":{"back_default":"https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/back/67.png","back_shiny":"https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/back/shiny/67.png","front_default":"https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/67.png","front_shiny":"https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/shiny/67.png"},"types":[{"slot":1,"type":{"name":"fighting","url":"https://pokeapi.co/api/v2/type/2/"}}]},{"name":"nuzleaf","sprites":{"back_default":"https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/back/274.png","back_shiny":"https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/back/shiny/274.png","front_default":"https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/274.png","front_shiny":"https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/shiny/274.png"},"types":[{"slot":1,"type":{"name":"grass","url":"https://pokeapi.co/api/v2/type/12/"}},{"slot":2,"type":{"name":"dark","url":"https://pokeapi.co/api/v2/type/17/"}}]},{"name":"magby","sprites":{"back_default":"https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/back/240.png","back_shiny":"https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/back/shiny/240.png","front_default":"https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/240.png","front_shiny":"https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/shiny/240.png"},"types":[{"slot":1,"type":{"name":"fire","url":"https://pokeapi.co/api/v2/type/10/"}}]},{"name":"empoleon","sprites":{"back_default":"https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/back/395.png","back_shiny":"https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/back/shiny/395.png","front_default":"https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/395.png","front_shiny":"https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/shiny/395.png"},"types":[{"slot":1,"type":{"name":"water","url":"https://pokeapi.co/api/v2/type/11/"}},{"slot":2,"type":{"name":"steel","url":"https://pokeapi.co/api/v2/type/9/"}}]},{"name":"torkoal","sprites":{"back_default":"https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/back/324.png","back_shiny":"https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/back/shiny/324.png","front_default":"https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/324.png","front_shiny":"https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/shiny/324.png"},"types":[{"slot":1,"type":{"name":"fire","url":"https://pokeapi.co/api/v2/type/10/"}}]},{"name":"pupitar","sprites":{"back_default":"https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/back/247.png","back_shiny":"https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/back/shiny/247.png","front_default":"https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/247.png","front_shiny":"https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/shiny/247.png"},"types":[{"slot":1,"type":{"name":"rock","url":"https://pokeapi.co/api/v2/type/6/"}},{"slot":2,"type":{"name":"ground","url":"https://pokeapi.co/api/v2/type/5/"}}]}]