import 'rxjs/add/observable/of'; import 'rxjs/add/operator/delay'; import 'rxjs/add/operator/concatMap'; import { Injectable } from '@angular/core'; import { HttpClient, HttpParams } from '@angular/common/http'; import { Observable } from 'rxjs/Observable'; import { TokenRefreshService } from '../token-refresh/token-refresh.service'; @Injectable() export class DataFetcher { constructor(private http: HttpClient, private tokenRefreshService: TokenRefreshService) {} fetch(url: string, lang: string, opts: any = {}, body: any = null): Observable { let params = new HttpParams(); params = params.append('apiCall', 'true'); if (lang) { params = params.append('lang', lang); } const queryOpts = {...opts, params}; switch (opts.req) { case 'delete': return this.tokenRefreshService.checkExpiry().concatMap(() => this.http.delete(url, queryOpts)); case 'put': return this.tokenRefreshService.checkExpiry().concatMap(() => this.http.put(url, body, queryOpts)); case 'post': return this.tokenRefreshService.checkExpiry().concatMap(() => this.http.post(url, body, queryOpts)); default: return this.tokenRefreshService.checkExpiry().concatMap(() => this.http.get(url, queryOpts)); } } }