import type { Environment } from './Environment'; import type { ProxyGraphNode } from './GraphNode'; import type { FetchResponse, ResourceRequest, ResourceRequestOverride, ResourceIngest, ResourceRequestContext } from './network'; import type { Selector } from './Select'; import type { ErrorSnapshot, PendingSnapshot, AvailableSnapshot, Snapshot, SnapshotRefresh, ErrorResponse } from './interfaces/Snapshot'; import type { SnapshotSubscriptionCallback, StoreEntry, Unsubscribe, WatchSubscriptionCallback, CreateStoreMetadataParams, DurableStoreKeyMetadataMap, GetNotifyChangeEntry } from './interfaces/Store'; import type { AdapterWithContext, OnContextLoaded } from './AdapterContext'; import type { BuildCachedSnapshot, BuildNetworkSnapshot } from './CachePolicy'; import type { AdapterRequestContext } from './AdapterRequestContext'; import type { LuvioAdapterEventObserver } from './events'; import type { KeyMetadata, NormalizedKeyMetadata } from './interfaces/queryability'; import type { StoreQueryEvaluator } from './interfaces/queryability/StoreQueryEvaluator'; import type { Adapter } from './adapter'; type Instrument = (params: unknown) => void; type InstrumentParamsBuilder = () => Parameters[0]; interface LuvioOptions { instrument?: Instrument; } export interface DispatchResourceRequestContext { overrides?: ResourceRequestOverride; resourceRequestContext?: ResourceRequestContext; eventObservers?: LuvioAdapterEventObserver[]; } export interface WithContextOptions { /** * An optional callback that runs after the context is loaded the first time. */ onContextLoaded?: OnContextLoaded; /** * An id for the context. For most adapters it is recommended to use the * namespace + adapter name (ex: "myApiFamily_getBooks"). If multiple adapters * would like to share a context then they can use the same contextId. */ contextId: string; /** * The version of the context data stored. Context data is opaque to luvio, it is * up to the adapter using withContext to version its own data. In the case where * there is a version mismatch luvio will not return the outdated data. * * If not defined, versions will not be checked and the data is essentially versionless. * It is completely up to the adapter to manage breaking changes in this case */ contextVersion?: string; } export declare class Luvio { private environment; private options; constructor(environment: Environment, options?: LuvioOptions); storePublish(key: string | NormalizedKeyMetadata, data: Data): void; storeRedirect(existingKey: string | NormalizedKeyMetadata, canonicalKey: string): void; storeRetain(keys: string[] | NormalizedKeyMetadata[]): () => void; storeGetCanonicalKey(key: string | NormalizedKeyMetadata): string | NormalizedKeyMetadata; /** * Broadcast any cache entry changes to subscribers. * * NOTE: this MUST be called *AFTER* storeLookup in the ingestion flow as * some environments (namely, makeDurable) modify the store in this method. */ storeBroadcast(): Promise; storeIngest(key: string | NormalizedKeyMetadata, ingest: ResourceIngest | null, response: Response): void; storeIngestError(key: string | NormalizedKeyMetadata, errorSnapshot: ErrorSnapshot, storeMetadataParams?: CreateStoreMetadataParams): void; /** * Subscribe to the Luvio store to observe any changes to the data in the given * snapshot. * * NOTE: Errors are terminal - the callback will never be called after an ErrorSnapshot * is emitted (or if the given Snapshot is an ErrorSnapshot). * * @template D * @template V * @param {Snapshot} snapshot The snapshot that contains data to observe. * @param {SnapshotSubscriptionCallback} callback The callback to be called * whenever the given snapshot's data changes. NOTE: the snapshot passed to the * callback will have consistent, normalized data - however it is not guaranteed * to be within the TTL of that data type. * @returns {Unsubscribe} A function that will unsubscribe when invoked. * @memberof Luvio */ storeSubscribe(snapshot: Snapshot, callback: SnapshotSubscriptionCallback): Unsubscribe; storeWatch(prefix: string | Partial, callback: WatchSubscriptionCallback): Unsubscribe; storeLookup(sel: Selector, refresh?: SnapshotRefresh): Snapshot; storeEvict(key: string | NormalizedKeyMetadata): void; storeCleanup(): void; storeExpirePossibleStaleRecords(keys: string[] | NormalizedKeyMetadata[], config?: Config, refresh?: Adapter): Promise; createSnapshot(selector: Selector, refresh?: SnapshotRefresh): Snapshot; errorSnapshot(error: ErrorResponse, refresh?: SnapshotRefresh): ErrorSnapshot; dispatchResourceRequest(resourceRequest: ResourceRequest, context?: DispatchResourceRequestContext): Promise>; refreshSnapshot(snapshot: Snapshot): Promise>; /** * This method is meant for custom scenarios and should not be used for * general store lookups. Use applyCachePolicy instead. * * NOTE: this method works against synchronous stores only. */ getNode(key: string | NormalizedKeyMetadata): ProxyGraphNode; wrapNormalizedGraphNode(normalized: StoreEntry, key: string | NormalizedKeyMetadata): ProxyGraphNode; instrument(paramsBuilder: InstrumentParamsBuilder): void; /** * Returns true if the given snapshot can be returned to userland without * requiring any additional resolution. */ snapshotAvailable(snapshot: Snapshot): snapshot is AvailableSnapshot; withContext(adapter: AdapterWithContext, options: WithContextOptions): (config: C, requestContext?: AdapterRequestContext | undefined) => Snapshot | Promise> | null; /** * Returns a Promise that resolves once the given PendingSnapshot is available. This * is syntactic sugar for calling storeSubscribe and unsubscribing after the * first emit. Useful for one-shot data reads. Use storeSubscribe instead of * this to get continual updates when data changes. * * @template D * @template V * @param {PendingSnapshot} snapshot * @returns {Promise>} */ resolvePendingSnapshot(snapshot: PendingSnapshot): Promise>; publishStoreMetadata(key: string | NormalizedKeyMetadata, storeMetadataParams: CreateStoreMetadataParams): void; /** * Sets the TTL value for a specific namespace-representation Type. The given * TTL takes precedence over TTL values defined in RAML and also over the * default TTL value (if set using storeSetDefaultTTLOverride). * * @param {number} ttl Time-to-live in milliseconds * @returns {Promise} */ storeSetTTLOverride(namespace: string, representationName: string, ttl: number): Promise; storeGetTTLOverride(namespace: string, representationName: string): Promise; /** * Sets the default TTL value. The given TTL takes precedence over TTL values * defined in RAML, but defers to namespace-representation-specific override * values. * * @param {number} ttl Time-to-live in milliseconds * @returns {Promise} */ storeSetDefaultTTLOverride(ttl: number): Promise; applyCachePolicy(adapterRequestContext: AdapterRequestContext, buildSnapshotContext: C, buildCachedSnapshot: BuildCachedSnapshot, buildNetworkSnapshot: BuildNetworkSnapshot): Snapshot | Promise>; /** * A method to be called any time an adapter gets a successful response * from the network adapter * * @param ingestAndBroadcastFunc A function that ingests a response and broadcasts * @param getResponseCacheKeysFunc A function that returns the set of cache keys present in a response body * @returns A snapshot or the Promise of a snapshot that comes from resource ingestion. Can return undefined * or Promise for adapters that do not return a response (ie: DELETE adapters). */ handleSuccessResponse(ingestAndBroadcastFunc: () => Promise>, getResponseCacheKeysFunc: () => DurableStoreKeyMetadataMap): Promise>; /** * A method to be called any time an adapter gets an error response * from the network adapter * * @param ingestAndBroadcastFunc A function that ingests a response and broadcasts * @returns An ErrorSnapshot or the Promise of an ErrorSnapshot that comes from resource ingestion */ handleErrorResponse(ingestAndBroadcastFunc: () => Promise): Promise; /** * This method is meant to be used by adapter's notifyChange function. It * accepts a set of cache keys and returns normalized store entries for each * cache key that is present in the store. Results are returned in a Promise * to support Environments that use asynchronous stores. * * If a cache key is not present in the store then that key will not be included * in the returned set. * * NOTE: this method is meant to be used by notifyChange and SHOULD NOT be used * as a general purpose way to get data out of the cache. Use luvio.applyCachePolicy * for general-purpose cache lookups. * * @param keys A list of cache keys to check * @returns A Promise of a set of store entries that are present in the cache */ getNotifyChangeStoreEntries(keys: string[] | NormalizedKeyMetadata[]): Promise[]>; notifyStoreUpdateAvailable(keys: string[] | NormalizedKeyMetadata[]): Promise; registerStoreQueryEvaluator(queryEvaluator: StoreQueryEvaluator): void; getStoreQueryEvaluator(): StoreQueryEvaluator | undefined; registerTypeQueryEvaluator(namespace: string, representationName: string, queryEvaluator: T): void; getTypeQueryEvaluator(namespace: string, representationName: string): T | undefined; buildStructuredKey(namespace: string, representationName: string, idValues: KeyMetadata): NormalizedKeyMetadata; } export {};