Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
@aapzu/tsdotenv
Advanced tools
Tsdotenv is a package which generates a validated and strongly typed config object from .env
file (or process.env
variables).
# using npm
npm install @aapzu/tsdotenv
# using yarn
yarn add @aapzu/tsdotenv
Create a .env file in the root directory of your project. Add environment-specific variables on new lines in the form of NAME=VALUE. For example:
DB_HOST=localhost
DB_PORT=5432
DEBUG=true
Create a config file as a single place of truth for the environment variables
import { parse } from '@aapzu/tsdotenv'
const SCHEMA = {
DB_HOST: String,
DB_PORT: { type: Number, default: 3000 },
DEBUG: { type: Boolean, optional: true },
CUSTOM_ENV: { type: ['test', 'prod'] },
// enums and optionals only get typed properly if the
// schema is a readonly object
} as const
const config = parse(SCHEMA, {
path: 'path_to_dotenv_file',
})
/*
typeof config = {
DB_HOST: string,
DB_PORT: number,
DEBUG: boolean | undefined
CUSTOM_ENV: 'test' | 'prod'
}
*/
export default config
Use that config file in other files
import config from '../path/to/config'
console.log(DB_HOST, typeof DB_HOST)
// localhost string
console.log(DB_PORT, typeof DB_PORT)
// 5432 number
console.log(DEBUG, typeof DEBUG)
// true boolean
Schema is the heart of the library. The parsing and validation of the values is done according to the schema. Possible schema value types are:
name | syntax |
---|---|
string | String |
number | Number |
boolean | Boolean |
enum | ['value1', 'value2'] |
string array | Array(String) |
number array | Array(Number) |
boolean array | Array(Boolean) |
A schema item has type and possibly a default value. If the item has optional: true
without a default value, it's possible that the value ends up being undefined.
An example schema is as follows:
const schema = {
BOOLEAN_ARRAY: Array(Boolean),
BOOLEAN: {
type: Boolean,
optional: true,
},
ENUM: {
type: ['foo', 'bar'],
default: 'foo',
},
NUMBER_ARRAY: Array(Number),
NUMBER: {
type: Number,
default: 42,
},
STRING: String,
STRING_ARRAY: Array(String),
}
Default: path.resolve(process.cwd(), '.env')
You may specify a custom path if your file containing environment variables is located elsewhere.
parse(schema, { path: '/custom/path/to/.env' })
Default: false
Maps the keys of the config object into camelCase. Example:
import { parse } from '@aapzu/tsdotenv'
const config = parse({
DB_HOST: String,
DB_PORT: { type: Number, default: 3000 },
}, {
path: 'path_to_dotenv_file',
camelCaseKeys: true
})
/*
typeof config = {
dbHost: string,
dbPort: number,
}
*/
export default config
Default: utf8
You may specify the encoding of your file containing environment variables.
parse(schema, { encoding: 'latin1' })
Default: false
You may turn on logging to help debug why certain keys or values are not being set as you expect.
parse(schema, { debug: process.env.DEBUG })
FAQs
Creates and parses a typed dotenv config from a given schema
The npm package @aapzu/tsdotenv receives a total of 13 weekly downloads. As such, @aapzu/tsdotenv popularity was classified as not popular.
We found that @aapzu/tsdotenv demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.