Skip to content

Commit

Permalink
Merge pull request #10 from hapinessjs/next
Browse files Browse the repository at this point in the history
v1.0.0-rc.6
  • Loading branch information
akanass committed Jul 18, 2017
2 parents f668798 + 222ba53 commit 9e02ce2
Show file tree
Hide file tree
Showing 13 changed files with 654 additions and 83 deletions.
84 changes: 69 additions & 15 deletions README.md
Expand Up @@ -59,8 +59,8 @@ $ yarn add @hapiness/config

```javascript
"dependencies": {
"@hapiness/core": "^1.0.0-rc.4",
"@hapiness/config": "^1.0.0-rc.4",
"@hapiness/core": "^1.0.0-rc.6",
"@hapiness/config": "^1.0.0-rc.6",
//...
}
//...
Expand Down Expand Up @@ -93,8 +93,14 @@ if (Config.has('my.config')) {
`./config/default.yml`:

```yaml
my:
external_service:
baseUrl: 'test'

mymodule_database:
provider: postgresql
hostname: localhost
user: pguser
password: keyboard cat
```

`Hapiness module`:
Expand All @@ -109,26 +115,23 @@ my:
Optional,
} from '@hapiness/core';

const CONFIG = new InjectionToken('config');
interface Config {
baseUrl: string;
}
import { ConfigHelper, ConfigInterface } from '@hapiness/config';

@HapinessModule({
...
})

export class ExternalModule {
static setConfig(config: Config): CoreModuleWithProviders {
static setConfig(config: ConfigInterface): CoreModuleWithProviders {
return {
module: ExternalModule,
providers: [{ provide: CONFIG, useValue: config }]
providers: [ConfigHelpers.getProvider('mymodule_database', config)]
};
}
}

export class Service {
constructor(@Optional() @Inject(CONFIG) config) { // @Optional to not throw errors if config is not passed
constructor(@Optional() @Inject(ConfigHelper.getInjectionToken('mymodule_database')) config) { // @Optional to not throw errors if config is not passed
...
}
}
Expand All @@ -142,20 +145,71 @@ my:
} from '@hapiness/core';
import { ExternalModule } from 'external-module';
import { Config } from '@hapiness/config';


Config.load(); // Load config, see node-config

@HapinessModule({
...
imports: [ ExternalModule.setConfig(Config.get('mymodule_database')) ]
})
...
```

`Hapiness service`:

```javascript

// main-module.ts
import {
HapinessModule,
} from '@hapiness/core';
import { ConfigHelper, Config } from '@hapiness/config';
import { MyCustomService } from './services';

Config.load(); // Load config, see node-config

@HapinessModule({
...
imports: [ ExternalModule.setConfig(Config.get('my')) ]
providers: [
ConfigHelper.getProvider('external_service'),
MyCustomService,
...
]
})
...
```


```javascript
import { Injectable } from '@hapiness/core';
import { ConfigInterface } from '@hapiness/config';

// my-custom-service.ts
@Injectable()
class MyCustomService {

private _baseUrl: string;

constrcutor(
@Inject(ConfigHelper.getInjectionToken('external_service'))
private _config: ConfigInterface
) {}

connect() {
this._baseUrl = this._config.get<string>('baseUrl');
}

}
...
```

[Back to top](#table-of-contents)

## Change History

* v1.0.0-rc.6 (2017-07-18)
* Latest packages versions.
* Config provider helper.
* Module version related to core version.
* v1.0.0-rc.4 (2017-07-11)
* Latest packages versions
* Module version related to core version.
Expand All @@ -175,7 +229,7 @@ my:
* Tests module.
* Documentation.
* Module version related to core version.

[Back to top](#table-of-contents)

## Maintainers
Expand Down Expand Up @@ -204,4 +258,4 @@ my:

Copyright (c) 2017 **Hapiness** Licensed under the [MIT license](https://github.com/hapinessjs/config/blob/master/LICENSE.md).

[Back to top](#table-of-contents)
[Back to top](#table-of-contents)
10 changes: 10 additions & 0 deletions config/default.json
@@ -0,0 +1,10 @@
{
"prop": {
"foo": "test"
},
"my_module": {
"user": "michel",
"password": "pancetta",
"host": "localhost"
}
}
16 changes: 11 additions & 5 deletions package.json
@@ -1,7 +1,7 @@
{
"name": "@hapiness/config",
"version": "1.0.0-rc.4",
"description": "Configuration Library to use it inside Hapiness framework or standalone",
"version": "1.0.0-rc.6",
"description": "Configuration Library to use it inside Hapiness framework",
"main": "index.js",
"types": "index.d.ts",
"private": false,
Expand Down Expand Up @@ -59,26 +59,32 @@
"homepage": "https://github.com/hapinessjs/config#readme",
"dependencies": {
"@types/config": "^0.0.32",
"@types/node": "^8.0.10",
"@types/node": "^8.0.14",
"clone": "^2.1.1",
"config": "^1.26.1",
"debug": "^2.6.8",
"js-yaml": "^3.8.4",
"rxjs": "^5.4.2"
"js-yaml": "^3.8.4"
},
"devDependencies": {
"@hapiness/core": "^1.0.0-rc.6",
"@types/fs-extra": "^3.0.2",
"coveralls": "^2.13.1",
"fs-extra": "^3.0.1",
"istanbul": "^1.1.0-alpha.1",
"mocha": "^3.4.2",
"mocha-typescript": "^1.1.7",
"rimraf": "^2.6.1",
"rxjs": "^5.4.2",
"ts-node": "^3.2.0",
"tslint": "^5.5.0",
"typescript": "^2.4.1",
"unit.js": "^2.0.0"
},
"engines": {
"node": ">=7.0.0"
},
"peerDependencies": {
"@hapiness/core": "^1.0.0-rc.6",
"rxjs": "^5.4.2"
}
}
37 changes: 1 addition & 36 deletions src/index.ts
@@ -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';
64 changes: 64 additions & 0 deletions src/lib/config.ts
@@ -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;
}

}
53 changes: 53 additions & 0 deletions src/lib/helper.ts
@@ -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()
};
}

}
2 changes: 2 additions & 0 deletions src/lib/index.ts
@@ -0,0 +1,2 @@
export * from './config';
export * from './helper';

0 comments on commit 9e02ce2

Please sign in to comment.