p3x-angular-http-cache-interceptor
Version:
🔥 Cache every request in Angular, not only the GET, but all methods of this interceptor, and allows you to interact with the interceptor via specific headers and modify the request, and these specific headers will be not included in the final request
1 lines • 11 kB
Source Map (JSON)
{"version":3,"file":"p3x-angular-http-cache-interceptor.mjs","sources":["../../../projects/angular-http-cache-interceptor/src/lib/caching-headers.enum.ts","../../../projects/angular-http-cache-interceptor/src/lib/caching-store.enum.ts","../../../projects/angular-http-cache-interceptor/src/lib/http-cache-config.token.ts","../../../projects/angular-http-cache-interceptor/src/lib/http-cache-interceptor.interceptor.ts","../../../projects/angular-http-cache-interceptor/src/lib/http-cache-interceptor.module.ts","../../../projects/angular-http-cache-interceptor/src/public-api.ts","../../../projects/angular-http-cache-interceptor/src/p3x-angular-http-cache-interceptor.ts"],"sourcesContent":["// cache headers has to have a x- prefix\nexport enum CachingHeaders {\n NoCache = \"x-p3x-no-cache\",\n Cache = \"x-p3x-cache\",\n}\n","// cache headers has to have a x- prefix\nexport enum CachingStore {\n Global,\n PerModule\n}\n","import { InjectionToken } from \"@angular/core\";\nimport { HttpCacheConfig } from \"./http-cache-config\";\n\nexport const P3X_HTTP_CACHE_CONFIG = new InjectionToken(\n 'P3X_HTTP_CACHE_CONFIG'\n)\n","import {Inject, Injectable, Optional} from '@angular/core';\nimport { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse } from '@angular/common/http';\nimport {Observable, of} from 'rxjs';\n\nimport hash from 'object-hash'\nimport {tap} from \"rxjs/operators\";\n\nimport {CachingHeaders} from \"./caching-headers.enum\";\nimport {CachingStore} from \"./caching-store.enum\";\n\nimport {P3X_HTTP_CACHE_CONFIG} from \"./http-cache-config.token\";\nimport {HttpCacheConfig} from \"./http-cache-config\";\n\n\nconst hashOptions = {\n algorithm: 'md5',\n encoding: 'hex'\n}\n\nconst globalCache = new Map<string, any>()\n\n@Injectable()\nexport class HttpCacheInterceptorInterceptor implements HttpInterceptor {\n\n private cachedData = new Map<string, any>()\n\n httpCacheConfig : HttpCacheConfig = {\n behavior: CachingHeaders.Cache,\n store: CachingStore.Global,\n }\n\n getCache(key: string) {\n if (this.httpCacheConfig.store === CachingStore.Global) {\n return globalCache.get(key)\n } else {\n return this.cachedData.get(key)\n }\n }\n\n setCache(key: string, value: any) {\n if (this.httpCacheConfig.store === CachingStore.Global) {\n globalCache.set(key, value)\n } else {\n this.cachedData.set(key, value)\n }\n }\n\n constructor(@Inject(P3X_HTTP_CACHE_CONFIG) @Optional() httpCacheConfigToken: HttpCacheConfig) {\n if (httpCacheConfigToken) {\n this.httpCacheConfig = httpCacheConfigToken\n }\n }\n\n httpToKey(httpRequest: HttpRequest<any>) {\n const body = JSON.parse(JSON.stringify(httpRequest.body))\n const key = httpRequest.method + '@' + httpRequest.urlWithParams + '@' + hash(httpRequest.params, hashOptions) + '@' + hash(body, hashOptions)\n return key\n }\n\n intercept(httpRequest: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {\n\n\n //console.log(httpRequest)\n //console.log('has', httpRequest.headers.has(CachingHeaders.NoCache))\n //console.log('value', httpRequest.headers.get(CachingHeaders.NoCache))\n\n const forcedCache = httpRequest.headers.get(CachingHeaders.Cache) !== null\n const forcedNoneCache = httpRequest.headers.get(CachingHeaders.NoCache) !== null\n\n //console.log('forcedCache', forcedCache, 'forcedNoneCache', forcedNoneCache)\n\n let headers = httpRequest.headers.delete(CachingHeaders.NoCache)\n headers = headers.delete(CachingHeaders.Cache)\n httpRequest = httpRequest.clone({\n headers: headers\n })\n\n if (forcedCache && forcedNoneCache) {\n throw new Error('You cannot use cache and non-cache header at once!')\n } else if (forcedNoneCache || (this.httpCacheConfig.behavior === CachingHeaders.NoCache && !forcedCache)) {\n return next.handle(httpRequest);\n } else if (forcedCache || (this.httpCacheConfig.behavior === CachingHeaders.Cache && !forcedNoneCache)) {\n // Checked if there is cached data for this URI\n const key = this.httpToKey(httpRequest)\n const lastResponse = this.getCache(key);\n if (lastResponse) {\n // In case of parallel requests to same URI,\n // return the request already in progress\n // otherwise return the last cached data\n\n //console.info('http cache interceptor hit cache', key)\n\n return (lastResponse instanceof Observable)\n ? lastResponse : of(lastResponse.clone());\n }\n\n //console.info('http cache interceptor', key)\n\n // If the request of going through for first time\n // then let the request proceed and cache the response\n const requestHandle = next.handle(httpRequest).pipe(\n tap((stateEvent: any) => {\n if (stateEvent instanceof HttpResponse) {\n this.setCache(\n key,\n stateEvent.clone()\n );\n }\n })\n )\n\n // Meanwhile cache the request Observable to handle parallel request\n //this.cachedData.set(key, requestHandle);\n\n return requestHandle;\n } else {\n console.error(this.httpCacheConfig)\n console.error(httpRequest.headers)\n throw new Error('There is a configuration in your setup')\n }\n\n /*\n // Also leave scope of resetting already cached data for a URI\n if (httpRequest.headers.get(\"reset-cache\")) {\n this.cachedData.delete(httpRequest.urlWithParams);\n }\n */\n\n\n }\n}\n","import {ModuleWithProviders, NgModule} from '@angular/core';\nimport { HTTP_INTERCEPTORS } from \"@angular/common/http\";\nimport { HttpCacheInterceptorInterceptor } from './http-cache-interceptor.interceptor'\n\nimport { P3X_HTTP_CACHE_CONFIG } from \"./http-cache-config.token\";\nimport { HttpCacheConfig } from \"./http-cache-config\";\n\n@NgModule({\n declarations: [],\n imports: [\n ],\n providers: [\n {\n provide: HTTP_INTERCEPTORS,\n useClass: HttpCacheInterceptorInterceptor,\n multi: true\n }\n ],\n exports: []\n})\nexport class P3XHttpCacheInterceptorModule {\n\n static forRoot(httpCacheConfig: HttpCacheConfig): ModuleWithProviders<P3XHttpCacheInterceptorModule> {\n return {\n ngModule: P3XHttpCacheInterceptorModule,\n providers: [\n {\n provide: P3X_HTTP_CACHE_CONFIG,\n useValue: httpCacheConfig\n }\n ]\n };\n }\n\n}\n","/*\n * Public API Surface of angular-http-cache-interceptor\n */\n\nexport * from './lib/http-cache-interceptor.module';\nexport * from './lib/caching-headers.enum'\nexport * from './lib/http-cache-config'\nexport * from './lib/http-cache-config.token'\nexport * from './lib/caching-store.enum'\n","/**\n * Generated bundle index.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