Skip to content

Commit

Permalink
feat(getSettings): added generic type for getSetting function (#146)
Browse files Browse the repository at this point in the history
* feat(getSettings): added generic type for getSetting function

* feat(docs): updated docs, added generic types

* style(core): remove type casting

* docs(readme): update readme
  • Loading branch information
NarHakobyan authored and Burak Tasci committed Dec 22, 2018
1 parent 3fe1c70 commit e1cb64b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
18 changes: 10 additions & 8 deletions packages/@ngx-config/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ You can find detailed information about the usage guidelines for the `ConfigMerg
When the `getSettings` method is invoked without parameters, it returns entire application configuration. However, the `getSettings`
method can be invoked using two optional parameters: **`key`** and **`defaultValue`**.

To specify returning value type you can add generic type in `getSettings`.

The following example shows how to read configuration settings using all available overloads of `getSettings` method.

#### anyclass.ts
Expand All @@ -156,42 +158,42 @@ export class AnyClass {

myMethodToGetUrl1a() {
// will retrieve 'http://localhost:8000'
let url:string = this.config.getSettings('system.applicationUrl');
const url = this.config.getSettings<string>('system.applicationUrl');
}

myMethodToGetUrl1b() {
// will retrieve 'http://localhost:8000'
let url:string = this.config.getSettings(['system', 'applicationUrl']);
const url = this.config.getSettings<string>(['system', 'applicationUrl']);
}

myMethodToGetUrl2a() {
// will retrieve 'http://localhost:8000'
let url:string = this.config.getSettings('system').applicationUrl;
const url = this.config.getSettings<string>('system').applicationUrl;
}

myMethodToGetUrl2b() {
// will retrieve 'http://localhost:8000'
let url:string = this.config.getSettings().system.applicationUrl;
const url = this.config.getSettings<string>().system.applicationUrl;
}

myMethodToGetUrl3a() {
// will throw an exception (system.non_existing is not in the application settings)
let url:string = this.config.getSettings('system.non_existing');
const url = this.config.getSettings<string>('system.non_existing');
}

myMethodToGetUrl3b() {
// will retrieve 'no data' (system.non_existing is not in the application settings)
let url:string = this.config.getSettings('system.non_existing', 'no data');
const url = this.config.getSettings<string>('system.non_existing', 'no data');
}

myMethodToGetSeo1() {
// will retrieve {"pageTitle":"Tweeting bird"}
let seoSettings: string = this.config.getSettings('seo');
const seoSettings = this.config.getSettings<string>('seo');
}

myMethodToGetSeo1() {
// will retrieve {"pageTitle":"Tweeting bird"}
let seoSettings: string = this.config.getSettings().seo;
const seoSettings = this.config.getSettings<string>().seo;
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion packages/@ngx-config/core/src/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class ConfigService {
.then((res: any) => this.settings = res);
}

getSettings(key?: string | Array<string>, defaultValue?: any): any {
getSettings<T = any>(key?: string | Array<string>, defaultValue?: any): T {
if (!key || (Array.isArray(key) && !key[0]))
return this.settings;

Expand Down

0 comments on commit e1cb64b

Please sign in to comment.