Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(local-storage): set http headers correctly #96

Merged
merged 4 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: 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
Loading