Skip to content

Commit

Permalink
feat: ✅ handle plugin-cache request condition
Browse files Browse the repository at this point in the history
Forward request condition to the plugin-cache to allow user configuration. By default cache only GET requests.
  • Loading branch information
edbzn committed Nov 21, 2019
1 parent 2cd22b8 commit 9e1bff6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
2 changes: 1 addition & 1 deletion libs/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { HttpExtPlugin, HandlerArgs } from './lib/plugin';
export { HttpExtPlugin, HandlerArgs, RequestCondition } from './lib/plugin';
export { matchOrigin, matchMethod } from './lib/matchers';
export { HttpExt, RequestHandlerFn } from './lib/http-ext';
export { HttpExtRequest, createRequest, HttpMethod } from './lib/request';
Expand Down
24 changes: 24 additions & 0 deletions libs/plugin-cache/src/lib/plugin-cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,30 @@ describe('CachePlugin', () => {
expect((cachePlugin as any)._storeAdapter).toBeInstanceOf(MemoryAdapter);
});

it('should use given request condition', () => {
const spyCondition = jest.fn().mockReturnValue(true);
const cachePlugin = createCachePlugin({ condition: spyCondition });

cachePlugin.condition({ request });

expect(spyCondition).toHaveBeenCalledWith({ request });
});

it('should cache only GET requests by default', () => {
const cachePlugin = createCachePlugin();
const getRequest = { ...request, method: 'GET' as any };
const postRequest = { ...request, method: 'POST' as any };
const putRequest = { ...request, method: 'PUT' as any };
const patchRequest = { ...request, method: 'PATCH' as any };
const deleteRequest = { ...request, method: 'DELETE' as any };

expect(cachePlugin.condition({ request: getRequest })).toBe(true);
expect(cachePlugin.condition({ request: postRequest })).toBe(false);
expect(cachePlugin.condition({ request: putRequest })).toBe(false);
expect(cachePlugin.condition({ request: patchRequest })).toBe(false);
expect(cachePlugin.condition({ request: deleteRequest })).toBe(false);
});

it('should use given `StoreAdapter` implementation to store cache', () => {
const spyAdapter = { set: jest.fn() };
const cachePlugin = createCachePlugin({
Expand Down
29 changes: 19 additions & 10 deletions libs/plugin-cache/src/lib/plugin-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,49 @@ import {
HandlerArgs,
HttpExtPlugin,
HttpExtRequest,
HttpExtResponse
HttpExtResponse,
RequestCondition
} from '@http-ext/core';
import { defer, EMPTY, merge, Observable, of } from 'rxjs';
import { shareReplay, takeUntil, tap, map } from 'rxjs/operators';
import { map, shareReplay, takeUntil, tap } from 'rxjs/operators';

import { applyMetadata } from './apply-metadata';
import {
HttpExtCacheResponse,
PartialCacheMetadata,
ResponseAndCacheMetadata
} from './metadata';
import { HttpExtCacheResponse, ResponseAndCacheMetadata } from './metadata';
import { MemoryAdapter } from './store-adapters/memory-adapter';
import { StoreAdapter } from './store-adapters/store-adapter';
import { toString } from './to-string';

export interface CachePluginOptions {
addCacheMetadata: boolean;
storeAdapter: StoreAdapter;
condition: RequestCondition;
}

export function cachePlugin({
addCacheMetadata = false,
storeAdapter = new MemoryAdapter()
storeAdapter = new MemoryAdapter(),
condition = ({ request }) => request.method === 'GET'
}: Partial<CachePluginOptions> = {}): HttpExtPlugin {
return new CachePlugin({ addCacheMetadata, storeAdapter });
return new CachePlugin({ addCacheMetadata, storeAdapter, condition });
}

export class CachePlugin implements HttpExtPlugin {
private _addCacheMetadata: boolean;
private _storeAdapter: StoreAdapter;
private _condition: RequestCondition;

constructor({ addCacheMetadata, storeAdapter }: CachePluginOptions) {
constructor({
addCacheMetadata,
storeAdapter,
condition
}: CachePluginOptions) {
this._storeAdapter = storeAdapter;
this._addCacheMetadata = addCacheMetadata;
this._condition = condition;
}

condition({ request }: { request: HttpExtRequest }) {
return this._condition({ request });
}

handle({
Expand Down

0 comments on commit 9e1bff6

Please sign in to comment.