1.0.4 • Published 9 months ago

@noticia-systems/ngx-json-config v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

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) {}
}
1.0.4

9 months ago

1.0.3

9 months ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago