Skip to content

Commit

Permalink
fix: 🐛 method for View Engine users to pass partial config
Browse files Browse the repository at this point in the history
cashewConfig() enables View Engine users to pass partial config options
in the module providers
  • Loading branch information
theblushingcrow committed Aug 20, 2020
1 parent 3300fee commit 7f7a934
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,14 @@ To your `AppModule` providers list. Note that `ttl` will also be calculated via

## Config Options

Using the library, you might need to change the default behavior of the caching mechanism. You could do that by passing a configuration object to the static `forRoot` method of the `HttpCacheInterceptorModule` module.
Using the library, you might need to change the default behavior of the caching mechanism. You could do that by passing a configuration (a partial `HttpCacheConfig` object) to the static `forRoot` method of the `HttpCacheInterceptorModule` module.


**Important note:** View Engine users - instead of adding the config to the `forRoot()` method, add it in the app module providers in the following manner, using the supplied `cashewConfig()` method:

```ts
{ provide: HTTP_CACHE_CONFIG, useValue: cashewConfig(config) }
```

Let's go over each of the configuration options:

Expand Down
10 changes: 10 additions & 0 deletions projects/ngneat/cashew/src/lib/httpCacheConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,14 @@ export function withCache(params: Params = {}): { params: Params } {
};
}

export function cashewConfig(config: Partial<HttpCacheConfig> = defaultConfig): HttpCacheConfig {
return {
strategy: config.strategy || defaultConfig.strategy,
ttl: config.ttl || defaultConfig.ttl,
localStorageKey: config.localStorageKey || defaultConfig.localStorageKey,
responseSerializer: config.responseSerializer,
parameterCodec: config.parameterCodec
};
}

export const HTTP_CACHE_CONFIG = new InjectionToken<HttpCacheConfig>('HTTP_CACHE_CONFIG');
31 changes: 31 additions & 0 deletions projects/ngneat/cashew/src/lib/test/cashewConfig.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { cashewConfig, defaultConfig } from '../httpCacheConfig';

describe('cashewConfig', () => {
const responseSerializer = v => '';

it('should return the default config when called with an empty object', () => {
expect(cashewConfig({})).toEqual(defaultConfig);
});

it('should override the default config values when called with values in the config parameter', () => {
const configWithStrategy = cashewConfig({ strategy: 'implicit' });
expect(configWithStrategy.ttl).toEqual(defaultConfig.ttl);
expect(configWithStrategy.localStorageKey).toEqual(defaultConfig.localStorageKey);
expect(configWithStrategy.strategy).toEqual('implicit');

const configWithTtl = cashewConfig({ ttl: 10000 });
expect(configWithTtl.localStorageKey).toEqual(defaultConfig.localStorageKey);
expect(configWithTtl.strategy).toEqual(defaultConfig.strategy);
expect(configWithTtl.ttl).toEqual(10000);

const configWithLocalStorageKey = cashewConfig({ localStorageKey: 'x' });
expect(configWithLocalStorageKey.strategy).toEqual(defaultConfig.strategy);
expect(configWithLocalStorageKey.ttl).toEqual(defaultConfig.ttl);
expect(configWithLocalStorageKey.localStorageKey).toEqual('x');
});

it('should retain additional parameters passed in the config, such as responseSerializer', () => {
const configWithResponseSerializer = cashewConfig({ responseSerializer });
expect(configWithResponseSerializer.responseSerializer({ a: 'a' })).toEqual('');
});
});

0 comments on commit 7f7a934

Please sign in to comment.