From 779cc110222e8e338e9e53b57595350cfd30329b Mon Sep 17 00:00:00 2001 From: scissorsneedfoodtoo Date: Thu, 16 May 2024 18:47:14 +0900 Subject: [PATCH] fix: simplify protocol handling --- 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/` ) }; })