85 lines
3.1 KiB
TypeScript
85 lines
3.1 KiB
TypeScript
import { Injectable } from '@angular/core';
|
|
import { Http, RequestOptions, Response, Headers } from '@angular/http';
|
|
import { Observable } from 'rxjs/Observable';
|
|
import { Router, ActivatedRoute, RouterStateSnapshot } from '@angular/router';
|
|
import { MatSnackBar } from '@angular/material';
|
|
import { TranslateService } from '@ngx-translate/core';
|
|
import { HttpClient } from '@angular/common/http';
|
|
import { AuthService } from '../../services/auth/auth.service';
|
|
import { SnackBarNotificationComponent } from '../../shared/components/notificaiton/snack-bar-notification.component';
|
|
|
|
@Injectable()
|
|
export class BaseHttpService {
|
|
constructor(
|
|
protected http: HttpClient,
|
|
private router: Router,
|
|
private authService: AuthService,
|
|
public language: TranslateService,
|
|
public snackBar: MatSnackBar,
|
|
|
|
) {
|
|
}
|
|
get<T>(url: string, options?: any): Observable<T> {
|
|
return this.interceptRepsonse<T>(this.http.get(url, this.buildRequestOptions(options)));
|
|
}
|
|
post<T>(url: string, body: any, options?: any): Observable<T> {
|
|
return this.interceptRepsonse<T>(this.http.post(url, body, this.buildRequestOptions(options)));
|
|
}
|
|
put<T>(url: string, body: any, options?: any): Observable<T> {
|
|
return this.interceptRepsonse<T>(this.http.put(url, body, this.buildRequestOptions(options)));
|
|
}
|
|
delete<T>(url: string, options?: any): Observable<T> {
|
|
return this.interceptRepsonse<T>(this.http.delete(url, this.buildRequestOptions(options)));
|
|
}
|
|
patch<T>(url: string, body: any, options?: any): Observable<T> {
|
|
return this.interceptRepsonse(this.http.patch(url, body, this.buildRequestOptions(options)));
|
|
}
|
|
head<T>(url: string, options?: any): Observable<T> {
|
|
return this.interceptRepsonse<T>(this.http.head(url, this.buildRequestOptions(options)));
|
|
}
|
|
options<T>(url: string, options?: any): Observable<T> {
|
|
return this.interceptRepsonse<T>(this.http.options(url, this.buildRequestOptions(options)));
|
|
}
|
|
|
|
protected buildRequestOptions(options?: any): Object {
|
|
if (options == null) {
|
|
options = new RequestOptions();
|
|
}
|
|
if (options.headers == null) {
|
|
options.headers = new Headers();
|
|
}
|
|
if (!options.headers.has('Content-Type')) {
|
|
options.headers = options.headers.set('Content-Type', 'application/json');
|
|
}
|
|
if (!options.headers.has('Content-Type')) {
|
|
options.headers = options.headers.set('Content-Type', 'application/json');
|
|
}
|
|
if (!options.headers.has('AuthToken')) {
|
|
const principal = this.authService.current();
|
|
if (principal) {
|
|
options.headers = options.headers.set('AuthToken', principal.token);
|
|
}
|
|
}
|
|
|
|
return options;
|
|
}
|
|
|
|
private interceptRepsonse<T>(observable: Observable<Object>): Observable<T> {
|
|
return observable
|
|
.catch((error) => {
|
|
if (error.status === 401) {
|
|
this.snackBar.openFromComponent(SnackBarNotificationComponent, {
|
|
data: { message: 'GENERAL.SNACK-BAR.SUCCESSFUL-LOGOUT', language: this.language },
|
|
duration: 3000,
|
|
extraClasses: ['snackbar-success']
|
|
})
|
|
this.router.navigate(['/unauthorized']);
|
|
//this.notification.httpError(error);
|
|
return Observable.of<T>();
|
|
} else {
|
|
return Observable.throw(error);
|
|
}
|
|
});
|
|
}
|
|
}
|