@noticia-systems/ngx-json-config
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

Node.js Package Node.js CI License: MIT CodeQL

ngx-json-config allows for loading of json config files on Angular startup for dynamic configurations that are not included during compile time (like environment vars).

Installation

npm install @noticia-systems/ngx-json-config

Usage

Define one or more config interfaces:

export interface AppConfig {
    exampleApiUrl: string;
    exampleApiKey: string;
    
    ...
}

Create an InjectionToken (e.g. in the AppModule or the interface file) for the defined config interfaces:

export const APP_CONFIG = new InjectionToken<AppConfig>('AppConfig');

Configure the module for the config usage:

@NgModule({
  providers: [
    // include the JsonConfigService 
    JsonConfigService,
    
    // load the configs in the APP_INITIALIZER
    {
      provide: APP_INITIALIZER,
      useFactory: (jsonConfigService: JsonConfigService) => () => jsonConfigService.load$([
        {
          identifier: APP_CONFIG,
          url: '/assets/app-config.json'
        }
      ]),
      deps: [JsonConfigService],
      multi: true
    },
    
    // create provider to inject the config into classes.
    {
      provide: APP_CONFIG,
      useFactory: (jsonConfigService: JsonConfigService) => jsonConfigService.get(APP_CONFIG),
      deps: [JsonConfigService]
    }
  ]
  ...
})
export class AppModule {
}

Inject the configs into classes:

export class TestService {
  constructor(@Inject(APP_CONFIG) public appConfig: AppConfig) {}
}

Package Sidebar

Install

npm i @noticia-systems/ngx-json-config

Weekly Downloads

0

Version

1.0.4

License

MIT

Unpacked Size

21.4 kB

Total Files

12

Last publish

Collaborators

  • cynceyd