Skip to main content

Class: Env<T>

Sometimes you need to store sensitive information, like API keys, database URLs, or Discord Credentials.

import { Env } from 'robo'

Env.loadSync({ mode: 'dev' })
Env.data().NODE_ENV // 'development'

Use the Env class to load environment variables from a file and access them in a type-safe way.

Learn more: Environment Variables

Type Parameters

Type Parameter
T

Constructors

new Env()

new Env<T>(schema): Env<T>

Creates a new instance of the Env class with the specified schema with type-checking and default values.

const env = new Env({
discord: {
clientId: { env: 'DISCORD_CLIENT_ID' }
},
example: {
default: 'This is an example',
env: 'EXAMPLE_ENV'
},
nodeEnv: { env: 'NODE_ENV' }
})

// Returns the value of the DISCORD_CLIENT_ID environment variable
env.get('discord.clientId')

Parameters

ParameterTypeDescription
schemaTThe schema of environment variables to use for type-checking and default values.

Returns

Env<T>

Methods

get()

get<K>(key): ValueAtPath<T, K>

Retrieves the value of the environment variable specified by the dot-separated key. If the environment variable is not set, it returns the default value if provided.

Type Parameters

Type Parameter
K extends string

Parameters

ParameterTypeDescription
keyKThe dot-separated path to the environment variable in the schema.

Returns

ValueAtPath<T, K>

The value of the environment variable or its default.


data()

static data(): Record<string, string>

Returns

Record<string, string>

The environment variables that have been loaded most recently.


load()

static load(options): Promise<Record<string, string>>

Loads environment variables from a file and applies them to the current process.

Parameters

ParameterTypeDescription
optionsLoadOptionsCustomize where the file path, mode, and overwrite behavior.

Returns

Promise<Record<string, string>>

Record object containing loaded environment variables.


loadSync()

static loadSync(options): Record<string, string>

Loads environment variables from a file and applies them to the current process.

This operation is synchronous and will block the event loop. Use load for asynchronous loading.

Parameters

ParameterTypeDescription
optionsLoadOptionsCustomize where the file path, mode, and overwrite behavior.

Returns

Record<string, string>

Record object containing loaded environment variables.

Robo.js Logo

MIT © 2024 Robo.js By WavePlay