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

@schaman/angular-config

v1.0.0

Published

This module provides configuration functionality. If you want to use the same built code for different environments, you can use this module to provide different configuration for each environment. This is the III. factor of the [Twelve-Factor App](https:

Downloads

57

Readme

@schaman/angular-config

This module provides configuration functionality. If you want to use the same built code for different environments, you can use this module to provide different configuration for each environment. This is the III. factor of the Twelve-Factor App.

Features

  • [x] Provide configuration for different environments
  • [x] Retry loading configuration
  • [x] Guard for loading configuration
  • [x] Signal support
  • [x] RxJS support
  • [ ] Pass configuration without load during SSR

Installation

npm install @schaman/angular-config

Usage

Provide the configuration in the assets/config.json.

{
  "backendUrl": "https://api.example.com"
}

Import the ConfigModule in your AppModule and provide the path to the configuration file.

import { ConfigModule } from '@schaman/angular-config';

@NgModule({
  imports: [ConfigModule.forRoot()],
})
export class AppModule {}

Inject the ConfigService in your component and use it.

import { ConfigService } from '@schaman/angular-config';

@Component({
  selector: 'app-root',
  template: ` <p>Backend URL: {{ config().backendUrl }}</p> `,
})
export class AppComponent {
  public readonly config = inject(ConfigService).config;
}

Standalone component

If you want to use the ConfigService in a standalone component, you have to import the ConfigModule in the component module.

import { ConfigModule } from '@schaman/angular-config';

@Component({
  selector: 'app-root',
  template: ` <p>Backend URL: {{ config().backendUrl }}</p> `,
  imports: [ConfigModule.forRoot()],
})
export class AppComponent {
  public readonly config = inject(ConfigService).config;
}

Configuration

The ConfigModule can be configured with the following options:

interface ConfigModuleOptions {
  /**
   * Number of retries when loading the config file.
   * @default 3
   */
  retry: number;
  /**
   * Path to the config file.
   */
  configPath: string;
  /**
   * If true, the config will be loaded when the module is imported.
   * @default true
   */
  loadConfig: boolean;
  /**
   * If true, the config will be loaded when the app is initialized.
   * @default false
   */
  runAsAppInitializer: boolean;
}

Wait for config during app initialization

If you want to wait for the config to be loaded before the app is initialized, you can use the runAsAppInitializer option.

import { ConfigModule } from '@schaman/angular-config';

@NgModule({
  imports: [ConfigModule.forRoot({ runAsAppInitializer: true })],
})
export class AppModule {}

If you need the config in your app initializer, you can inject the ConfigService and use the afterConfigLoad$ observable.

import { ConfigService } from '@schaman/angular-config';

@NgModule({
  imports: [ConfigModule.forRoot({ runAsAppInitializer: true })],
  providers: [
    {
      provide: APP_INITIALIZER,
      useFactory: (configService: ConfigService) => () =>
        configService.afterConfigLoad$.pipe(
          tap((config) => {
            /* use config here */
          }),
        ),
      deps: [ConfigService],
    },
  ],
})
export class AppModule {}

Configure your generated backend base url

If you generate the backend using openapi-generator then provide the values like this:

import { ConfigModule } from '@schaman/angular-config';
import { ApiConfiguration } from 'your-data-package';

@NgModule({
  imports: [ConfigModule.forRoot()],
  providers: [
    {
      provide: ApiConfiguration,
      useFactory: (configService: ConfigService): ApiConfiguration => {
        const configuration = new ApiConfiguration();
        configService.afterConfigLoad$.subscribe((config) => (configuration.basePath = config.backendUrl));
        return configuration;
      },
      deps: [ConfigService],
    },
  ],
})
export class AppModule {}

Use as guard

If not all routes should be available before the config is loaded, you can use the configLoadedGuard.

import { configLoadedGuard } from '@schaman/angular-config';

const routes: Routes = [
  {
    path: '',
    component: HomeComponent,
  },
  {
    path: 'login',
    canActivate: [configLoadedGuard],
    component: LoginComponent,
  },
];

Best Practices

If you have the interface defined in your code, then you can skip the Template parameter.

import { ConfigService } from '@schaman/angular-config';
import { computed } from '@angular/core';

interface Config {
  backendUrl: string;
}

@Injectable()
export class MyConfigService {
  private readonly configService = inject<ConfigService<Config>>(ConfigService);
  public readonly config = this.configService.config;
  public readonly config$ = this.configService.config$;
  public readonly afterConfigLoad$ = this.configService.afterConfigLoad$;
  // Computed signals can be added here, also.
  public readonly backendUrl = computed(() => this.configService.config().backendUrl);
  // Or RxJS operators
  public readonly backendUrl$ = this.configService.config$.pipe(map((config) => config.backendUrl));
}

And provide it in your app module.

import { ConfigModule } from '@schaman/angular-config';
import { MyConfigService } from './my-config.service';

@NgModule({
  imports: [ConfigModule.forRoot()],
  providers: [MyConfigService],
})
export class AppModule {}