UNPKG

browser-cache-mock

Version:

a mock for the browser cache that can be used in a node environment

59 lines (58 loc) 3.28 kB
/** An in-memory implementation of the browser cache that can be used to test caching strategies in a node environment. */ declare class CacheMock { cache: Map<Request, Response>; constructor(); private get; private getAll; private set; /** * Takes a URL, retrieves it and adds the resulting response object to the given cache. This is functionally equivalent to calling fetch(), then using put() to add the results to the cache. * @param {RequestInfo} request The request to add to the Cache * @returns {Promise<void>} */ add(request: RequestInfo): Promise<void>; /** * Takes an array of URLs, retrieves them, and adds the resulting response objects to the given cache. * @param {Array<RequestInfo>} requests An array of requests to add to the cache * @returns {Promise<void>} */ addAll(requests: RequestInfo[]): Promise<void>; /** * Finds the Cache entry whose key is the request, returning a Promise that resolves to true if a matching Cache entry is found and deleted. If no Cache entry is found, the promise resolves to false. * @param {RequestInfo} request The request to search for * @param {CacheQueryOptions} options Additional options to be condsidered when searching * @returns {Promise<boolean>} A boolean value indicating whether the a key was deleted from the cache */ delete(request: RequestInfo, options?: CacheQueryOptions): Promise<boolean>; private getRequestUrl; /** * Returns a Promise that resolves to an array of Cache keys. * @param {RequestInfo} request The request to search for * @param {CacheQueryOptions} options Additional options to be condsidered when searching * @returns {Promise<ReadonlyArray<Request>>} A readonly array of matching cache keys */ keys(request?: RequestInfo, options?: CacheQueryOptions): Promise<ReadonlyArray<Request>>; /** * Returns a Promise that resolves to the response associated with the first matching request in the Cache object. * @param {RequestInfo} request The request to search for * @param {CacheQueryOptions} options Additional options to be condsidered when searching * @returns {Promise<Response | undefined>} The match found in the cache, if anything has been found */ match(request: RequestInfo, options?: CacheQueryOptions): Promise<Response | undefined>; private validateRequest; /** * Returns a Promise that resolves to an array of all matching requests in the Cache object. * @param {RequestInfo} request The request key to match against * @param {CacheQueryOptions} options Additional options to be condsidered when searching * @returns {Promise<ReadonlyArray<Response>>} A readonly array of matching cache responses */ matchAll(request?: RequestInfo, options?: CacheQueryOptions): Promise<ReadonlyArray<Response>>; /** * Takes both a request and its response and adds it to the given cache. * @param {RequestInfo} request The request key to add to the cache * @param {Response} response The response value to add to the cache key * @returns {Promise<void>} */ put(request: RequestInfo, response: Response): Promise<void>; } export default CacheMock;