134 lines
5.4 KiB
TypeScript
134 lines
5.4 KiB
TypeScript
import { Message } from '@angular/compiler/src/i18n/i18n_ast';
|
|
import { ApiMessageCode } from '../types/ApiMessageCode';
|
|
import { Injectable } from '@angular/core';
|
|
import { Http, RequestOptions, Response, Headers } from '@angular/http';
|
|
import { Observable } from 'rxjs';
|
|
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';
|
|
import { BaseHttpResponseModel } from '../../models/http/BaseHttpResponseModel';
|
|
import { ApiResponseCode } from '../../utilities/types/ApiResponseCode';
|
|
|
|
@Injectable()
|
|
export class BaseHttpService {
|
|
constructor(
|
|
protected http: HttpClient,
|
|
private router: Router,
|
|
private authService: AuthService,
|
|
public language: TranslateService,
|
|
public snackBar: MatSnackBar,
|
|
public route: ActivatedRoute
|
|
) {
|
|
}
|
|
get<T>(url: string, options?: any, appendOptions: boolean = true): Observable<T> {
|
|
return this.interceptRepsonse<T>(this.http.get(url, this.buildRequestOptions(appendOptions, options)));
|
|
}
|
|
post<T>(url: string, body: any, options?: any, appendOptions: boolean = true): Observable<T> {
|
|
return this.interceptRepsonse<T>(this.http.post(url, body, this.buildRequestOptions(appendOptions, options)));
|
|
}
|
|
put<T>(url: string, body: any, options?: any, appendOptions: boolean = true): Observable<T> {
|
|
return this.interceptRepsonse<T>(this.http.put(url, body, this.buildRequestOptions(appendOptions, options)));
|
|
}
|
|
delete<T>(url: string, options?: any, appendOptions: boolean = true): Observable<T> {
|
|
return this.interceptRepsonse<T>(this.http.delete(url, this.buildRequestOptions(appendOptions, options)));
|
|
}
|
|
patch<T>(url: string, body: any, options?: any, appendOptions: boolean = true): Observable<T> {
|
|
return this.interceptRepsonse(this.http.patch(url, body, this.buildRequestOptions(appendOptions, options)));
|
|
}
|
|
head<T>(url: string, options?: any, appendOptions: boolean = true): Observable<T> {
|
|
return this.interceptRepsonse<T>(this.http.head(url, this.buildRequestOptions(appendOptions, options)));
|
|
}
|
|
options<T>(url: string, options?: any, appendOptions: boolean = true): Observable<T> {
|
|
return this.interceptRepsonse<T>(this.http.options(url, this.buildRequestOptions(appendOptions, options)));
|
|
}
|
|
|
|
protected buildRequestOptions(appendOptions: boolean, options?: any): Object {
|
|
if (options == null) {
|
|
options = new RequestOptions();
|
|
}
|
|
if (options.headers == null) {
|
|
options.headers = new Headers();
|
|
}
|
|
if (!options.headers.has('Content-Type') && appendOptions) {
|
|
options.headers = options.headers.set('Content-Type', 'application/json');
|
|
}
|
|
if (!options.headers.has('Content-Type') && appendOptions) {
|
|
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((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<T>();
|
|
} 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'];
|
|
}
|
|
});
|
|
}
|
|
|
|
// public handleResponse<T>(response: BaseHttpResponseModel<T>) {
|
|
// if (response.statusCode < 200 || response.statusCode >= 300) {
|
|
// //throw new Error('Request failed');
|
|
// this.snackBar.openFromComponent(SnackBarNotificationComponent, {
|
|
// data: { message: 'GENERAL.ERRORS.HTTP-REQUEST-ERROR', language: this.language },
|
|
// duration: 3000,
|
|
// extraClasses: ['snackbar-warning']
|
|
// })
|
|
// }
|
|
// else {
|
|
// return response.payload;
|
|
// }
|
|
// }
|
|
}
|