diff --git a/projects/ngneat/cashew/src/lib/local-storage/local-storage-cache.ts b/projects/ngneat/cashew/src/lib/local-storage/local-storage-cache.ts index 3bb8bce..9bbd61f 100644 --- a/projects/ngneat/cashew/src/lib/local-storage/local-storage-cache.ts +++ b/projects/ngneat/cashew/src/lib/local-storage/local-storage-cache.ts @@ -1,4 +1,4 @@ -import { HttpResponse } from '@angular/common/http'; +import { HttpResponse, HttpHeaders } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { HttpCacheStorage } from '../cache-storage'; import { storage } from './local-storage'; @@ -12,35 +12,51 @@ function createKey(key: string) { @Injectable() export class HttpCacheLocalStorage extends HttpCacheStorage { has(key: string): boolean { - return super.has(createKey(key)) || !!storage.getItem(createKey(key)); + const generatedKey = createKey(key); + + return super.has(generatedKey) || !!storage.getItem(generatedKey); } get(key: string): HttpResponse | boolean { - const cacheValue = super.get(createKey(key)); + const generatedKey = createKey(key); + + const cacheValue = super.get(generatedKey); if (cacheValue) { return cacheValue; } - const value = storage.getItem(createKey(key)); + const value = storage.getItem(generatedKey); if (value) { - super.set(createKey(key), new HttpResponse(value)); + value.headers = new HttpHeaders(value.headers); + value.headers.init(); + const response = new HttpResponse(value); + super.set(generatedKey, response); } - return super.get(createKey(key))!; + return super.get(generatedKey)!; } set(key: string, response: HttpResponse) { - storage.setItem(createKey(key), response); + const generatedKey = createKey(key); + + const httpResponse: any = { ...response, headers: {} }; + response.headers.keys().forEach(headerKey => { + httpResponse.headers[headerKey] = response.headers.get(headerKey); + }); + + storage.setItem(generatedKey, httpResponse); - return super.set(createKey(key), response); + return super.set(generatedKey, response); } delete(key: string) { - storage.clearItem(createKey(key)); + const generatedKey = createKey(key); + + storage.clearItem(generatedKey); - return super.delete(createKey(key)); + return super.delete(generatedKey); } clear() { diff --git a/projects/ngneat/cashew/src/lib/specs/local-storage.spec.ts b/projects/ngneat/cashew/src/lib/specs/local-storage.spec.ts index 871759b..3e1b568 100644 --- a/projects/ngneat/cashew/src/lib/specs/local-storage.spec.ts +++ b/projects/ngneat/cashew/src/lib/specs/local-storage.spec.ts @@ -1,5 +1,6 @@ import { httpResponse, localStorageMock } from './mocks'; import { HttpCacheLocalStorage } from '../local-storage/local-storage-cache'; +import { HttpHeaders, HttpResponse } from '@angular/common/http'; describe('httpCacheLocalStorage', () => { let storage: HttpCacheLocalStorage; @@ -28,6 +29,12 @@ describe('httpCacheLocalStorage', () => { expect(storage.get(existingKey)).toBe(response); }); + it('should get the cached response headers', () => { + const { headers } = storage.get(existingKey) as HttpResponse; + expect(headers).toBeInstanceOf(HttpHeaders); + expect(headers).toBe(response.headers); + }); + it('should not access local storage when key is found in memory', () => { jest.spyOn(localStorage, 'setItem'); const value = storage.get(existingKey); diff --git a/projects/ngneat/cashew/src/lib/specs/mocks.ts b/projects/ngneat/cashew/src/lib/specs/mocks.ts index 811a415..751300b 100644 --- a/projects/ngneat/cashew/src/lib/specs/mocks.ts +++ b/projects/ngneat/cashew/src/lib/specs/mocks.ts @@ -1,4 +1,4 @@ -import { HttpRequest, HttpResponse } from '@angular/common/http'; +import { HttpHeaders, HttpRequest, HttpResponse } from '@angular/common/http'; import { CacheBucket } from '../cache-bucket'; import { defaultConfig } from '../cache-config'; import { DefaultHttpCacheGuard } from '../cache-guard'; @@ -16,7 +16,10 @@ export const ttl = config.ttl; export const httpRequest = (method: string = 'GET', options: any = {}, url: string = 'api/mock') => new HttpRequest(method, url, {}, options); -export const httpResponse = () => new HttpResponse(); +export const httpResponse = () => + new HttpResponse({ + headers: new HttpHeaders().set('test', 'test') + }); export const requestQueue = () => new RequestsQueue(); export const cacheBucket = () => new CacheBucket(); export const httpCacheStorage = () => new DefaultHttpCacheStorage();