Skip to content

11. Config Service

Ali Badakhshan edited this page Apr 15, 2019 · 3 revisions

Narik Config Service

ConfigService helps you to define your configuration in a JSON file.

/**
 * Config service
 */
export abstract class ConfigService {
  /**
   * Config loaded of config service
   * An `Observable` that emit whenever config loaded.
   */
  readonly configLoaded: Observable<any>;
  abstract init(): Promise<any>;
  /**
   * Gets config
   * T Return type of config value.
   * @param key  Config key;
   * @returns config value
   */
  abstract getConfig<T>(key: string): T;

  /**
   * Gets all config keys
   * @returns all keys
   */
  abstract getAllKeys(): string[];
}

The path of the config file is determined in NarikCore module import.

 NarikCoreModule.forRoot({
      configFilePath: "assets/app-config.json",
      ...
    }),

Sample Config Data:

{
  "modulesMetaDataRoot": "assets/modules",
  "dynamicResourcesPath": "assets",
  "translationsPath":"assets/i18n"
}

Sample Use Of Config Service:

export class test{
    @NarikInject(ConfigService)
    private configService: ConfigService;

    init()
    {
        this.configService.configLoaded.pipe(first()).subscribe(() => {
        this.moduleRootPath = this.configService.getConfig("modulesMetaDataRoot");
            if (!this.moduleRootPath) {
                throw new Error("modulesMetaDataRoot is null");
            }
            this.registerModule();
        });
    }
}