|
| 1 | +import axios from 'axios'; |
| 2 | +import { getCache, setCache } from '../utils/cache.mjs'; |
| 3 | + |
| 4 | +export const getPokemonEndpointResources = async (req, res, next) => { |
| 5 | + try { |
| 6 | + const { pokemonIdOrName } = req.params; |
| 7 | + // Attempt to get all resources for the Pokémon endpoint from the cache |
| 8 | + let pokemonEndpointResources = getCache('pokemonEndpointResources'); |
| 9 | + |
| 10 | + if (!pokemonEndpointResources) { |
| 11 | + console.log( |
| 12 | + 'Fetching all resources for the Pokémon endpoint from PokéAPI' |
| 13 | + ); |
| 14 | + const { data } = await axios.get( |
| 15 | + `https://pokeapi.co/api/v2/pokemon/?limit=9000` |
| 16 | + ); |
| 17 | + const { count, results } = data; |
| 18 | + |
| 19 | + pokemonEndpointResources = { |
| 20 | + count, |
| 21 | + results: results.map(obj => { |
| 22 | + const { name, url } = obj; |
| 23 | + return { |
| 24 | + id: Number(url.split('/').filter(Boolean).pop()), |
| 25 | + name, |
| 26 | + url: url.replace( |
| 27 | + 'https://pokeapi.co/api/v2/', |
| 28 | + `${req.protocol}://${req.get('host')}/api/` |
| 29 | + ) |
| 30 | + }; |
| 31 | + }) |
| 32 | + }; |
| 33 | + |
| 34 | + // Cache all Pokémon names and routes |
| 35 | + setCache('pokemonEndpointResources', pokemonEndpointResources); |
| 36 | + } |
| 37 | + |
| 38 | + if (pokemonIdOrName) { |
| 39 | + // User is requesting a specific Pokémon, so pass the data to the next middleware |
| 40 | + // for id or name validation |
| 41 | + res.locals.pokemonEndpointResources = pokemonEndpointResources; |
| 42 | + next(); |
| 43 | + } else { |
| 44 | + // User is requesting all Pokémon names and routes, so send the data as a response |
| 45 | + res.send(pokemonEndpointResources); |
| 46 | + } |
| 47 | + } catch (err) { |
| 48 | + next(err); |
| 49 | + } |
| 50 | +}; |
| 51 | + |
| 52 | +export const getPokemonData = async (req, res, next) => { |
| 53 | + try { |
| 54 | + const { pokemonIdOrName } = req.params; |
| 55 | + console.log('Fetching Pokémon data from PokéAPI'); |
| 56 | + const { data } = await axios.get( |
| 57 | + `https://pokeapi.co/api/v2/pokemon/${pokemonIdOrName}` |
| 58 | + ); |
| 59 | + const { |
| 60 | + base_experience, |
| 61 | + height, |
| 62 | + id, |
| 63 | + name, |
| 64 | + order, |
| 65 | + sprites, |
| 66 | + stats, |
| 67 | + types, |
| 68 | + weight |
| 69 | + } = data; |
| 70 | + |
| 71 | + // Remove unnecessary data for the required project |
| 72 | + const simplifiedPokemonData = { |
| 73 | + base_experience, |
| 74 | + height, |
| 75 | + id, |
| 76 | + name, |
| 77 | + order, |
| 78 | + sprites: Object.keys(sprites) |
| 79 | + .filter(key => typeof sprites[key] === 'string') |
| 80 | + .reduce((obj, key) => { |
| 81 | + obj[key] = sprites[key]; |
| 82 | + return obj; |
| 83 | + }, {}), |
| 84 | + stats, |
| 85 | + types, |
| 86 | + weight |
| 87 | + }; |
| 88 | + |
| 89 | + // Cache simplified data by id and name, then send it as a response |
| 90 | + setCache(simplifiedPokemonData.id, simplifiedPokemonData); |
| 91 | + setCache(simplifiedPokemonData.name, simplifiedPokemonData); |
| 92 | + |
| 93 | + res.send(simplifiedPokemonData); |
| 94 | + } catch (err) { |
| 95 | + next(err); |
| 96 | + } |
| 97 | +}; |
0 commit comments