common-ui/src/lib/services/api-path.interceptor.ts
2024-06-19 13:05:12 +03:00

22 lines
828 B
TypeScript

import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { inject, Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { UI_ROOT_PATH_FN } from '../utils/tokens';
import { getConfig } from './iqser-config.service';
@Injectable()
export class ApiPathInterceptor implements HttpInterceptor {
readonly #config = getConfig();
readonly #convertPath = inject(UI_ROOT_PATH_FN);
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.#convertPath(req.url);
return next.handle(req.clone({ url }));
}
}