UNPKG

@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>

54 lines (50 loc) 2.14 kB
import { HttpResponse } from '@angular/common/http'; import * as i0 from '@angular/core'; import { Injectable } from '@angular/core'; import { tap, shareReplay } from 'rxjs/operators'; class NgDocCacheInterceptor { constructor() { this.cache = new Map(); } static { this.TOKEN = Math.random().toString(36).slice(-8); } intercept(request, next) { // Only GET requests can be cached if (request.method !== 'GET') { return next.handle(request); } // Do not cache request when the token is not provided if (!request.params.has(NgDocCacheInterceptor.TOKEN)) { return next.handle(request); } // Return cached response const cachedRequest = this.cache.get(request.url); if (cachedRequest) { return cachedRequest; } // Clone the request, delete the TOKEN from the params const newRequest = request.clone({ params: request.params.delete(NgDocCacheInterceptor.TOKEN), }); // Create a new request handler const newHandler = next.handle(newRequest).pipe(tap({ error: (event) => { if (event instanceof HttpResponse) { this.cache.delete(event.url || ''); } }, }), shareReplay(1)); // Cache the request and return the new handler this.cache.set(request.url, newHandler); return newHandler; } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.0.3", ngImport: i0, type: NgDocCacheInterceptor, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.0.3", ngImport: i0, type: NgDocCacheInterceptor }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.0.3", ngImport: i0, type: NgDocCacheInterceptor, decorators: [{ type: Injectable }] }); /** * Generated bundle index. Do not edit. */ export { NgDocCacheInterceptor }; //# sourceMappingURL=ng-doc-ui-kit-interceptors.mjs.map