Skip to content

Commit

Permalink
Merge pull request #96 from muuvmuuv/bugfix/set-http-headers
Browse files Browse the repository at this point in the history
fix(local-storage): set http headers correctly
  • Loading branch information
NetanelBasal authored Jan 4, 2024
2 parents 4e41b67 + 77ee031 commit 8edf11f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
36 changes: 26 additions & 10 deletions projects/ngneat/cashew/src/lib/local-storage/local-storage-cache.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<any> | 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<any>) {
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() {
Expand Down
7 changes: 7 additions & 0 deletions projects/ngneat/cashew/src/lib/specs/local-storage.spec.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<any>;
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);
Expand Down
7 changes: 5 additions & 2 deletions projects/ngneat/cashew/src/lib/specs/mocks.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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();
Expand Down

0 comments on commit 8edf11f

Please sign in to comment.