Skip to content

Commit d1a219c

Browse files
fix: simplify protocol handling (#599)
1 parent ce2fec6 commit d1a219c

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

apps/pokeapi-proxy/api/pokemon/pokemon.handlers.mjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export const getPokemonEndpointResources = async (req, res, next) => {
1111
console.log(
1212
'Fetching all resources for the Pokémon endpoint from PokéAPI'
1313
);
14+
const host = req.get('host');
15+
const protocol = host.match(/localhost\:\d+/) ? 'http' : 'https';
1416
const { data } = await axios.get(
1517
`https://pokeapi.co/api/v2/pokemon/?limit=9000`
1618
);
@@ -20,12 +22,13 @@ export const getPokemonEndpointResources = async (req, res, next) => {
2022
count,
2123
results: results.map(obj => {
2224
const { name, url } = obj;
25+
2326
return {
2427
id: Number(url.split('/').filter(Boolean).pop()),
2528
name,
2629
url: url.replace(
2730
'https://pokeapi.co/api/v2/',
28-
`${req.protocol}://${req.get('host')}/api/`
31+
`${protocol}://${host}/api/`
2932
)
3033
};
3134
})

0 commit comments

Comments
 (0)