diff --git a/src/lib/common-ui.module.ts b/src/lib/common-ui.module.ts index a0b52f2..7f9e883 100644 --- a/src/lib/common-ui.module.ts +++ b/src/lib/common-ui.module.ts @@ -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, + }, + ], }; } diff --git a/src/lib/services/api-path.interceptor.ts b/src/lib/services/api-path.interceptor.ts new file mode 100644 index 0000000..e10395e --- /dev/null +++ b/src/lib/services/api-path.interceptor.ts @@ -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, next: HttpHandler): Observable> { + 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 })); + } +} diff --git a/src/lib/services/index.ts b/src/lib/services/index.ts index 0e219ae..8549ef8 100644 --- a/src/lib/services/index.ts +++ b/src/lib/services/index.ts @@ -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';