Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update readme to add identifier feature #38

Merged
merged 1 commit into from
Mar 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ Install the package from the npm packages registry as follows.

```sh
npm i @adonisjs/env

yarn add @adonisjs/env
```

## EnvLoader
Expand Down Expand Up @@ -52,7 +50,7 @@ const envParser = new EnvParser(`
HOST=localhost
`)

console.log(envParser.parse()) // { PORT: '3000', HOST: 'localhost' }
console.log(await envParser.parse()) // { PORT: '3000', HOST: 'localhost' }
```

The return value of `parser.parse` is an object with key-value pair. The parser also has support for interpolation.
Expand All @@ -63,6 +61,27 @@ By default, the parser prefers existing `process.env` values when they exist. Ho
new EnvParser(envContents, { ignoreProcessEnv: true })
```

### Identifier

You can define an "identifier" to be used for interpolation. The identifier is a string that prefix the environment variable value and let you customize the value resolution.

```ts
import { readFile } from 'node:fs/promises'
import { EnvParser } from '@adonisjs/env'

EnvParser.identifier('file', (value) => {
return readFile(value, 'utf-8')
})

const envParser = new EnvParser(`
DB_PASSWORD=file:/run/secret/db_password
`)

console.log(await envParser.parse()) // { DB_PASSWORD: 'Value from file /run/secret/db_password' }
```

This can be useful when you are using secrets manager like `Docker Secret`, `HashiCorp Vault`, `Google Secrets Manager` and others to manage your secrets.

## Validating environment variables
Once you have the parsed objects, you can optionally validate them against a pre-defined schema. We recommend validation for the following reasons.

Expand Down
Loading