The API request works, but there is still much room for improvement.
The PokéApi endpoint url is still hardcoded inside fetchRequest
:
const fetchRequest = Effect.tryPromise({
/// 👇 Hardcoded endpoint url
try: () => fetch("https://pokeapi.co/api/v2/pokemon/garchomp/"),
catch: () => new FetchError(),
});
This may cause problems:
- Hard to find source API url
- Risk of code duplication
- No way to change this value when testing
Those problems have the potential to make testing difficult and the code a lot less easy to understand and maintain. No good. Let's fix all these with effect!
What are configuration values
Configuration values are usually constants extracted from the process (process.env
) or hardcoded at build time.
It's useful to organize all these values in one place, so that we can inspect and update them at a glance.
As an example, for environmental variables you usually create a .env
file. You can then inject different configurations for different environments (.env.development
or .env.testing
).
Here is an example of .env
file that I use for this website:
CONTENT_DIR=
CONVERTKIT_API_KEY=
CONVERTKIT_API_URL=
CONVERTKIT_FORM_ID=
SUPABASE_KEY=
SUPABASE_URL=
In our example, the PokéApi base url "https://pokeapi.co" is such a constant.