tsframework / ts-framework

A Web Framework for Nodejs
http://tsframework.github.io
MIT License
43 stars 6 forks source link

Configuration.ts proposals #13

Closed Paradoxis closed 8 years ago

Paradoxis commented 8 years ago

Configuration#lock(): void

Allows for the configuration to be locked at runtime, preventing strange errors where certain keys are set after the server starts.

private locked: boolean = false;

public lock(): void
{
    if (this.locked === false) {
        this.locked = true;
    } else {
        throw new ConfigurationException("Configuration has already been locked!");
    }
}

Configuration#set(key: string, value: any): void

The configuration is currently being split into parts on the . character, is there any reason it's doing this? (I copied the code from gitter.im, but it seems pretty counter productive as it's probably easier to simply save it as a {[s: string]: any} object, and just reference the keys by name.). Also we could potentially make it throw an Exception if the key is already set (avoiding accidental overwrites).

public set(key: string, defaultValue: any = undefined): any
{
    if (this.locked === true) {
        throw new ConfigurationException("Unable to set configuration key, configuration object has been locked");
    }

    if (this.data.hasOwnProperty(key) === false) {
        this.data[key] = value;
    } else {
        throw new ConfigurationException(`Key ${key} already exists in configuration`);
    }
}

Configuration#get(key: string): any

Wouldn't getting a key from Configuration be smarter with a default value, and if none is specified it could throw an exception, this would make it more clear for developers who want to use the class, it currently has potential to lead to Uncaught TypeError: Cannot read property '...' of undefined errors.

public get(key: string, defaultValue: any = undefined): any
{
    if (this.data.hasOwnProperty(key) {
        return this.data[key]
    } else {
        if (defaultValue !== undefined) {
            return defaultValue;
        } else {
            throw new ConfigurationException(`Unknown key ${key} in configuration`);
        }
    }
}

ConfigurationException

export class ConfigurationException extends Exception 
{
    public name: string = "ConfigurationException";

    constructor (public message?: string)
    {
        super(message);
    }
}