notification.interceptor.ts 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { JhiAlertService } from 'ng-jhipster';
  2. import { HttpInterceptor, HttpRequest, HttpResponse, HttpHandler, HttpEvent } from '@angular/common/http';
  3. import { Injectable } from '@angular/core';
  4. import { Observable } from 'rxjs';
  5. import { tap } from 'rxjs/operators';
  6. @Injectable()
  7. export class NotificationInterceptor implements HttpInterceptor {
  8. constructor(private alertService: JhiAlertService) {}
  9. intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
  10. return next.handle(request).pipe(
  11. tap(
  12. (event: HttpEvent<any>) => {
  13. if (event instanceof HttpResponse) {
  14. const arr = event.headers.keys();
  15. let alert = null;
  16. let alertParams = null;
  17. arr.forEach(entry => {
  18. if (entry.toLowerCase().endsWith('app-alert')) {
  19. alert = event.headers.get(entry);
  20. } else if (entry.toLowerCase().endsWith('app-params')) {
  21. alertParams = event.headers.get(entry);
  22. }
  23. });
  24. if (alert) {
  25. if (typeof alert === 'string') {
  26. this.alertService.success(alert, { param: alertParams }, null);
  27. }
  28. }
  29. }
  30. },
  31. (err: any) => {}
  32. )
  33. );
  34. }
  35. }