
Product
Introducing Scala and Kotlin Support in Socket
Socket now supports Scala and Kotlin, bringing AI-powered threat detection to JVM projects with easy manifest generation and fast, accurate scans.
ts-configurable
Advanced tools
TS-Configurable implements the @Configurable()
decorator to make any class configurable via environment variables and command line arguments! Additionally, the BaseConfig<T>
class can be extended to allow the passing of an options (options: Partial<T>
) object that can partially override the property defaults specified in the configuration class:
Install via npm i ts-configurable
and create a config class with default values for each property:
// server-config.ts
import { Configurable } from 'ts-configurable';
@Configurable()
class ServerConfig {
host = 'localhost';
port = 3000;
}
console.log(new ServerConfig());
Due to the @Configurable()
decorator, the values for all properties can now be set via environment variables and command line arguments:
# Default config instance
$ ts-node server-config.ts
ServerConfig { host: 'localhost', port: 3000 }
# Change port via command line argument
$ ts-node server-config.ts --port=4200
ServerConfig { host: 'localhost', port: 4200 }
# Change host via environment variable
$ host=0.0.0.0 ts-node server-config.ts
ServerConfig { host: '0.0.0.0', port: 3000 }
# Throw an error if a value with a different type was assigned
$ port=random ts-node server-config.ts
Property 'ServerConfig.port' is of type number but a value of type string ('"random"') was assigned!
url
from host
and port
)get debugPort() { return this.port + 1000; }
)Configurable(options?: IDecoratorOptions
)
Class decorator for marking a class configurable: The values of all class properties can be set using the following sources, listed by priority (1 = highest):
The final values for the config instance's properties are calculated upon instantiation.
boolean
Enforce that all properties are read-only by using Object.freeze()
(default: true)
false
| DotenvConfigOptionsApply environment variables from a file to the current process.env
false
| IArgvOptions
Whether to parse command line arguments (default: true)
string
Prefix for command line arguments (default: no prefix)
false
| IEnvOptions
Whether to parse environment variables (default: true)
boolean
Whether to lower-case environment variables (default: false)
string
Prefix for environment variables (default: no prefix)
string
Seperator for environment variables (default: '__')
boolean
Attempt to parse well-known values (e.g. 'false', 'null', 'undefined' and JSON values) into their proper types (default: true)
boolean
Throw an error if a config entry is set to a value of a different type than the default value (e.g. assigning a number to a string property) (default: true)
import { Configurable, BaseConfig } from '@tfs/config';
type TOrder = Partial<{
recipient: string;
priceTotal: number;
delivered: boolean;
billing: Partial<{ address: string }>;
}>;
class BasePizzaConfig extends BaseConfig<BasePizzaConfig> {
id = 5;
topping = 'cheese';
rating = null;
ingredients = ['tomato', 'bread'];
order: TOrder = {
recipient: 'John Doe',
priceTotal: 6.2,
delivered: true,
billing: {
address: 'Jerkstreet 53a, 1234 Whatevertown',
},
};
}
const pizzaConfig = new PizzaConfig({ topping: 'bacon' });
console.log(JSON.stringify(pizzaConfig, null, 2));
By adding the @Configurable()
decorator to any class, all of its properties can be configured via environment variables and command line arguments:
export pizza_order__delivered=false
$ start pizza-app --order.recipient=Jonny"
{
"id": 5,
"topping": "bacon",
"rating": null,
"ingredients": [
"tomato",
"bread"
],
"order": {
"recipient": "Jonny",
"priceTotal": 6.2,
"delivered": false,
"billing": {
"address": "Jerkstreet 53a, 1234 Whatevertown"
}
}
}
If you want to explicitly set a field to false
instead of just leaving it undefined
or to override a default you can add a no-
before the key: --no-key
.
$ start pizza-app --cash --no-paypal
{ cash: true, paypal: false }
You are welcome to contribute to the ts-configurable GitHub repository! All infos can be found here: How to contribute
0.0.2
FAQs
Make all properties of a class configurable using only one decorator!
The npm package ts-configurable receives a total of 713 weekly downloads. As such, ts-configurable popularity was classified as not popular.
We found that ts-configurable 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.
Product
Socket now supports Scala and Kotlin, bringing AI-powered threat detection to JVM projects with easy manifest generation and fast, accurate scans.
Application Security
/Security News
Socket CEO Feross Aboukhadijeh and a16z partner Joel de la Garza discuss vibe coding, AI-driven software development, and how the rise of LLMs, despite their risks, still points toward a more secure and innovative future.
Research
/Security News
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.