Browse Source

transfer state interceptor refactored

feature/ssr-migration-schematic
erdemcaygor 1 year ago
parent
commit
0224f37cfc
  1. 1
      npm/ng-packs/packages/core/src/lib/interceptors/index.ts
  2. 58
      npm/ng-packs/packages/core/src/lib/interceptors/transfer-state.interceptor.ts
  3. 4
      npm/ng-packs/packages/core/src/lib/providers/core-module-config.provider.ts

1
npm/ng-packs/packages/core/src/lib/interceptors/index.ts

@ -1,2 +1,3 @@
export * from './api.interceptor'; export * from './api.interceptor';
export * from './timezone.interceptor'; export * from './timezone.interceptor';
export * from './transfer-state.interceptor';

58
npm/ng-packs/packages/core/src/lib/interceptors/transfer-state.interceptor.ts

@ -1,8 +1,8 @@
import { inject, Injectable, makeStateKey, PLATFORM_ID, TransferState } from '@angular/core'; import { inject, makeStateKey, PLATFORM_ID, TransferState } from '@angular/core';
import { import {
HttpEvent, HttpEvent,
HttpHandler, HttpHandlerFn,
HttpInterceptor, HttpInterceptorFn,
HttpRequest, HttpRequest,
HttpResponse, HttpResponse,
} from '@angular/common/http'; } from '@angular/common/http';
@ -10,35 +10,33 @@ import { Observable, of } from 'rxjs';
import { isPlatformBrowser, isPlatformServer } from '@angular/common'; import { isPlatformBrowser, isPlatformServer } from '@angular/common';
import { tap } from 'rxjs/operators'; import { tap } from 'rxjs/operators';
@Injectable() export const transferStateInterceptor: HttpInterceptorFn = (
export class TransferStateInterceptor implements HttpInterceptor { req: HttpRequest<any>,
private transferState = inject(TransferState); next: HttpHandlerFn,
private platformId = inject(PLATFORM_ID); ): Observable<HttpEvent<any>> => {
const transferState = inject(TransferState);
const platformId = inject(PLATFORM_ID);
constructor() {} if (req.method !== 'GET') {
return next(req);
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { }
if (req.method !== 'GET') {
return next.handle(req);
}
const stateKey = makeStateKey<HttpResponse<any>>(req.urlWithParams); const stateKey = makeStateKey<HttpResponse<any>>(req.urlWithParams);
if (isPlatformBrowser(this.platformId)) { if (isPlatformBrowser(platformId)) {
const storedResponse = this.transferState.get<HttpResponse<any>>(stateKey, null); const storedResponse = transferState.get<HttpResponse<any>>(stateKey, null);
if (storedResponse) { if (storedResponse) {
this.transferState.remove(stateKey); transferState.remove(stateKey);
return of(new HttpResponse<any>({ body: storedResponse, status: 200 })); return of(new HttpResponse<any>({ body: storedResponse, status: 200 }));
}
} }
return next.handle(req).pipe(
tap(event => {
if (isPlatformServer(this.platformId) && event instanceof HttpResponse) {
this.transferState.set(stateKey, event.body);
console.log(`Interceptor: ${req.urlWithParams} verisi TransferState'e kaydedildi.`);
}
}),
);
} }
}
return next(req).pipe(
tap(event => {
if (isPlatformServer(platformId) && event instanceof HttpResponse) {
transferState.set(stateKey, event.body);
console.log(`Interceptor: ${req.urlWithParams} verisi TransferState'e kaydedildi.`);
}
}),
);
};

4
npm/ng-packs/packages/core/src/lib/providers/core-module-config.provider.ts

@ -4,6 +4,7 @@ import {
HTTP_INTERCEPTORS, HTTP_INTERCEPTORS,
provideHttpClient, provideHttpClient,
withFetch, withFetch,
withInterceptors,
withInterceptorsFromDi, withInterceptorsFromDi,
withXsrfConfiguration, withXsrfConfiguration,
} from '@angular/common/http'; } from '@angular/common/http';
@ -26,7 +27,7 @@ import { DEFAULT_DYNAMIC_LAYOUTS } from '../constants';
import { LocalizationService, LocalStorageListenerService, AbpTitleStrategy } from '../services'; import { LocalizationService, LocalStorageListenerService, AbpTitleStrategy } from '../services';
import { DefaultQueueManager, getInitialData, localeInitializer } from '../utils'; import { DefaultQueueManager, getInitialData, localeInitializer } from '../utils';
import { CookieLanguageProvider, IncludeLocalizationResourcesProvider, LocaleProvider } from './'; import { CookieLanguageProvider, IncludeLocalizationResourcesProvider, LocaleProvider } from './';
import { TimezoneInterceptor } from '../interceptors'; import { TimezoneInterceptor, transferStateInterceptor } from '../interceptors';
export enum CoreFeatureKind { export enum CoreFeatureKind {
Options, Options,
@ -107,6 +108,7 @@ export function provideAbpCore(...features: CoreFeature<CoreFeatureKind>[]) {
headerName: 'RequestVerificationToken', headerName: 'RequestVerificationToken',
}), }),
withFetch(), withFetch(),
withInterceptors([transferStateInterceptor]),
), ),
provideAppInitializer(() => { provideAppInitializer(() => {
getInitialData(); getInitialData();

Loading…
Cancel
Save