common-ui/src/lib/services/api-path.interceptor.ts
2022-07-28 00:57:38 +03:00

22 lines
804 B
TypeScript

import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { inject, Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { getConfig } from './iqser-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 }));
}
}