Socket
Socket
Sign inDemoInstall

ts-configurable

Package Overview
Dependencies
21
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.1.0 to 0.1.1

6

CHANGELOG.md
# CHANGELOG
## 0.1.1
- Bugfix: remove the "is-plain-object" dependency and replace it with a simple object check as class instances should be treated as objects as well but are not "plain" objects
- Add example for a NestJS webserver using the ts-configurable package
- Update documentation
## 0.1.0

@@ -4,0 +10,0 @@

3

lib/configurable.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const dotenv = require("dotenv"); // https://www.npmjs.com/package/dotenv
const isPlainObject = require("is-plain-object"); // https://www.npmjs.com/package/is-plain-object
const nconf_1 = require("nconf"); // https://www.npmjs.com/package/nconf

@@ -111,3 +110,3 @@ const util_1 = require("./util");

// Parse options provided by the constructor (first argument) if the BaseConfig class was extended, ignore otherwise
const constructorOptions = new ConfigClass() instanceof base_config_1.BaseConfig && isPlainObject(args[0]) ? args[0] : {};
const constructorOptions = new ConfigClass() instanceof base_config_1.BaseConfig && util_1.isObject(args[0]) ? args[0] : {};
const options = getOptions(decoratorOptions, constructorOptions.options);

@@ -114,0 +113,0 @@ if (options.loadEnvFromFile) {

import { IDecoratorOptions } from './interfaces';
/**
* Check whether a given value is a Javascript object
* @param val value to check
*/
export declare function isObject(val: any): boolean;
/**
* Assign values to an object based on a template object's properties and their respective types

@@ -4,0 +9,0 @@ * @param obj Object to set the properties on

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const isPlainObject = require("is-plain-object"); // https://www.npmjs.com/package/is-plain-object
/**
* Check whether a given value is a Javascript object
* @param val value to check
*/
function isObject(val) {
return val !== null && val !== undefined && typeof val === 'object' && Array.isArray(val) === false;
}
exports.isObject = isObject;
/**
* Assign values to an object based on a template object's properties and their respective types

@@ -16,4 +23,4 @@ * @param obj Object to set the properties on

const value = config[key];
if (isPlainObject(templateValue)) {
if (isPlainObject(value)) {
if (isObject(templateValue)) {
if (isObject(value)) {
obj[key] = {};

@@ -20,0 +27,0 @@ assignValuesByTemplate(obj[key], templateValue, value, options, `${parent}.${key}`);

{
"version": "0.1.0",
"version": "0.1.1",
"name": "ts-configurable",

@@ -38,5 +38,4 @@ "description": "",

"dotenv": "~6.2.0",
"is-plain-object": "~2.0.4",
"nconf": "~0.10.0"
}
}

@@ -259,3 +259,3 @@ # TS-Configurable

The ts-configurable package encourages developers to keep all configuration values static during the lifetime of the application. All sources (environment variables, command line arguments, ...) are parsed and merged during the instantiation of the configuration class object. After an object has been created, the object should be considered read-only, which can be enforced using the `enforceReadonly` option. This ensures that the configuration object is not modified at runtime (using [Object.freeze](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze)). Additionally, all class properties can be marked with the TypeScript `readonly` modifier to prevent assignments at compile time. This way, the developer gets instant feedback from his IDE if he accidentially tries to set a read-only configuration value.
The ts-configurable package encourages developers to keep all configuration values static throughout the lifetime of the application. All sources (environment variables, command line arguments, ...) are parsed and merged during the instantiation of the configuration class object. After an object has been created, the object should be considered read-only, which can be enforced using the `enforceReadonly` option. This ensures that the configuration object is not modified at runtime (using [Object.freeze](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze)). Additionally, all class properties can be marked with the TypeScript `readonly` modifier to prevent assignments at compile time. This way, the developer gets instant feedback from his IDE if he accidentially tries to set a read-only configuration value.

@@ -283,4 +283,24 @@ Keeping all configuration values read-only after initialization has several benefits:

## :rotating_light: Troubleshooting
#### TypeError: Class constructor BaseConfig cannot be invoked without 'new'
This error occurs when a custom class extends the `BaseConfig` class and the TypeScript compiler target is below ES6. The root cause for this issue is that the ts-configurable package is compiled with the ES6 target and an ES5 class cannot extend an ES6 class (classes are compiled to functions in ES5 while ES6 natively supports classes). Therefore, this error can be fixed by setting the compiler target to ES6 or higher:
```json
// tsconfig.json
...
"compilerOptions": {
"target": "es6"
```
## :books: Examples
Here are some example applications showcasing how to use the ts-configurable package:
- [Minimal webserver](https://github.com/derbenoo/ts-configurable/tree/master/apps/example-webserver)
- [NestJS webserver](https://github.com/derbenoo/ts-configurable/tree/master/apps/example-nestjs)
## :pray: Contributing
You are welcome to contribute to the ts-configurable GitHub repository! All infos can be found here: [How to contribute](https://github.com/derbenoo/ts-configurable/blob/master/CONTRIBUTING.md)

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc