Skip to main content

Constructor

new Kweri(options: KweriOptions, timer?: TimerAdapter)

KweriOptions

PropertyTypeDefaultDescription
baseURLstringRequired. Base URL prepended to all request paths
fetcherFetcherglobal fetchCustom HTTP transport
staleTimenumber0Milliseconds data is considered fresh
cacheTimenumber300_000Milliseconds to retain stale data with no observers
maxRetriesnumber0Max automatic retries for retryable errors
gcIntervalnumberundefinedMilliseconds between GC sweeps (disabled if omitted)
persistencePersistenceAdapterundefinedAdapter for cross-session cache persistence
enableDevToolsbooleanfalseMount the DevTools overlay (no-op in production)
devtoolsMountKweriDevToolsOptions{}DevTools panel options

Methods

query

query<E extends Endpoint>(endpoint: E, params: InferParams<E>): Promise<InferResponse<E>>
Executes a query. Returns cached data if fresh; otherwise fetches, caches, and returns.

mutate

mutate<E extends Endpoint, TContext = unknown>(
  endpoint: E,
  params: InferParams<E>,
  options?: MutationOptions<InferParams<E>, InferResponse<E>, TContext>
): Promise<InferResponse<E>>
Executes a mutation with lifecycle hooks. See Mutations.

getCachedData

getCachedData<E extends Endpoint>(endpoint: E, params: InferParams<E>): InferResponse<E> | undefined
Returns cached data without triggering a network request. Returns undefined if not cached.

setCachedData

setCachedData<E extends Endpoint>(endpoint: E, params: InferParams<E>, data: InferResponse<E>): void
Writes data into the cache directly. Marks entry as success with updatedAt = Date.now().

invalidateQuery

invalidateQuery<E extends Endpoint>(endpoint: E, params: InferParams<E>): void
Marks a cache entry as stale. The entry remains in cache; a background refetch fires on next access.

invalidateByPath

invalidateByPath(pattern: string | RegExp): void
Marks all entries whose serialized cache key contains pattern as stale.

invalidateQueryByKey

invalidateQueryByKey(key: string): void
Invalidates by raw serialized cache key.

removeQuery

removeQuery<E extends Endpoint>(endpoint: E, params: InferParams<E>): void
Deletes a cache entry entirely.

removeQueryByKey

removeQueryByKey(key: string): void
Deletes by raw serialized cache key.

subscribe

subscribe<E extends Endpoint>(
  endpoint: E,
  params: InferParams<E>,
  callback: (entry: CacheEntry) => void
): () => void
Subscribes to cache changes for a query. Returns an unsubscribe function.

onCacheChange

onCacheChange(callback: (key: string, entry: CacheEntry) => void): () => void
Subscribes to all cache changes globally.

isInFlight

isInFlight<E extends Endpoint>(endpoint: E, params: InferParams<E>): boolean
Returns true if a fetch is currently in progress for this query.

getQueryKey

getQueryKey<E extends Endpoint>(endpoint: E, params: InferParams<E>): string
Returns the serialized cache key string (e.g. GET:/users:{}).

startGC / stopGC

startGC(intervalMs: number): void
stopGC(): void
Start or stop the eviction engine.

getDevToolsSnapshot

getDevToolsSnapshot(): KweriDevToolsSnapshot
Returns a snapshot of the current cache, observer counts, and in-flight requests.

destroy

destroy(): void
Stops GC, unmounts DevTools, clears all subscriptions.

Properties

readonly queryClient: QueryClient
readonly apiClient:   ApiClient