Git Product home page Git Product logo

runtime-config-loader's Introduction

Angular Runtime Configuration Loader

Most applications require certain configuration values that can be changed at runtime of the app. The environment.ts files in an Angular application technically work for setting configuration values in an app, but those are buildtime configuration values. This means that they are set when the application is built, and can't be changed unless the app is built again.

Overview

This library provides an easy way to load one or more JSON files with configuration values or make one or more HTTP GET calls to an API endpoint that returns those values. The config objects that are returned from the call(s) will be combined into a single configuration object. You can then use that configuration throughout the application. The default location of the JSON file is in the assets folder, at ./assets/config.json. When the service loads the file, it stores that configuration object in a local variable which can be accessed via the getConfig() and getConfigObjectKey(key: string) methods. getConfig returns the entire configuration object; getConfigObjectKey(key: string) returns part of the configuration object, namely the part defined by the key passed in. In some cases, the config.json is not finished loading before other modules/services are, so the above methods will return null. If that is the case, subscribe to the configSubject and access the configuration object that way.

How to Implement

In your app.module.ts file, add the following to the @NgModule decorator:

imports: [..., RuntimeConfigLoaderModule, ...],

That's it; it's that simple. In the RuntimeConfigLoaderModule, the APP_INITIALIZER token is used to run a function which loads the configuration from a file or an API endpoint that can be used throughout the application.

If you implement the library exactly as it is above, the configuration file needs to be in the ./assets/config.json location as mentioned above. If you'd like to load the file from a different location, provide that location in the .forRoot() method when importing the RuntimeConfigLoaderModule:

imports: [
  ...,
  RuntimeConfigLoaderModule.forRoot(
    { configUrl: './path/to/config/config.json' }
  ),
  ...]

If you want to load multiple files, the value of configUrl should be an array of strings:

imports: [
  ...,
  RuntimeConfigLoaderModule.forRoot(
    { configUrl: ['./path/to/config/config-1.json', './path/to/config/config-2.json'] }
  ),
  ...]

Make sure that the path(s) you provide here is accessible by the Angular application, meaning that the file is somewhere the app can load it. In my opinion, the assets folder is the easiest place to work from.

Multiple Config Paths

One reason you may want to load multiple configuration objects is so that you can set the configuration on your machine without affecting anyone else. For example, you could have a local.config.json file that is not included in source control. Some of the values in that file would overwrite the values in a config file that everyone can use. Another use case is that some config values don't change between environments, and some do. The ones that don't change could go in one file, the ones that do change could go in a second file. Each developer/environment can provide the second file with values they want or need.

It's important to know that if an attribute is repeated in two configuration files, the latest value is kept. So, let's say you have apiUrl in both files, config-1.json and config-2.json. Let's assume the files are passed in to the forRoot method like this:

imports: [
  ...,
  RuntimeConfigLoaderModule.forRoot(
    { configUrl: ['./path/to/config/config-1.json', './path/to/config/config-2.json'] }
  ),
  ...]

In this case, the apiUrl value from config-2.json will override the value from config-1.json.

runtime-config-loader's People

Contributors

gitter-badger avatar pegaltier avatar pjlamb12 avatar shanmukhateja avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

runtime-config-loader's Issues

Allow Multiple File Paths

Allow for multiple file paths being passed in for configuration to be loaded. This would be ideal for situations where a configuration file is saved in source control, but a local, untracked configuration file could also be used. The files would be loaded in the order in which they're passed, and merged together.

deepmerge looks like the perfect package to help with this.

The config.json is not finished loading before other modules are!

I'm facing the same issue as mentioned in the documentation:

In some cases, the config.json is not finished loading before other modules/services are, so the above methods will return null. If that is the case, subscribe to the configSubject and access the configuration object that way.

I'm using runtime-config-loader to load the config file for angular-auth-oidc-client's AuthModule!
but I couldn't use configSubject and couldn't find any sample code for using it! Maybe a sample code solves my problem!
I mean I tried subscribing to configSubject but it's still null!

image

Provide the Config as an Injection Token

This is something that is related to the discussion in #48.

I've figured out a way to provide the config as an Injection token, so you can reference it like this:

constructor(@Inject(APP_CONFIG) private appConfigObject: any) {}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.