Skip to content

Commit

Permalink
fix: 🐛 fix default ttl
Browse files Browse the repository at this point in the history
  • Loading branch information
NetanelBasal committed Jun 16, 2021
1 parent 2375253 commit 991f078
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 4 additions & 2 deletions projects/ngneat/cashew/src/lib/cache-interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Inject, Injectable, PLATFORM_ID } from '@angular/core';
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { finalize, share, tap } from 'rxjs/operators';
import { HTTP_CACHE_CONFIG, HttpCacheConfig } from './cache-config';

import { HttpCacheManager } from './cache-manager.service';
import { KeySerializer } from './key-serializer';
Expand All @@ -21,7 +22,8 @@ export class HttpCacheInterceptor implements HttpInterceptor {
constructor(
private httpCacheManager: HttpCacheManager,
private keySerializer: KeySerializer,
@Inject(PLATFORM_ID) private platformId: Object
@Inject(PLATFORM_ID) private platformId: Object,
@Inject(HTTP_CACHE_CONFIG) private config: HttpCacheConfig
) {}

intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
Expand Down Expand Up @@ -94,7 +96,7 @@ export class HttpCacheInterceptor implements HttpInterceptor {
tap(event => {
if (event instanceof HttpResponse) {
const cache = this.httpCacheManager._resolveResponse(event);
this.httpCacheManager._set(key, cache, +ttl!);
this.httpCacheManager._set(key, cache, ttl || this.config.ttl);
}
}),
finalize(() => queue.delete(key)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('HttpCacheInterceptor', () => {

beforeEach(() => {
handler = httpHandler();
httpCacheInterceptor = new HttpCacheInterceptor(httpCacheManager(), keySerializer(), config);
httpCacheInterceptor = new HttpCacheInterceptor(httpCacheManager(), keySerializer(), {}, config);
expect.hasAssertions();
});

Expand All @@ -55,7 +55,7 @@ describe('HttpCacheInterceptor', () => {
it('should cache a return a serialized request when passing a serializer', () => {
const responseSerializer = jest.fn(v => 'serialized');
const cacheManager: any = httpCacheManager({ ...config, responseSerializer });
httpCacheInterceptor = new HttpCacheInterceptor(cacheManager, keySerializer(), config);
httpCacheInterceptor = new HttpCacheInterceptor(cacheManager, keySerializer(), {}, config);
call(request({ cache: true }));
expect(handler.handle).toHaveBeenCalledTimes(1);
/* The serializer is called when adding the value to the cache and when retrieving it */
Expand All @@ -67,6 +67,7 @@ describe('HttpCacheInterceptor', () => {
httpCacheInterceptor = new HttpCacheInterceptor(
httpCacheManager({ ...config, strategy: 'implicit' }),
keySerializer(),
{},
config
);
call(request({}));
Expand All @@ -77,6 +78,7 @@ describe('HttpCacheInterceptor', () => {
httpCacheInterceptor = new HttpCacheInterceptor(
httpCacheManager({ ...config, strategy: 'implicit' }),
keySerializer(),
{},
config
);
call(request({ cache: false }));
Expand Down

0 comments on commit 991f078

Please sign in to comment.