Skip to content

Commit

Permalink
fix timezoneInterceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
mucsi96 committed Aug 31, 2023
1 parent c9b9927 commit b9d2b28
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 36 deletions.
9 changes: 4 additions & 5 deletions client/src/app/app.config.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { ApplicationConfig, Provider } from '@angular/core';
import { provideRouter } from '@angular/router';

import { provideHttpClient } from '@angular/common/http';
import { provideHttpClient, withInterceptors } from '@angular/common/http';
import { provideAnimations } from '@angular/platform-browser/animations';
import * as echarts from 'echarts';
import { NGX_ECHARTS_CONFIG } from 'ngx-echarts';
import { routes } from './app.routes';
import { NotificationService } from './common-components/notification.service';
import { provideTimezoneInterceptor } from './http-interceptors/timezone-interceptor';
import { timezoneInterceptor } from './http-interceptors/timezone-interceptor';
import { BackupService } from './services/backup.service';
import { WeightService } from './services/weight.service';
import { WithingsService } from './services/withings.service';
import * as echarts from 'echarts';

function provideECharts(): Provider {
return {
Expand All @@ -27,9 +27,8 @@ export const appConfig: ApplicationConfig = {
providers: [
provideAnimations(),
provideRouter(routes),
provideHttpClient(),
provideHttpClient(withInterceptors([timezoneInterceptor])),
provideLocation(),
provideTimezoneInterceptor(),
provideECharts(),
WeightService,
WithingsService,
Expand Down
47 changes: 16 additions & 31 deletions client/src/app/http-interceptors/timezone-interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,19 @@
import {
HTTP_INTERCEPTORS,
HttpEvent,
HttpHandler,
HttpInterceptor,
HttpRequest,
HttpHandlerFn,
HttpInterceptorFn,
HttpRequest
} from '@angular/common/http';
import { Injectable, Provider } from '@angular/core';
import { Observable } from 'rxjs';

@Injectable()
export class TimezoneInterceptor implements HttpInterceptor {
intercept(
req: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
return next.handle(
req.clone({
headers: req.headers.set(
'X-Timezone',
Intl.DateTimeFormat().resolvedOptions().timeZone
),
})
);
}
}

export function provideTimezoneInterceptor(): Provider {
return {
provide: HTTP_INTERCEPTORS,
useClass: TimezoneInterceptor,
multi: true,
};
}
export const timezoneInterceptor: HttpInterceptorFn = (
req: HttpRequest<unknown>,
next: HttpHandlerFn
) => {
return next(
req.clone({
headers: req.headers.set(
'X-Timezone',
Intl.DateTimeFormat().resolvedOptions().timeZone
),
})
);
};

0 comments on commit b9d2b28

Please sign in to comment.