import { AxiosError, AxiosPromise, AxiosRequestConfig, AxiosResponse } from "axios"; import Dexie from "dexie"; import { AjaxRequest, AjaxRequestExecute, AjaxRequestInternal, AjaxRequestWithCache, CacheDataWithId, CachedData, CachedType, DataDualResponse, DataResponse, DataSource, FetchType, LogError, LogInfo, OnGoingAjaxRequest, PerformanceRequestInsight, ObjectRequestWithCache } from "./model"; export declare class DataAccessIndexDbDatabase extends Dexie { data: Dexie.Table, string>; constructor(databaseName: string); dropTable(): Promise; } export interface DataAccessSingletonOptions { isCacheEnabled: boolean; isCacheMandatoryIfEnabled: boolean; defaultLifeSpanInSeconds: number; logError: (error: LogError) => void; logInfo: (info: LogInfo) => void; alterObjectBeforeHashing?: (obj: T) => any; onBackgroundAjaxFetchFailure: (response: AxiosResponse | AxiosError) => void; onBeforeAjaxRequest: (request: AxiosRequestConfig) => void; onAfterAjaxRequest: (request: AxiosRequestConfig) => void; } /** * The role of this interface is to limit what is public. This allow to have almost every * functions public in the concrete class which ease the unitestability of the code and * preserve a define set of available feature through the singleton with the interface. */ export interface IDataAccessSingleton { setConfiguration(options?: Partial): void; fetchFresh(request: AjaxRequestWithCache): Promise>; fetchFast(request: AjaxRequestWithCache): Promise>; fetchWeb(request: AjaxRequestWithCache): Promise>; fetchFastAndFresh(request: AjaxRequestWithCache): Promise>; fetchFastAndFreshObject(request: ObjectRequestWithCache): Promise>; deleteDataFromCache(request: AjaxRequest, options?: DeleteCacheOptions): Promise; deleteAllDataFromAllCache(): Promise; deletePersistentStorage(name: string): Promise; forceDeleteAndFetch(request: AjaxRequestWithCache, options?: DeleteCacheOptions): Promise>; execute(request: AjaxRequestExecute): Promise>; } export interface DeleteCacheOptions { memory?: boolean; persistent?: boolean; } /** * The Data Access is a singleton because we want to keep all the application data into * a single source avoiding duplication of cache. */ export declare class DataAccessSingleton implements IDataAccessSingleton { private static instance; generateSignature: boolean; DefaultOptions: Readonly; options: DataAccessSingletonOptions; onGoingAjaxRequest: Map; performanceInsights: Map; cachedResponse: Map; openIndexDb?: DataAccessIndexDbDatabase; constructor(databaseName: string); onListenMessage(event: MessageEvent): void; static getInstance(databaseName: string): IDataAccessSingleton; logInfo(info: Pick>): void; logError(error: Pick>): void; setConfiguration(options?: Partial): void; fetchWeb(request: AjaxRequestWithCache): Promise>; /** * Go in the memory cache first, then the persisted cache. In all level of cache, if the data is outdated it will fetch and * wait the response to cache it and return it. It means that each time the data is obsolete that the fetch takes time but * subsequent request will be faster. This function focus on accuracy first. */ fetchFresh(request: AjaxRequestWithCache): Promise>; /** * Fetch fast always returns the data from the cache if available. It returns data that can be obsolete, older than the lifetime * specified in the configuration. The lifespan specified is only to indicate when the data must be refreshed which mean that * an obsolete value is returned but the system will do the Ajax call to get it for the NEXT invocation. It is important to * understand that the fetch fast principle is that it's better to return a stale value than nothing BUT will respect the lifespan * to fetch the new value. Fetch fast works better if most of the data (if not all) is stored with a persistence */ fetchFast(request: AjaxRequestWithCache): Promise>; fetchFastAndFreshObject(request: ObjectRequestWithCache): Promise>; fetchFastAndFresh(request: AjaxRequestWithCache): Promise>; isPromise(o: Promise> | DataResponse): o is Promise>; fetchObjectAndSaveInCacheIfExpired(requestInternal: AjaxRequestInternal, request: ObjectRequestWithCache, source: DataSource, cacheEntry?: CachedData | undefined): Promise> | DataResponse; /** * fetchAndSaveInCacheIfExpired verifies if the data in the cache entry is expired. If it is, it performs * an Ajax call to get a fresh version and saves it in the cache. If the data is not expired, it returns * a resolved promise with the cache entry. Ideally, it should return the cacheEntry without wrapping it * in a Promise but a function returning a Promise | Entity is disallowed with async/await. * * This cause an issue with the fetchFastAndFresh because it returns the promise of this function in the result * (in the webPromise) which might NOT be a web promise but a cache promise. It forces the user to have to * consult the DataSource to ensure that the Promise returned is from the web. */ fetchAndSaveInCacheIfExpired(requestInternal: AjaxRequestInternal, source: DataSource, cacheEntry?: CachedData | undefined): Promise> | DataResponse; generateId(request: AjaxRequestWithCache): string; setDefaultRequestValues(request: AjaxRequest, fetchType?: FetchType): AjaxRequestInternal; setDefaultCache(requestInternal: AjaxRequestInternal): void; setDefaultFastCache(requestInternal: AjaxRequestInternal): void; saveCache(requestInternal: AjaxRequestInternal, responseFromCacheOrAjax: DataResponse): Promise>; tryMemoryCacheFetching(requestInternal: AjaxRequestInternal): CachedData | undefined; tryPersistentStorageFetching(requestInternal: AjaxRequestInternal): Promise | undefined>; ajax(request: AxiosRequestConfig): AxiosPromise; fetchWithAjax(requestInternal: AjaxRequestInternal): AxiosPromise; getActualTimeTick(): number; getPerformanceInsight(requestId: string): PerformanceRequestInsight; startPerformanceInsight(insight: PerformanceRequestInsight, source?: DataSource): PerformanceRequestInsight; startPerformanceInsight(requestId: string, source?: DataSource): PerformanceRequestInsight; stopPerformanceInsight(insight: PerformanceRequestInsight, source?: DataSource): PerformanceRequestInsight; stopPerformanceInsight(requestId: string, source?: DataSource): PerformanceRequestInsight; exhaustiveCheck(source: never): never; deletePerformanceInsight(id: string): void; setDataSize(insight: PerformanceRequestInsight, data: T): PerformanceRequestInsight; deleteFromMemoryCache(requestInternal: AjaxRequestInternal): void; addOnGoingAjaxRequest(requestInternal: AjaxRequestInternal, promiseAjaxResponse: Promise): void; deleteOnGoingAjaxRequest(requestInternal: AjaxRequestInternal): void; addInMemoryCache(requestInternal: AjaxRequestInternal, dataToAdd: T): void; addInPersistentStore(requestInternal: AjaxRequestInternal, cacheData: CachedData): Promise; getMemoryStoreData(requestInternal: AjaxRequestInternal): CachedData | undefined; getPersistentStoreData(requestInternal: AjaxRequestInternal): Promise | undefined>; deleteFromPersistentStorage(requestInternal: AjaxRequestInternal): Promise; forceDeleteAndFetch(request: AjaxRequestWithCache, options?: DeleteCacheOptions): Promise>; deleteDataFromCache(request: AjaxRequest, options?: DeleteCacheOptions): Promise; deleteAllDataFromAllCache(): Promise; deletePersistentStorage(name: string): Promise; writeSignature(payload: T): string; execute(request: AjaxRequestExecute): Promise>; invalidateRequests(request: AjaxRequestExecute): void; hashCode(toHash: CachedType): string; getCurrentDateTimeMs(): number; } declare const DataAccessGateway: (databaseName: string) => IDataAccessSingleton; export default DataAccessGateway;