Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from hapinessjs/next
v1.0.0-rc.6
- Loading branch information
Showing
13 changed files
with
654 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"prop": { | ||
"foo": "test" | ||
}, | ||
"my_module": { | ||
"user": "michel", | ||
"password": "pancetta", | ||
"host": "localhost" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1 @@ | ||
import { IConfig } from 'config'; | ||
|
||
export class Config { | ||
|
||
private static data: IConfig; | ||
|
||
/** | ||
* Load the config | ||
*/ | ||
static load() { | ||
this.data = require('config'); | ||
} | ||
|
||
/** | ||
* Check if the settings exists | ||
* | ||
* @param {string} key | ||
* @returns boolean | ||
*/ | ||
static has(key: string): boolean { | ||
return this.data.has(key); | ||
} | ||
|
||
/** | ||
* Return the config value | ||
* from a key | ||
* | ||
* @param {string} key | ||
* @param {any} defaultValue | ||
* @returns any | ||
*/ | ||
static get(key: string, defaultValue?: any): any { | ||
return this.data.has(key) ? this.data.get(key) : | ||
!!defaultValue ? defaultValue : undefined; | ||
} | ||
} | ||
export * from './lib'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import * as c from 'config'; | ||
import * as clone from 'clone'; | ||
import { IConfig } from 'config'; | ||
|
||
export interface ConfigInterface extends IConfig { | ||
get<T = ConfigInterface>(key: string): T; | ||
} | ||
|
||
export class Config { | ||
|
||
private static _data: ConfigInterface; | ||
|
||
/** | ||
* Load the config | ||
*/ | ||
static load(payload?: any): ConfigInterface { | ||
if (payload) { | ||
// This method will make the payload object have | ||
// .get (chainable) .has and .util method | ||
// Allowing custom object to be feed in the config module | ||
const _payload = clone(payload); | ||
this._data = this.attachProtoDeep(_payload); | ||
return _payload; | ||
} | ||
|
||
this._data = c; | ||
return this._data; | ||
} | ||
|
||
static attachProtoDeep(payload): ConfigInterface { | ||
return this._data.util['attachProtoDeep'](payload); | ||
} | ||
|
||
/** | ||
* Return config data | ||
*/ | ||
static getData() { | ||
return this._data; | ||
} | ||
|
||
/** | ||
* Check if the settings exists | ||
* | ||
* @param {string} key | ||
* @returns boolean | ||
*/ | ||
static has(key: string): boolean { | ||
return this._data.has(key); | ||
} | ||
|
||
/** | ||
* Return the config value | ||
* from a key | ||
* | ||
* @param {string} key | ||
* @param {any} defaultValue | ||
* @returns any | ||
*/ | ||
static get<T = ConfigInterface>(key: string, defaultValue?: any): T { | ||
return this._data.has(key) ? this._data.get<T>(key) : | ||
!!defaultValue ? defaultValue : undefined; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { InjectionToken } from '@hapiness/core'; | ||
import { Config, ConfigInterface } from './config'; | ||
|
||
const ConfigInjectionTokens = {}; | ||
const ConfigFileInjectionToken = new InjectionToken('HAPINESS_CONFIG'); | ||
|
||
export type ConfigProvider = { | ||
provide: InjectionToken<any>, | ||
useValue: ConfigInterface | ||
} | ||
|
||
export class ConfigHelper { | ||
|
||
static getInjectionToken(key?: string) { | ||
if (!key) { | ||
return ConfigFileInjectionToken; | ||
} | ||
|
||
if (!ConfigInjectionTokens[key]) { | ||
ConfigInjectionTokens[key] = new InjectionToken(`HAPINESS_CONFIG_${key}`); | ||
} | ||
|
||
return ConfigInjectionTokens[key]; | ||
} | ||
|
||
static getProvider(key?: string, value?: any): ConfigProvider { | ||
const provide = ConfigHelper.getInjectionToken(key); | ||
if (key) { | ||
// key and value provided, we load the value | ||
if ((typeof value === 'object' && value !== null)) { | ||
return { | ||
provide, | ||
useValue: Config.attachProtoDeep(value) | ||
} | ||
} else | ||
// If we dont have any value but a key we check if | ||
// the key exists in config and use it as value | ||
if (Config.has(key)) { | ||
return { | ||
provide, | ||
useValue: Config.get(key) | ||
}; | ||
} | ||
} | ||
|
||
// Otherwise just return the default config object | ||
return { | ||
provide, | ||
useValue: Config.load() | ||
}; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './config'; | ||
export * from './helper'; |
Oops, something went wrong.