-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(request): CHP-6060 adds optional cache functionality
- Loading branch information
Showing
7 changed files
with
205 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { DefaultCache } from './cache'; | ||
import { getResponse } from './responses.mock'; | ||
|
||
describe('DefaultCache', () => { | ||
let cache: DefaultCache; | ||
|
||
beforeEach(() => { | ||
cache = new DefaultCache(); | ||
}); | ||
|
||
it('returns null when a cache key does not exist', () => { | ||
expect(cache.read('https://example.com', {})).toBe(null); | ||
}); | ||
|
||
it('returns a stored response', () => { | ||
const response = getResponse('Test Body'); | ||
const url = 'https://example.com'; | ||
|
||
cache.write(url, {}, response); | ||
|
||
expect(cache.read(url, {})).toBe(response); | ||
}); | ||
|
||
it('reads a specific stored response', () => { | ||
const response = getResponse('Test Body'); | ||
const url = 'https://example.com'; | ||
|
||
cache.write('/test/1', {}, getResponse('Test Body 1')); | ||
cache.write(url, {}, response); | ||
cache.write('/test/2', {}, getResponse('Test Body 2')); | ||
|
||
expect(cache.read(url, {})).toBe(response); | ||
}); | ||
|
||
it('stores cache in different keys when giving the same url with different params', () => { | ||
const url = 'https://example.com'; | ||
|
||
const firstTestResponse = getResponse('Test Body 1'); | ||
const firstRequestOptions = { params: { testParam: 'first' } }; | ||
|
||
const secondTestResponse = getResponse('Test Body 2'); | ||
const secondRequestOptions = { params: { testParam: 'second' } }; | ||
|
||
cache.write(url, firstRequestOptions, firstTestResponse); | ||
cache.write(url, secondRequestOptions, secondTestResponse); | ||
|
||
const firstCachedResponse = cache.read(url, firstRequestOptions); | ||
const secondCachedResponse = cache.read(url, secondRequestOptions); | ||
|
||
expect(firstCachedResponse).toBe(firstTestResponse); | ||
expect(secondCachedResponse).toBe(secondTestResponse); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import * as queryString from 'query-string'; | ||
|
||
import RequestOptions from './request-options'; | ||
import Response from './response'; | ||
|
||
export default interface Cache { | ||
read<T>(url: string, options: RequestOptions): Response<T> | null; | ||
write<T>(url: string, options: RequestOptions, response: Response<T>): void; | ||
} | ||
|
||
interface CacheMap { | ||
[key: string]: Response<any>; | ||
} | ||
|
||
export class DefaultCache implements Cache { | ||
private readonly _cache: CacheMap = {}; | ||
|
||
read<T>(url: string, options: RequestOptions): Response<T> | null { | ||
const cacheKey = this.getKey(url, options.params); | ||
|
||
return this._cache[cacheKey] || null; | ||
} | ||
|
||
write<T>(url: string, options: RequestOptions, response: Response<T>) { | ||
const cacheKey = this.getKey(url, options.params); | ||
|
||
this._cache[cacheKey] = response; | ||
} | ||
|
||
private getKey(url: string, params: object = {}) { | ||
if (Object.keys(params).length === 0) { | ||
return url; | ||
} | ||
|
||
return `${url}?${queryString.stringify(params)}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
import Cache from './cache'; | ||
|
||
export default interface RequestSenderOptions { | ||
cache?: Cache; | ||
host?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters