@tramvai/module-environment 1.35.10 → 1.39.0
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 +39 -30
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
# Env
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The env module is used to retrieve global application environment variables in runtime and pass these parameters to the client.
|
|
4
|
+
With a pre-defined list of variables used by the application, dynamically extended and validated at application startup
|
|
4
5
|
|
|
5
|
-
##
|
|
6
|
+
## Installation
|
|
6
7
|
|
|
7
|
-
|
|
8
|
+
Already supplied inside `@tramvai/module-common` and does not need to be installed if module-common is connected.
|
|
8
9
|
|
|
9
|
-
|
|
10
|
+
Otherwise, you need to install `@tramvai/module-environment`
|
|
10
11
|
|
|
11
12
|
## Explanation
|
|
12
13
|
|
|
13
|
-
###
|
|
14
|
+
### Dynamically generated list of used env variables
|
|
14
15
|
|
|
15
|
-
|
|
16
|
+
All the parameters used in the application are registered by implementing the `ENV_USED_TOKEN` token in the DI and it is assumed that each module individually registers only the env parameters it needs. In this case, when a module is connected, there will be automatic validation of all passed parameters that are necessary for the application to work
|
|
16
17
|
|
|
17
18
|
```tsx
|
|
18
19
|
import { provide } from '@tramvai/core';
|
|
@@ -32,13 +33,13 @@ import { provide } from '@tramvai/core';
|
|
|
32
33
|
export class MyModule {}
|
|
33
34
|
```
|
|
34
35
|
|
|
35
|
-
|
|
36
|
+
In the above example, the module registers several env tokens, which will be initialized and will be available in `environmentManager.get('DEBUG_MODULE')`. In doing so, the `optional` parameter has been passed, which indicates that the variables are not required for the application to work.
|
|
36
37
|
|
|
37
|
-
###
|
|
38
|
+
### Validation of environment variables values
|
|
38
39
|
|
|
39
|
-
|
|
40
|
+
When the application starts, it checks the tokens that were registered in the DI and passed to env at startup. If all required env variables have not been passed to the application, the application will crash.
|
|
40
41
|
|
|
41
|
-
|
|
42
|
+
It is also possible to write validators for env values, which will run when the application is initialized.
|
|
42
43
|
|
|
43
44
|
```tsx
|
|
44
45
|
import { provide } from '@tramvai/core';
|
|
@@ -52,7 +53,7 @@ import { provide } from '@tramvai/core';
|
|
|
52
53
|
key: 'MY_ENV',
|
|
53
54
|
validator: (env) => {
|
|
54
55
|
if (!env.includes('https')) {
|
|
55
|
-
return '
|
|
56
|
+
return 'Incorrect link format. The link should contain https';
|
|
56
57
|
}
|
|
57
58
|
},
|
|
58
59
|
},
|
|
@@ -64,25 +65,25 @@ import { provide } from '@tramvai/core';
|
|
|
64
65
|
export class MyModule {}
|
|
65
66
|
```
|
|
66
67
|
|
|
67
|
-
###
|
|
68
|
+
### Functionality works on the server and in the browser
|
|
68
69
|
|
|
69
|
-
|
|
70
|
+
All env variables will be available both on the server and in the browser without any additional actions or settings. Env variables that have `dehydrate: true` are automatically passed to the browser
|
|
70
71
|
|
|
71
|
-
###
|
|
72
|
+
### Priority of obtaining values for env variables
|
|
72
73
|
|
|
73
|
-
|
|
74
|
+
Since it is possible to overwrite the values of the variables, the variables are replaced according to certain rules
|
|
74
75
|
|
|
75
|
-
|
|
76
|
+
The replacement rules are arranged in order of priority, from lower to higher:
|
|
76
77
|
|
|
77
|
-
-
|
|
78
|
-
-
|
|
79
|
-
-
|
|
78
|
+
- Parameters set in tokens `{ key: 'ENV_PARAM', value: 'env value' }`
|
|
79
|
+
- Parameters written in `env.development.js` file
|
|
80
|
+
- Passing application launch parameters `MY_ENV=j node server.js`
|
|
80
81
|
|
|
81
82
|
## API
|
|
82
83
|
|
|
83
84
|
<p>
|
|
84
85
|
<details>
|
|
85
|
-
<summary
|
|
86
|
+
<summary>Exported tokens and TS interface</summary>
|
|
86
87
|
|
|
87
88
|
@inline ../../tokens/common/src/env.ts
|
|
88
89
|
|
|
@@ -91,9 +92,9 @@ export class MyModule {}
|
|
|
91
92
|
|
|
92
93
|
## How to
|
|
93
94
|
|
|
94
|
-
###
|
|
95
|
+
### How to read data in an application
|
|
95
96
|
|
|
96
|
-
|
|
97
|
+
Suppose we registered the parameter `CONFIG_API` used by env with the `ENV_USED_TOKEN` token, now we need to connect environmentManager in the application and read the data:
|
|
97
98
|
|
|
98
99
|
```tsx
|
|
99
100
|
import { provide } from '@tramvai/core';
|
|
@@ -116,18 +117,26 @@ import { provide } from '@tramvai/core';
|
|
|
116
117
|
export class MyModule {}
|
|
117
118
|
```
|
|
118
119
|
|
|
119
|
-
|
|
120
|
+
This code will work both on the server and in the browser
|
|
120
121
|
|
|
121
|
-
###
|
|
122
|
+
### How you can simply pass parameters in local development
|
|
122
123
|
|
|
123
|
-
|
|
124
|
+
To do this, create a file `env.development.js` in the root of the project and write all env variables for the application. When the application is initialized, this file will be read.
|
|
124
125
|
|
|
125
|
-
####
|
|
126
|
+
#### Peculiarities of using env.developmen.js in production builds
|
|
126
127
|
|
|
127
|
-
|
|
128
|
+
The [twelve factors](https://12factor.net/ru/config) application stores the configuration in environment variables, so by default when `process.env.NODE_ENV === 'production'` EnvironmentManger will not read the `env.development.js` file.
|
|
128
129
|
|
|
129
|
-
|
|
130
|
+
If you want to test the application locally with `NODE_ENV=production`, you can pass the flag `DANGEROUS_UNSAFE_ENV_FILES='true'` so that EnvironmentManger will read the `env.development.js` file and not have to enter all variables by hand.
|
|
130
131
|
|
|
131
|
-
###
|
|
132
|
+
### How to pass env parameters to the application during the deploys
|
|
132
133
|
|
|
133
|
-
|
|
134
|
+
To do this, pass env parameters when starting the application. For example in Docker you can do this with the parameter -e `docker run -e MY_ENV_VAR=/ my-image`.
|
|
135
|
+
|
|
136
|
+
### How to view all env variables of an application
|
|
137
|
+
|
|
138
|
+
> This method allows you to see only client variables
|
|
139
|
+
|
|
140
|
+
To get a list of variables, there is a `/papi/apiList` method
|
|
141
|
+
|
|
142
|
+
Request example: `http://localhost:3000/${appName}/papi/apiList`
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tramvai/module-environment",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.39.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"browser": "lib/browser.js",
|
|
6
6
|
"main": "lib/server.js",
|
|
@@ -20,11 +20,11 @@
|
|
|
20
20
|
},
|
|
21
21
|
"peerDependencies": {
|
|
22
22
|
"@tinkoff/utils": "^2.1.2",
|
|
23
|
-
"@tramvai/core": "1.
|
|
24
|
-
"@tramvai/papi": "1.
|
|
25
|
-
"@tramvai/state": "1.
|
|
26
|
-
"@tramvai/tokens-common": "1.
|
|
27
|
-
"@tramvai/tokens-server": "1.
|
|
23
|
+
"@tramvai/core": "1.39.0",
|
|
24
|
+
"@tramvai/papi": "1.39.0",
|
|
25
|
+
"@tramvai/state": "1.39.0",
|
|
26
|
+
"@tramvai/tokens-common": "1.39.0",
|
|
27
|
+
"@tramvai/tokens-server": "1.39.0",
|
|
28
28
|
"@tinkoff/dippy": "0.7.35",
|
|
29
29
|
"react": ">=16.8.0",
|
|
30
30
|
"react-dom": ">=16.8.0",
|