262 lines
8.1 KiB
TypeScript
262 lines
8.1 KiB
TypeScript
import { Injectable } from '@angular/core';
|
|
import { Observable, Subject, throwError } from 'rxjs';
|
|
import { HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
|
|
import { catchError, retry } from 'rxjs/operators';
|
|
import { DatePipe } from '@angular/common';
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
|
|
export class ApiService {
|
|
|
|
// For Development
|
|
public static BASE_URL = 'https://localhost:44375/api';
|
|
public base_url = 'https://localhost:44375/api';
|
|
|
|
// For WeLink
|
|
/*public static BASE_URL = '/welinkventures/api';
|
|
public base_url = '/welinkventures/api';*/
|
|
|
|
// For BAT
|
|
/*public static BASE_URL = '/batDemo/api';
|
|
public base_url = '/batDemo/api';*/
|
|
|
|
// For Demo
|
|
/*public static BASE_URL = '/celDemo/api';
|
|
public base_url = '/celDemo/api';*/
|
|
|
|
// For LiFung App Test
|
|
//public base_url = '/lifungtest/api';
|
|
//public static BASE_URL = '/lifungtest/api';
|
|
|
|
// For Synovia Test
|
|
// public base_url = '/synovia/api';
|
|
// public static BASE_URL = '/synovia/api';
|
|
|
|
// For Synovia Live
|
|
// public base_url = '/synoviahr/api';
|
|
// public static BASE_URL = '/synoviahr/api';
|
|
|
|
// For BProperty Live
|
|
/*public base_url = '/api';
|
|
public static BASE_URL = '/api';*/
|
|
|
|
// For Azure
|
|
/*public static BASE_URL = '/api';
|
|
public base_url = '/api';*/
|
|
|
|
// For H&M Live
|
|
/*public base_url = '/hnm/api';
|
|
public static BASE_URL = '/hnm/api';*/
|
|
|
|
// For erecruitment
|
|
/*public base_url = '/erecruitment/api';
|
|
public static BASE_URL = '/erecruitment/api';*/
|
|
|
|
// public mobileAppDownloadLink = this.BASE_URL + 'MobileApp/Download';
|
|
|
|
|
|
public static AuthenticationToken = '';
|
|
public selectedMenuName: string = '';
|
|
constructor(public httpClient: HttpClient, public datepipe: DatePipe) {
|
|
}
|
|
|
|
|
|
createClientHeader(): { headers: HttpHeaders } {
|
|
let token = '';
|
|
|
|
if (ApiService.AuthenticationToken) {
|
|
token = `Bearer ${ApiService.AuthenticationToken}`;
|
|
}
|
|
|
|
const httpOptions = {
|
|
headers: new HttpHeaders({
|
|
'Content-Type': 'application/json',
|
|
'Accept': 'application/json',
|
|
'Authorization': token
|
|
})
|
|
|
|
};
|
|
return httpOptions;
|
|
}
|
|
|
|
|
|
getApiDefaultData(str: string) {
|
|
return (str === undefined || str === null || str.trim() === '') ? null : str;
|
|
}
|
|
|
|
getApiDateString(dDate: Date) {
|
|
if (dDate === undefined || dDate === null) return dDate;
|
|
var str: string = this.datepipe.transform(dDate, 'dd-MMM-yyyy');
|
|
return str;
|
|
}
|
|
getApiDefaultIntData(intData: number) {
|
|
return (intData === undefined || intData === null) ? undefined : intData;
|
|
}
|
|
|
|
|
|
public httpGet<T>(url: string, params?: any): Observable<T> {
|
|
|
|
var options: HttpHeaders;
|
|
if (!options) {
|
|
options = new HttpHeaders({ 'Content-Type': 'application/json;charset=utf-8' });
|
|
options = options.append('Accept', 'application/json');
|
|
}
|
|
|
|
if (ApiService.AuthenticationToken) {
|
|
options = options.append('Authorization', `Bearer ${ApiService.AuthenticationToken}`);
|
|
}
|
|
|
|
|
|
if (params) {
|
|
const p = [];
|
|
for (const k in params) {
|
|
p.push((k + '=' + params[k]) as any);
|
|
}
|
|
url = url + '?' + (p.join('&'));
|
|
}
|
|
|
|
var endpoint: string;
|
|
return this.httpClient.get<T>(this.base_url + url, { headers: options, withCredentials: true });
|
|
|
|
// return this.http.get<T>(`${url}`, { headers: options, withCredentials: true });
|
|
|
|
|
|
|
|
}
|
|
|
|
public httpGetByKey<T>(url: string, key: any): Observable<T> {
|
|
return this.httpClient.get<T>(ApiService.BASE_URL + url + key, this.createClientHeader())
|
|
.pipe(
|
|
catchError(this.handleClientError)
|
|
);
|
|
}
|
|
|
|
|
|
public httpPost<T>(url: string, body: any): Observable<T> {
|
|
|
|
var options: HttpHeaders;
|
|
if (!options) {
|
|
options = new HttpHeaders({ 'Content-Type': 'application/json;charset=utf-8' });
|
|
options = options.append('Accept', 'application/json');
|
|
}
|
|
debugger;
|
|
if (ApiService.AuthenticationToken) {
|
|
options = options.append('Authorization', `Bearer ${ApiService.AuthenticationToken}`);
|
|
}
|
|
|
|
return this.httpClient.post<T>(this.base_url + url, body, { headers: options, withCredentials: true });
|
|
//return this.httpClient.post<T>(ApiService.BASE_URL + url, body, this.createClientHeader())
|
|
// .pipe(
|
|
// catchError(this.handleClientError)
|
|
// );
|
|
}
|
|
|
|
public httpPostLoginWithGoogle<T>(url: string, body: any): Observable<T> {
|
|
|
|
var options: HttpHeaders;
|
|
if (!options) {
|
|
options = new HttpHeaders({ 'Content-Type': 'application/json;charset=utf-8' });
|
|
options = options.append('Accept', 'application/json');
|
|
}
|
|
return this.httpClient.post<T>(this.base_url + url, body, { headers: options, withCredentials: true });
|
|
}
|
|
|
|
//public LoginWithGoogle(credentials: string): Observable<any> {
|
|
// const header = new HttpHeaders().set('Content-type', 'application/json');
|
|
// return this.httpClient.post(this.base_url + "/auth/loginWithGoogle", JSON.stringify(credentials), { headers: header });
|
|
//}
|
|
|
|
|
|
public httpDelete<T>(url: string, key: any): Observable<T> {
|
|
return this.httpClient.delete<T>(ApiService.BASE_URL + url + key, this.createClientHeader())
|
|
.pipe(
|
|
catchError(this.handleClientError)
|
|
);
|
|
}
|
|
|
|
|
|
public httpPostFile(url: string, body: any): Observable<Blob> {
|
|
|
|
var options: HttpHeaders;
|
|
if (!options) {
|
|
options = new HttpHeaders({ 'Content-Type': 'application/json;charset=utf-8' });
|
|
options = options.append('Accept', 'application/json');
|
|
}
|
|
if (ApiService.AuthenticationToken) {
|
|
options = options.append('Authorization', `Bearer ${ApiService.AuthenticationToken}`);
|
|
}
|
|
return this.httpClient.post<Blob>(this.base_url + url, body, { headers: options, withCredentials: true })
|
|
.pipe(
|
|
catchError(this.handleClientError)
|
|
);
|
|
}
|
|
|
|
public httpPostBlob(url: string, body: any): Observable<any> {
|
|
var options: HttpHeaders;
|
|
if (!options) {
|
|
//options = new HttpHeaders({ 'Content-Type': 'application/json;charset=utf-8' });
|
|
//options = options.append('Accept', 'application/json');
|
|
}
|
|
if (ApiService.AuthenticationToken) {
|
|
options = new HttpHeaders();
|
|
options = options.append('Authorization', `Bearer ${ApiService.AuthenticationToken}`);
|
|
}
|
|
return this.httpClient.post<Blob>(this.base_url + url, body, { headers: options, withCredentials: true })
|
|
.pipe(
|
|
catchError(this.handleClientError)
|
|
);
|
|
}
|
|
|
|
public httpDownloadFile(url: string, body: any): Observable<Blob> {
|
|
var options: HttpHeaders;
|
|
if (!options) {
|
|
//options = new HttpHeaders({ 'Content-Type': 'application/json;charset=utf-8' });
|
|
//options = options.append('Accept', 'application/json');
|
|
}
|
|
if (ApiService.AuthenticationToken) {
|
|
options = new HttpHeaders();
|
|
options = options.append('Authorization', `Bearer ${ApiService.AuthenticationToken}`);
|
|
}
|
|
return this.httpClient.post<Blob>(this.base_url + url, body, { headers: options, withCredentials: true, responseType: 'blob' as 'json' })
|
|
.pipe(
|
|
catchError(this.handleClientError)
|
|
);
|
|
}
|
|
public httpDownloadFileNew(url: string, body: any): Observable<Blob> {
|
|
|
|
var options: HttpHeaders;
|
|
if (!options) {
|
|
options = new HttpHeaders({ 'Content-Type': 'application/json;charset=utf-8' });
|
|
options = options.append('Accept', 'application/json');
|
|
}
|
|
|
|
return this.httpClient.post<Blob>(this.base_url + url, body, { headers: options, withCredentials: true, responseType: 'blob' as 'json' });
|
|
|
|
}
|
|
|
|
public handleClientError(error: HttpErrorResponse) {
|
|
// return Observable.throw(error.message || 'An error has been occurred. Please try again or contact to system administrator');
|
|
return throwError(error.error || 'An error has been occurred. Please try again or contact to system administrator');
|
|
}
|
|
}
|
|
|
|
@Injectable()
|
|
export class PubSubService {
|
|
public subjects: Subject<any>[] = [];
|
|
|
|
publish(eventName: string) {
|
|
this.subjects[eventName] = this.subjects[eventName] || new Subject<any>();
|
|
this.subjects[eventName].next();
|
|
}
|
|
|
|
on(eventName: string): Observable<any> {
|
|
this.subjects[eventName] = this.subjects[eventName] || new Subject<any>();
|
|
return this.subjects[eventName].asObservable();
|
|
}
|
|
}
|
|
@Injectable()
|
|
export class BaseHrefService {
|
|
|
|
}
|