add api path interceptor

This commit is contained in:
Dan Percic 2022-07-27 15:35:59 +03:00
parent 91777db7fb
commit d849082482
3 changed files with 33 additions and 2 deletions

View File

@ -33,8 +33,9 @@ import { DragDropFileUploadDirective } from './upload-file/drag-drop-file-upload
import { MatProgressBarModule } from '@angular/material/progress-bar';
import { ConfirmationDialogComponent } from './dialog';
import { MatTooltipModule } from '@angular/material/tooltip';
import { BaseConfigService, BaseUserPreferenceService } from './services';
import { ApiPathInterceptor, BaseConfigService, BaseUserPreferenceService } from './services';
import { DefaultUserPreferenceService } from './services/default-user-preference.service';
import { HTTP_INTERCEPTORS } from '@angular/common/http';
const matModules = [
MatIconModule,
@ -96,7 +97,15 @@ export class CommonUiModule extends ModuleWithOptions {
return {
ngModule: CommonUiModule,
providers: [userPreferenceService, configServiceProviders],
providers: [
userPreferenceService,
configServiceProviders,
{
provide: HTTP_INTERCEPTORS,
multi: true,
useClass: ApiPathInterceptor,
},
],
};
}

View File

@ -0,0 +1,21 @@
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { inject, Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { getConfig } from './base-config.service';
import { BASE_HREF } from '../utils';
@Injectable()
export class ApiPathInterceptor implements HttpInterceptor {
readonly #config = getConfig();
readonly #baseHref = inject(BASE_HREF);
intercept(req: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
if (!req.url.startsWith('/assets')) {
const apiUrl = `${this.#config.API_URL}${req.url}`;
return next.handle(req.clone({ url: apiUrl }));
}
const url = this.#baseHref + req.url;
return next.handle(req.clone({ url }));
}
}

View File

@ -8,3 +8,4 @@ export * from './entities-map.service';
export * from './base-user-preference.service';
export * from './language.service';
export * from './base-config.service';
export * from './api-path.interceptor';