changes for md
This commit is contained in:
@@ -4,6 +4,7 @@ import { provideHttpClient, withInterceptors } from '@angular/common/http';
|
||||
|
||||
import { routes } from './app.routes';
|
||||
import { cacheInterceptor } from './interceptors/cache.interceptor';
|
||||
import { apiHeadersInterceptor } from './interceptors/api-headers.interceptor';
|
||||
import { provideServiceWorker } from '@angular/service-worker';
|
||||
|
||||
export const appConfig: ApplicationConfig = {
|
||||
@@ -15,7 +16,7 @@ export const appConfig: ApplicationConfig = {
|
||||
withInMemoryScrolling({ scrollPositionRestoration: 'top' })
|
||||
),
|
||||
provideHttpClient(
|
||||
withInterceptors([cacheInterceptor])
|
||||
withInterceptors([apiHeadersInterceptor, cacheInterceptor])
|
||||
),
|
||||
provideServiceWorker('ngsw-worker.js', {
|
||||
enabled: !isDevMode(),
|
||||
|
||||
37
src/app/interceptors/api-headers.interceptor.ts
Normal file
37
src/app/interceptors/api-headers.interceptor.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { HttpInterceptorFn } from '@angular/common/http';
|
||||
import { inject } from '@angular/core';
|
||||
import { LocationService } from '../services/location.service';
|
||||
import { LanguageService } from '../services/language.service';
|
||||
import { environment } from '../../environments/environment';
|
||||
|
||||
/**
|
||||
* Interceptor that attaches X-Region and X-Language headers
|
||||
* to every outgoing request aimed at our API.
|
||||
*
|
||||
* The backend reads these headers to:
|
||||
* - filter catalog by region
|
||||
* - return translated content in the requested language
|
||||
*/
|
||||
export const apiHeadersInterceptor: HttpInterceptorFn = (req, next) => {
|
||||
// Only attach headers to our own API requests
|
||||
if (!req.url.startsWith(environment.apiUrl)) {
|
||||
return next(req);
|
||||
}
|
||||
|
||||
const locationService = inject(LocationService);
|
||||
const languageService = inject(LanguageService);
|
||||
|
||||
const regionId = locationService.regionId(); // '' when global
|
||||
const lang = languageService.currentLanguage(); // 'ru' | 'en' | 'hy'
|
||||
|
||||
let headers = req.headers;
|
||||
|
||||
if (regionId) {
|
||||
headers = headers.set('X-Region', regionId);
|
||||
}
|
||||
if (lang) {
|
||||
headers = headers.set('X-Language', lang);
|
||||
}
|
||||
|
||||
return next(req.clone({ headers }));
|
||||
};
|
||||
Reference in New Issue
Block a user