@ng-doc/ui-kit
Version:
<!-- PROJECT LOGO --> <br /> <div align="center"> <a href="https://github.com/ng-doc/ng-doc"> <img src="https://ng-doc.com/assets/images/ng-doc.svg?raw=true" alt="Logo" height="150px"> </a>
1 lines • 3.44 kB
Source Map (JSON)
{"version":3,"file":"ng-doc-ui-kit-interceptors.mjs","sources":["../../../../libs/ui-kit/interceptors/cache.interceptor.ts","../../../../libs/ui-kit/interceptors/ng-doc-ui-kit-interceptors.ts"],"sourcesContent":["import {\n HttpEvent,\n HttpHandler,\n HttpInterceptor,\n HttpRequest,\n HttpResponse,\n} from '@angular/common/http';\nimport { Injectable } from '@angular/core';\nimport { Observable } from 'rxjs';\nimport { shareReplay, tap } from 'rxjs/operators';\n\n@Injectable()\nexport class NgDocCacheInterceptor implements HttpInterceptor {\n static readonly TOKEN: string = Math.random().toString(36).slice(-8);\n private cache: Map<string, Observable<HttpEvent<unknown>>> = new Map<\n string,\n Observable<HttpEvent<unknown>>\n >();\n\n intercept<T>(request: HttpRequest<T>, next: HttpHandler): Observable<HttpEvent<T>> {\n // Only GET requests can be cached\n if (request.method !== 'GET') {\n return next.handle(request);\n }\n\n // Do not cache request when the token is not provided\n if (!request.params.has(NgDocCacheInterceptor.TOKEN)) {\n return next.handle(request);\n }\n\n // Return cached response\n const cachedRequest: Observable<HttpEvent<T>> | undefined = this.cache.get(\n request.url,\n ) as Observable<HttpEvent<T>>;\n\n if (cachedRequest) {\n return cachedRequest;\n }\n\n // Clone the request, delete the TOKEN from the params\n const newRequest: HttpRequest<T> = request.clone({\n params: request.params.delete(NgDocCacheInterceptor.TOKEN),\n });\n\n // Create a new request handler\n const newHandler: Observable<HttpEvent<T>> = next.handle(newRequest).pipe(\n tap({\n error: (event: HttpEvent<Error>) => {\n if (event instanceof HttpResponse) {\n this.cache.delete(event.url || '');\n }\n },\n }),\n shareReplay(1),\n );\n\n // Cache the request and return the new handler\n this.cache.set(request.url, newHandler);\n\n return newHandler;\n }\n}\n","/**\n * Generated bundle index.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