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
A parsing function that converts environment variables to strongly-typed values
<
T
,R
,D
>(key
,options
):R
extendstrue
?EnvInferType
<T
> :D
extendsundefined
?undefined
|EnvInferType
<T
> :EnvInferType
<T
>
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