Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
@tramvai/module-environment
Advanced tools
The env module is used to retrieve global application environment variables in runtime and pass these parameters to the client. With a pre-defined list of variables used by the application, dynamically extended and validated at application startup
The env module is used to retrieve global application environment variables in runtime and pass these parameters to the client. With a pre-defined list of variables used by the application, dynamically extended and validated at application startup
Already supplied inside @tramvai/module-common
and does not need to be installed if module-common is connected.
Otherwise, you need to install @tramvai/module-environment
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
import { provide } from '@tramvai/core';
@Module({
providers: [
provide({
provide: ENV_USED_TOKEN,
useValue: [
{ key: 'DEBUG_MODULE' },
{ key: 'DEBUG_MODULE_URL' },
],
multi: true,
}),
],
})
export class MyModule {}
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.
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.
It is also possible to write validators for env values, which will run when the application is initialized.
import { provide } from '@tramvai/core';
@Module({
providers: [
provide({
provide: ENV_USED_TOKEN,
useValue: [
{
key: 'MY_ENV',
validator: (env) => {
if (!env.includes('https')) {
return 'Incorrect link format. The link should contain https';
}
},
},
],
multi: true,
}),
],
})
export class MyModule {}
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
Since it is possible to overwrite the values of the variables, the variables are replaced according to certain rules
The replacement rules are arranged in order of priority, from lower to higher:
{ key: 'ENV_PARAM', value: 'env value' }
env.development.js
fileMY_ENV=j node server.js
@inline ../../tokens/common/src/env.ts
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:
import { provide } from '@tramvai/core';
@Module({
providers: [
provide({
provide: 'MY_SERVICE',
useClass: class MyService {
constructor({ environmentManager }) {
console.log(environmentManager.get('CONFIG_API'));
}
},
deps: {
environmentManager: ENV_MANAGER_TOKEN,
},
}),
],
})
export class MyModule {}
This code will work both on the server and in the browser
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.
The twelve factors 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.
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.
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
.
This method allows you to see only client variables
To get a list of variables, there is a /papi/apiList
method
Request example: http://localhost:3000/${appName}/papi/apiList
To do this, pass optional: true
parameter. For example { key: 'DEBUG_MODULE', optional: true }
FAQs
The env module is used to retrieve global application environment variables in runtime and pass these parameters to the client. With a pre-defined list of variables used by the application, dynamically extended and validated at application startup
We found that @tramvai/module-environment demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.