Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(getSettings): added generic type for getSetting function #146

Merged
merged 5 commits into from
Dec 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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