Signature
Parameters
The name of the environment variable to read
Optional fallback URL if the environment variable is not set. If not provided and the variable is missing, an error will be thrown.
Returns
The parsed URL object from the environment variable or fallback
Behavior
- Reads the environment variable and trims whitespace
- Falls back to the string representation of the fallback URL if the variable is not set
- Throws an error if both the variable and fallback are missing
- Parses the string value using the
URLconstructor - Validates that the value is a valid URL
- Throws a
TypeErrorif the value cannot be parsed as a URL
Error Handling
- Throws
Errorwith message"$KEY is missing"when the environment variable is not set and no fallback is provided - Throws
TypeErrorwith message"$KEY is not a URL: VALUE"when the value cannot be parsed as a valid URL. The original error is attached as thecause.
