ngneat / cashew

🐿 A flexible and straightforward library that caches HTTP requests in Angular
https://www.netbasal.com
MIT License
682 stars 33 forks source link

HttpHeaders are not handled #95

Closed muuvmuuv closed 9 months ago

muuvmuuv commented 9 months ago

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report  
[ ] Performance issue
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request
[ ] Other... Please describe:

Current behavior

I am not seeing HttpHeaders beeing saved or received in cache.

Expected behavior

HttpHeaders should be serialized as HttpHeaders otherwise a .get() wont longer work in interceptors.

Minimal reproduction of the problem with instructions

What is the motivation / use case for changing the behavior?

We have request and correlation ids set in backend in response headers that I put into the response body for better access in templates. This is done in an interceptor.

Environment


Angular version: 17


Browser:
- [ ] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX

For Tooling issues:
- Node version: 20  
- Platform:  Mac 

Others:

muuvmuuv commented 9 months ago

Tested, and it works by modifing HttpCacheLocalStorage.

    get(key) {
        const cacheValue = super.get(createKey$2(key));
        if (cacheValue) {
            return cacheValue;
        }
        const value = storage.getItem(createKey$2(key));
        if (value) {
                        const response = new HttpResponse(value)
                        response.headers = new HttpHeaders(value.headers);
            super.set(createKey$2(key), response);
        }
        return super.get(createKey$2(key));
    }
    set(key, response) {
            const httpResponse = {...response, headers: {} }
            if (response.headers instanceof HttpHeaders) {
                response.headers.keys().forEach(key => {
                    httpResponse.headers[key] = response.headers.get(key);
                });
            }
        storage.setItem(createKey$2(key), httpResponse);
        return super.set(createKey$2(key), httpResponse);
    }