loading...

Remote configurations for Angular8+

endykaufman profile image ILshat Khamitov ・1 min read

Remote configurations for Angular8+ applications, with built-in interceptor for mock REST data and non-permanent api

Installation

npm i --save ngx-remote-config

Usage

app.module.ts

import { HttpClientModule } from '@angular/common/http';
import { NgxRemoteConfigModule } from 'ngx-remote-config';

@NgModule({
  imports: [
    ...
    HttpClientModule,
    NgxRemoteConfigModule.forRoot({
      url: 'https://testapi.io/api/EndyKaufman/ngx-remote-config.json'
    }),
    ...
  ],
  ...
})
export class AppModule {}

app.component.html

...
<p>Load with directive</p>
<ng-container *remoteConfig="let config">
  {{config|json}}
</ng-container>
<p>Load with service</p>
<ng-container *ngIf="serviceConfig$ | async as serviceConfig">
  {{serviceConfig|json}}
</ng-container>
...

app.component.ts

import { NgxRemoteConfigService } from 'ngx-remote-config';
import { Observable } from 'rxjs';
...
serviceConfig$: Observable<any>;
constructor(
  private _ngxRemoteConfigService: NgxRemoteConfigService
) {
  this.serviceConfig$ = this._ngxRemoteConfigService.config$.asObservable();
}
...

settings.json (https://testapi.io/api/EndyKaufman/ngx-remote-config.json)

{
  "options":{
    "name":"Remote name",
    "description":"Remote description"
  },
  "/api/resource/2": {
    "get": {
      "status": 404,
      "body": {
        "message": "Fake not found message"
      }
    }
  },
  "/api/resource": {
    "get": [
      {
        "name": "get:item1"
      },
      {
        "name": "get:item2"
      },
      {
        "name": "get:item3"
      }
    ]
  },
  "/api/(.*)": "https://todo-nestjs.rucken.io/api/"
}

Links

Demo - Demo application with ngx-remote-config.

Stackblitz - Simply sample of usage on https://stackblitz.com

Demo settings - Settings for demo application stored on https://testapi.io

Demo Ionic - Example usage in ionic application with native HTTP module

path-to-regexp - Library usage for match url on interceptor

Discussion

pic
Editor guide