From d1a219cad0084695616c564c9ab29e76eb73dac6 Mon Sep 17 00:00:00 2001 From: Kristofer Koishigawa Date: Sat, 18 May 2024 16:22:21 +0900 Subject: [PATCH] fix: simplify protocol handling (#599) --- apps/pokeapi-proxy/api/pokemon/pokemon.handlers.mjs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/apps/pokeapi-proxy/api/pokemon/pokemon.handlers.mjs b/apps/pokeapi-proxy/api/pokemon/pokemon.handlers.mjs index 71f52621..4903cd37 100644 --- a/apps/pokeapi-proxy/api/pokemon/pokemon.handlers.mjs +++ b/apps/pokeapi-proxy/api/pokemon/pokemon.handlers.mjs @@ -11,6 +11,8 @@ export const getPokemonEndpointResources = async (req, res, next) => { console.log( 'Fetching all resources for the Pokémon endpoint from PokéAPI' ); + const host = req.get('host'); + const protocol = host.match(/localhost\:\d+/) ? 'http' : 'https'; const { data } = await axios.get( `https://pokeapi.co/api/v2/pokemon/?limit=9000` ); @@ -20,12 +22,13 @@ export const getPokemonEndpointResources = async (req, res, next) => { count, results: results.map(obj => { const { name, url } = obj; + return { id: Number(url.split('/').filter(Boolean).pop()), name, url: url.replace( 'https://pokeapi.co/api/v2/', - `${req.protocol}://${req.get('host')}/api/` + `${protocol}://${host}/api/` ) }; })