Security News
Combatting Alert Fatigue by Prioritizing Malicious Intent
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Platform agnostic configuration management library, with environmental variables and validation, like convict
Platform agnostic configuration management library, with environmental variables and validation, like convict (but simpler, more modern, and written in ts).
Install package:
# npm
npm install figue
# yarn
yarn install figue
# pnpm
pnpm install figue
Import:
// ESM
import { figue } from 'figue';
// CommonJS
const { figue } = require('figue');
import { figue } from 'figue';
// Define the schema
const config = figue({
env: {
doc: 'Application current environment',
format: 'enum',
values: ['production', 'development', 'test'],
default: 'development',
env: 'NODE_ENV',
},
port: {
doc: 'Application port to listen',
format: 'integer',
default: 3000,
env: 'PORT',
},
db: {
host: {
doc: 'Database server host',
format: 'string',
default: 'localhost',
env: 'APP_DB_HOST',
},
username: {
doc: 'Database server username',
format: 'string',
default: 'pg',
env: 'APP_DB_USERNAME',
},
password: {
doc: 'Database server password',
format: 'string',
default: '',
env: 'APP_DB_PASSWORD',
},
},
})
// Load the environnement variables
.loadEnv(process.env)
// Validate the config
.validate()
// Get the config
.getConfig();
console.log(config);
// {
// env: 'development',
// port: 3000,
// db: {
// host: 'localhost',
// username: 'pg',
// password: '',
// },
// }
Use the loadEnv
method to specify you environnement variables that will be used by the env
keys
import { figue } from 'figue';
// Define the schema
const config = figue({
/* schema */
})
.loadEnv(process.env)
.validate()
.getConfig();
In some case you don't have access to a process.env
variable, like with vite
, just simply load what stores your env variables :
import { figue } from 'figue';
// Define the schema
const config = figue({
/* schema */
})
.loadEnv(import.meta.env)
.validate()
.getConfig();
You can even specify you custom environment storage as long as it's a simple flat object map, for example:
import { figue } from 'figue';
// Define the schema
const config = figue({
db: {
host: {
doc: 'Database server host',
format: 'string',
default: 'localhost',
env: 'APP_DB_HOST',
},
username: {
doc: 'Database server username',
format: 'string',
default: 'pg',
env: 'APP_DB_USERNAME',
},
},
})
.loadConfig({
db: {
host: 'prod.example.com',
username: 'super-root',
},
})
.validate()
.getConfig();
From a json file :
import { figue } from 'figue';
import configValues from '../settings.json';
// Define the schema
const config = figue({
/**/
})
.loadConfig(configValues)
.validate()
.getConfig();
If you call loadEnv
multiple times, the objects passed as argument will be merged and in cas of a conflict, the value of the last env loaded will be used.
Sometime you may want to load you config value from a custom object (maybe from a config file ?)
import { figue } from 'figue';
// Define the schema
const config = figue({
var: {
doc: 'Dummy example',
format: 'string',
default: 'foo',
env: 'my-env-key',
},
})
.loadEnv({
'my-env-key': 'bar',
})
.validate()
.getConfig();
When a config variable has multiple possible value, the order of priority is:
Env value (if exists) > Config value (if exists) > Default value
Format name | Description | Example |
---|---|---|
String | Basically an string |
|
Integer | Basically an integer, no floating point |
|
Float | A floating point value |
|
Enum | A variable from an enum specified by the `values` key |
|
Boolean | A boolean variable. Env variable (string) are coerced with `value.trim().toLowerCase() === 'true'` |
|
Custom | You can define your own validation and coercion function |
|
Any | It can be anything |
|
Convict is meant to be used in node based environnement, it needs to have access to global variables that may may not be present in some environnement (like process
, global
), and it also imports fs
.
Figue is the french for fig -> con-fig.
pnpm install
pnpm dev
Coded with ❤️ by Corentin Thomasset.
This project is under the MIT license.
FAQs
Platform agnostic configuration management library, with environmental variables and validation, like convict but cross-env and zod schemas
The npm package figue receives a total of 903 weekly downloads. As such, figue popularity was classified as not popular.
We found that figue demonstrated a healthy version release cadence and project activity because the last version was released less than 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.
Security News
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.