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