Skip to content

Commit

Permalink
Functional HTTP interceptors #9696
Browse files Browse the repository at this point in the history
  • Loading branch information
PowerKiKi committed Jul 24, 2023
1 parent 7e15d2e commit 02fbf32
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 31 deletions.
14 changes: 14 additions & 0 deletions client/app/shared/services/activity-interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {inject} from '@angular/core';
import {HttpInterceptorFn} from '@angular/common/http';
import {finalize} from 'rxjs/operators';
import {NetworkActivityService} from './network-activity.service';

/**
* Intercept HTTP request from Angular to show them as activity
*/
export const activityInterceptor: HttpInterceptorFn = (req, next) => {
const networkActivityService = inject(NetworkActivityService);
networkActivityService.increase();

return next(req).pipe(finalize(() => networkActivityService.decrease()));
};
23 changes: 0 additions & 23 deletions client/app/shared/services/network-interceptor.service.ts

This file was deleted.

11 changes: 3 additions & 8 deletions client/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {MAT_TOOLTIP_DEFAULT_OPTIONS, MatTooltipDefaultOptions} from '@angular/ma
import {apolloOptionsProvider} from './app/shared/config/apolloDefaultOptions';
import {LocalizedPaginatorIntlService} from './app/shared/services/localized-paginator-intl.service';
import {DATE_PIPE_DEFAULT_OPTIONS, registerLocaleData} from '@angular/common';
import {NetworkInterceptorService} from './app/shared/services/network-interceptor.service';
import {HTTP_INTERCEPTORS, provideHttpClient, withInterceptorsFromDi} from '@angular/common/http';
import {activityInterceptor} from './app/shared/services/activity-interceptor';
import {provideHttpClient, withInterceptors} from '@angular/common/http';
import {MAT_PAGINATOR_DEFAULT_OPTIONS, MatPaginatorIntl} from '@angular/material/paginator';
import {
DateAdapter,
Expand Down Expand Up @@ -82,11 +82,6 @@ bootstrapApplication(AppComponent, {
formFieldAppearance: 'fill',
},
},
{
provide: HTTP_INTERCEPTORS,
useClass: NetworkInterceptorService,
multi: true,
},
{
provide: LOCALE_ID,
useValue: 'fr-CH',
Expand All @@ -103,7 +98,7 @@ bootstrapApplication(AppComponent, {
},
apolloOptionsProvider,
{provide: MAT_TOOLTIP_DEFAULT_OPTIONS, useValue: matTooltipCustomConfig},
provideHttpClient(withInterceptorsFromDi()),
provideHttpClient(withInterceptors([activityInterceptor])),
provideRouter(
routes,
withRouterConfig({
Expand Down

0 comments on commit 02fbf32

Please sign in to comment.