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

Allow override of cache storage provider #74

Closed muuvmuuv closed 2 years ago

muuvmuuv commented 2 years ago

I'm submitting a...


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

Current behavior

I am not able to change the storage provider from localStorage to something else

Expected behavior

A exposed HttpCacheStorage provider to modify it. We for example use CapacitorStorage.

Minimal reproduction of the problem with instructions

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

Environment


Angular version: 14


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: 17  
- Platform:  Mac 

Others:

NetanelBasal commented 2 years ago

https://github.com/ngneat/cashew#hack-the-library

muuvmuuv commented 2 years ago

Oh.. yes my bad. I was aware of that the time I installed cashew a while back and just forgot to look at it again. Sorry!

Unfortunately, I am stuck with overriding the http cache class provider. None of the internal classes are exported so my TypeScript compiler is reporting errors when importing them.

./src/app/core/http/http.module.ts:3:0-68 - Error: Module not found: Error: Package path ./lib/cache-storage is not exported from package @ngneat/cashew
@Injectable()
export class HttpCache /* implements HttpCacheLocalStorage */ {
  async has(key: string) {
    return !!(await Storage.get({ key: createKey(key) }))
  }

  async get(key: string) {
    return await Storage.get({ key: createKey(key) })
  }

  async set(key: string, response: HttpResponse<unknown>) {
    await Storage.set({
      key: createKey(key),
      value: JSON.stringify(response),
    })
  }

  async delete(key: string) {
    await Storage.remove({ key: createKey(key) })
  }

  clear() {
    // WIP
  }
}
import { HttpCacheStorage } from '@ngneat/cashew/lib/cache-storage'
// module provider...
    useHttpCacheLocalStorage,
    {
      provide: HttpCacheStorage,
      useClass: HttpCache,
    },
NetanelBasal commented 2 years ago

Can you open a PR and export it, please?

muuvmuuv commented 2 years ago

Working on it but don't have much free time ATM because I am still moving out. I am still testing the async stuff because my Provider is async.