From e63ae823e088beb7bee6842ee5f76fee13bce85f Mon Sep 17 00:00:00 2001
From: Perreal Guillaume <guillaume.perreal@irstea.fr>
Date: Mon, 14 Oct 2019 13:02:50 +0200
Subject: [PATCH] Suppression de Configuration au profit d'InjectionTokens.

---
 src/lib/api/api-debug.component.ts   | 8 +++-----
 src/lib/api/api-debug.interceptor.ts | 9 +--------
 src/lib/debug-state.service.ts       | 8 ++++++--
 src/lib/debug.module.ts              | 2 --
 4 files changed, 10 insertions(+), 17 deletions(-)

diff --git a/src/lib/api/api-debug.component.ts b/src/lib/api/api-debug.component.ts
index 9fd875f..fb3e65e 100644
--- a/src/lib/api/api-debug.component.ts
+++ b/src/lib/api/api-debug.component.ts
@@ -7,8 +7,6 @@ import {
   Output,
 } from '@angular/core';
 
-// TODO injecter
-import { Configuration } from '../configuration';
 import { DebugStateService } from '../debug-state.service';
 
 import { ApiDebugInterceptor, DebugEvent } from './api-debug.interceptor';
@@ -28,6 +26,8 @@ interface ResponseInfo {
   tokenLink?: string;
 }
 
+export const ApiBaseUrlToken = new InjectionToken<string>('apiBaseUrl');
+
 @Component({
   selector: 'dbg-api-debug',
   templateUrl: './api-debug.component.html',
@@ -46,10 +46,8 @@ export class ApiDebugComponent {
     interceptor: ApiDebugInterceptor,
     public readonly debug: DebugStateService,
     private readonly cd: ChangeDetectorRef,
-    conf: Configuration
+    @Inject(ApiBaseUrlToken) private readonly apiPrefix: string,
   ) {
-    this.apiPrefix = conf.apiBaseURL;
-
     interceptor.events$.subscribe(ev => {
       this.handle(ev);
     });
diff --git a/src/lib/api/api-debug.interceptor.ts b/src/lib/api/api-debug.interceptor.ts
index d8f9b8d..a42608d 100644
--- a/src/lib/api/api-debug.interceptor.ts
+++ b/src/lib/api/api-debug.interceptor.ts
@@ -9,9 +9,6 @@ import * as _ from 'lodash';
 import { Observable, ReplaySubject } from 'rxjs';
 import { finalize, tap } from 'rxjs/operators';
 
-// TODO injecter
-import { Configuration } from '../configuration';
-
 export interface RequestDebugEvent {
   id: string;
   type: 'request';
@@ -37,11 +34,7 @@ export type DebugEvent =
 export class ApiDebugInterceptor implements HttpInterceptor {
   private readonly sink$ = new ReplaySubject<DebugEvent>(10);
   public readonly events$ = this.sink$.asObservable();
-  private readonly enabled: boolean;
-
-  constructor(conf: Configuration) {
-    this.enabled = conf.debug;
-  }
+  private readonly enabled = true;
 
   public intercept(
     request: HttpRequest<any>,
diff --git a/src/lib/debug-state.service.ts b/src/lib/debug-state.service.ts
index 1a5c072..f78d883 100644
--- a/src/lib/debug-state.service.ts
+++ b/src/lib/debug-state.service.ts
@@ -1,4 +1,4 @@
-import { Injectable } from '@angular/core';
+import { Inject, Injectable, InjectionToken, Optional } from '@angular/core';
 import { BehaviorSubject, Observable } from 'rxjs';
 import { distinctUntilChanged, map } from 'rxjs/operators';
 
@@ -8,6 +8,8 @@ export const enum DebugState {
   ENABLED = 1,
 }
 
+export const ProductionToken = new InjectionToken<boolean>('production');
+
 @Injectable()
 export class DebugStateService {
   private readonly state$: BehaviorSubject<DebugState>;
@@ -26,7 +28,9 @@ export class DebugStateService {
     return this.state === DebugState.ENABLED;
   }
 
-  public constructor(production: boolean = false) {
+  public constructor(
+    @Inject(ProductionToken) @Optional() production: boolean = false
+  ) {
     this.state$ = new BehaviorSubject<DebugState>(
       production ? DebugState.UNAVAILABLE : DebugState.ENABLED
     );
diff --git a/src/lib/debug.module.ts b/src/lib/debug.module.ts
index cb97aeb..a00db58 100644
--- a/src/lib/debug.module.ts
+++ b/src/lib/debug.module.ts
@@ -15,7 +15,6 @@ import { TreeModule } from 'primeng/tree';
 import { TreeTableModule } from 'primeng/treetable';
 
 import { ApiDebugComponent } from './api/api-debug.component';
-import { Configuration, configurationFactory } from './configuration';
 import { DebugStateService } from './debug-state.service';
 import { DebugToggleComponent } from './debug-toggle.component';
 import { DumpPanelComponent } from './dump-panel.component';
@@ -51,7 +50,6 @@ const EXPORTED_COMPONENTS = [
   providers: [
     WatchService,
     DebugStateService,
-    { provide: Configuration, useFactory: configurationFactory },
     { provide: LOCALE_ID, useValue: 'fr' },
   ],
   declarations: [
-- 
GitLab