npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tilde-nlp/ngx-config

v6.0.1

Published

Angular library for easy configuration loading from json file/files and using in in app. It is important to note that files will be merged in the same order as they are passed in url array. For example, if there is 2 config files, 1st file in array will

Downloads

49

Readme

NgxConfig

Angular library for easy configuration loading from json file/files and using in in app. It is important to note that files will be merged in the same order as they are passed in url array. For example, if there is 2 config files, 1st file in array will be overriden with 2nd config (of course it happens only if configurations overlap).

Examples

Import module passing configuration urls to it

There are multiple ways for using library with configuration. Easiest way is to import module with loadConfig method and pass array with file urls to be loaded.

// app.module.ts (or any other root module where you want to import this config module)
...
import { ConfigModule } from 'ngx-config';

@NgModule({
  declarations: [
    ...
  ],
  imports: [
        ConfigModule.loadConfig(["assets/config.local.json", "assets/config-overrides.local.json"])
  ],
  providers: [
    ...
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

When using this method, you need to call ConfigService.loadAppConfig method. It makes sense to call this method from APP_INITIALIZER, but you can do it wherever you like.

PS. It is also possible to pass urls straight to ConfigService.loadAppConfig method. In that case you don't need to import ConfigModule in your app.

Load config before app starts and use injection token

There is occasions when you want configuration to be loaded already before application bootstrapping - in that case you need to modify main.ts file and use global js window object. This method is usefull when you are using injection tokens and you need to use configuration in provider factories. When creating this method, couldn't find any better way to access config from json file in provider factories.

// main.ts
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
import { ConfigHelper, CONFIG_TOKEN } from 'ngx-config';

const configs = ["assets/config.local.json", "assets/config-overrides.local.json"];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
ConfigHelper.fetchConfigs(configs).then((data: any) => {
  const mergedConfig = ConfigHelper.deepMerge(...data);
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  (window as any)[CONFIG_TOKEN] = mergedConfig;
  platformBrowserDynamic().bootstrapModule(AppModule, {})
    .catch(err => console.error(err));
})

When you have added configuration to window object, you can use injection token from your module to provide configuration for config service.

// app.module.ts
...
import { CONFIG_TOKEN } from 'ngx-config';
...
@NgModule({
  declarations: [
    ...
  ],
  imports: [
    ...,
  ],
  providers: [
    {
      provide: CONFIG_TOKEN,
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      useFactory: ()=> (window as any)[CONFIG_TOKEN]
    }
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

When these steps are done, you can inject ConfigService in your application and use provided methods for config.