Modules@robinpath/env
env

@robinpath/env

0.1.1Node.jsPublic

Secure environment variable management with sensitive value redaction and protected system vars

Env

Secure environment variable management with sensitive value redaction and protected system vars

Package: @robinpath/env | Category: Other | Type: Utility

Authentication

No authentication required. All functions are available immediately.

Use Cases

Use the env module when you need to:

  • Get the value of an environment variable -- Use env.get to perform this operation
  • Check if an environment variable exists -- Use env.has to perform this operation
  • Get all environment variables (sensitive values are redacted by default) -- Use env.all to perform this operation
  • Delete an environment variable (protected system vars cannot be deleted) -- Use env.delete to perform this operation
  • Mark an environment variable as sensitive (will be redacted in env.all output) -- Use env.secret to perform this operation

Quick Reference

FunctionDescriptionReturns
getGet the value of an environment variableThe environment variable value, the default value, or null if not set
setSet an environment variable (protected system vars cannot be overwritten)True if the variable was set
hasCheck if an environment variable existsTrue if the variable exists, false otherwise
allGet all environment variables (sensitive values are redacted by default)Object with all environment variable key-value pairs (secrets shown as ***)
deleteDelete an environment variable (protected system vars cannot be deleted)True if the variable was deleted
secretMark an environment variable as sensitive (will be redacted in env.all output)True if marked
loadLoad environment variables from a .env file (won't override existing or protected vars)Number of new variables loaded

Functions

get

Get the value of an environment variable

Module: env | Returns: string -- The environment variable value, the default value, or null if not set

env.get "NODE_ENV"
ParameterTypeRequiredDescription
namestringYesName of the environment variable
defaultValuestringNoDefault value if the variable is not set

set

Set an environment variable (protected system vars cannot be overwritten)

Module: env | Returns: boolean -- True if the variable was set

env.set "API_URL" "https://api.example.com"
ParameterTypeRequiredDescription
namestringYesName of the environment variable (must match [A-Za-z_][A-Za-z0-9_]*)
valuestringYesValue to set

has

Check if an environment variable exists

Module: env | Returns: boolean -- True if the variable exists, false otherwise

env.has "NODE_ENV"
ParameterTypeRequiredDescription
namestringYesName of the environment variable

all

Get all environment variables (sensitive values are redacted by default)

Module: env | Returns: object -- Object with all environment variable key-value pairs (secrets shown as ***)

env.all
ParameterTypeRequiredDescription
showSensitivebooleanNoPass true to show sensitive values unredacted (default: false)

delete

Delete an environment variable (protected system vars cannot be deleted)

Module: env | Returns: boolean -- True if the variable was deleted

env.delete "OLD_KEY"
ParameterTypeRequiredDescription
namestringYesName of the environment variable to delete

secret

Mark an environment variable as sensitive (will be redacted in env.all output)

Module: env | Returns: boolean -- True if marked

env.secret "SMTP_PASS"
ParameterTypeRequiredDescription
namestringYesName of the environment variable to mark as sensitive

load

Load environment variables from a .env file (won't override existing or protected vars)

Module: env | Returns: number -- Number of new variables loaded

env.load ".env"
ParameterTypeRequiredDescription
pathstringYesPath to the .env file

Error Handling

All functions throw on failure. Common errors:

ErrorCause
Environment variable name cannot be emptyCheck the error message for details
Invalid environment variable name: "...". Must match /^[A-Za-z_][A-Za-z0-9_]*$/Check the error message for details
Cannot overwrite protected system variable: "..."Check the error message for details
Cannot delete protected system variable: "..."Check the error message for details
@desc "Get and validate result"
do
  set $result as env.get "NODE_ENV"
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. List and iterate

Retrieve all items and loop through them.

@desc "Get and iterate results"
do
  set $result as env.get "NODE_ENV"
  each $item in $result
    print $item
  end
enddo

2. Multi-step Env workflow

Chain multiple env operations together.

@desc "Get, has, and more"
do
  set $r_get as env.get "NODE_ENV"
  set $r_has as env.has "NODE_ENV"
  set $r_all as env.all
  print "All operations complete"
enddo

3. Safe get with validation

Check results before proceeding.

@desc "Get and validate result"
do
  set $result as env.get "NODE_ENV"
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

  • json -- JSON module for complementary functionality

Versions (1)

VersionTagPublished
0.1.1latest1 months ago
Install
$ robinpath add @robinpath/env

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.1
LicenseMIT
Unpacked Size6.0 KB
Versions1
Weekly Downloads26
Total Downloads26
Stars0
Last Publish1 months ago
Created1 months ago

Keywords

Category

utilities