import {map} from 'rxjs/operators'; import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { Observable } from 'rxjs'; import { ApiMessageCode } from '../../common/enum/api-message-code'; @Injectable() export class BaseHttpService { constructor( protected http: HttpClient ) { } get(url: string, options?: Object): Observable { return this.interceptRepsonse(this.http.get(url, options)); } post(url: string, body: any, options?: Object): Observable { return this.interceptRepsonse(this.http.post(url, body, options)); } put(url: string, body: any, options?: Object): Observable { return this.interceptRepsonse(this.http.put(url, body, options)); } delete(url: string, options?: Object): Observable { return this.interceptRepsonse(this.http.delete(url, options)); } patch(url: string, body: any, options?: Object): Observable { return this.interceptRepsonse(this.http.patch(url, body, options)); } head(url: string, options?: Object): Observable { return this.interceptRepsonse(this.http.head(url, options)); } options(url: string, options?: Object): Observable { return this.interceptRepsonse(this.http.options(url, options)); } private interceptRepsonse(observable: Observable): Observable { return observable.pipe( // .catch((errorResponse) => { // if (errorResponse.status === 401) { // this.snackBar.openFromComponent(SnackBarNotificationComponent, { // data: { message: 'GENERAL.SNACK-BAR.SUCCESSFUL-LOGOUT', language: this.language }, // duration: 3000, // }); // const currentPage = this.router.url; // this.router.navigate(['/unauthorized'], { queryParams: { returnUrl: currentPage } }); // //this.notification.httpError(error); // return Observable.of(); // } else { // const error: any = errorResponse.error; // if (error.statusCode === ApiMessageCode.ERROR_MESSAGE) { // this.snackBar.openFromComponent(SnackBarNotificationComponent, { // data: { message: error.message, language: null }, // duration: 3000, // }); // return Observable.throw(errorResponse); // } else if (error.statusCode === ApiMessageCode.VALIDATION_MESSAGE) { // return Observable.throw(errorResponse); // } else { // this.snackBar.openFromComponent(SnackBarNotificationComponent, { // data: { message: 'GENERAL.ERRORS.HTTP-REQUEST-ERROR', language: this.language }, // duration: 3000, // }); // return Observable.throw(errorResponse); // } // } // }) map(response => { if (response instanceof Blob) { return response; } if (response['statusCode'] === ApiMessageCode.SUCCESS_MESSAGE) { //throw new Error('Request failed'); // this.snackBar.openFromComponent(SnackBarNotificationComponent, { // data: { message: response['message'], language: null }, // duration: 3000, // }); return response['payload']; } else if (response['statusCode'] === ApiMessageCode.NO_MESSAGE) { return response['payload']; } else { return response['payload']; } })); } }