@baeta/env
Interfaces
EnvOptions<T, R, D>
Type Parameters
Type Parameter |
---|
|
|
|
Properties
Property | Type | Description |
---|---|---|
|
|
The expected type of the environment variable. |
|
|
Default value if the environment variable is not provided. |
|
|
Whether the environment variable is required. |
|
( |
Custom resolver to convert the environment variable to the expected type. |
Type Aliases
EnvInferType<T>
EnvInferType<
T
>:T
extends"string"
?string
:T
extends"number"
?number
:boolean
Maps environment variable types to their TypeScript equivalents.
Type Parameters
Type Parameter | Description |
---|---|
|
The environment variable type |
EnvTypes
EnvTypes:
"string"
|"number"
|"boolean"
Supported environment variable types.
Functions
createEnvParser()
createEnvParser(
getValue
): <T
,R
,D
>(key
,options
) =>R
extendstrue
?EnvInferType
<T
> :D
extendsundefined
?undefined
|EnvInferType
<T
> :EnvInferType
<T
>
Creates an environment variable parser.. See https://baeta.io/docs/guides/environment
Parameters
Parameter | Type | Description |
---|---|---|
|
( |
Function to retrieve environment variable values |
Returns
Function
A parsing function that converts environment variables to strongly-typed values
Type Parameters
Type Parameter |
---|
|
|
|
Parameters
Parameter | Type |
---|---|
|
|
|
|
Returns
R
extends true
? EnvInferType
<T
> : D
extends undefined
? undefined
| EnvInferType
<T
> : EnvInferType
<T
>
Example
const parse = createEnvParser((key) => process.env[key]);
const port = parse("PORT", {
type: "number",
required: true,
default: 3000,
});
const debug = parse("DEBUG", {
type: "boolean",
default: false,
});
Throws
When:
- A required value is missing and has no default
- The value type doesn't match the specified type
- A custom resolver returns an incorrect type