From 03364dfc7ae47a17a6e10b4eaa2cac7ea0bb80a4 Mon Sep 17 00:00:00 2001 From: bnymncoskuner Date: Wed, 16 Sep 2020 16:27:18 +0300 Subject: [PATCH] feat: complete todo and utilize getHeaders --- .../src/lib/interceptors/api.interceptor.ts | 30 ++++--------------- 1 file changed, 6 insertions(+), 24 deletions(-) diff --git a/npm/ng-packs/packages/core/src/lib/interceptors/api.interceptor.ts b/npm/ng-packs/packages/core/src/lib/interceptors/api.interceptor.ts index b3f34a0524..3227919b4a 100644 --- a/npm/ng-packs/packages/core/src/lib/interceptors/api.interceptor.ts +++ b/npm/ng-packs/packages/core/src/lib/interceptors/api.interceptor.ts @@ -1,5 +1,5 @@ import { Injectable } from '@angular/core'; -import { HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http'; +import { HttpInterceptor, HttpHandler, HttpRequest, HttpHeaders } from '@angular/common/http'; import { OAuthService } from 'angular-oauth2-oidc'; import { Store } from '@ngxs/store'; import { SessionState } from '../states'; @@ -13,48 +13,30 @@ export class ApiInterceptor implements HttpInterceptor { intercept(request: HttpRequest, next: HttpHandler) { this.store.dispatch(new StartLoader(request)); - const headers = {} as any; - - // TODO: utilize getHeaders method here WITH GREAT CARE! - const token = this.oAuthService.getAccessToken(); - if (!request.headers.has('Authorization') && token) { - headers['Authorization'] = `Bearer ${token}`; - } - - const lang = this.store.selectSnapshot(SessionState.getLanguage); - if (!request.headers.has('Accept-Language') && lang) { - headers['Accept-Language'] = lang; - } - - const tenant = this.store.selectSnapshot(SessionState.getTenant); - if (!request.headers.has('__tenant') && tenant) { - headers['__tenant'] = tenant.id; - } - return next .handle( request.clone({ - setHeaders: headers, + setHeaders: this.getHeaders(request.headers), }), ) .pipe(finalize(() => this.store.dispatch(new StopLoader(request)))); } - getHeaders() { + getHeaders(existingHeaders?: HttpHeaders) { const headers = {} as any; const token = this.oAuthService.getAccessToken(); - if (token) { + if (!existingHeaders?.has('Authorization') && token) { headers['Authorization'] = `Bearer ${token}`; } const lang = this.store.selectSnapshot(SessionState.getLanguage); - if (lang) { + if (!existingHeaders?.has('Accept-Language') && lang) { headers['Accept-Language'] = lang; } const tenant = this.store.selectSnapshot(SessionState.getTenant); - if (tenant) { + if (!existingHeaders?.has('__tenant') && tenant) { headers['__tenant'] = tenant.id; }