From 2d257ed99f5f5b95ab53ee1adb158e7f37eb211d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Berthaud-M=C3=BCller?= Date: Mon, 18 Dec 2023 18:54:07 +0100 Subject: [PATCH] Avoid bundling the mock interceptor in prod env --- src/app/app.module.ts | 15 +++------------ src/app/interceptors/request.interceptor.ts | 14 -------------- src/environments/environment.prod.ts | 3 ++- src/environments/environment.tests.ts | 9 +++++++++ src/environments/environment.ts | 11 ++++++++++- 5 files changed, 24 insertions(+), 28 deletions(-) delete mode 100644 src/app/interceptors/request.interceptor.ts diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 4bb390d0..df1b7e96 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -1,7 +1,7 @@ import { NgModule, APP_INITIALIZER } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { RouterModule, Routes } from '@angular/router'; -import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http'; +import { HttpClientModule } from '@angular/common/http'; import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; import { FormsModule, ReactiveFormsModule } from '@angular/forms'; import { environment } from '../environments/environment'; @@ -29,11 +29,6 @@ import { AlertService } from './services/alert.service'; import { NavigationService } from './services/navigation.service'; import { HeaderComponent } from './components/header/header.component'; -import { HttpRequestInterceptor } from './interceptors/request.interceptor'; -import { HttpMockRequestInterceptor } from './interceptors/mock.interceptor'; - -export const isMock = environment.mock; - const appRoutes: Routes = [ { path: 'run-test/:domain', component: DomainComponent }, { path: 'run-test', component: DomainComponent }, @@ -82,11 +77,6 @@ const appRoutes: Routes = [ DnsCheckService, AlertService, NavigationService, - { - provide: HTTP_INTERCEPTORS, - useClass: isMock ? HttpMockRequestInterceptor : HttpRequestInterceptor, - multi: true - }, { provide: APP_INITIALIZER, useFactory: (appService: AppService) => { @@ -94,7 +84,8 @@ const appRoutes: Routes = [ }, multi: true, deps: [AppService] - } + }, + ...environment.extraProvider, ], bootstrap: [AppComponent] }) diff --git a/src/app/interceptors/request.interceptor.ts b/src/app/interceptors/request.interceptor.ts deleted file mode 100644 index b602dd8d..00000000 --- a/src/app/interceptors/request.interceptor.ts +++ /dev/null @@ -1,14 +0,0 @@ - -import { Injectable, Injector } from '@angular/core'; -import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse } from '@angular/common/http'; -import { Observable, of } from 'rxjs'; - - -@Injectable() -export class HttpRequestInterceptor implements HttpInterceptor { - constructor(private injector: Injector) {} - - intercept(request: HttpRequest, next: HttpHandler): Observable> { - return next.handle(request); - } -} diff --git a/src/environments/environment.prod.ts b/src/environments/environment.prod.ts index 0efb85c2..b77cf492 100644 --- a/src/environments/environment.prod.ts +++ b/src/environments/environment.prod.ts @@ -4,5 +4,6 @@ export const environment = { ...common, production: true, apiEndpoint: '/api', - mock: false + mock: false, + extraProvider: [], }; diff --git a/src/environments/environment.tests.ts b/src/environments/environment.tests.ts index 43e958ad..cfd8cc98 100644 --- a/src/environments/environment.tests.ts +++ b/src/environments/environment.tests.ts @@ -1,4 +1,6 @@ import { common } from "./common"; +import { HTTP_INTERCEPTORS } from "@angular/common/http"; +import { HttpMockRequestInterceptor } from "../app/interceptors/mock.interceptor"; export const environment = { ...common, @@ -8,4 +10,11 @@ export const environment = { // Use a non existent file to always load the config from the environment file configUrl: 'assets/app.config.non-existent.json', pollingInterval: 0.1 * 1000, + extraProvider: [ + { + provide: HTTP_INTERCEPTORS, + useClass: HttpMockRequestInterceptor, + multi: true + } + ] }; diff --git a/src/environments/environment.ts b/src/environments/environment.ts index 919eb5eb..3354051a 100644 --- a/src/environments/environment.ts +++ b/src/environments/environment.ts @@ -3,10 +3,19 @@ // `ng build --env=prod` then `environment.prod.ts` will be used instead. // The list of which env maps to which file can be found in `.angular-cli.json`. import { common } from "./common"; +import { HttpMockRequestInterceptor } from "../app/interceptors/mock.interceptor"; +import { HTTP_INTERCEPTORS } from "@angular/common/http"; export const environment = { ...common, production: false, apiEndpoint: '/api', - mock: true + mock: true, + extraProvider: [ + { + provide: HTTP_INTERCEPTORS, + useClass: HttpMockRequestInterceptor, + multi: true + } + ] };