Skip to content

Commit

Permalink
fix: simplify protocol handling (#599)
Browse files Browse the repository at this point in the history
  • Loading branch information
scissorsneedfoodtoo committed May 18, 2024
1 parent ce2fec6 commit d1a219c
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion apps/pokeapi-proxy/api/pokemon/pokemon.handlers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
);
Expand All @@ -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/`
)
};
})
Expand Down

0 comments on commit d1a219c

Please sign in to comment.