@efebia/env-loader 0.9.12 → 0.9.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +114 -0
- package/package.json +7 -2
package/README.md
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# @efebia/env-loader
|
|
2
|
+
|
|
3
|
+
Type-safe environment variable loading and validation from YAML files or JSON environment variables.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @efebia/env-loader
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Declaring your schema
|
|
14
|
+
|
|
15
|
+
Extend the `EnvSchema` interface via module augmentation to get type-safe key access:
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
// env.d.ts
|
|
19
|
+
import '@efebia/env-loader';
|
|
20
|
+
|
|
21
|
+
declare module '@efebia/env-loader' {
|
|
22
|
+
interface EnvSchema {
|
|
23
|
+
DB_HOST: string;
|
|
24
|
+
DB_PORT: number;
|
|
25
|
+
API_KEY: string;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### Loading from a YAML file
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import { EnvironmentClient, yamlFileKeyLoader } from '@efebia/env-loader';
|
|
34
|
+
import { readFileSync } from 'node:fs';
|
|
35
|
+
|
|
36
|
+
const yamlContent = readFileSync('./config.yaml', 'utf-8');
|
|
37
|
+
|
|
38
|
+
const env = new EnvironmentClient({
|
|
39
|
+
syncKeyLoaders: [yamlFileKeyLoader(yamlContent)],
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
await env.loadKeys();
|
|
43
|
+
|
|
44
|
+
const dbHost = env.getEnv('DB_HOST'); // string
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Loading from a JSON environment variable
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
import { EnvironmentClient, jsonProcessEnvKeyLoader } from '@efebia/env-loader';
|
|
51
|
+
|
|
52
|
+
// Expects process.env.APP_CONFIG to contain a JSON string
|
|
53
|
+
const env = new EnvironmentClient({
|
|
54
|
+
asyncKeyLoaders: [jsonProcessEnvKeyLoader('APP_CONFIG')],
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
await env.loadKeys();
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Using `process.env` as storage
|
|
61
|
+
|
|
62
|
+
`ProcessEnvStorageEnvironmentClient` reads directly from `process.env`, auto-parsing numbers, booleans, and JSON strings:
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
import { ProcessEnvStorageEnvironmentClient } from '@efebia/env-loader';
|
|
66
|
+
|
|
67
|
+
const env = new ProcessEnvStorageEnvironmentClient();
|
|
68
|
+
|
|
69
|
+
const port = env.getEnv('DB_PORT'); // auto-parsed to number
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## API
|
|
73
|
+
|
|
74
|
+
### `EnvironmentClient`
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
new EnvironmentClient(opts?: Partial<EnvironmentClientOptions>)
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
| Option | Type | Default | Description |
|
|
81
|
+
|--------|------|---------|-------------|
|
|
82
|
+
| `storage` | `object` | `{}` | Object used as the key/value store |
|
|
83
|
+
| `syncKeyLoaders` | `(() => Record<string, unknown>)[]` | `[]` | Loaders executed synchronously at construction |
|
|
84
|
+
| `asyncKeyLoaders` | `(() => Promise<Record<string, unknown>>)[]` | `[]` | Loaders executed when `loadKeys()` is called |
|
|
85
|
+
| `loadSyncKeysAtStartup` | `boolean` | `true` | Run sync loaders in the constructor |
|
|
86
|
+
|
|
87
|
+
**Methods:**
|
|
88
|
+
|
|
89
|
+
- `loadKeys()` — runs all async and sync loaders
|
|
90
|
+
- `getEnv(...keys)` — returns value(s) for the given key(s); throws if a key is missing
|
|
91
|
+
- `getOptionalEnv(...keys)` — like `getEnv` but silently skips missing keys
|
|
92
|
+
- `setEnv(key, value)` — sets a key in the storage
|
|
93
|
+
|
|
94
|
+
Keys accessed via `getEnv` / `getOptionalEnv` are also available in camelCase (e.g. `DB_HOST` → `dbHost`).
|
|
95
|
+
|
|
96
|
+
### `ProcessEnvStorageEnvironmentClient`
|
|
97
|
+
|
|
98
|
+
Extends `EnvironmentClient` with `process.env` as storage. Automatically parses string values to their native types (number, boolean, JSON object/array).
|
|
99
|
+
|
|
100
|
+
### `yamlFileKeyLoader(yamlContent: string)`
|
|
101
|
+
|
|
102
|
+
Returns a sync key loader that parses a YAML string.
|
|
103
|
+
|
|
104
|
+
### `jsonProcessEnvKeyLoader(key: string)`
|
|
105
|
+
|
|
106
|
+
Returns an async key loader that reads and JSON-parses `process.env[key]`.
|
|
107
|
+
|
|
108
|
+
### `EnvSchema`
|
|
109
|
+
|
|
110
|
+
Empty interface — extend it via module augmentation to declare your environment keys and their types.
|
|
111
|
+
|
|
112
|
+
## License
|
|
113
|
+
|
|
114
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@efebia/env-loader",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.14",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"devDependencies": {
|
|
6
6
|
"@types/jest": "^29.2.5",
|
|
@@ -19,5 +19,10 @@
|
|
|
19
19
|
"scripts": {
|
|
20
20
|
"build": "tsc"
|
|
21
21
|
},
|
|
22
|
-
"main": "./lib"
|
|
22
|
+
"main": "./lib",
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git+https://github.com/efebia-com/be-libs.git",
|
|
26
|
+
"directory": "packages/env-loader"
|
|
27
|
+
}
|
|
23
28
|
}
|