From f2571c5c8d35ca2cb37c73b5146b68b27e793edd Mon Sep 17 00:00:00 2001 From: Marvin Heilemann <11534760+muuvmuuv@users.noreply.github.com> Date: Mon, 1 Jan 2024 16:30:19 +0100 Subject: [PATCH 1/4] fix(local-storage): set http headers correctly --- .../lib/local-storage/local-storage-cache.ts | 37 ++++++++++++++----- projects/ngneat/cashew/src/lib/specs/mocks.ts | 6 ++- 2 files changed, 32 insertions(+), 11 deletions(-) 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..95df4a9 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,52 @@ 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); + 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: {} }; + if (response.headers instanceof HttpHeaders) { + response.headers.keys().forEach(headerKey => { + httpResponse.headers[headerKey] = response.headers.get(headerKey); + }); + } + + storage.setItem(generatedKey, response); - 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/mocks.ts b/projects/ngneat/cashew/src/lib/specs/mocks.ts index 811a415..5c3d7b4 100644 --- a/projects/ngneat/cashew/src/lib/specs/mocks.ts +++ b/projects/ngneat/cashew/src/lib/specs/mocks.ts @@ -16,7 +16,11 @@ 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 = () => { + const response = new HttpResponse(); + response.headers.set('test', 'test'); + return response; +}; export const requestQueue = () => new RequestsQueue(); export const cacheBucket = () => new CacheBucket(); export const httpCacheStorage = () => new DefaultHttpCacheStorage(); From fbc584026291c3cdacba2abbaf0617123f3fa4b4 Mon Sep 17 00:00:00 2001 From: Marvin Heilemann <11534760+muuvmuuv@users.noreply.github.com> Date: Mon, 1 Jan 2024 16:48:14 +0100 Subject: [PATCH 2/4] fix(test): headers not correctly set in mock and saved in map storage --- .../src/lib/local-storage/local-storage-cache.ts | 10 ++++------ projects/ngneat/cashew/src/lib/specs/mocks.ts | 11 +++++------ 2 files changed, 9 insertions(+), 12 deletions(-) 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 95df4a9..7083c26 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 @@ -41,13 +41,11 @@ export class HttpCacheLocalStorage extends HttpCacheStorage { const generatedKey = createKey(key); const httpResponse: any = { ...response, headers: {} }; - if (response.headers instanceof HttpHeaders) { - response.headers.keys().forEach(headerKey => { - httpResponse.headers[headerKey] = response.headers.get(headerKey); - }); - } + response.headers.keys().forEach(headerKey => { + httpResponse.headers[headerKey] = response.headers.get(headerKey); + }); - storage.setItem(generatedKey, response); + storage.setItem(generatedKey, httpResponse); return super.set(generatedKey, response); } diff --git a/projects/ngneat/cashew/src/lib/specs/mocks.ts b/projects/ngneat/cashew/src/lib/specs/mocks.ts index 5c3d7b4..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,11 +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 = () => { - const response = new HttpResponse(); - response.headers.set('test', 'test'); - return response; -}; +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(); From 34656f343b96057f9e9a9c8845cd0cf71a4be105 Mon Sep 17 00:00:00 2001 From: Marvin Heilemann <11534760+muuvmuuv@users.noreply.github.com> Date: Mon, 1 Jan 2024 17:27:38 +0100 Subject: [PATCH 3/4] fix: init must be called manually on headers when no getter is used --- .../ngneat/cashew/src/lib/local-storage/local-storage-cache.ts | 1 + 1 file changed, 1 insertion(+) 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 7083c26..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 @@ -30,6 +30,7 @@ export class HttpCacheLocalStorage extends HttpCacheStorage { if (value) { value.headers = new HttpHeaders(value.headers); + value.headers.init(); const response = new HttpResponse(value); super.set(generatedKey, response); } From 77ee031ce947489878ce689949d63dfa3839b640 Mon Sep 17 00:00:00 2001 From: Marvin Heilemann <11534760+muuvmuuv@users.noreply.github.com> Date: Tue, 2 Jan 2024 15:57:21 +0100 Subject: [PATCH 4/4] feat(test): add response headers tests --- projects/ngneat/cashew/src/lib/specs/local-storage.spec.ts | 7 +++++++ 1 file changed, 7 insertions(+) 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);