@braine/quantum-query 1.2.6 → 1.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,40 +1,57 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
- import React, { ReactNode } from 'react';
2
+ import React$1, { ReactNode } from 'react';
3
3
 
4
- declare function subscribe(store: object, callback: (target: object, prop: string | symbol, value: unknown) => void): () => boolean;
5
- declare function createState<T extends object>(initialState: T): T;
4
+ /**
5
+ * Micro-Signals (Quantum Reactive Engine)
6
+ *
7
+ * Industry-standard ultra-fast fine-grained reactivity.
8
+ * API encapsulated to prevent abstraction leaks and ensure safety.
9
+ */
10
+ interface Signal<T> {
11
+ get: () => T;
12
+ set: (value: T) => void;
13
+ subscribe: (fn: (value: T) => void) => () => void;
14
+ isWatched: () => boolean;
15
+ }
6
16
 
7
17
  interface StorageAdapter {
8
18
  getItem(key: string): string | null | Promise<string | null>;
9
19
  setItem(key: string, value: string): void | Promise<void>;
10
20
  removeItem(key: string): void | Promise<void>;
11
21
  }
12
- type PersistOptions<S> = {
13
- key: string;
22
+ interface AtomOptions<T> {
23
+ key?: string;
14
24
  storage?: 'local' | 'session' | StorageAdapter;
15
- paths?: (keyof S)[];
25
+ validate?: (data: unknown) => T;
16
26
  debug?: boolean;
17
- };
18
- type ModelDefinition<S, A, C> = {
19
- state: S;
20
- actions?: A & ThisType<S & A & C>;
21
- computed?: C & ThisType<S & A & C>;
22
- persist?: PersistOptions<S>;
23
- };
24
- type ComputedValues<T> = {
25
- [K in keyof T]: T[K] extends (...args: any[]) => infer R ? R : never;
26
- };
27
- declare function defineModel<S extends object, A extends object, C extends object>(def: ModelDefinition<S, A, C>): S & A & ComputedValues<C>;
27
+ }
28
+ /**
29
+ * createAtom (Unified Client State)
30
+ * A Signal with built-in persistence powers.
31
+ */
32
+ declare function atom<T>(initialValue: T, options?: AtomOptions<T>): Signal<T>;
28
33
 
29
- declare function useStore<T extends object>(store: T): T;
34
+ interface SignalValueProps<T> {
35
+ signal: Signal<T>;
36
+ render?: (value: T) => React.ReactNode;
37
+ children?: (value: T) => React.ReactNode;
38
+ }
39
+ /**
40
+ * SignalValue
41
+ * A component that renders the value of a signal directly.
42
+ * The component subscribes to the signal and updates ONLY itself when the signal changes.
43
+ * This prevents re-rendering the parent component.
44
+ *
45
+ * Usage:
46
+ * <SignalValue signal={count$} render={count => <span>{count}</span>} />
47
+ * or
48
+ * <SignalValue signal={count$}>{count => <span>{count}</span>}</SignalValue>
49
+ */
50
+ declare function SignalValue<T>({ signal, render, children }: SignalValueProps<T>): react_jsx_runtime.JSX.Element | null;
30
51
 
31
52
  declare function scheduleUpdate(callback: () => void): void;
32
53
 
33
- declare function enableDevTools(store: any, name?: string): void;
34
-
35
- declare function computed<T>(fn: () => T): {
36
- readonly value: T;
37
- };
54
+ declare function enableDevTools(store: object, name?: string): void;
38
55
 
39
56
  type RetryConfig = {
40
57
  retries: number;
@@ -46,7 +63,15 @@ type CacheConfig = {
46
63
  ttl: number;
47
64
  force?: boolean;
48
65
  };
49
- type HttpClient$1 = any;
66
+ interface HttpClient {
67
+ get: <T>(url: string, config?: RequestConfig<T>) => Promise<T>;
68
+ post: <T>(url: string, data?: unknown, config?: RequestConfig<T>) => Promise<T>;
69
+ put: <T>(url: string, data?: unknown, config?: RequestConfig<T>) => Promise<T>;
70
+ delete: <T>(url: string, config?: RequestConfig<T>) => Promise<T>;
71
+ patch: <T>(url: string, data?: unknown, config?: RequestConfig<T>) => Promise<T>;
72
+ request: <T>(url: string, config?: RequestConfig<T>) => Promise<T>;
73
+ config: HttpClientConfig;
74
+ }
50
75
  type HttpClientConfig = {
51
76
  baseURL?: string;
52
77
  headers?: Record<string, string>;
@@ -54,7 +79,7 @@ type HttpClientConfig = {
54
79
  retry?: number | RetryConfig;
55
80
  auth?: {
56
81
  getToken: () => string | Promise<string | null>;
57
- onTokenExpired: (client: HttpClient$1) => Promise<string | null>;
82
+ onTokenExpired: (client: HttpClient) => Promise<string | null>;
58
83
  onAuthFailed?: () => void;
59
84
  };
60
85
  interceptors?: {
@@ -66,33 +91,38 @@ type Validator<T> = {
66
91
  parse?: (data: unknown) => T;
67
92
  validateSync?: (data: unknown) => T;
68
93
  };
69
- type RequestConfig<T> = RequestInit & {
94
+ type RequestConfig<T> = Omit<RequestInit, 'cache'> & {
70
95
  schema?: Validator<T> | undefined;
71
96
  timeout?: number | undefined;
72
97
  retry?: number | RetryConfig | undefined;
73
- cache?: CacheConfig;
98
+ cache?: CacheConfig | RequestCache;
74
99
  };
75
100
 
76
- type HttpClient = {
77
- get: <T>(url: string, config?: RequestConfig<T>) => Promise<T>;
78
- post: <T>(url: string, data?: unknown, config?: RequestConfig<T>) => Promise<T>;
79
- put: <T>(url: string, data?: unknown, config?: RequestConfig<T>) => Promise<T>;
80
- delete: <T>(url: string, config?: RequestConfig<T>) => Promise<T>;
81
- patch: <T>(url: string, data?: unknown, config?: RequestConfig<T>) => Promise<T>;
82
- request: <T>(url: string, config?: RequestConfig<T>) => Promise<T>;
83
- config: HttpClientConfig;
84
- };
85
101
  declare function createHttpClient(config: HttpClientConfig): HttpClient;
86
102
 
87
- declare function isPromise(value: any): value is Promise<any>;
88
- declare function handlePromise(promise: Promise<any>, triggerUpdate: () => void): void;
89
- declare function unwrapPromise(promise: Promise<any>): any;
90
- declare function getPromiseState(promise: Promise<any>): {
103
+ declare function isPromise(value: unknown): value is Promise<unknown>;
104
+ declare function handlePromise(promise: Promise<unknown>, triggerUpdate: () => void): void;
105
+ declare function unwrapPromise(promise: Promise<unknown>): unknown;
106
+ declare function getPromiseState(promise: Promise<unknown>): {
91
107
  status: "pending" | "fulfilled" | "rejected";
92
- value: any;
93
- error: any;
108
+ value: unknown;
109
+ error: unknown;
94
110
  };
95
111
 
112
+ /**
113
+ * Generic Schema Interface
114
+ * Compatible with Zod, Valibot, ArkType, etc.
115
+ */
116
+ interface Schema<T> {
117
+ parse: (data: unknown) => T;
118
+ }
119
+ interface QueryClientConfig {
120
+ defaultStaleTime?: number;
121
+ defaultCacheTime?: number;
122
+ defaultSchema?: Schema<unknown>;
123
+ enableGC?: boolean;
124
+ maxCacheSize?: number;
125
+ }
96
126
  /**
97
127
  * Middleware Plugin Interface
98
128
  */
@@ -109,21 +139,8 @@ interface InfiniteData<T> {
109
139
  pageParams: unknown[];
110
140
  }
111
141
 
112
- /**
113
- * Micro-Signals (Powered by @preact/signals-core)
114
- *
115
- * Industry-standard ultra-fast fine-grained reactivity.
116
- * API adapted to match original interface for backward compatibility.
117
- */
118
-
119
- interface Signal<T> {
120
- get: () => T;
121
- set: (value: T) => void;
122
- subscribe: (fn: (value: T) => void) => () => void;
123
- }
124
-
125
- type QueryStatus$1 = 'pending' | 'success' | 'error';
126
- type FetchDirection$1 = 'initial' | 'next' | 'previous' | 'idle';
142
+ type QueryStatus = 'pending' | 'success' | 'error';
143
+ type FetchDirection = 'initial' | 'next' | 'previous' | 'idle';
127
144
  interface QueryKey {
128
145
  key: readonly unknown[];
129
146
  params?: Record<string, unknown>;
@@ -131,10 +148,10 @@ interface QueryKey {
131
148
  type QueryKeyInput = readonly unknown[] | QueryKey;
132
149
  interface CacheEntry<T = unknown> {
133
150
  data: T | undefined;
134
- status: QueryStatus$1;
151
+ status: QueryStatus;
135
152
  error: Error | null;
136
153
  isFetching: boolean;
137
- fetchDirection: FetchDirection$1;
154
+ fetchDirection: FetchDirection;
138
155
  timestamp: number;
139
156
  staleTime: number;
140
157
  cacheTime: number;
@@ -176,17 +193,15 @@ declare class MutationCache {
176
193
  }) => number;
177
194
  }
178
195
 
179
- declare class QueryCache {
196
+ declare class QueryClient {
180
197
  private storage;
181
198
  private remotes;
182
199
  mutationCache: MutationCache;
183
200
  private pluginManager;
184
201
  private readonly defaultStaleTime;
185
202
  private readonly defaultCacheTime;
186
- constructor(config?: {
187
- defaultStaleTime?: number;
188
- defaultCacheTime?: number;
189
- });
203
+ private readonly defaultSchema?;
204
+ constructor(config?: QueryClientConfig);
190
205
  /**
191
206
  * Get data from storage
192
207
  */
@@ -201,23 +216,25 @@ declare class QueryCache {
201
216
  set: <T>(queryKey: QueryKeyInput, data: T, options?: {
202
217
  staleTime?: number;
203
218
  cacheTime?: number;
204
- fetchDirection?: FetchDirection$1;
219
+ fetchDirection?: FetchDirection;
205
220
  tags?: string[];
206
221
  }) => void;
207
222
  /**
208
223
  * Restore cache entry (Hydration)
209
224
  */
210
- restore: (queryKey: QueryKeyInput, entry: CacheEntry<any>) => void;
225
+ restore: (queryKey: QueryKeyInput, entry: CacheEntry<unknown>) => void;
211
226
  /**
212
227
  * Fetch data (Orchestration)
213
228
  */
214
229
  fetch: <T>(queryKey: QueryKeyInput, fn: (context: {
215
230
  signal?: AbortSignal;
216
231
  }) => Promise<T>, options?: {
217
- fetchDirection?: FetchDirection$1;
232
+ fetchDirection?: FetchDirection;
218
233
  signal?: AbortSignal;
219
234
  retry?: number | boolean;
220
235
  retryDelay?: number | ((attemptIndex: number) => number);
236
+ tags?: string[];
237
+ schema?: Schema<T>;
221
238
  }) => Promise<T>;
222
239
  /**
223
240
  * Invalidate queries
@@ -227,7 +244,7 @@ declare class QueryCache {
227
244
  use: (plugin: QueryPlugin) => this;
228
245
  isStale: (queryKey: QueryKeyInput) => boolean;
229
246
  getAll: () => Map<string, CacheEntry<unknown>>;
230
- snapshot: () => Map<string, Signal<CacheEntry<unknown> | undefined>>;
247
+ snapshot: () => Map<string, CacheEntry<unknown>>;
231
248
  clear: () => void;
232
249
  remove: (key: QueryKeyInput) => void;
233
250
  prefetch: <T>(queryKey: QueryKeyInput, data: T, options?: {
@@ -238,10 +255,14 @@ declare class QueryCache {
238
255
  getStats: () => {
239
256
  size: number;
240
257
  keys: string[];
258
+ tags: number;
241
259
  };
242
- getSnapshot: () => Map<string, Signal<CacheEntry<unknown> | undefined>>;
260
+ getSnapshot: () => Map<string, CacheEntry<unknown>>;
243
261
  invalidateAll: () => void;
244
262
  private normalizeKey;
263
+ private isCacheEntry;
264
+ debugSet: <T>(key: QueryKeyInput, data: T) => void;
265
+ debugInvalidate: (key: QueryKeyInput) => void;
245
266
  }
246
267
 
247
268
  /**
@@ -272,41 +293,42 @@ interface PaginatedQueryResult<T> {
272
293
  }
273
294
  declare function usePaginatedQuery<T>({ queryKey, queryFn, pageSize, staleTime, cacheTime, enabled, retry }: UsePaginatedQueryOptions<T>): PaginatedQueryResult<T>;
274
295
 
275
- /**
276
- * useQuery Hook
277
- * Base query hook with stale-while-revalidate and background refetching
278
- *
279
- * Refactored to use QueryObserver (Clean Architecture)
280
- */
281
-
282
- interface UseQueryOptions<T, TData = T> {
283
- queryKey: unknown[];
296
+ interface QueryObserverOptions<T, TData = T> {
297
+ queryKey: QueryKeyInput;
284
298
  queryFn: (context?: {
285
299
  signal?: AbortSignal;
286
- }) => Promise<T>;
300
+ }) => Promise<unknown>;
301
+ schema?: Schema<T>;
287
302
  staleTime?: number;
288
303
  cacheTime?: number;
289
304
  enabled?: boolean;
290
305
  refetchOnWindowFocus?: boolean;
306
+ refetchOnReconnect?: boolean;
307
+ refetchInterval?: number;
308
+ tags?: string[];
291
309
  retry?: number | boolean;
310
+ retryDelay?: number | ((attemptIndex: number) => number);
292
311
  select?: (data: T) => TData;
293
- tags?: string[];
294
312
  }
295
- type QueryStatus = 'pending' | 'error' | 'success';
296
- type FetchDirection = 'idle' | 'fetching';
297
- interface UseQueryResult<TData, TError = Error> {
313
+ interface QueryObserverResult<TData> {
298
314
  data: TData | undefined;
299
- status: QueryStatus;
300
- fetchStatus: FetchDirection;
301
- isPending: boolean;
302
- isSuccess: boolean;
315
+ isLoading: boolean;
303
316
  isError: boolean;
317
+ isSuccess: boolean;
318
+ isPending: boolean;
304
319
  isFetching: boolean;
305
- error: TError | null;
306
- refetch: () => void;
307
- signal: Signal<CacheEntry<any> | undefined>;
320
+ isStale: boolean;
321
+ error: Error | null;
322
+ status: QueryStatus;
323
+ refetch: () => Promise<void>;
324
+ }
325
+
326
+ interface UseQueryOptions<T, TData = T> extends QueryObserverOptions<T, TData> {
327
+ }
328
+ interface UseQueryResult<TData, T> extends QueryObserverResult<TData> {
329
+ signal: Signal<QueryObserverResult<TData>>;
308
330
  }
309
- declare function useQuery<T, TData = T, TError = Error>(options: UseQueryOptions<T, TData>): UseQueryResult<TData, TError>;
331
+ declare function useQuery<T, TData = T>(options: UseQueryOptions<T, TData>): UseQueryResult<TData, T>;
310
332
 
311
333
  interface UseMutationOptions<TData, TVariables, TContext = unknown> {
312
334
  mutationFn: (variables: TVariables) => Promise<TData>;
@@ -335,24 +357,13 @@ interface MutationResult<TData, TVariables> {
335
357
  }
336
358
  declare function useMutation<TData = unknown, TVariables = void, TContext = unknown>(options: UseMutationOptions<TData, TVariables, TContext>): MutationResult<TData, TVariables>;
337
359
 
338
- type QueryClient = QueryCache;
339
360
  declare const QueryClientProvider: ({ client, children }: {
340
361
  client: QueryClient;
341
- children: ReactNode;
362
+ children: ReactNode | undefined;
342
363
  }) => react_jsx_runtime.JSX.Element;
343
364
  declare const useQueryClient: () => QueryClient;
344
365
 
345
- /**
346
- * Infinite Query Hook (Reactive)
347
- * Provides infinite scroll with Signal-based reactivity
348
- *
349
- * Refactored to "Senior Standards":
350
- * - No local state (useReducer removed)
351
- * - All state derived from Global QueryCache Signal
352
- * - Manual signal updates for aggregate key to track fetching state of pages
353
- */
354
-
355
- interface UseInfiniteQueryOptions<T, TPageParam = any> {
366
+ interface InfiniteQueryObserverOptions<T, TPageParam = unknown> {
356
367
  queryKey: unknown[];
357
368
  queryFn: (context: {
358
369
  pageParam: TPageParam;
@@ -363,23 +374,27 @@ interface UseInfiniteQueryOptions<T, TPageParam = any> {
363
374
  staleTime?: number;
364
375
  cacheTime?: number;
365
376
  enabled?: boolean;
377
+ refetchOnWindowFocus?: boolean;
378
+ refetchOnReconnect?: boolean;
366
379
  retry?: number | boolean;
367
380
  }
368
- interface InfiniteQueryResult<T> {
381
+ interface InfiniteQueryObserverResult<T> {
369
382
  data: InfiniteData<T> | undefined;
370
- fetchNextPage: () => Promise<void>;
371
- fetchPreviousPage: () => Promise<void>;
372
- hasNextPage: boolean;
373
- hasPreviousPage: boolean;
374
383
  isFetching: boolean;
375
384
  isFetchingNextPage: boolean;
376
385
  isFetchingPreviousPage: boolean;
377
386
  isLoading: boolean;
378
387
  isError: boolean;
388
+ hasNextPage: boolean;
389
+ hasPreviousPage: boolean;
379
390
  error: Error | null;
391
+ status: 'pending' | 'success' | 'error';
392
+ fetchNextPage: () => Promise<void>;
393
+ fetchPreviousPage: () => Promise<void>;
380
394
  refetch: () => Promise<void>;
381
395
  }
382
- declare function useInfiniteQuery<T, TPageParam = any>({ queryKey, queryFn, getNextPageParam, getPreviousPageParam, initialPageParam, staleTime, cacheTime, enabled, retry }: UseInfiniteQueryOptions<T, TPageParam>): InfiniteQueryResult<T>;
396
+
397
+ declare function useInfiniteQuery<T, TPageParam = unknown>(options: InfiniteQueryObserverOptions<T, TPageParam>): InfiniteQueryObserverResult<T>;
383
398
 
384
399
  interface DehydratedState {
385
400
  queries: Array<{
@@ -391,41 +406,36 @@ interface DehydratedState {
391
406
  /**
392
407
  * Serialize the query cache for transport.
393
408
  */
394
- declare function dehydrate(client: QueryCache): DehydratedState;
409
+ declare function dehydrate(client: QueryClient): DehydratedState;
395
410
  /**
396
411
  * Hydrate the query cache from serialized state.
397
412
  */
398
- declare function hydrate(client: QueryCache, state: DehydratedState): void;
413
+ declare function hydrate(client: QueryClient, state: DehydratedState): void;
399
414
 
400
415
  interface HydrationBoundaryProps {
401
416
  state?: DehydratedState;
402
- children: React.ReactNode;
417
+ children: React$1.ReactNode;
403
418
  }
404
419
  declare function HydrationBoundary({ state, children }: HydrationBoundaryProps): react_jsx_runtime.JSX.Element;
405
420
 
406
421
  interface UseSuspenseQueryOptions<T> extends UseQueryOptions<T> {
407
422
  }
408
- type SuspenseQueryResult<T> = Omit<UseQueryResult<T>, 'data' | 'isLoading' | 'isError' | 'error'> & {
423
+ type SuspenseQueryResult<T> = Omit<UseQueryResult<T, T>, 'data' | 'isLoading' | 'isError' | 'error'> & {
409
424
  data: T;
410
425
  };
411
426
  declare function useSuspenseQuery<T>(options: UseSuspenseQueryOptions<T>): SuspenseQueryResult<T>;
412
427
 
428
+ declare function useQueryStore(): Map<string, CacheEntry<unknown>>;
429
+
413
430
  /**
414
- * Bridge: Signal -> Proxy (Readonly)
431
+ * useQuery$ (The Quantum Hook)
415
432
  *
416
- * Creates a reactive proxy object that mirrors a signal's value.
417
- * Useful for using Query results inside a Store.
418
- */
419
- type ProxyResult<T> = T extends object ? T : {
420
- value: T;
421
- };
422
- declare function fromSignal<T>(signal: Signal<T>): ProxyResult<T>;
423
- /**
424
- * Bridge: Proxy -> Signal
433
+ * Returns a reactive SIGNAL of the query result.
434
+ * Accessing .value (or .get()) inside a tracked context (like <SignalValue>) will update fine-grained.
435
+ * Accessing it in a standard component render will NOT trigger re-renders when data changes.
425
436
  *
426
- * Creates a Signal that updates whenever the accessed parts of the proxy change.
427
- * Uses the Proxy's `activeListener` tracking mechanism.
437
+ * This is the "Zero-Render" primitive.
428
438
  */
429
- declare function toSignal<T>(selector: () => T): Signal<T>;
439
+ declare function useQuery$<T>(options: QueryObserverOptions<T>): Signal<QueryObserverResult<T>>;
430
440
 
431
- export { type CacheEntry, type HttpClient, HydrationBoundary, type MutationResult, type PaginatedQueryResult, QueryCache, QueryClientProvider, type QueryKey, type QueryKeyInput, type QueryPlugin, type QueryStatus$1 as QueryStatus, type Signal, type SuspenseQueryResult, type UseMutationOptions, type UsePaginatedQueryOptions, type UseQueryOptions, type UseQueryResult, type UseSuspenseQueryOptions, computed, createHttpClient, createState, defineModel, dehydrate, enableDevTools, fromSignal, getPromiseState, handlePromise, hydrate, isPromise, scheduleUpdate, subscribe, toSignal, unwrapPromise, useInfiniteQuery, useMutation, usePaginatedQuery, useQuery, useQueryClient, useStore, useSuspenseQuery };
441
+ export { type CacheEntry, HydrationBoundary, type MutationResult, type PaginatedQueryResult, QueryClient, QueryClientProvider, type QueryKey, type QueryKeyInput, type QueryPlugin, type QueryStatus, type Signal, SignalValue, type SuspenseQueryResult, type UseMutationOptions, type UsePaginatedQueryOptions, type UseQueryOptions, type UseQueryResult, type UseSuspenseQueryOptions, atom, createHttpClient, dehydrate, enableDevTools, getPromiseState, handlePromise, hydrate, isPromise, scheduleUpdate, unwrapPromise, useInfiniteQuery, useMutation, usePaginatedQuery, useQuery, useQuery$, useQueryClient, useQueryStore, useSuspenseQuery };