@pitininja/envious 6.0.0-beta8 → 6.0.1
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 +159 -0
- package/package.json +6 -5
package/README.md
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
# Envious
|
|
2
|
+
|
|
3
|
+
[](https://badge.fury.io/js/@pitininja%2Fenvious)
|
|
4
|
+
|
|
5
|
+
> Parse and validate environment variables
|
|
6
|
+
|
|
7
|
+
- [Install](#install)
|
|
8
|
+
- [Resolvers](#resolvers)
|
|
9
|
+
- [Usage](#usage)
|
|
10
|
+
- [Options](#options)
|
|
11
|
+
- [Errors](#errors)
|
|
12
|
+
- [Migration](#migration)
|
|
13
|
+
- [Legacy versions](#legacy-versions)
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
Envious works by installing and using two packages : the core package and a resolver.
|
|
18
|
+
|
|
19
|
+
### Core package
|
|
20
|
+
|
|
21
|
+
```shell
|
|
22
|
+
npm i @pitininja/envious
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### Resolvers
|
|
26
|
+
|
|
27
|
+
#### Typebox
|
|
28
|
+
|
|
29
|
+
```shell
|
|
30
|
+
npm i @pitininja/envious-typebox
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
- [Typebox resolver documentation](https://codeberg.org/pitininja/envious/src/branch/master/packages/resolvers/typebox/README.md)
|
|
34
|
+
- [Official Typebox documentation](https://sinclairzx81.github.io/typebox/)
|
|
35
|
+
|
|
36
|
+
#### Zod
|
|
37
|
+
|
|
38
|
+
```shell
|
|
39
|
+
npm i @pitininja/envious-zod
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
- [Zod resolver documentation](https://codeberg.org/pitininja/envious/src/branch/master/packages/resolvers/zod/README.md)
|
|
43
|
+
- [Official Zod documentation](https://zod.dev/)
|
|
44
|
+
|
|
45
|
+
## Usage
|
|
46
|
+
|
|
47
|
+
Here is an example using the Typebox resolver :
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
import { envious } from '@pitininja/envious';
|
|
51
|
+
import { typeboxResolver } from '@pitininja/envious-typebox';
|
|
52
|
+
import { Type } from '@sinclair/typebox';
|
|
53
|
+
|
|
54
|
+
const env = envious({
|
|
55
|
+
resolver: typeboxResolver({
|
|
56
|
+
schema: Type.Object({
|
|
57
|
+
STRING_VAR: Type.String(),
|
|
58
|
+
NUMBER_VAR: Type.Integer(),
|
|
59
|
+
BOOLEAN_VAR_WITH_DEFAULT: Type.Boolean({ default: false }),
|
|
60
|
+
OPTIONAL_VAR: Type.Optional(Type.String())
|
|
61
|
+
})
|
|
62
|
+
})
|
|
63
|
+
});
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Then run the script with loaded environment variables, and these variables will be parsed and validated.
|
|
67
|
+
|
|
68
|
+
```shell
|
|
69
|
+
# Example loading environment variables with Node.js
|
|
70
|
+
node --env-file=.env index.js
|
|
71
|
+
# Example loading environment variables with TSX
|
|
72
|
+
tsx --env-file=.env index.ts
|
|
73
|
+
# Example loading environment variables with DotEnv
|
|
74
|
+
dotenv -e .env -- node index.js
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Options
|
|
78
|
+
|
|
79
|
+
### Logging
|
|
80
|
+
|
|
81
|
+
In addition to throwing an `EnviousError`, errors can be logged in the console. To do so, use the `logErrors` option :
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
import { envious } from '@pitininja/envious';
|
|
85
|
+
import { typeboxResolver } from '@pitininja/envious-typebox';
|
|
86
|
+
import { Type } from '@sinclair/typebox';
|
|
87
|
+
|
|
88
|
+
const env = envious({
|
|
89
|
+
resolver: typeboxResolver({
|
|
90
|
+
schema: Type.Object({
|
|
91
|
+
STRING_VAR: Type.String()
|
|
92
|
+
})
|
|
93
|
+
}),
|
|
94
|
+
logErrors: true
|
|
95
|
+
});
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
You can also provide your own logging function :
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
import { envious } from '@pitininja/envious';
|
|
102
|
+
import { typeboxResolver } from '@pitininja/envious-typebox';
|
|
103
|
+
import { Type } from '@sinclair/typebox';
|
|
104
|
+
|
|
105
|
+
const env = envious({
|
|
106
|
+
resolver: typeboxResolver({
|
|
107
|
+
schema: Type.Object({
|
|
108
|
+
STRING_VAR: Type.String()
|
|
109
|
+
})
|
|
110
|
+
}),
|
|
111
|
+
logErrors: true,
|
|
112
|
+
logger: (message) => {
|
|
113
|
+
myCustomLogger(message)
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## Errors
|
|
119
|
+
|
|
120
|
+
If something goes wrong, Envious will throw an error of class `EnviousError`.
|
|
121
|
+
|
|
122
|
+
An `EnviousError` error contains a message and a list of `EnviousErrorVariable`.
|
|
123
|
+
|
|
124
|
+
Here is a simple example of how to handle Envious errors :
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
import { envious, EnviousError } from '@pitininja/envious';
|
|
128
|
+
import { typeboxResolver } from '@pitininja/envious-typebox';
|
|
129
|
+
import { Type } from '@sinclair/typebox';
|
|
130
|
+
|
|
131
|
+
try {
|
|
132
|
+
const env = envious({
|
|
133
|
+
resolver: typeboxResolver({
|
|
134
|
+
schema: Type.Object({
|
|
135
|
+
STRING_VAR: Type.String()
|
|
136
|
+
})
|
|
137
|
+
})
|
|
138
|
+
});
|
|
139
|
+
} catch (err: unknown) {
|
|
140
|
+
if (err instanceof EnviousError) {
|
|
141
|
+
// General error message
|
|
142
|
+
console.log('Message:', err.message);
|
|
143
|
+
// Array of EnviousErrorVariable
|
|
144
|
+
console.log('Errors:', err.errors);
|
|
145
|
+
for (const { name, messages } of err.errors) {
|
|
146
|
+
// Variable name and error messages related to that variable
|
|
147
|
+
console.log(`${name} : ${messages.join(', ')}`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## Migration
|
|
154
|
+
|
|
155
|
+
[Migration guide to v6](https://codeberg.org/pitininja/envious/src/branch/master/doc/migration-v6.md)
|
|
156
|
+
|
|
157
|
+
## Legacy versions
|
|
158
|
+
|
|
159
|
+
[Legacy documentation for v5 or lower](https://codeberg.org/pitininja/envious/src/branch/master/doc/legacy.md)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pitininja/envious",
|
|
3
|
-
"version": "6.0.
|
|
3
|
+
"version": "6.0.1",
|
|
4
4
|
"license": "AGPL-3.0-or-later",
|
|
5
5
|
"homepage": "https://codeberg.org/pitininja/envious",
|
|
6
6
|
"repository": {
|
|
@@ -12,11 +12,12 @@
|
|
|
12
12
|
},
|
|
13
13
|
"files": [
|
|
14
14
|
"./dist",
|
|
15
|
-
"./package.json"
|
|
15
|
+
"./package.json",
|
|
16
|
+
"./README.md"
|
|
16
17
|
],
|
|
17
|
-
"main": "./dist/
|
|
18
|
-
"types": "./dist/
|
|
18
|
+
"main": "./dist/index.js",
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
19
20
|
"scripts": {
|
|
20
|
-
"build": "npx tsgo --build --clean tsconfig.json && npx tsgo -b ./tsconfig.json"
|
|
21
|
+
"build": "npx tsgo --build --clean ./tsconfig.build.json && npx tsgo -b ./tsconfig.build.json"
|
|
21
22
|
}
|
|
22
23
|
}
|