http-repository-cache
Version:
A library for making HTTP requests using the repository pattern with an built in caching system.
253 lines (252 loc) • 11 kB
TypeScript
import HttpMethod from "../httpRequest/HttpMethod";
import HttpRequestAdapter, { HttpRequestParams } from "../httpRequest/HttpRequestAdapter";
export declare enum RequestType {
LIST = 0,
OCCURRENCE = 1
}
/**
* RepositoryCache class handle cache for http requests
*
* @template O type of request options
*/
export default class RepositoryCache<O = unknown> {
private httpRequest;
private idKey;
private eternalCache;
private cache;
/**
* Cache validity in seconds
*/
private _cacheValidity;
/**
*
* @param httpRequest HttpRequestAdapter instance to make http requests
* @param idKey The key to identify the occurrence in the response data
* @param eternalCache True if cache never expire, false otherwise
* @param cacheValidity Cache validity in seconds
*/
constructor(httpRequest: HttpRequestAdapter<O>, idKey: string, eternalCache?: boolean, cacheValidity?: number);
/**
* Sets the HTTP request adapter for the repository cache.
*
* @param httpRequest - The HTTP request adapter to be used.
*/
setHttpRequest(httpRequest: HttpRequestAdapter<O>): void;
private createRequestSignature;
private cacheResponse;
/**
* Clears the entire cache by resetting it to an empty object.
* This method should be used when you want to invalidate all cached data.
*/
clearCache(): void;
/**
* Clears all cached entries in the repository cache that have a request type of `LIST`.
* Iterates over the keys in the cache and deletes any entry where the request type is `LIST`.
*/
clearListsCache(): void;
/**
* Clears the occurrence cache for a specific ID.
*
* @param id - The ID of the occurrence to clear from the cache. Can be a string or a number.
* @param subPropertyResponseOccurrence - An optional array of string sub-properties response occurrences to consider when clearing the cache.
*/
clearOccurrenceCache(id: string | number, subPropertyResponseOccurrence?: string[]): void;
/**
* If response has sub properties, get the occurrence from nested property that has data occurrence in the response
* @param response
* @param subPropertyResponseOccurrence
* @returns
*/
private getOccurrenceFromResponse;
/**
* If response has sub properties, get the list from nested property that has data list in the response
* @param response
* @param subPropertyResponseList
* @returns
*/
private getListFromResponse;
/**
* Find an item in cache according to key and key value
*
* @template T Response type
* @param key
* @param value
* @param subPropertyResponseList an array of strings to get from the request list response body the nested sub property where the list is
* @param subPropertyResponseOccurrence an array of strings to get from the request occurrence response body the nested sub property where the occurrence is
* @returns
*/
private findInCache;
/**
* Check if cache is expired according to last fetch time
* cache is expired if last fetch is older than cache validity
* return true if cache is expired, false otherwise
*
* @param cache
* @returns
*/
private isCacheExpired;
/**
* Get request response from cache if it exist
*
* @param requestMethod
* @param httpRequestParams
* @returns
*/
private getRequestCache;
/**
* Removes all expired cache entries from the cache storage.
* If `eternalCache` is set to true, this method does nothing.
* Otherwise, it iterates through all cache entries and deletes
* those that are expired.
*
* @private
*/
private removeAllExpiredCache;
/**
* Getting request from cache if available. If not, request parameter are called to make the request
* it removes all expired cache before checking if cache is available
*
* @template R Response type
* @template B request body type
* @param httpRequest
* @param method
* @param httpRequestParams
* @returns
*/
private requestWithCache;
/**
* Do the Http request according to method and httpRequestParams
*
* @param method
* @param httpRequestParams
* @returns
*/
doHttpRequest<R, B = void>(method: HttpMethod, httpRequestParams: HttpRequestParams<B, O>): Promise<R>;
private doCachedHttpRequest;
/**
* Get list from cache according httpRequestParams parameter to identify specific request cache.
* If cache is not found or expired, request will be made and cache will be updated with the response of this request.
*
* @template R Response type
* @template B request body type
* @param method
* @param httpRequestParams
* @returns
*/
getList<R, B = void>(method: HttpMethod, httpRequestParams: HttpRequestParams<B, O>): Promise<R>;
/**
* Get item from cache according httpRequestParams parameter to identify specific request cache.
* If cache is not found or expired, request will be made and cache will be updated with the response of this request.
*
* @template R Response type
* @template B request body type
* @param method
* @param httpRequestParams
* @returns
*/
get<R, B = void>(method: HttpMethod, httpRequestParams: HttpRequestParams<B, O>): Promise<R>;
/**
* Find an item by key from cache
*
* @template T Response type
* @param key
* @param value
* @param method
* @param httpRequestParams
* @param subPropertyResponseList an array of strings to get from the body of list type request response the nested sub property where the list is
* @param subPropertyResponseOccurrence an array of strings to get from the body of occurrence type request response the nested sub property where the occurrence is
* @returns
*/
findByKey<T>(key: string, value: string | number, subPropertyResponseList?: string[], subPropertyResponseOccurrence?: string[]): Promise<T>;
/**
* Find an item by key from cache or request to get all and cache it
*
* @template R Response type
* @template B request body type
* @param key
* @param value
* @param method
* @param httpRequestParams
* @param subPropertyResponseList an array of strings to get from the body of list type request response the nested sub property where the list is
* @param subPropertyResponseOccurrence an array of strings to get from the body of occurrence type request response the nested sub property where the occurrence is
* @returns
*/
findByKeyOrRequestGetList<R, B = void>(key: string, value: string | number, method: HttpMethod, httpRequestParams: HttpRequestParams<B, O>, subPropertyResponseList?: string[], subPropertyResponseOccurrence?: string[]): Promise<R>;
/**
* Find an item by key from cache or request and cache it
*
* @template R Response type
* @template B request body type
* @param key
* @param value
* @param method
* @param httpRequestParams
* @param subPropertyResponseList an array of strings to get from the body of list type request response the nested sub property where the list is
* @param subPropertyResponseOccurrence an array of strings to get from the body of occurrence type request response the nested sub property where the occurrence is
* @returns
*/
findByKeyOrRequestGet<R, B = void>(key: string, value: string | number, method: HttpMethod, httpRequestParams: HttpRequestParams<B, O>, subPropertyResponseList?: string[], subPropertyResponseOccurrence?: string[]): Promise<R>;
/**
* Find an item by id key from cache
*
* @template T Response type
* @param key
* @param value
* @param method
* @param httpRequestParams
* @param subPropertyResponseList an array of strings to get from the body of list type request response the nested sub property where the list is
* @param subPropertyResponseOccurrence an array of strings to get from the body of occurrence type request response the nested sub property where the occurrence is
* @returns
*/
find<T>(id: string | number, subPropertyResponseList?: string[], subPropertyResponseOccurrence?: string[]): Promise<T>;
/**
* Find an item by id key from cache or request to get all and cache it
*
* @template R Response type
* @template B request body type
* @param key
* @param value
* @param method
* @param httpRequestParams
* @param subPropertyResponseList an array of strings to get from the body of list type request response the nested sub property where the list is
* @param subPropertyResponseOccurrence an array of strings to get from the body of occurrence type request response the nested sub property where the occurrence is
* @returns
*/
findOrRequestGetList<R, B = void>(id: string | number, method: HttpMethod, httpRequestParams: HttpRequestParams<B, O>, subPropertyResponseList?: string[], subPropertyResponseOccurrence?: string[]): Promise<R>;
/**
* Find an item by id key from cache or request and cache it
*
* @template R Response type
* @template B request body type
* @param key
* @param value
* @param method
* @param httpRequestParams
* @param subPropertyResponseList an array of strings to get from the body of list type request response the nested sub property where the list is
* @param subPropertyResponseOccurrence an array of strings to get from the body of occurrence type request response the nested sub property where the occurrence is
* @returns
*/
findOrRequestGet<R, B = void>(id: string | number, method: HttpMethod, httpRequestParams: HttpRequestParams<B, O>, subPropertyResponseList?: string[], subPropertyResponseOccurrence?: string[]): Promise<R>;
/**
* Request to create an item and clear lists cache
*
* @template R Response type
* @template B request body type
* @param method
* @param httpRequestParams
* @returns
*/
create<R, B = void>(method: HttpMethod, httpRequestParams: HttpRequestParams<B, O>): Promise<R>;
/**
* Request to update an item and clear lists cache and specific occurrence cache
*
* @template R Response type
* @template B request body type
* @param id
* @param method
* @param httpRequestParams
* @param subPropertyResponseOccurrence an array of strings to get from the body of occurrence type request response the nested sub property where the occurrence is
* @returns
*/
update<R, B = void>(id: string | number, method: HttpMethod, httpRequestParams: HttpRequestParams<B, O>, subPropertyResponseOccurrence?: string[]): Promise<R>;
}