mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
feat: add custom AlfrescoApiHttpClient [ci:force]
This commit is contained in:
committed by
Amedeo Lepore
parent
03a888fb5e
commit
a5e415bd91
@@ -20,7 +20,7 @@ import { APP_INITIALIZER, NgModule } from '@angular/core';
|
|||||||
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
|
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
|
||||||
import { FlexLayoutModule } from '@angular/flex-layout';
|
import { FlexLayoutModule } from '@angular/flex-layout';
|
||||||
import { ChartsModule } from 'ng2-charts';
|
import { ChartsModule } from 'ng2-charts';
|
||||||
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
|
import { HttpClientModule } from '@angular/common/http';
|
||||||
import { BrowserAnimationsModule, NoopAnimationsModule } from '@angular/platform-browser/animations';
|
import { BrowserAnimationsModule, NoopAnimationsModule } from '@angular/platform-browser/animations';
|
||||||
import { TranslateModule } from '@ngx-translate/core';
|
import { TranslateModule } from '@ngx-translate/core';
|
||||||
import {
|
import {
|
||||||
@@ -28,8 +28,7 @@ import {
|
|||||||
TRANSLATION_PROVIDER,
|
TRANSLATION_PROVIDER,
|
||||||
DebugAppConfigService,
|
DebugAppConfigService,
|
||||||
CoreModule,
|
CoreModule,
|
||||||
CoreAutomationService,
|
CoreAutomationService
|
||||||
AuthBearerInterceptor
|
|
||||||
} from '@alfresco/adf-core';
|
} from '@alfresco/adf-core';
|
||||||
import { ExtensionsModule } from '@alfresco/adf-extensions';
|
import { ExtensionsModule } from '@alfresco/adf-extensions';
|
||||||
import { AppComponent } from './app.component';
|
import { AppComponent } from './app.component';
|
||||||
@@ -209,10 +208,6 @@ registerLocaleData(localeSv);
|
|||||||
SearchFilterChipsComponent
|
SearchFilterChipsComponent
|
||||||
],
|
],
|
||||||
providers: [
|
providers: [
|
||||||
{
|
|
||||||
provide: HTTP_INTERCEPTORS, useClass:
|
|
||||||
AuthBearerInterceptor, multi: true
|
|
||||||
},
|
|
||||||
{ provide: AppConfigService, useClass: DebugAppConfigService }, // not use this service in production
|
{ provide: AppConfigService, useClass: DebugAppConfigService }, // not use this service in production
|
||||||
{
|
{
|
||||||
provide: TRANSLATION_PROVIDER,
|
provide: TRANSLATION_PROVIDER,
|
||||||
|
@@ -27,14 +27,13 @@ import { catchError, mergeMap } from 'rxjs/operators';
|
|||||||
@Injectable()
|
@Injectable()
|
||||||
export class AuthBearerInterceptor implements HttpInterceptor {
|
export class AuthBearerInterceptor implements HttpInterceptor {
|
||||||
private excludedUrlsRegex: RegExp[];
|
private excludedUrlsRegex: RegExp[];
|
||||||
private authService: AuthenticationService;
|
|
||||||
|
|
||||||
constructor(private injector: Injector) { }
|
constructor(private injector: Injector, private authService: AuthenticationService) { }
|
||||||
|
|
||||||
private loadExcludedUrlsRegex() {
|
private loadExcludedUrlsRegex() {
|
||||||
const excludedUrls: string[] = this.authService.getBearerExcludedUrls();
|
const excludedUrls: string[] = this.authService.getBearerExcludedUrls();
|
||||||
this.excludedUrlsRegex = excludedUrls.map((urlPattern) => new RegExp(urlPattern, 'gi')) || [];
|
|
||||||
|
|
||||||
|
this.excludedUrlsRegex = [...excludedUrls].map((urlPattern) => new RegExp(urlPattern, 'i')) || [];
|
||||||
}
|
}
|
||||||
|
|
||||||
intercept(req: HttpRequest<any>, next: HttpHandler):
|
intercept(req: HttpRequest<any>, next: HttpHandler):
|
||||||
@@ -51,7 +50,7 @@ export class AuthBearerInterceptor implements HttpInterceptor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const urlRequest = req.url;
|
const urlRequest = req.url;
|
||||||
const shallPass: boolean = !!this.excludedUrlsRegex.find((regex) => regex.test(urlRequest));
|
const shallPass: boolean = this.excludedUrlsRegex.some((regex) => regex.test(urlRequest));
|
||||||
if (shallPass) {
|
if (shallPass) {
|
||||||
return next.handle(req)
|
return next.handle(req)
|
||||||
.pipe(
|
.pipe(
|
||||||
@@ -73,7 +72,19 @@ export class AuthBearerInterceptor implements HttpInterceptor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private appendJsonContentType(headers: HttpHeaders): HttpHeaders {
|
private appendJsonContentType(headers: HttpHeaders): HttpHeaders {
|
||||||
return headers.set('Content-Type', 'application/json;charset=UTF-8');
|
|
||||||
|
// prevent adding any content type, to properly handle formData with boundary browser generated value,
|
||||||
|
// as adding any Content-Type its going to break the upload functionality
|
||||||
|
|
||||||
|
if (headers.get('Content-Type') === 'multipart/form-data') {
|
||||||
|
return headers.delete('Content-Type');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!headers.get('Content-Type')) {
|
||||||
|
return headers.set('Content-Type', 'application/json;charset=UTF-8');
|
||||||
|
}
|
||||||
|
|
||||||
|
return headers;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user