-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor the validation and query logic and optimize the naming
- Loading branch information
Showing
1 changed file
with
32 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,68 @@ | ||
import { cloneDeep } from 'lodash'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { This } from '@vodyani/class-decorator'; | ||
import { toDeepMerge, toDeepMatch, toDeepRestore, isValidDict, isKeyof } from '@vodyani/utils'; | ||
import { toDeepMerge, toDeepMatch, toDeepRestore, isValidObject, isKeyof } from '@vodyani/utils'; | ||
import { ArgumentValidator, CustomValidated, Required, This } from '@vodyani/class-decorator'; | ||
|
||
import { Dictionary } from '../common'; | ||
|
||
/** | ||
* Configuration Accessor | ||
*/ | ||
@Injectable() | ||
export class ConfigProvider<T = any> { | ||
/** | ||
* The configuration details store. | ||
*/ | ||
private store: Dictionary<T> = Object(); | ||
|
||
/** | ||
* get the configuration for the given key. | ||
* Get the configuration for the given key. | ||
* | ||
* @publicApi | ||
*/ | ||
@This | ||
public match(key: string) { | ||
@ArgumentValidator() | ||
public match( | ||
@Required() key: string, | ||
) { | ||
const result = toDeepMatch(this.store, key); | ||
return isValidDict(result) ? cloneDeep(result) as any : result; | ||
return isValidObject(result) ? cloneDeep(result) as any : result; | ||
} | ||
/** | ||
* get the configuration for the given key. | ||
* Get the configuration for the given key. | ||
* | ||
* @tips Only the specified key can be queried, deep query is not supported. | ||
* | ||
* @usageNotes | ||
* - Only the specified key can be queried, deep query is not supported | ||
* @publicApi | ||
*/ | ||
@This | ||
public get<K extends keyof Dictionary<T>>(key: K) { | ||
let result = null; | ||
@ArgumentValidator() | ||
public get<K extends keyof Dictionary<T>>( | ||
@Required() key: K, | ||
) { | ||
if (isKeyof(this.store, key as string | number)) { | ||
const result = this.store[key]; | ||
|
||
if (isKeyof(this.store, key as number | string)) { | ||
result = this.store[key]; | ||
return isValidObject(result) ? cloneDeep(result) : result; | ||
} | ||
|
||
return isValidDict(result) ? cloneDeep(result) : result; | ||
} | ||
/** | ||
* set the configuration for the given key. | ||
*/ | ||
@This | ||
public set(key: string, value: any): void { | ||
@ArgumentValidator() | ||
public set( | ||
@Required() key: string, | ||
@Required() value: any, | ||
): void { | ||
const result = toDeepRestore(value, key); | ||
this.merge(result); | ||
} | ||
/** | ||
* merge the configuration | ||
*/ | ||
@This | ||
public merge(value: object): void { | ||
if (isValidDict(value)) { | ||
this.store = toDeepMerge(this.store, cloneDeep(value)); | ||
} | ||
@ArgumentValidator() | ||
public merge( | ||
@CustomValidated(isValidObject, 'value must be object !') value: object, | ||
): void { | ||
this.store = toDeepMerge(this.store, cloneDeep(value)); | ||
} | ||
} |